nfp_sites/nfp_moe/app/article/article.js

204 lines
7 KiB
JavaScript
Raw Normal View History

const m = require('mithril')
const ApiArticle = require('../api/article.p')
const Authentication = require('../authentication')
const Fileinfo = require('../widgets/fileinfo')
2022-07-27 08:41:18 +00:00
const EditorBlock = require('../widgets/editorblock')
const Article = {
oninit: function(vnode) {
this.error = ''
2022-07-20 00:33:06 +00:00
this.loading = false
this.showLoading = null
this.data = {
article: null,
files: [],
2022-07-27 08:41:18 +00:00
pictureFallback: null,
pictureJpeg: null,
pictureAvif: null,
pictureCover: null,
2022-07-20 00:33:06 +00:00
}
2019-10-02 00:55:15 +00:00
this.showcomments = false
if (window.__nfpdata) {
this.path = m.route.param('id')
2022-07-20 00:33:06 +00:00
this.data.article = window.__nfpdata
window.__nfpdata = null
} else {
this.fetchArticle(vnode)
}
},
2022-07-20 00:33:06 +00:00
onbeforeupdate: function(vnode) {
if (this.path !== m.route.param('id')) {
this.fetchArticle(vnode)
}
},
fetchArticle: function(vnode) {
2021-02-05 11:50:01 +00:00
this.error = ''
this.path = m.route.param('id')
2019-10-02 00:55:15 +00:00
this.showcomments = false
2022-07-20 00:33:06 +00:00
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
}
2019-10-01 11:35:00 +00:00
ApiArticle.getArticle(this.path)
2022-07-20 00:33:06 +00:00
.then((result) => {
this.data = result
2022-07-27 08:41:18 +00:00
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
}
2022-07-20 00:33:06 +00:00
if (!this.data.article) {
this.error = 'Article not found'
}
2022-07-20 00:33:06 +00:00
}, (err) => {
this.error = err.message
})
2022-07-20 00:33:06 +00:00
.then(() => {
clearTimeout(this.showLoading)
this.showLoading = null
this.loading = false
m.redraw()
})
},
view: function(vnode) {
2022-07-27 08:41:18 +00:00
let article = this.data.article
return (
this.loading ?
2019-10-02 21:48:50 +00:00
m('article.article', m('div.loading-spinner'))
2021-02-05 11:50:01 +00:00
: this.error
? m('div.error-wrapper', m('div.error', {
onclick: function() {
vnode.state.error = ''
vnode.state.fetchArticle(vnode)
},
}, 'Article error: ' + this.error))
: m('article.article', [
2022-07-27 08:41:18 +00:00
article.page_path
? m('div.goback', ['« ', m(m.route.Link, { href: '/page/' + article.page_path }, article.page_name)])
2022-07-20 00:33:06 +00:00
: null,
2022-07-27 08:41:18 +00:00
m('header', m('h1', article.name)),
2021-02-05 11:50:01 +00:00
m('.fr-view', [
2022-07-27 08:41:18 +00:00
article.pictureFallback
2021-02-05 11:50:01 +00:00
? m('a.cover', {
rel: 'noopener',
2022-07-27 08:41:18 +00:00
href: article.media_path,
}, [
m('picture', [
m('source', {
srcset: article.pictureAvif,
sizes: article.pictureCover,
type: 'image/avif',
}),
m('img', {
srcset: article.pictureJpeg,
sizes: article.pictureCover,
alt: 'Image for news item ' + article.name,
src: article.pictureFallback,
}),
]),
])
2021-02-05 11:50:01 +00:00
: null,
2022-07-27 08:41:18 +00:00
article.content.blocks.map(block => {
return m(EditorBlock, { block: block })
}),
2022-07-20 00:33:06 +00:00
this.data.files.map(function(file) {
return m(Fileinfo, { file: file })
}),
2021-02-05 11:50:01 +00:00
m('div.entrymeta', [
'Posted ',
2022-07-27 08:41:18 +00:00
article.page_path
2022-07-20 00:33:06 +00:00
? [
'in',
2022-07-27 08:41:18 +00:00
m(m.route.Link, { href: '/page/' + article.page_path }, article.page_name)
2022-07-20 00:33:06 +00:00
]
: '',
2022-07-27 08:41:18 +00:00
'at ' + (article.publish_at.replace('T', ' ').split('.')[0]).substr(0, 16),
' by ' + (article.admin_name || 'Admin'),
2021-02-05 11:50:01 +00:00
]),
2019-10-02 18:47:20 +00:00
]),
2021-02-05 11:50:01 +00:00
Authentication.currentUser
? m('div.admin-actions', [
m('span', 'Admin controls:'),
2022-07-27 08:41:18 +00:00
m(m.route.Link, { href: '/admin/articles/' + article.path }, 'Edit article'),
2019-10-02 00:55:15 +00:00
])
2021-02-05 11:50:01 +00:00
: null,
this.showcomments
? m('div.commentcontainer', [
m('div#disqus_thread', { oncreate: function() {
let fullhost = window.location.protocol + '//' + window.location.host
/*eslint-disable */
window.disqus_config = function () {
this.page.url = fullhost + '/article/' + vnode.state.article.path
this.page.identifier = 'article-' + vnode.state.article.id
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://nfp-moe.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})()
/*eslint-enable */
}}, m('div.loading-spinner')),
])
: m('button.opencomments', {
onclick: function() { vnode.state.showcomments = true },
}, 'Open comment discussion'),
])
)
},
}
module.exports = Article
/*
<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
/*
var disqus_config = function () {
this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://nfp-moe.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
*/