nfp_sites/app/widgets/newsentry.js

100 lines
3 KiB
JavaScript
Raw Normal View History

2019-09-14 19:03:38 +00:00
const Fileinfo = require('./fileinfo')
2019-09-13 13:33:10 +00:00
const Newsentry = {
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 || ''
var splitted = out.split('.')
if (splitted.length > 2) {
2019-09-14 19:03:38 +00:00
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
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
}
}
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'
2019-10-02 21:48:50 +00:00
2022-07-27 08:41:18 +00:00
this.pictureCover = '(max-width: 440px) calc(100vw - 40px), '
+ '124px'
2019-10-02 21:48:50 +00:00
} else {
2022-07-27 08:41:18 +00:00
this.pictureFallback = null
this.pictureJpeg = null
this.pictureAvif = null
this.pictureCover = null
2019-10-02 21:48:50 +00:00
}
}
2022-07-27 08:41:18 +00:00
},
view: function(vnode) {
let article = vnode.attrs.article
2019-09-13 13:33:10 +00:00
return m('newsentry', [
2022-07-27 08:41:18 +00:00
this.pictureFallback
2021-01-05 19:12:10 +00:00
? m(m.route.Link, {
2022-07-27 08:41:18 +00:00
class: 'cover',
href: '/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,
}),
])
)
2019-09-13 13:33:10 +00:00
: m('a.cover.nobg'),
m('div.entrycontent', [
2019-09-14 19:03:38 +00:00
m('div.title', [
m(m.route.Link,
2022-07-27 08:41:18 +00:00
{ href: '/article/' + article.path },
m('h3', [article.name])
2019-09-14 19:03:38 +00:00
),
]),
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('span.entrydescription', this.description)
2019-09-14 19:03:38 +00:00
: null),
]),
2019-09-13 13:33:10 +00:00
])
},
}
module.exports = Newsentry