nfp_sites/nfp_moe/app/article_slim.js

75 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-09-14 19:03:38 +00:00
const Fileinfo = require('./fileinfo')
2022-08-08 07:22:41 +00:00
const media = require('./media')
2019-09-13 13:33:10 +00:00
const Articleslim = {
2022-07-27 08:41:18 +00:00
oninit: function(vnode) {
this.lastId = null
this.onbeforeupdate(vnode)
},
2019-09-14 19:03:38 +00:00
strip: function(html) {
2022-07-27 08:41:18 +00:00
var doc = new DOMParser().parseFromString(html, 'text/html')
var out = (doc.body.textContent || '').replace(/([\.!?])/g, '$1 ')
2022-07-27 08:41:18 +00:00
var splitted = out.split('.')
if (splitted.length > 2) {
return splitted.slice(0, 2).join('.') + '...'
2022-07-27 08:41:18 +00:00
}
return out
2019-09-14 19:03:38 +00:00
},
2022-07-27 08:41:18 +00:00
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
}
2022-07-27 08:41:18 +00:00
}
}
2022-08-08 07:22:41 +00:00
this.pictureData = media.generatePictureSource(
article,
'(max-width: 440px) calc(100vw - 40px), '
+ '124px')
2019-10-02 21:48:50 +00:00
}
2022-07-27 08:41:18 +00:00
},
view: function(vnode) {
let article = vnode.attrs.article
return m('articleslim', [
2022-08-08 07:22:41 +00:00
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
),
2022-07-27 08:41:18 +00:00
(article.files && article.files.length
? article.files.map(function(file) {
2019-09-14 19:03:38 +00:00
return m(Fileinfo, { file: file, slim: true })
})
2022-07-27 08:41:18 +00:00
: this.description
? m('p.description', this.description)
2019-09-14 19:03:38 +00:00
: null),
]),
2019-09-13 13:33:10 +00:00
])
},
}
module.exports = Articleslim