nfp_sites/nfp_moe/app/frontpage/frontpage.js

147 lines
3.8 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
2022-07-20 00:33:06 +00:00
this.showLoading = null
this.data = {
page: null,
articles: [],
total_articles: 0,
featured: null,
2019-10-02 00:16:11 +00:00
}
2022-07-20 00:33:06 +00:00
this.currentPage = Number(m.route.param('page')) || 1
2019-10-02 00:16:11 +00:00
2022-07-20 00:33:06 +00:00
if (window.__nfpdata) {
this.lastpage = this.currentPage
2019-10-01 03:45:44 +00:00
window.__nfpdata = null
if (this.articles.length === 0) {
m.route.set('/')
}
2019-10-01 03:45:44 +00:00
} else {
2022-07-20 00:33:06 +00:00
this.fetchPage(vnode)
2019-10-01 03:45:44 +00:00
}
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.lastpage !== this.currentPage) {
this.fetchPage(vnode)
2019-09-14 19:03:38 +00:00
}
},
2022-07-20 00:33:06 +00:00
fetchPage: function(vnode) {
2019-09-14 19:03:38 +00:00
this.error = ''
2022-07-20 00:33:06 +00:00
this.lastpage = this.currentPage
if (this.showLoading) {
clearTimeout(this.showLoading)
}
this.showLoading = setTimeout(() => {
this.showLoading = null
this.loading = true
m.redraw()
}, 150)
2019-09-14 19:03:38 +00:00
2022-07-20 00:33:06 +00:00
if (this.lastpage !== 1) {
document.title = 'Page ' + this.lastpage + ' - NFP Moe - Anime/Manga translation group'
} else {
document.title = 'NFP Moe - Anime/Manga translation group'
}
2022-06-28 08:15:51 +00:00
return Page.getPage(null, this.lastpage)
2022-07-20 00:33:06 +00:00
.then((result) => {
this.data = result
}, (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-14 19:03:38 +00:00
m.redraw()
})
},
view: function(vnode) {
var deviceWidth = window.innerWidth
2022-07-27 08:41:18 +00:00
var bannerPath = this.data.featured && this.data.featured.banner_alt_prefix
2022-07-26 02:55:37 +00:00
if (bannerPath) {
var pixelRatio = window.devicePixelRatio || 1
2022-07-26 02:55:37 +00:00
if ((deviceWidth < 720 && pixelRatio <= 1)
|| (deviceWidth < 360 && pixelRatio <= 2)) {
bannerPath += '_small'
} else if ((deviceWidth < 1300 && pixelRatio <= 1)
|| (deviceWidth < 650 && pixelRatio <= 2)) {
bannerPath += '_medium'
} else {
2022-07-26 02:55:37 +00:00
bannerPath += '_large'
}
if (window.supportsavif) {
bannerPath += '.avif'
} else {
bannerPath += '.jpg'
}
}
2019-09-14 19:03:38 +00:00
return [
2019-10-01 03:45:44 +00:00
(bannerPath
2021-01-05 19:12:10 +00:00
? m(m.route.Link, {
class: 'frontpage-banner',
2022-07-20 00:33:06 +00:00
href: '/article/' + this.data.featured.path,
style: { 'background-image': 'url("' + bannerPath + '")' },
2019-09-14 19:03:38 +00:00
},
2022-07-20 00:33:06 +00:00
this.data.featured.name
2019-09-14 19:03:38 +00:00
)
: null),
m('frontpage', [
m('aside.sidebar', [
m('div.categories', [
m('h4', 'Categories'),
m('div',
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),
]
})
),
]),
2021-01-05 19:12:10 +00:00
m('div.asunaside', {
class: window.supportsavif ? 'avif' : 'jpeg'
}),
]),
m('.frontpage-news', [
(this.loading
? m('div.loading-spinner')
: null),
2022-07-20 00:33:06 +00:00
this.data.articles.map(function(article) {
return m(Newsitem, { article: article })
}),
m(Pages, {
base: '/',
2022-07-20 00:33:06 +00:00
total: this.data.total_articles,
page: this.currentPage,
}),
]),
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