nfp_sites/app/frontpage/frontpage.js

130 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-02-20 16:10:37 +00:00
const m = require('mithril')
const { Tree } = require('../api/page')
2019-09-14 19:03:38 +00:00
const { getAllArticlesPagination } = require('../api/article')
const { fetchPage } = require('../api/pagination')
const Pages = require('../widgets/pages')
const Newsitem = require('../widgets/newsitem')
2019-10-01 03:45:44 +00:00
const Frontpage = {
2019-09-14 19:03:38 +00:00
oninit: function(vnode) {
this.error = ''
this.loading = false
this.featured = null
this.links = null
2019-10-01 03:45:44 +00:00
if (window.__nfpdata
&& window.__nfplinks) {
this.links = window.__nfplinks
this.articles = window.__nfpdata
this.lastpage = '1'
window.__nfpdata = null
window.__nfplinks = null
Frontpage.processFeatured(vnode, this.articles)
} else {
this.fetchArticles(vnode)
}
2019-09-14 19:03:38 +00:00
},
onupdate: function(vnode) {
if (this.lastpage !== (m.route.param('page') || '1')) {
this.fetchArticles(vnode)
}
},
fetchArticles(vnode) {
this.error = ''
this.loading = true
this.links = null
this.articles = []
this.lastpage = m.route.param('page') || '1'
return fetchPage(getAllArticlesPagination({
2019-10-01 03:45:44 +00:00
per_page: 2,
2019-09-14 19:03:38 +00:00
page: this.lastpage,
includes: ['parent', 'files', 'media', 'banner'],
}))
.then(function(result) {
vnode.state.articles = result.data
vnode.state.links = result.links
2019-10-01 03:45:44 +00:00
Frontpage.processFeatured(vnode, result.data)
2019-09-14 19:03:38 +00:00
})
.catch(function(err) {
vnode.state.error = err.message
})
.then(function() {
vnode.state.loading = false
m.redraw()
})
},
2019-10-01 03:45:44 +00:00
processFeatured: function(vnode, data) {
for (var i = data.length - 1; i >= 0; i--) {
if (data[i].banner) {
vnode.state.featured = data[i]
}
}
},
2019-09-14 19:03:38 +00:00
view: function(vnode) {
var deviceWidth = window.innerWidth
var bannerPath = ''
if (this.featured && this.featured.banner) {
var pixelRatio = window.devicePixelRatio || 1
if (deviceWidth < 400 && pixelRatio <= 1) {
bannerPath = this.featured.banner.small_url
} else if ((deviceWidth < 800 && pixelRatio <= 1)
|| (deviceWidth < 600 && pixelRatio > 1)) {
bannerPath = this.featured.banner.medium_url
} else {
2019-10-01 03:45:44 +00:00
bannerPath = this.featured.banner.large_url
}
}
2019-09-14 19:03:38 +00:00
return [
2019-10-01 03:45:44 +00:00
(bannerPath
2019-09-14 19:03:38 +00:00
? m('a.frontpage-banner', {
href: '/article/' + this.featured.path,
style: { 'background-image': 'url("' + bannerPath + '")' },
2019-09-14 19:03:38 +00:00
},
this.featured.name
)
: null),
m('frontpage', [
m('aside.sidebar', [
m('div.categories', [
m('h4', 'Categories'),
Tree.map(function(page) {
return [
m(m.route.Link, { class: 'root', href: '/page/' + page.path }, page.name),
(page.children.length
? m('ul', page.children.map(function(subpage) {
return m('li', m(m.route.Link, { class: 'child', href: '/page/' + subpage.path }, subpage.name))
}))
: null),
]
}),
]),
2019-10-01 03:45:44 +00:00
m('div.asunaside'),
]),
m('.frontpage-news', [
(this.loading
? m('div.loading-spinner')
: null),
this.articles.map(function(article) {
return m(Newsitem, article)
}),
m(Pages, {
base: '/',
links: this.links,
}),
]),
2019-02-20 16:10:37 +00:00
]),
2019-09-14 19:03:38 +00:00
]
},
2019-02-20 16:10:37 +00:00
}
2019-10-01 03:45:44 +00:00
module.exports = Frontpage