nfp_sites/app/pages/page.js

136 lines
4.5 KiB
JavaScript
Raw Normal View History

2019-09-13 13:33:10 +00:00
const m = require('mithril')
const { getPage } = require('../api/page')
2019-09-14 19:03:38 +00:00
const { getAllPageArticlesPagination } = require('../api/article')
const { fetchPage } = 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 = ''
2019-09-14 19:03:38 +00:00
this.lastpage = m.route.param('page') || '1'
this.loadingnews = false
this.fetchPage(vnode)
},
fetchPage: function(vnode) {
this.path = m.route.param('id')
this.news = []
this.newslinks = null
2019-09-13 13:33:10 +00:00
this.page = {
id: 0,
name: '',
path: '',
description: '',
media: null,
}
this.loading = true
getPage(this.path)
.then(function(result) {
vnode.state.page = result
})
.catch(function(err) {
vnode.state.error = err.message
})
.then(function() {
2019-09-14 19:03:38 +00:00
return vnode.state.fetchArticles(vnode)
})
},
onupdate: function(vnode) {
if (this.path !== m.route.param('id')) {
this.fetchPage(vnode)
} else if (m.route.param('page') && m.route.param('page') !== this.lastpage) {
this.fetchArticles(vnode)
}
},
fetchArticles: function(vnode) {
this.loadingnews = true
this.newslinks = null
this.lastpage = m.route.param('page') || '1'
return fetchPage(getAllPageArticlesPagination(this.page.id, {
per_page: 10,
page: this.lastpage,
includes: ['files', 'media'],
}))
.then(function(result) {
vnode.state.news = result.data
vnode.state.newslinks = result.links
})
.catch(function(err) {
vnode.state.error = err.message
})
.then(function() {
vnode.state.loading = vnode.state.loadingnews = false
2019-09-13 13:33:10 +00:00
m.redraw()
})
},
view: function(vnode) {
return (
this.loading ?
m('div.loading-spinner')
: m('article.page', [
2019-09-14 19:03:38 +00:00
this.page.banner ? m('.div.page-banner', { style: { 'background-image': 'url("' + this.page.banner.url + '")' } } ) : null,
2019-09-13 13:33:10 +00:00
m('header', m('h1', this.page.name)),
m('.container', {
class: this.page.children.length ? 'multi' : '',
}, [
this.page.children.length
? m('aside.sidebar', [
m('h4', 'View ' + this.page.name + ':'),
this.page.children.map(function(page) {
2019-09-14 19:03:38 +00:00
return m(m.route.Link, { href: '/page/' + page.path }, page.name)
2019-09-13 13:33:10 +00:00
}),
])
: null,
this.page.description
? m('.fr-view', [
this.page.media ? m('img.page-cover', { src: this.page.media.url, alt: 'Cover image for ' + this.page.name } ) : null,
2019-09-13 13:33:10 +00:00
m.trust(this.page.description),
2019-09-14 19:03:38 +00:00
this.news.length && this.page.description
2019-09-13 13:33:10 +00:00
? m('aside.news', [
2019-09-14 19:03:38 +00:00
m('h4', 'Latest posts under ' + this.page.name + ':'),
this.loadingnews ? m('div.loading-spinner') : this.news.map(function(article) {
2019-09-13 13:33:10 +00:00
return m(Newsentry, article)
}),
2019-09-14 19:03:38 +00:00
m(Pages, {
base: '/page/' + this.page.path,
links: this.newslinks,
}),
2019-09-13 13:33:10 +00:00
])
2019-09-14 19:03:38 +00:00
: null,
2019-09-13 13:33:10 +00:00
])
2019-09-14 19:03:38 +00:00
: this.news.length
? m('aside.news.single', [
this.page.media ? m('img.page-cover', { src: this.page.media.url, alt: 'Cover image for ' + this.page.name } ) : null,
2019-09-14 19:03:38 +00:00
m('h4', 'Latest posts under ' + this.page.name + ':'),
this.loadingnews ? m('div.loading-spinner') : this.news.map(function(article) {
return m(Newsentry, article)
}),
m(Pages, {
base: '/page/' + this.page.path,
links: this.newslinks,
}),
])
: this.page.media
? m('img.page-cover.single', { src: this.page.media.url, alt: 'Cover image for ' + this.page.name } )
2019-09-14 19:03:38 +00:00
: null,
2019-09-13 13:33:10 +00:00
]),
Authentication.currentUser
? m('div.admin-actions', [
m('span', 'Admin controls:'),
m(m.route.Link, { href: '/admin/pages/' + this.page.id }, 'Edit page'),
])
: null,
])
)
},
}
module.exports = Page