nfp_sites/app/frontpage/frontpage.js

147 lines
4.0 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
var bannerPath = ''
2022-07-20 00:33:06 +00:00
if (this.data.featured && this.data.featured.banner) {
var pixelRatio = window.devicePixelRatio || 1
if (deviceWidth < 400 && pixelRatio <= 1) {
2021-01-04 17:47:59 +00:00
bannerPath = window.supportsavif
2022-07-20 00:33:06 +00:00
&& this.data.featured.banner.small_url_avif
|| this.data.featured.banner.small_url
} else if ((deviceWidth < 800 && pixelRatio <= 1)
|| (deviceWidth < 600 && pixelRatio > 1)) {
2021-01-04 17:47:59 +00:00
bannerPath = window.supportsavif
2022-07-20 00:33:06 +00:00
&& this.data.featured.banner.medium_url_avif
|| this.data.featured.banner.medium_url
} else {
2021-01-04 17:47:59 +00:00
bannerPath = window.supportsavif
2022-07-20 00:33:06 +00:00
&& this.data.featured.banner.large_url_avif
|| this.data.featured.banner.large_url
}
}
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