nfp_sites/app/pages/page.js

199 lines
6.5 KiB
JavaScript
Raw Normal View History

2019-09-13 13:33:10 +00:00
const m = require('mithril')
const ApiPage = require('../api/page.p')
const Article = require('../api/article.p')
2019-10-01 11:35:00 +00:00
const pagination = require('../api/pagination')
2019-09-13 13:33:10 +00:00
const Authentication = require('../authentication')
const Newsentry = require('../widgets/newsentry')
2019-09-14 19:03:38 +00:00
const Pages = require('../widgets/pages')
2019-09-13 13:33:10 +00:00
const Page = {
oninit: function(vnode) {
this.error = ''
2022-07-20 00:33:06 +00:00
this.loading = false
this.showLoading = null
this.data = {
page: null,
articles: [],
total_articles: 0,
featured: null,
}
this.children = []
this.currentPage = Number(m.route.param('page')) || 1
if (window.__nfpdata) {
this.path = m.route.param('id')
2022-07-20 00:33:06 +00:00
this.data = window.__nfpdata
2021-02-05 11:50:01 +00:00
window.__nfpdata = null
2021-02-05 11:50:01 +00:00
window.__nfpsubdata = null
} else {
this.fetchPage(vnode)
}
2019-09-14 19:03:38 +00:00
},
2022-07-20 00:33:06 +00:00
onbeforeupdate: function(vnode) {
this.currentPage = Number(m.route.param('page')) || 1
if (this.path !== m.route.param('id') || this.currentPage !== this.lastpage) {
this.fetchPage(vnode)
}
},
2019-09-14 19:03:38 +00:00
fetchPage: function(vnode) {
2022-07-20 00:33:06 +00:00
this.error = ''
this.lastpage = this.currentPage
2019-09-14 19:03:38 +00:00
this.path = m.route.param('id')
2022-07-20 00:33:06 +00:00
if (this.showLoading) {
clearTimeout(this.showLoading)
2019-09-13 13:33:10 +00:00
}
2019-09-14 19:03:38 +00:00
2022-07-20 00:33:06 +00:00
if (this.data.page) {
this.showLoading = setTimeout(() => {
this.showLoading = null
this.loading = true
m.redraw()
}, 150)
} else {
this.loading = true
2019-09-14 19:03:38 +00:00
}
2022-07-20 00:33:06 +00:00
this.children = ApiPage.TreeMap.get(this.path)
this.children = this.children && this.children.children || []
ApiPage.getPage(this.path, this.lastpage)
.then((result) => {
this.data = result
if (!this.data.page) {
this.error = 'Page not found'
return
}
if (this.lastpage !== 1) {
document.title = 'Page ' + this.lastpage + ' - ' + this.data.page.name + ' - NFP Moe'
} else {
document.title = this.data.page.name + ' - NFP Moe'
}
}, (err) => {
this.error = err.message
2019-09-14 19:03:38 +00:00
})
2022-07-20 00:33:06 +00:00
.then(() => {
clearTimeout(this.showLoading)
this.showLoading = null
this.loading = false
2019-09-13 13:33:10 +00:00
m.redraw()
})
},
view: function(vnode) {
var deviceWidth = window.innerWidth
var pixelRatio = window.devicePixelRatio || 1
var bannerPath = ''
var imagePath = ''
2022-07-20 00:33:06 +00:00
if (this.data.page && this.data.page.banner) {
if (deviceWidth < 400 && pixelRatio <= 1) {
2022-07-20 00:33:06 +00:00
bannerPath = this.data.page.banner.small_url
} else if ((deviceWidth < 800 && pixelRatio <= 1)
|| (deviceWidth < 600 && pixelRatio > 1)) {
2022-07-20 00:33:06 +00:00
bannerPath = this.data.page.banner.medium_url
} else {
2022-07-20 00:33:06 +00:00
bannerPath = this.data.page.banner.large_url
}
}
2022-07-20 00:33:06 +00:00
if (this.data.page && this.data.page.media) {
if ((deviceWidth < 1000 && pixelRatio <= 1)
|| (deviceWidth < 800 && pixelRatio > 1)) {
2022-07-20 00:33:06 +00:00
imagePath = this.data.page.media.medium_url
} else {
2022-07-20 00:33:06 +00:00
imagePath = this.data.page.media.large_url
}
}
2022-07-20 00:33:06 +00:00
return ([
this.loading
? m('article.page', m('div.loading-spinner'))
: null,
!this.loading && this.error
2021-02-05 11:50:01 +00:00
? m('div.error-wrapper', m('div.error', {
onclick: function() {
vnode.state.error = ''
vnode.state.fetchPage(vnode)
},
2022-07-20 00:33:06 +00:00
}, 'Page error: ' + this.error))
: null,
!this.loading && !this.error
? m('article.page', [
bannerPath
? m('.div.page-banner', { style: { 'background-image': 'url("' + bannerPath + '")' } } )
: null,
m('div.goback', ['« ', m(m.route.Link, {
href: this.data.page.parent_path
? '/page/' + this.data.page.parent_path
: '/'
}, this.data.page.parent_name || 'Home')]),
m('header', m('h1', this.data.page.name)),
2019-09-13 13:33:10 +00:00
m('.container', {
2022-07-20 00:33:06 +00:00
class: this.children.length ? 'multi' : '',
2019-09-13 13:33:10 +00:00
}, [
2022-07-20 00:33:06 +00:00
this.children.length
? m('aside.sidebar', [
m('h4', 'View ' + this.data.page.name + ':'),
this.children.map(function(page) {
return m(m.route.Link, { href: '/page/' + page.path }, page.name)
2019-09-14 19:03:38 +00:00
}),
])
2022-07-20 00:33:06 +00:00
: null,
this.data.page.content
? m('.fr-view', [
imagePath
? m('a', { href: this.data.page.media.link}, m('img.page-cover', { src: imagePath, alt: 'Cover image for ' + this.data.page.name } ))
: null,
m.trust(this.data.page.content),
this.data.articles.length && this.data.page.content
? m('aside.news', [
m('h4', 'Latest posts under ' + this.data.page.name + ':'),
this.data.articles.map(function(article) {
2022-07-27 08:41:18 +00:00
return m(Newsentry, { article: article })
2022-07-20 00:33:06 +00:00
}),
m(Pages, {
base: '/page/' + this.data.page.path,
total: this.data.total_articles,
page: this.currentPage,
}),
])
: null,
])
: this.data.articles.length
? m('aside.news.single', [
imagePath ? m('a', { href: this.data.page.media.link}, m('img.page-cover', { src: imagePath, alt: 'Cover image for ' + this.data.page.name } )) : null,
m('h4', 'Latest posts under ' + this.data.page.name + ':'),
this.data.articles.map(function(article) {
2022-07-27 08:41:18 +00:00
return m(Newsentry, { article: article })
2022-07-20 00:33:06 +00:00
}),
m(Pages, {
base: '/page/' + this.data.page.path,
total: this.data.total_articles,
page: this.currentPage,
}),
])
: this.data.page.media
? m('img.page-cover.single', { src: this.data.page.media.medium_url, alt: 'Cover image for ' + this.data.page.name } )
: null,
2019-09-13 13:33:10 +00:00
]),
Authentication.currentUser
? m('div.admin-actions', [
m('span', 'Admin controls:'),
2022-07-20 00:33:06 +00:00
m(m.route.Link, { href: '/admin/pages/' + this.data.page.path }, 'Edit page'),
2019-09-13 13:33:10 +00:00
])
: null,
])
2022-07-20 00:33:06 +00:00
: null,
])
2019-09-13 13:33:10 +00:00
},
}
module.exports = Page