2022-08-05 14:26:29 +00:00
|
|
|
const m = require('mithril')
|
|
|
|
const Authentication = require('./authentication')
|
|
|
|
const PageTree = require('./page_tree')
|
2024-12-14 07:05:17 +00:00
|
|
|
const common = require('./common')
|
2022-08-05 14:26:29 +00:00
|
|
|
|
|
|
|
const Menu = {
|
|
|
|
oninit: function(vnode) {
|
|
|
|
this.currentActive = 'home'
|
|
|
|
this.error = ''
|
|
|
|
this.loading = false
|
|
|
|
this.onbeforeupdate()
|
|
|
|
|
|
|
|
if (!PageTree.Tree.length) {
|
|
|
|
this.refreshTree()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onbeforeupdate: function() {
|
|
|
|
let currentPath = m.route.get()
|
|
|
|
|
|
|
|
if (currentPath === '/') this.currentActive = 'home'
|
|
|
|
else if (currentPath === '/login') this.currentActive = 'login'
|
|
|
|
else if (currentPath && currentPath.startsWith('/page')) this.currentActive = currentPath.slice(currentPath.lastIndexOf('/') + 1)
|
|
|
|
},
|
|
|
|
|
|
|
|
refreshTree: function(vnode) {
|
|
|
|
this.loading = true
|
|
|
|
this.error = ''
|
|
|
|
|
|
|
|
PageTree.refreshTree()
|
|
|
|
.catch((err) => {
|
|
|
|
this.error = 'Error while getting menu tree: ' + err.message + '. Click here to try again.'
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.loading = false
|
|
|
|
m.redraw()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
logOut: function() {
|
|
|
|
Authentication.clearToken()
|
|
|
|
m.route.set('/')
|
|
|
|
},
|
|
|
|
|
|
|
|
view: function() {
|
|
|
|
return [
|
|
|
|
m('header', [
|
|
|
|
m('div.inside', [
|
|
|
|
m(m.route.Link,
|
2022-08-18 09:59:48 +00:00
|
|
|
{ href: '/', class: 'title' },
|
|
|
|
[
|
|
|
|
m('div.logo.spritesheet'),
|
|
|
|
m('h1', 'NFP Moe'),
|
|
|
|
]
|
2022-08-05 14:26:29 +00:00
|
|
|
),
|
|
|
|
m('aside', [
|
|
|
|
Authentication.currentUser
|
|
|
|
? [
|
|
|
|
m('p', [
|
|
|
|
'Welcome ' + Authentication.currentUser.name + '. ',
|
|
|
|
m('button', { onclick: this.logOut }, '(Log out)'),
|
|
|
|
]),
|
2022-08-12 23:33:50 +00:00
|
|
|
m('div', [
|
2022-08-05 14:26:29 +00:00
|
|
|
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'),
|
2022-08-18 09:59:48 +00:00
|
|
|
// m(m.route.Link, { hidden: Authentication.currentUser.rank < 100, href: '/admin/staff' }, 'Staff'),
|
2022-08-05 14:26:29 +00:00
|
|
|
])
|
|
|
|
]
|
|
|
|
: null,
|
|
|
|
m('button',
|
2024-12-14 07:05:17 +00:00
|
|
|
{ onclick: common.toggleDarkMode.bind(this) },
|
|
|
|
common.darkIsOn ? 'Day mode' : 'Night mode'
|
2022-08-05 14:26:29 +00:00
|
|
|
),
|
|
|
|
])
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
m('nav', [
|
|
|
|
m('div.inside', [
|
|
|
|
m(m.route.Link, {
|
|
|
|
href: '/',
|
|
|
|
class: this.currentActive === 'home' ? 'active' : '',
|
|
|
|
}, 'Home'),
|
|
|
|
this.loading ? m('div.loading-spinner') : null,
|
|
|
|
PageTree.Tree.map((page) => {
|
|
|
|
let className = ''
|
|
|
|
if (this.currentActive === page.path) {
|
|
|
|
className += 'active '
|
|
|
|
}
|
|
|
|
return m(m.route.Link, {
|
|
|
|
href: '/page/' + page.path,
|
|
|
|
class: className,
|
|
|
|
}, page.name)
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
]),
|
|
|
|
this.error ? m('div.error', { onclick: this.refreshTree.bind(this) }, this.error) : null,
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Menu
|