2019-02-20 16:10:37 +00:00
|
|
|
const m = require('mithril')
|
|
|
|
|
2019-10-01 12:19:10 +00:00
|
|
|
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
|
2019-10-01 17:18:20 +00:00
|
|
|
|
|
|
|
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) {
|
2019-10-01 17:18:20 +00:00
|
|
|
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) {
|
2019-09-23 18:37:02 +00:00
|
|
|
var deviceWidth = window.innerWidth
|
2022-07-26 02:55:37 +00:00
|
|
|
var bannerPath = this.data.featured && this.data.featured.banner_prefix
|
2019-09-23 18:37:02 +00:00
|
|
|
|
2022-07-26 02:55:37 +00:00
|
|
|
if (bannerPath) {
|
2019-09-16 16:47:29 +00:00
|
|
|
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'
|
2019-09-16 16:47:29 +00:00
|
|
|
} else {
|
2022-07-26 02:55:37 +00:00
|
|
|
bannerPath += '_large'
|
|
|
|
}
|
|
|
|
if (window.supportsavif) {
|
|
|
|
bannerPath += '.avif'
|
|
|
|
} else {
|
|
|
|
bannerPath += '.jpg'
|
2019-09-16 16:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2019-09-16 16:47:29 +00:00
|
|
|
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', [
|
2019-09-23 18:37:02 +00:00
|
|
|
m('aside.sidebar', [
|
|
|
|
m('div.categories', [
|
|
|
|
m('h4', 'Categories'),
|
2019-10-03 12:55:13 +00:00
|
|
|
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),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
),
|
2019-09-23 18:37:02 +00:00
|
|
|
]),
|
2021-01-05 19:12:10 +00:00
|
|
|
m('div.asunaside', {
|
|
|
|
class: window.supportsavif ? 'avif' : 'jpeg'
|
|
|
|
}),
|
2019-09-23 18:37:02 +00:00
|
|
|
]),
|
|
|
|
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 })
|
2019-09-23 18:37:02 +00:00
|
|
|
}),
|
|
|
|
m(Pages, {
|
|
|
|
base: '/',
|
2022-07-20 00:33:06 +00:00
|
|
|
total: this.data.total_articles,
|
|
|
|
page: this.currentPage,
|
2019-09-23 18:37:02 +00:00
|
|
|
}),
|
|
|
|
]),
|
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
|