nfp_sites/nfp_moe/app/article.js

97 lines
3.1 KiB
JavaScript

const Fileinfo = require('./fileinfo')
const EditorBlock = require('./editorblock')
const Article = {
oninit: function(vnode) {
this.lastId = null
this.onbeforeupdate(vnode)
},
onbeforeupdate: function(vnode) {
let article = vnode.attrs.article
if (this.lastId !== article.id) {
this.lastId = article.id
if (article.media_alt_prefix) {
this.pictureFallback = article.media_alt_prefix + '_small.jpg'
this.pictureJpeg = article.media_alt_prefix + '_small.jpg' + ' 720w, '
+ article.media_alt_prefix + '_medium.jpg' + ' 1300w, '
+ article.media_alt_prefix + '_large.jpg 1920w'
this.pictureAvif = article.media_alt_prefix + '_small.avif' + ' 720w, '
+ article.media_alt_prefix + '_medium.avif' + ' 1300w, '
+ article.media_alt_prefix + '_large.avif 1920w'
if (vnode.attrs.full) {
this.pictureCover = '(max-width: 1280) calc(100vw - 2rem), '
+ '1248px'
} else {
this.pictureCover = '(max-width: 639px) calc(100vw - 40px), '
+ '(max-width: 1000px) 300px, '
+ '400px'
}
} else {
this.pictureFallback = null
this.pictureJpeg = null
this.pictureAvif = null
this.pictureCover = null
}
}
},
view: function(vnode) {
let article = vnode.attrs.article
let files = vnode.attrs.files || article.files || []
return m('article', {
class: vnode.attrs.full ? 'fullsize' : '',
}, [
m(m.route.Link,
{ href: '/article/' + article.path, class: 'title' },
m('h2', article.name)
),
m('div.row', [
this.pictureFallback
? m(vnode.attrs.full ? 'a' : m.route.Link, {
class: 'cover',
target: vnode.attrs.full ? '_blank' : '',
href: vnode.attrs.full ? article.media_path : '/article/' + article.path,
},
m('picture', [
m('source', {
srcset: this.pictureAvif,
sizes: this.pictureCover,
type: 'image/avif',
}),
m('img', {
srcset: this.pictureJpeg,
sizes: this.pictureCover,
alt: 'Image for news item ' + article.name,
src: this.pictureFallback,
}),
])
)
: null,
m('div', [
m('div.description',
article.content.blocks.map(block => {
return m(EditorBlock, { block: block })
}),
),
files.map(function(file) {
return m(Fileinfo, { file: file, trim: true })
}),
m('p.meta', [
'Posted ',
(article.page_path ? ['in ', m(m.route.Link, { href: '/page/' + article.page_path }, article.page_name), ' '] : ''),
'at ' + (article.publish_at.replace('T', ' ').split('.')[0]).substr(0, 16),
' by ' + (article.admin_name || 'Admin'),
]),
]),
]),
])
},
}
module.exports = Article