2022-03-30 11:40:05 +02:00
|
|
|
/**
|
2022-10-26 16:32:36 +02:00
|
|
|
* Prints VRE d4s-social-posts (from @from to @quantity number)
|
2022-03-30 11:40:05 +02:00
|
|
|
*/
|
2022-03-22 11:27:28 +01:00
|
|
|
window.customElements.define('d4s-social-posts', class extends HTMLElement {
|
|
|
|
|
2022-12-14 14:09:37 +01:00
|
|
|
#basepath = 'https://api.d4science.org'
|
2022-12-14 10:59:14 +01:00
|
|
|
|
2022-10-26 16:32:36 +02:00
|
|
|
#from = 1
|
|
|
|
#quantity = 10
|
2022-12-14 10:59:14 +01:00
|
|
|
#nextIdx = null
|
|
|
|
#busy = false
|
|
|
|
|
2022-10-26 16:32:36 +02:00
|
|
|
#boot = null
|
|
|
|
#shadowRoot = null
|
2022-12-14 10:59:14 +01:00
|
|
|
#unorderedlist = null
|
2022-10-26 16:32:36 +02:00
|
|
|
|
2022-12-14 14:09:37 +01:00
|
|
|
//#restRecentPosts = '/rest/2/posts/get-posts-vre' // old get, gets all posts unfiltered
|
|
|
|
#restRecentPosts = '/rest/2/posts/get-recent-posts-vre-by-range'
|
|
|
|
#restComments = '/rest/2/comments/get-comments-by-post-id'
|
2022-10-26 16:32:36 +02:00
|
|
|
|
2022-12-14 10:59:14 +01:00
|
|
|
#style = `<style>
|
|
|
|
.d4s-social-posts {
|
|
|
|
list-style: none;
|
|
|
|
padding-left: 0;
|
|
|
|
}
|
|
|
|
.d4s-social-post {
|
|
|
|
margin: 5px 0 5px 0;
|
|
|
|
padding: 5px 0 5px 0;
|
|
|
|
border-bottom: 1px solid lightgray;
|
|
|
|
}
|
|
|
|
.d4s-social-post:last-child {
|
|
|
|
border-bottom: none;
|
|
|
|
}
|
|
|
|
.d4s-post-heading {
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
gap: 10px;
|
|
|
|
margin-bottom: 5px;
|
|
|
|
}
|
|
|
|
.d4s-post-avatar img {
|
|
|
|
object-fit: cover;
|
|
|
|
width: 40px;
|
|
|
|
height: 40px;
|
|
|
|
border-radius: 50%;
|
|
|
|
}
|
|
|
|
.d4s-post-meta {
|
|
|
|
display: flex;
|
|
|
|
flex-flow: column;
|
|
|
|
}
|
|
|
|
.d4s-post-meta .fullname {
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
.d4s-post-meta .time {
|
|
|
|
font-size: small;
|
|
|
|
}
|
|
|
|
.d4s-post-body {
|
|
|
|
color: var(--color);
|
|
|
|
}
|
|
|
|
.d4s-post-footer {
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
gap: 10px;
|
|
|
|
}
|
|
|
|
.d4s-post-comments {
|
|
|
|
list-style: none;
|
|
|
|
}
|
|
|
|
.d4s-post-comment {
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
gap: 5px;
|
|
|
|
}
|
|
|
|
.d4s-comment-avatar img {
|
|
|
|
object-fit: cover;
|
|
|
|
width: 40px;
|
|
|
|
height: 40px;
|
|
|
|
border-radius: 50%;
|
|
|
|
}
|
|
|
|
.d4s-comment {
|
|
|
|
font-size: small;
|
|
|
|
margin: 0 5px;
|
|
|
|
}
|
|
|
|
.d4s-comment-body {
|
|
|
|
padding: 6px;
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
border-radius: 9px;
|
|
|
|
}
|
|
|
|
.d4s-comment-body .fullname {
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
.d4s-comment-footer {
|
|
|
|
margin: 3px 5px 6px 5px;
|
|
|
|
}
|
|
|
|
.d4s-post-footer {
|
|
|
|
margin: 8px 0 6px 0;
|
|
|
|
}
|
|
|
|
.comments-nr {
|
|
|
|
display: flex;
|
|
|
|
gap: 5px;
|
|
|
|
}
|
|
|
|
.likes-nr {
|
|
|
|
display: flex;
|
|
|
|
gap: 5px;
|
|
|
|
}
|
|
|
|
.d4s-post-attachment {
|
|
|
|
word-break: break-word;
|
|
|
|
font-size: small;
|
|
|
|
margin-top: 8px;
|
|
|
|
display: flex;
|
|
|
|
border: 1px solid lightgray;
|
|
|
|
border-radius: 2px;
|
|
|
|
}
|
|
|
|
.d4s-post-attachment .attachment-div {
|
|
|
|
display: contents;
|
|
|
|
}
|
|
|
|
.d4s-post-attachment .attachment-img {
|
|
|
|
object-fit: cover;
|
|
|
|
width: 120px;
|
|
|
|
height: 100px;
|
|
|
|
}
|
|
|
|
.d4s-post-attachment .attachment-info {
|
|
|
|
display: flex;
|
|
|
|
flex-flow: column;
|
|
|
|
gap: 8px;
|
|
|
|
padding: 8px;
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
flex: 1;
|
|
|
|
}
|
|
|
|
.d4s-post-attachment .attachment-info .title {
|
|
|
|
word-break: break-word;
|
|
|
|
}
|
|
|
|
</style>`
|
|
|
|
|
2022-04-28 15:55:38 +02:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.#boot = document.querySelector("d4s-boot-2")
|
|
|
|
this.#shadowRoot = this.attachShadow({mode: 'open'})
|
2022-12-14 10:59:14 +01:00
|
|
|
var template = document.createElement('template')
|
|
|
|
template.innerHTML = this.#style
|
|
|
|
this.#shadowRoot.appendChild(template.content.cloneNode(true))
|
|
|
|
var ul = document.createElement('ul')
|
|
|
|
ul.classList.add('d4s-social-posts')
|
|
|
|
this.#unorderedlist = ul
|
|
|
|
this.#shadowRoot.appendChild(ul)
|
2022-04-28 15:55:38 +02:00
|
|
|
}
|
2022-03-22 11:27:28 +01:00
|
|
|
|
2022-04-28 15:55:38 +02:00
|
|
|
connectedCallback() {
|
2022-10-26 16:32:36 +02:00
|
|
|
if (this.#boot) {
|
2022-12-14 10:59:14 +01:00
|
|
|
this.#nextIdx = this.#from
|
|
|
|
this.loadPosts()
|
2022-10-26 16:32:36 +02:00
|
|
|
} else {
|
|
|
|
console.error('<d4s-boot-2> webcomponent not found in this page')
|
|
|
|
}
|
2022-04-28 15:55:38 +02:00
|
|
|
}
|
2022-03-22 11:27:28 +01:00
|
|
|
|
2022-12-14 10:59:14 +01:00
|
|
|
loadPosts(nr) {
|
|
|
|
if (this.#busy) {
|
|
|
|
return
|
2022-10-26 16:32:36 +02:00
|
|
|
|
2022-12-14 10:59:14 +01:00
|
|
|
} else {
|
|
|
|
this.#busy = true
|
|
|
|
const url = new URL(this.#basepath + this.#restRecentPosts)
|
|
|
|
if (this.#nextIdx <= 0) {
|
|
|
|
this.#busy = false
|
|
|
|
throw 'Out of range index found'
|
|
|
|
}
|
|
|
|
var quparam = nr ? Number(nr) : this.#quantity
|
|
|
|
var params = {from: this.#nextIdx, quantity: quparam }
|
|
|
|
url.search = new URLSearchParams(params).toString()
|
|
|
|
console.log("url=" + url.href)
|
2022-04-28 15:55:38 +02:00
|
|
|
|
2022-12-14 10:59:14 +01:00
|
|
|
this.#boot.secureFetch(url).then(reply => {
|
|
|
|
if(reply.status !== 200) throw "status=" + reply.status
|
|
|
|
return reply.json()
|
|
|
|
|
|
|
|
}).then(data => {
|
|
|
|
//console.log(data)
|
|
|
|
//this.#nextIdx = Number(data.result.last_returned_post_timeline_index)
|
|
|
|
this.#nextIdx = Number(data.result.last_returned_post_timeline_index) - 1
|
|
|
|
//this.#nextIdx = this.#from + this.#quantity
|
|
|
|
this.renderPosts(data.result.posts)
|
|
|
|
//this.rawRenderResponse(data)
|
2022-04-28 15:55:38 +02:00
|
|
|
|
2022-12-14 10:59:14 +01:00
|
|
|
}).catch(err => {
|
|
|
|
const msg = 'Unable to load posts. '
|
|
|
|
console.error(msg + err)
|
|
|
|
throw msg
|
|
|
|
}).finally(() => {
|
|
|
|
|
|
|
|
this.#busy = false
|
|
|
|
})
|
|
|
|
}
|
2022-04-28 15:55:38 +02:00
|
|
|
}
|
2022-10-26 16:32:36 +02:00
|
|
|
|
2022-12-14 10:59:14 +01:00
|
|
|
morePosts(nr) {
|
|
|
|
this.loadPosts(nr)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPosts(posts) {
|
|
|
|
const ul = this.#unorderedlist
|
|
|
|
posts.forEach(post => {
|
|
|
|
//console.log(post)
|
2022-10-26 16:32:36 +02:00
|
|
|
var li = document.createElement('li')
|
|
|
|
li.id = post.key
|
|
|
|
li.classList.add('d4s-social-post')
|
|
|
|
const thumbnail = (!post.thumbnail_url.startsWith('http')) ? 'https://' + this.#boot.clientId + post.thumbnail_url : post.thumbnail_url
|
|
|
|
const datetime = new Date(0)
|
|
|
|
datetime.setUTCMilliseconds(post.time)
|
2022-12-14 10:59:14 +01:00
|
|
|
const datetimestr = datetime.toLocaleString([], {year: 'numeric', month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit'})
|
2022-10-26 16:32:36 +02:00
|
|
|
li.innerHTML = `
|
|
|
|
<div class="d4s-post-heading">
|
|
|
|
<div class="d4s-post-avatar">
|
2022-12-14 10:59:14 +01:00
|
|
|
<img src="${thumbnail}" onerror="this.style.display='none'"></slot>
|
2022-10-26 16:32:36 +02:00
|
|
|
</div>
|
|
|
|
<div class="d4s-post-meta">
|
|
|
|
<div class="fullname">${post.full_name}</div>
|
|
|
|
<div class="time">${datetimestr}</div>
|
2022-04-28 15:55:38 +02:00
|
|
|
</div>
|
2022-10-26 16:32:36 +02:00
|
|
|
</div>
|
|
|
|
<div class="d4s-post-body">${post.description}</div>
|
2022-12-14 10:59:14 +01:00
|
|
|
<div class="d4s-post-attachment"></div>
|
|
|
|
<div class="d4s-post-footer"></div>
|
2022-10-26 16:32:36 +02:00
|
|
|
`
|
2022-12-14 10:59:14 +01:00
|
|
|
|
|
|
|
if (post.comments_no != "0") {
|
|
|
|
var commsnr = document.createElement('div')
|
|
|
|
commsnr.classList.add('comments-nr')
|
|
|
|
commsnr.innerHTML += `
|
|
|
|
<img src="assets/comment-ellipsis.svg" width="18"/>
|
|
|
|
<span>${post.comments_no}</span>
|
|
|
|
`
|
|
|
|
li.querySelector('.d4s-post-footer').appendChild(commsnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post.likes_no != "0") {
|
|
|
|
var likesnr = document.createElement('div')
|
|
|
|
likesnr.classList.add('likes-nr')
|
|
|
|
likesnr.innerHTML += `
|
|
|
|
<img src="assets/thumbs-up.svg" width="18" />
|
|
|
|
<span>${post.likes_no}</span>
|
|
|
|
`
|
|
|
|
li.querySelector('.d4s-post-footer').appendChild(likesnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post.link_title !== "") {
|
|
|
|
var attimg = document.createElement('div')
|
|
|
|
attimg.classList.add('attachment-div')
|
2022-12-14 14:09:37 +01:00
|
|
|
if (post.uri_thumbnail != "null") {
|
|
|
|
attimg.innerHTML += `<img src="${post.uri_thumbnail}" class="attachment-img" onerror="this.style.display='none'"/>`
|
|
|
|
li.querySelector('.d4s-post-attachment').appendChild(attimg)
|
|
|
|
}
|
2022-12-14 10:59:14 +01:00
|
|
|
var attdesc = document.createElement('div')
|
|
|
|
attdesc.classList.add('attachment-info')
|
|
|
|
attdesc.innerHTML += `
|
|
|
|
<div class="title"><a href="${post.uri}">${post.link_title}</a></div>
|
|
|
|
<div class="url">${post.uri}</div>
|
|
|
|
<div class="description">${post.link_description}</div>
|
|
|
|
`
|
|
|
|
li.querySelector('.d4s-post-attachment').appendChild(attdesc)
|
|
|
|
} else {
|
|
|
|
li.querySelector('.d4s-post-attachment').remove()
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:32:36 +02:00
|
|
|
ul.appendChild(li)
|
|
|
|
|
2022-12-14 10:59:14 +01:00
|
|
|
let aimg = li.querySelector(".attachment-img")
|
|
|
|
if (aimg) {
|
|
|
|
aimg.addEventListener('load', (ev) => {
|
|
|
|
let prop = aimg.naturalWidth / aimg.naturalHeight
|
|
|
|
if (prop < 0.5 || prop > 2) {
|
|
|
|
aimg.style.objectFit = 'contain'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:32:36 +02:00
|
|
|
if (post.comments_no > 0) {
|
|
|
|
this.fetchComments(post.key)
|
2022-04-12 19:08:38 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-03-22 11:27:28 +01:00
|
|
|
|
2022-12-14 10:59:14 +01:00
|
|
|
rawRenderResponse(data) {
|
2022-10-26 16:32:36 +02:00
|
|
|
var pre = document.createElement('pre')
|
2022-12-14 10:59:14 +01:00
|
|
|
pre.innerText = JSON.stringify(data, null, " ")
|
2022-10-26 16:32:36 +02:00
|
|
|
this.#shadowRoot.appendChild(pre)
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchComments(postid) {
|
|
|
|
const url = new URL(this.#basepath + this.#restComments)
|
|
|
|
var params = {key: postid}
|
|
|
|
url.search = new URLSearchParams(params).toString()
|
|
|
|
|
|
|
|
this.#boot.secureFetch(url).then(reply => {
|
|
|
|
if(reply.status !== 200) throw "status=" + reply.status
|
|
|
|
return reply.json()
|
|
|
|
|
|
|
|
}).then(data => {
|
|
|
|
this.renderComments(postid, data)
|
|
|
|
|
|
|
|
}).catch(err => console.error(`Unable to fetch comments for post ${postid}. ` + err))
|
|
|
|
}
|
|
|
|
|
|
|
|
renderComments(postid, comments) {
|
|
|
|
var li = this.#shadowRoot.getElementById(postid)
|
|
|
|
var cul = document.createElement('ul')
|
|
|
|
cul.classList.add('d4s-post-comments')
|
|
|
|
comments.result.forEach(comment => {
|
|
|
|
var cli = document.createElement('li')
|
|
|
|
cli.id = comment.feedid
|
2022-12-14 10:59:14 +01:00
|
|
|
cli.classList.add('d4s-post-comment')
|
2022-10-26 16:32:36 +02:00
|
|
|
const thumbnail = (!comment.thumbnail_url.startsWith('http')) ? 'https://' + this.#boot.clientId + comment.thumbnail_url : comment.thumbnail_url
|
|
|
|
const datetime = new Date(0)
|
|
|
|
datetime.setUTCMilliseconds(comment.time)
|
2022-12-14 10:59:14 +01:00
|
|
|
const datetimestr = datetime.toLocaleString([], {year: 'numeric', month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit'})
|
2022-10-26 16:32:36 +02:00
|
|
|
cli.innerHTML = `
|
2022-12-14 10:59:14 +01:00
|
|
|
<div class="d4s-comment-avatar">
|
|
|
|
<img src="${thumbnail}" onerror="this.style.display='none'"></slot>
|
|
|
|
</div>
|
|
|
|
<div class="d4s-comment">
|
|
|
|
<div class="d4s-comment-body">
|
2022-10-26 16:32:36 +02:00
|
|
|
<div class="fullname">${comment.full_name}</div>
|
2022-12-14 10:59:14 +01:00
|
|
|
<div class="d4s-comment-text">${comment.text}</div>
|
|
|
|
</div>
|
|
|
|
<div class="d4s-comment-footer">
|
2022-10-26 16:32:36 +02:00
|
|
|
<div class="time">${datetimestr}</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
cul.appendChild(cli)
|
|
|
|
})
|
|
|
|
li.appendChild(cul)
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:08:38 +02:00
|
|
|
static get observedAttributes() {
|
2022-10-26 16:32:36 +02:00
|
|
|
return ["url", "from", "quantity"];
|
2022-04-12 19:08:38 +02:00
|
|
|
}
|
2022-03-22 11:27:28 +01:00
|
|
|
|
2022-04-12 19:08:38 +02:00
|
|
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
|
|
if (oldValue !== newValue) {
|
|
|
|
switch (name) {
|
|
|
|
case "url":
|
2022-04-28 15:55:38 +02:00
|
|
|
this.#basepath = newValue
|
|
|
|
break
|
2022-10-26 16:32:36 +02:00
|
|
|
case "from":
|
2022-12-14 10:59:14 +01:00
|
|
|
this.#from = Number(newValue)
|
2022-10-26 16:32:36 +02:00
|
|
|
break
|
2022-04-12 19:08:38 +02:00
|
|
|
case "quantity":
|
2022-12-14 10:59:14 +01:00
|
|
|
this.#quantity = Number(newValue)
|
2022-04-28 15:55:38 +02:00
|
|
|
break
|
2022-04-12 19:08:38 +02:00
|
|
|
}
|
2022-03-22 11:27:28 +01:00
|
|
|
}
|
2022-04-12 19:08:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get url() {
|
|
|
|
return this.#basepath
|
|
|
|
}
|
2022-03-22 11:27:28 +01:00
|
|
|
|
2022-04-12 19:08:38 +02:00
|
|
|
set url(url) {
|
|
|
|
this.#basepath = url
|
|
|
|
this.setAttribute("url", url)
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:32:36 +02:00
|
|
|
get from() {
|
|
|
|
return this.#from
|
|
|
|
}
|
|
|
|
|
|
|
|
set from(from) {
|
2022-12-14 10:59:14 +01:00
|
|
|
this.#from = Number(from)
|
2022-10-26 16:32:36 +02:00
|
|
|
this.setAttribute("from", from)
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:08:38 +02:00
|
|
|
get quantity() {
|
|
|
|
return this.#quantity
|
|
|
|
}
|
|
|
|
|
|
|
|
set quantity(quantity) {
|
2022-12-14 10:59:14 +01:00
|
|
|
this.#quantity = Number(quantity)
|
2022-10-26 16:32:36 +02:00
|
|
|
this.setAttribute("quantity", quantity)
|
2022-04-12 19:08:38 +02:00
|
|
|
}
|
2022-12-14 10:59:14 +01:00
|
|
|
});
|