nfp_sites/nfp_moe/app/index.js

136 lines
4 KiB
JavaScript
Raw Normal View History

require('./polyfill')
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
m.route.prefix = ''
window.adminRoutes = {}
2019-10-01 03:45:44 +00:00
let loadingAdmin = false
let loadedAdmin = false
let loaded = 0
let elements = []
2019-10-01 03:45:44 +00:00
const onLoaded = function() {
loaded++
if (loaded < 2) return
2022-07-20 00:33:06 +00:00
Authentication.setAdmin(Authentication.currentUser && Authentication.currentUser.rank >= 10)
2019-10-01 03:45:44 +00:00
loadedAdmin = true
m.route.set(m.route.get())
}
2022-07-20 00:33:06 +00:00
const onError = function(a, b, c) {
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) {
2022-07-20 00:33:06 +00:00
Authentication.setAdmin(user && user.rank >= 10)
2019-10-01 03:45:44 +00:00
}
return
}
2022-07-20 00:33:06 +00:00
if (!user || user.rank < 10) return
2019-10-01 03:45:44 +00:00
loadingAdmin = true
let token = Authentication.getToken()
2019-10-01 03:45:44 +00:00
let element = document.createElement('link')
elements.push(element)
2019-10-01 03:45:44 +00:00
element.setAttribute('rel', 'stylesheet')
element.setAttribute('type', 'text/css')
element.setAttribute('href', '/assets/admin.css?token=' + token)
2019-10-01 03:45:44 +00:00
element.onload = onLoaded
element.onerror = onError
2019-10-01 03:45:44 +00:00
document.getElementsByTagName('head')[0].appendChild(element)
element = document.createElement('script')
elements.push(element)
2019-10-01 03:45:44 +00:00
element.setAttribute('type', 'text/javascript')
element.setAttribute('src', '/assets/admin.js?token=' + token)
2019-10-01 03:45:44 +00:00
element.onload = onLoaded
element.onerror = onError
2019-10-01 03:45:44 +00:00
document.body.appendChild(element)
2022-07-20 00:33:06 +00:00
element = document.createElement('script')
elements.push(element)
element.setAttribute('type', 'text/javascript')
element.setAttribute('src', '/assets/editor.js')
element.onload = onLoaded
element.onerror = onError
document.body.appendChild(element)
2019-10-01 03:45:44 +00:00
}
Authentication.addEvent(loadAdmin)
if (Authentication.currentUser) {
loadAdmin(Authentication.currentUser)
}
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,
}
2021-01-05 19:12:10 +00:00
// Wait until we finish checking avif support, some views render immediately and will ask for this immediately before the callback gets called.
/*
* imgsupport.js from leechy/imgsupport
*/
const AVIF = new Image();
AVIF.onload = AVIF.onerror = function () {
window.supportsavif = (AVIF.height === 2)
document.body.className = document.body.className + ' ' + (window.supportsavif ? 'avifsupport' : 'jpegonly')
m.route(mainRoot, '/', allRoutes)
m.mount(menuRoot, Menu)
m.mount(footerRoot, Footer)
}
AVIF.src = 'data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=';