web-components/social/d4s-social.js

139 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-03-30 11:40:05 +02:00
/**
* Prints VRE d4s-social-posts (limiting to @quantity number)
*/
2022-03-22 11:27:28 +01:00
window.customElements.define('d4s-social-posts', class extends HTMLElement {
2022-04-22 20:00:15 +02:00
#basepath = 'https://api.d4science.org/rest'
//#basepath = 'https://api.dev.d4science.org/rest'
2022-03-22 11:27:28 +01:00
#quantity = 20
#boot = null;
#data = null;
#shadowRoot = null;
2022-03-22 11:27:28 +01:00
constructor() {
super()
this.#boot = document.querySelector("d4s-boot-2")
this.#shadowRoot = this.attachShadow({mode: 'open'})
2022-03-22 11:27:28 +01:00
}
connectedCallback() {
this.fetchPosts()
2022-03-22 11:27:28 +01:00
}
fetchPosts(){
const url = this.#basepath + '/2/posts/get-posts-vre'
this.#boot.secureFetch(url).then(
reply=>{
if(reply.status !== 200) throw "Unable to fetch posts";
return reply.json()
2022-03-22 11:27:28 +01:00
}
).then(data => {
this.#data = data
this.renderPosts()
}).catch(err=>alert("Unable to fetch posts " + err))
2022-03-22 11:27:28 +01:00
}
renderPosts(){
var ul = document.createElement('ul')
this.#data.result.reverse().forEach(post => {
if (this.#quantity-- > 0) {
var li = document.createElement('li')
li.setAttribute('class', 'd4s-social-post')
const datetime = new Date(0)
datetime.setUTCMilliseconds(post.time)
const thumbnail = (!post.thumbnail_url.startsWith('http')) ? 'https://' + this.#boot.clientId + post.thumbnail_url : post.thumbnail_url
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">${datetime.toLocaleString()}</div>
</div>
</div>
<div class="d4s-post-body">${post.description}</div>
<div class="d4s-post-footer"></div>
`
ul.appendChild(li)
}
})
this.#shadowRoot.appendChild(ul)
}
2022-03-22 11:27:28 +01:00
static get observedAttributes() {
return ["url", "quantity"];
}
2022-03-22 11:27:28 +01:00
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) {
switch (name) {
case "url":
this.#basepath = newValue
break
case "quantity":
this.#quantity = newValue
break
}
2022-03-22 11:27:28 +01:00
}
}
get url() {
return this.#basepath
}
2022-03-22 11:27:28 +01:00
set url(url) {
this.#basepath = url
this.setAttribute("url", url)
}
get quantity() {
return this.#quantity
}
set quantity(quantity) {
this.#quantity = quantity
this.setAttribute("quantity", quantity)
}
2022-03-30 11:40:05 +02:00
})
window.customElements.define('d4s-social-search', class extends HTMLElement {
constructor() {
super()
}
connectedCallback() {
//console.log(typeof d4s)
//console.log(this)
//this.attachShadow({mode: "open"})
//console.log(this.shadowRoot.querySelector("*[name=search]"))
//const shadowRoot = this.attachShadow({mode: 'open'})
//console.log(shadowRoot.innerHTML)
console.log("d4s-social-search connectedCallback")
var d4s = document.querySelector('d4s-boot-2')
/*
d4s.service(
'https://api.dev.d4science.org/rest/2/full-text-search/search-by-query?query=d4science',
'GET', null,
(resp) => {
document.getElementById('output').innerText = resp
},
() => { alert('Forbidden') }
)
d4s.service(
'https://api.dev.d4science.org/rest/2/posts/get-posts-user-quantity',
'GET', null,
(resp) => {
document.getElementById('output').innerText = resp
},
() => { alert('Forbidden') }
)
*/
}
})