nfp_sites/app/index.js

80 lines
2 KiB
JavaScript
Raw Normal View History

2019-02-20 16:10:37 +00:00
const m = require('mithril')
2019-10-01 03:45:44 +00:00
window.m = m
2019-02-20 16:10:37 +00:00
2019-09-13 13:33:10 +00:00
m.route.prefix = ''
2019-02-20 16:10:37 +00:00
const Menu = require('./menu/menu')
const Footer = require('./footer/footer')
2019-02-20 16:10:37 +00:00
const Frontpage = require('./frontpage/frontpage')
const Login = require('./login/login')
const Logout = require('./login/logout')
2019-09-13 13:33:10 +00:00
const Page = require('./pages/page')
const Article = require('./article/article')
2019-10-01 03:45:44 +00:00
const Authentication = require('./authentication')
2019-02-20 16:10:37 +00:00
const menuRoot = document.getElementById('nav')
const mainRoot = document.getElementById('main')
const footerRoot = document.getElementById('footer')
2019-02-20 16:10:37 +00:00
2019-10-01 03:45:44 +00:00
const allRoutes = {
2019-02-20 16:10:37 +00:00
'/': Frontpage,
'/login': Login,
'/logout': Logout,
2019-09-14 19:03:38 +00:00
'/page/:id': Page,
'/article/:id': Article,
2019-10-01 03:45:44 +00:00
}
m.route(mainRoot, '/', allRoutes)
2019-02-20 16:10:37 +00:00
m.mount(menuRoot, Menu)
m.mount(footerRoot, Footer)
2019-10-01 03:45:44 +00:00
let loadingAdmin = false
let loadedAdmin = false
let loaded = 0
const onLoaded = function() {
loaded++
if (loaded < 2) return
if (window.addAdminRoutes) {
window.addAdminRoutes.forEach(function (item) {
allRoutes[item[0]] = item[1]
})
m.route(mainRoot, '/', allRoutes)
}
Authentication.setAdmin(Authentication.currentUser && Authentication.currentUser.level >= 10)
loadedAdmin = true
m.redraw()
}
const loadAdmin = function(user) {
if (loadingAdmin) {
if (loadedAdmin) {
Authentication.setAdmin(user && user.level >= 10)
}
return
}
if (!user || user.level < 10) return
loadingAdmin = true
let element = document.createElement('link')
element.setAttribute('rel', 'stylesheet')
element.setAttribute('type', 'text/css')
element.setAttribute('href', '/assets/admin.css')
element.onload = onLoaded
document.getElementsByTagName('head')[0].appendChild(element)
element = document.createElement('script')
element.setAttribute('type', 'text/javascript')
element.setAttribute('src', '/assets/admin.js')
element.onload = onLoaded
document.body.appendChild(element)
}
Authentication.addEvent(loadAdmin)
if (Authentication.currentUser) {
loadAdmin(Authentication.currentUser)
}