nfp_sites/app/menu/menu.js

96 lines
3 KiB
JavaScript
Raw Normal View History

2019-02-20 16:10:37 +00:00
const m = require('mithril')
const Authentication = require('../authentication')
const Darkmode = require('../darkmode')
const Page = require('../api/page.p')
2019-02-20 16:10:37 +00:00
const Menu = {
currentActive: 'home',
2019-09-13 13:33:10 +00:00
error: '',
loading: false,
2019-02-20 16:10:37 +00:00
onbeforeupdate: function() {
let currentPath = m.route.get()
if (currentPath === '/') Menu.currentActive = 'home'
else if (currentPath === '/login') Menu.currentActive = 'login'
2019-09-13 13:33:10 +00:00
else Menu.currentActive = currentPath
2019-02-20 16:10:37 +00:00
},
2019-09-13 13:33:10 +00:00
oninit: function(vnode) {
2019-02-20 16:10:37 +00:00
Menu.onbeforeupdate()
2019-09-13 13:33:10 +00:00
2019-10-01 11:35:00 +00:00
if (Page.Tree.length) return
2019-10-01 03:45:44 +00:00
2019-09-13 13:33:10 +00:00
Menu.loading = true
2019-10-01 11:35:00 +00:00
Page.getTree()
2019-09-13 13:33:10 +00:00
.then(function(results) {
2019-10-01 11:35:00 +00:00
Page.Tree.splice(0, Page.Tree.length)
Page.Tree.push.apply(Page.Tree, results)
2019-09-13 13:33:10 +00:00
})
.catch(function(err) {
Menu.error = err.message
})
.then(function() {
Menu.loading = false
m.redraw()
})
2019-02-20 16:10:37 +00:00
},
view: function() {
return [
m('div.top', [
2019-09-14 19:03:38 +00:00
m(m.route.Link,
2019-10-01 03:45:44 +00:00
{ href: '/', class: 'logo' },
2019-09-14 19:03:38 +00:00
m('h2', 'NFP Moe')
),
2019-02-20 16:10:37 +00:00
m('aside', Authentication.currentUser ? [
2019-09-14 19:03:38 +00:00
m('p', [
'Welcome ' + Authentication.currentUser.email,
m(m.route.Link, { href: '/logout' }, 'Logout'),
(Darkmode.darkIsOn
? m('button.dark', { onclick: Darkmode.setDarkMode.bind(Darkmode, false) }, 'Day mode')
: m('button.dark', { onclick: Darkmode.setDarkMode.bind(Darkmode, true) }, 'Night mode')
2019-10-01 03:45:44 +00:00
),
2019-09-14 19:03:38 +00:00
]),
2019-10-01 03:45:44 +00:00
(Authentication.isAdmin
? m('div.adminlinks', [
m(m.route.Link, { href: '/admin/articles/add' }, 'Create article'),
m(m.route.Link, { href: '/admin/articles' }, 'Articles'),
m(m.route.Link, { href: '/admin/pages' }, 'Pages'),
m(m.route.Link, { hidden: Authentication.currentUser.level < 100, href: '/admin/staff' }, 'Staff'),
])
2019-10-01 03:45:44 +00:00
: (Authentication.currentUser.level > 10 ? m('div.loading-spinner') : null)
2019-02-20 16:10:37 +00:00
),
] : (Darkmode.darkIsOn
? m('button.dark', { onclick: Darkmode.setDarkMode.bind(Darkmode, false) }, 'Day mode')
: m('button.dark', { onclick: Darkmode.setDarkMode.bind(Darkmode, true) }, 'Night mode')
)
),
2019-02-20 16:10:37 +00:00
]),
m('nav', [
2019-09-13 13:33:10 +00:00
m(m.route.Link, {
href: '/',
class: Menu.currentActive === 'home' ? 'active' : '',
2019-02-20 16:10:37 +00:00
}, 'Home'),
2019-10-01 11:35:00 +00:00
Menu.loading ? m('div.loading-spinner') : Page.Tree.map(function(page) {
2019-09-13 13:33:10 +00:00
if (page.children.length) {
return m('div.hassubmenu', [
m(m.route.Link, {
href: '/page/' + page.path,
class: Menu.currentActive === ('/page/' + page.path) ? 'active' : '',
2019-09-14 19:03:38 +00:00
}, page.name),
2019-09-13 13:33:10 +00:00
])
}
return m(m.route.Link, {
href: '/page/' + page.path,
class: Menu.currentActive === ('/page/' + page.path) ? 'active' : '',
}, page.name)
}),
2019-02-20 16:10:37 +00:00
]),
2019-09-13 13:33:10 +00:00
Menu.error ? m('div.menuerror', Menu.error) : null,
2019-02-20 16:10:37 +00:00
]
2019-09-14 19:03:38 +00:00
},
2019-02-20 16:10:37 +00:00
}
module.exports = Menu