nfp_sites/nfp_moe/app/site_article.js

156 lines
4.6 KiB
JavaScript

const m = require('mithril')
const Article = require('./article')
const api = require('./api')
const media = require('./media')
window.LoadComments = false
window.HyvorLoaded = false
window.HYVOR_TALK_WEBSITE = 7544
const SiteArticle = {
oninit: function(vnode) {
this.error = ''
this.loading = false
this.showLoading = null
this.data = {
article: null,
files: [],
}
this.showcomments = false
if (window.__nfpdata) {
this.path = m.route.param('id')
this.data.article = window.__nfpdata
window.__nfpdata = null
} else {
this.fetchArticle(vnode)
}
},
onbeforeupdate: function(vnode) {
if (this.path !== m.route.param('id')) {
this.fetchArticle(vnode)
}
},
fetchArticle: function(vnode) {
this.error = ''
this.path = m.route.param('id')
this.showcomments = false
if (this.showLoading) {
clearTimeout(this.showLoading)
}
if (this.data.article) {
this.showLoading = setTimeout(() => {
this.showLoading = null
this.loading = true
m.redraw()
}, 150)
} else {
this.loading = true
}
api.sendRequest({
method: 'GET',
url: '/api/articles/' + this.path,
})
.then((result) => {
this.data = result
if (this.data.article.media_alt_prefix) {
this.data.article.pictureFallback = this.data.article.media_alt_prefix + '_small.jpg'
this.data.article.pictureJpeg = this.data.article.media_alt_prefix + '_small.jpg' + ' 720w, '
+ this.data.article.media_alt_prefix + '_medium.jpg' + ' 1300w, '
+ this.data.article.media_alt_prefix + '_large.jpg 1920w'
this.data.article.pictureAvif = this.data.article.media_alt_prefix + '_small.avif' + ' 720w, '
+ this.data.article.media_alt_prefix + '_medium.avif' + ' 1300w, '
+ this.data.article.media_alt_prefix + '_large.avif 1920w'
this.data.article.pictureCover = '(max-width: 840px) calc(100vw - 82px), '
+ '758px'
} else {
this.data.article.pictureFallback = null
this.data.article.pictureJpeg = null
this.data.article.pictureAvif = null
this.data.article.pictureCover = null
}
if (!this.data.article) {
this.error = 'Article not found'
}
}, (err) => {
this.error = err.message
})
.then(() => {
clearTimeout(this.showLoading)
this.showLoading = null
this.loading = false
m.redraw()
})
},
view: function(vnode) {
let article = this.data.article
let banner = media.getBannerImage(article, '/article/')
return [
this.loading
? m('div.loading-spinner')
: null,
!this.loading && this.error
? m('div.wrapper', m('div.error', {
onclick: () => {
this.error = ''
this.fetchPage(vnode)
},
}, 'Page error: ' + this.error + '. Click here to try again'))
: null,
/*(banner
? m('a.page-banner', {
href: banner.original,
target: '_blank',
style: { 'background-image': 'url("' + banner.banner + '")' },
},
)
: null),*/
(article
? m('.inside.vertical', [
m('div.page-goback', ['« ', m(m.route.Link, {
href: article.page_path
? '/page/' + article.page_path
: '/'
}, article.page_name || 'Home')]
),
article ? m(Article, { full: true, files: this.data.files, article: article }) : null,
window.LoadComments
? m('div#hyvor-talk-view', { oncreate: function() {
window.HYVOR_TALK_CONFIG = {
url: false,
id: article.path,
loadMode: scroll,
}
if (!window.HyvorLoaded) {
window.HyvorLoaded = true
var s = document.createElement('script')
s.src = 'https://talk.hyvor.com/web-api/embed.js';
s.type = 'text/javascript'
// s.setAttribute('crossorigin', '')
// s.setAttribute('data-timestamp', +new Date());
document.body.appendChild(s);
} else {
hyvor_talk.reload()
}
}}, m('div.loading-spinner'))
: m('button', {
onclick: function() { window.LoadComments = true },
}, 'Open comment discussion'),
])
: null),
]
},
}
module.exports = SiteArticle