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-10-02 17:55:00 +00:00
|
|
|
m.route.setOrig = m.route.set
|
|
|
|
m.route.set = function(path, data, options){
|
|
|
|
m.route.setOrig(path, data, options)
|
|
|
|
window.scrollTo(0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.route.linkOrig = m.route.link
|
|
|
|
m.route.link = function(vnode){
|
|
|
|
m.route.linkOrig(vnode)
|
|
|
|
window.scrollTo(0, 0)
|
|
|
|
}
|
|
|
|
|
2019-10-01 03:45:44 +00:00
|
|
|
const Authentication = require('./authentication')
|
2019-02-20 16:10:37 +00:00
|
|
|
|
2019-10-01 17:18:20 +00:00
|
|
|
m.route.prefix = ''
|
|
|
|
window.adminRoutes = {}
|
2019-10-01 03:45:44 +00:00
|
|
|
let loadingAdmin = false
|
|
|
|
let loadedAdmin = false
|
|
|
|
let loaded = 0
|
2019-10-01 17:18:20 +00:00
|
|
|
let elements = []
|
2019-10-01 03:45:44 +00:00
|
|
|
|
|
|
|
const onLoaded = function() {
|
|
|
|
loaded++
|
|
|
|
if (loaded < 2) return
|
|
|
|
|
|
|
|
Authentication.setAdmin(Authentication.currentUser && Authentication.currentUser.level >= 10)
|
|
|
|
loadedAdmin = true
|
2019-10-01 17:18:20 +00:00
|
|
|
m.route.set(m.route.get())
|
|
|
|
}
|
|
|
|
|
|
|
|
const onError = function() {
|
|
|
|
elements.forEach(function(x) { x.remove() })
|
|
|
|
loadedAdmin = loadingAdmin = false
|
|
|
|
loaded = 0
|
|
|
|
m.route.set('/logout')
|
2019-10-01 03:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const loadAdmin = function(user) {
|
|
|
|
if (loadingAdmin) {
|
|
|
|
if (loadedAdmin) {
|
|
|
|
Authentication.setAdmin(user && user.level >= 10)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!user || user.level < 10) return
|
|
|
|
|
|
|
|
loadingAdmin = true
|
|
|
|
|
2019-10-01 17:18:20 +00:00
|
|
|
let token = Authentication.getToken()
|
2019-10-01 03:45:44 +00:00
|
|
|
let element = document.createElement('link')
|
2019-10-01 17:18:20 +00:00
|
|
|
elements.push(element)
|
2019-10-01 03:45:44 +00:00
|
|
|
element.setAttribute('rel', 'stylesheet')
|
|
|
|
element.setAttribute('type', 'text/css')
|
2019-10-01 17:18:20 +00:00
|
|
|
element.setAttribute('href', '/assets/admin.css?token=' + token)
|
2019-10-01 03:45:44 +00:00
|
|
|
element.onload = onLoaded
|
2019-10-01 17:18:20 +00:00
|
|
|
element.onerror = onError
|
2019-10-01 03:45:44 +00:00
|
|
|
document.getElementsByTagName('head')[0].appendChild(element)
|
|
|
|
|
|
|
|
element = document.createElement('script')
|
2019-10-01 17:18:20 +00:00
|
|
|
elements.push(element)
|
2019-10-01 03:45:44 +00:00
|
|
|
element.setAttribute('type', 'text/javascript')
|
2019-10-01 17:18:20 +00:00
|
|
|
element.setAttribute('src', '/assets/admin.js?token=' + token)
|
2019-10-01 03:45:44 +00:00
|
|
|
element.onload = onLoaded
|
2019-10-01 17:18:20 +00:00
|
|
|
element.onerror = onError
|
2019-10-01 03:45:44 +00:00
|
|
|
document.body.appendChild(element)
|
|
|
|
}
|
|
|
|
|
|
|
|
Authentication.addEvent(loadAdmin)
|
|
|
|
if (Authentication.currentUser) {
|
|
|
|
loadAdmin(Authentication.currentUser)
|
|
|
|
}
|
2019-10-01 17:18:20 +00:00
|
|
|
|
|
|
|
const Menu = require('./menu/menu')
|
|
|
|
const Footer = require('./footer/footer')
|
|
|
|
const Frontpage = require('./frontpage/frontpage')
|
|
|
|
const Login = require('./login/login')
|
|
|
|
const Logout = require('./login/logout')
|
|
|
|
const Page = require('./pages/page')
|
|
|
|
const Article = require('./article/article')
|
|
|
|
|
|
|
|
const menuRoot = document.getElementById('nav')
|
|
|
|
const mainRoot = document.getElementById('main')
|
|
|
|
const footerRoot = document.getElementById('footer')
|
|
|
|
|
|
|
|
const Loader = {
|
|
|
|
view: function() { return m('div.loading-spinner') },
|
|
|
|
}
|
|
|
|
const AdminResolver = {
|
|
|
|
onmatch: function(args, requestedPath) {
|
|
|
|
if (window.adminRoutes[args.path]) {
|
|
|
|
return window.adminRoutes[args.path][args.id && 1 || 0]
|
|
|
|
}
|
|
|
|
return Loader
|
|
|
|
},
|
|
|
|
render: function(vnode) { return vnode },
|
|
|
|
}
|
|
|
|
|
|
|
|
const allRoutes = {
|
|
|
|
'/': Frontpage,
|
|
|
|
'/login': Login,
|
|
|
|
'/logout': Logout,
|
|
|
|
'/page/:id': Page,
|
|
|
|
'/article/:id': Article,
|
|
|
|
'/admin/:path': AdminResolver,
|
|
|
|
'/admin/:path/:id': AdminResolver,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.route(mainRoot, '/', allRoutes)
|
|
|
|
m.mount(menuRoot, Menu)
|
|
|
|
m.mount(footerRoot, Footer)
|