nfp_sites/app/frontpage/frontpage.js

148 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-02-20 16:10:37 +00:00
const m = require('mithril')
const Page = 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-14 19:03:38 +00:00
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
2019-10-02 00:16:11 +00:00
if (window.__nfpfeatured) {
this.featured = window.__nfpfeatured
}
console.log(this.featured)
2019-10-01 03:45:44 +00:00
if (window.__nfpdata
&& window.__nfplinks) {
this.links = window.__nfplinks
this.articles = window.__nfpdata
this.lastpage = m.route.param('page') || '1'
2019-10-01 03:45:44 +00:00
window.__nfpdata = null
window.__nfplinks = null
if (this.articles.length === 0) {
m.route.set('/')
} else {
Frontpage.processFeatured(vnode, this.articles)
}
2019-10-01 03:45:44 +00:00
} 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)
}
},
2019-10-01 11:35:00 +00:00
fetchArticles: function(vnode) {
2019-09-14 19:03:38 +00:00
this.error = ''
this.loading = true
this.links = null
this.articles = []
this.lastpage = m.route.param('page') || '1'
if (this.lastpage !== '1') {
document.title = 'Page ' + this.lastpage + ' - NFP Moe - Anime/Manga translation group'
} else {
document.title = 'NFP Moe - Anime/Manga translation group'
}
2019-10-01 11:35:00 +00:00
return Pagination.fetchPage(Article.getAllArticlesPagination({
2019-10-01 04:02:46 +00:00
per_page: 10,
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) {
if (vnode.state.featured) return
2019-10-01 03:45:44 +00:00
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'),
2019-10-01 11:35:00 +00:00
Page.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