nfp_sites/nfp_moe/app/article_slim.js

75 lines
2.0 KiB
JavaScript

const Fileinfo = require('./fileinfo')
const media = require('./media')
const Articleslim = {
oninit: function(vnode) {
this.lastId = null
this.onbeforeupdate(vnode)
},
strip: function(html) {
var doc = new DOMParser().parseFromString(html, 'text/html')
var out = (doc.body.textContent || '').replace(/([\.!?])/g, '$1 ')
var splitted = out.split('.')
if (splitted.length > 2) {
return splitted.slice(0, 2).join('.') + '...'
}
return out
},
onbeforeupdate: function(vnode) {
let article = vnode.attrs.article
if (this.lastId !== article.id) {
this.lastId = article.id
this.description = null
if (article.content) {
for (let i = 0; i < article.content.blocks.length; i++) {
if (article.content.blocks[i].type === 'paragraph') {
this.description = article.content.blocks[i].data.text
break
} else if (article.content.blocks[i].type === 'htmlraw') {
this.description = this.strip(article.content.blocks[i].data.html)
break
}
}
}
this.pictureData = media.generatePictureSource(
article,
'(max-width: 440px) calc(100vw - 40px), '
+ '124px')
}
},
view: function(vnode) {
let article = vnode.attrs.article
return m('articleslim', [
media.getArticlePicture(
this.pictureData,
true,
'/article/' + article.path,
'Image for news item ' + article.name,
m('a.cover.nobg')
),
m('div', [
m(m.route.Link,
{ class: 'title', href: '/article/' + article.path },
article.name
),
(article.files && article.files.length
? article.files.map(function(file) {
return m(Fileinfo, { file: file, slim: true })
})
: this.description
? m('p.description', this.description)
: null),
]),
])
},
}
module.exports = Articleslim