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-04-28 15:55:38 +02:00
|
|
|
#basepath = 'https://api.d4science.org/rest'
|
|
|
|
//#basepath = 'https://api.dev.d4science.org/rest'
|
2022-10-26 16:32:36 +02:00
|
|
|
#from = 1
|
|
|
|
#quantity = 10
|
|
|
|
#boot = null
|
|
|
|
#data = null
|
|
|
|
#shadowRoot = null
|
|
|
|
|
|
|
|
//const url = this.#basepath + '/2/posts/get-posts-vre' // old get, gets all posts unfiltered
|
|
|
|
#restRecentPosts = '/2/posts/get-recent-posts-vre-by-range'
|
|
|
|
#restComments = '/2/comments/get-comments-by-post-id'
|
|
|
|
|
2022-04-28 15:55:38 +02:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.#boot = document.querySelector("d4s-boot-2")
|
|
|
|
this.#shadowRoot = this.attachShadow({mode: 'open'})
|
|
|
|
}
|
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) {
|
|
|
|
this.fetchPosts()
|
|
|
|
} 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-04-28 15:55:38 +02:00
|
|
|
fetchPosts() {
|
2022-10-26 16:32:36 +02:00
|
|
|
const url = new URL(this.#basepath + this.#restRecentPosts)
|
|
|
|
var params = {from: this.#from, quantity: this.#quantity}
|
|
|
|
url.search = new URLSearchParams(params).toString()
|
|
|
|
|
2022-04-28 15:55:38 +02:00
|
|
|
this.#boot.secureFetch(url).then(reply => {
|
2022-10-26 16:32:36 +02:00
|
|
|
if(reply.status !== 200) throw "status=" + reply.status
|
2022-04-28 15:55:38 +02:00
|
|
|
return reply.json()
|
|
|
|
|
|
|
|
}).then(data => {
|
2022-10-26 16:32:36 +02:00
|
|
|
//console.log(data)
|
2022-04-28 15:55:38 +02:00
|
|
|
this.#data = data
|
|
|
|
this.renderPosts()
|
2022-10-26 16:32:36 +02:00
|
|
|
//this.rawRenderPosts()
|
2022-04-28 15:55:38 +02:00
|
|
|
|
2022-10-26 16:32:36 +02:00
|
|
|
}).catch(err => console.error("Unable to fetch posts. " + err))
|
2022-04-28 15:55:38 +02:00
|
|
|
}
|
2022-10-26 16:32:36 +02:00
|
|
|
|
2022-04-28 15:55:38 +02:00
|
|
|
renderPosts() {
|
|
|
|
var ul = document.createElement('ul')
|
2022-10-26 16:32:36 +02:00
|
|
|
this.#data.result.posts.forEach(post => {
|
|
|
|
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)
|
|
|
|
const datetimestr = datetime.toLocaleString()
|
|
|
|
li.innerHTML = `
|
|
|
|
<div class="d4s-post-heading">
|
|
|
|
<div class="d4s-post-avatar">
|
|
|
|
<img src="${thumbnail}" onerror="this.style.display='none'" width="32px" height="32px"></slot>
|
|
|
|
</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>
|
|
|
|
<div class="d4s-post-footer">
|
|
|
|
<div class="comments-nr">${post.comments_no}</div>
|
|
|
|
<div class="likes-nr">${post.likes_no}</div>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
ul.appendChild(li)
|
|
|
|
|
|
|
|
if (post.comments_no > 0) {
|
|
|
|
this.fetchComments(post.key)
|
2022-04-12 19:08:38 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
this.#shadowRoot.appendChild(ul)
|
|
|
|
}
|
2022-03-22 11:27:28 +01:00
|
|
|
|
2022-10-26 16:32:36 +02:00
|
|
|
rawRenderPosts() {
|
|
|
|
var pre = document.createElement('pre')
|
|
|
|
console.log(this.#data)
|
|
|
|
pre.innerText = JSON.stringify(this.#data, null, " ")
|
|
|
|
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) {
|
|
|
|
console.log(comments)
|
|
|
|
var li = this.#shadowRoot.getElementById(postid)
|
|
|
|
console.log(li)
|
|
|
|
var cul = document.createElement('ul')
|
|
|
|
cul.classList.add('d4s-post-comments')
|
|
|
|
comments.result.forEach(comment => {
|
|
|
|
var cli = document.createElement('li')
|
|
|
|
cli.id = comment.feedid
|
|
|
|
cli.classList.add('d4s-comment')
|
|
|
|
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)
|
|
|
|
const datetimestr = datetime.toLocaleString()
|
|
|
|
cli.innerHTML = `
|
|
|
|
<div class="d4s-comment-heading">
|
|
|
|
<div class="d4s-comment-avatar">
|
|
|
|
<img src="${thumbnail}" onerror="this.style.display='none'" width="32px" height="32px"></slot>
|
|
|
|
</div>
|
|
|
|
<div class="d4s-comment-meta">
|
|
|
|
<div class="fullname">${comment.full_name}</div>
|
|
|
|
<div class="time">${datetimestr}</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="d4s-comment-body">${comment.text}</div>
|
|
|
|
<div class="d4s-comment-footer">
|
|
|
|
</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":
|
|
|
|
this.#from = newValue
|
|
|
|
break
|
2022-04-12 19:08:38 +02:00
|
|
|
case "quantity":
|
2022-04-28 15:55:38 +02:00
|
|
|
this.#quantity = newValue
|
|
|
|
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) {
|
|
|
|
this.#from = from
|
|
|
|
this.setAttribute("from", from)
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:08:38 +02:00
|
|
|
get quantity() {
|
|
|
|
return this.#quantity
|
|
|
|
}
|
|
|
|
|
|
|
|
set quantity(quantity) {
|
|
|
|
this.#quantity = quantity
|
2022-10-26 16:32:36 +02:00
|
|
|
this.setAttribute("quantity", quantity)
|
2022-04-12 19:08:38 +02:00
|
|
|
}
|
2022-03-30 11:40:05 +02:00
|
|
|
})
|
|
|
|
|