2022-05-02 12:21:55 +02:00
|
|
|
var D4S_STORAGE_SCRIPT = document.currentScript
|
|
|
|
|
2022-03-30 11:42:08 +02:00
|
|
|
/**
|
|
|
|
* Base class of <d4s-storage-tree> and <d4s-storage-folder>
|
|
|
|
*/
|
|
|
|
class D4SStorageHtmlElement extends HTMLElement {
|
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
#d4smissmsg = 'Required d4s-boot-2 component not found'
|
|
|
|
#baseurl = 'https://api.d4science.org/workspace'
|
|
|
|
//#baseurl = 'https://workspace-repository.dev.d4science.org/storagehub/workspace'
|
|
|
|
#boot = null
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.#boot = document.querySelector('d4s-boot-2')
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
get boot() {
|
|
|
|
if (this.#boot == null) {
|
|
|
|
this.#boot = document.querySelector('d4s-boot-2')
|
|
|
|
if (!this.#boot) {
|
|
|
|
throw this.#d4smissmsg
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
return this.#boot
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
appendStylesheets(root) {
|
|
|
|
const linkElem1 = document.createElement('link')
|
|
|
|
linkElem1.setAttribute('rel', 'stylesheet')
|
|
|
|
linkElem1.setAttribute('href', 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css')
|
|
|
|
root.appendChild(linkElem1)
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
buildListUrl(id) {
|
|
|
|
return this.#baseurl + '/items/' + id + '/children'
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
get baseUrl() {
|
|
|
|
return this.#baseurl
|
|
|
|
}
|
|
|
|
|
|
|
|
set baseUrl(url) {
|
|
|
|
this.#baseurl = url
|
|
|
|
this.setAttribute("baseurl", url)
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds storagehub VREFolder tree
|
|
|
|
* <d4s-storage-tree
|
|
|
|
* /@filelist-id: identifier of d4s-storage-folder component
|
2022-03-30 16:40:55 +02:00
|
|
|
* /@base-url: the base endpoint url [defaults to: https://api.d4science.org/workspace]
|
2022-03-30 11:42:08 +02:00
|
|
|
*/
|
|
|
|
window.customElements.define('d4s-storage-tree', class extends D4SStorageHtmlElement {
|
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
#storageFilelistId = null
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
#loadedClass = 'loaded'
|
|
|
|
#selectedClass = 'selected'
|
|
|
|
#selectedbgcolor = 'lightgray'
|
|
|
|
#shadowRoot
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.#shadowRoot = this.attachShadow({mode: 'open'})
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Notes:
|
|
|
|
API docs: https://gcube.wiki.gcube-system.org/gcube/StorageHub_REST_API
|
|
|
|
Preprod: https://accounts.pre.d4science.org
|
|
|
|
Sample context: %2Fpred4s%2Fpreprod%2FpreVRE
|
|
|
|
User workspace: https://storagehub.pre.d4science.net/storagehub/workspace
|
|
|
|
vrefolder: https://storagehub.pre.d4science.net/storagehub/workspace/vrefolder
|
|
|
|
-> and if "id": "e0cd15d2-0071-43ca-bc42-aef8d80660fe",
|
|
|
|
the tree dir of vrefolder: https://storagehub.pre.d4science.net/storagehub/workspace/items/e0cd15d2-0071-43ca-bc42-aef8d80660fe/children
|
|
|
|
download of a file (domenica.jpg): https://storagehub.pre.d4science.net/storagehub/workspace/items/a5086811-59d1-4230-99fa-98431e820cf1/download
|
|
|
|
*/
|
|
|
|
connectedCallback() {
|
|
|
|
this.appendStylesheets(this.#shadowRoot)
|
|
|
|
|
|
|
|
var div = document.createElement('div')
|
|
|
|
const style = `
|
|
|
|
<style>
|
|
|
|
.selected > *:first-child {
|
|
|
|
background-color: ${this.#selectedbgcolor};
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
ul, li {
|
|
|
|
list-style-type: none;
|
|
|
|
}
|
|
|
|
li {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
`
|
|
|
|
div.innerHTML = style
|
|
|
|
|
|
|
|
this.boot.secureFetch(this.baseUrl + '/vrefolder').then((reply) => {
|
|
|
|
if (reply.status !== 200) {
|
|
|
|
throw "Unable to load folder"
|
|
|
|
}
|
|
|
|
return reply.json()
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
}).then(data => {
|
|
|
|
this.parseResponse(div, data)
|
|
|
|
this.#shadowRoot.appendChild(div)
|
|
|
|
|
|
|
|
}).catch(err => console.error(err))
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
parseResponse(parentElement, jresp) {
|
|
|
|
parentElement.classList.add(this.#loadedClass)
|
|
|
|
var ul = document.createElement('ul')
|
|
|
|
ul.classList.add('nested')
|
|
|
|
if (jresp.item) {
|
|
|
|
const itemname = jresp.item.displayName
|
|
|
|
const itemid = jresp.item.id
|
|
|
|
const path = this.buildListUrl(itemid)
|
|
|
|
ul.appendChild(this.createListItem(itemname, itemid))
|
|
|
|
parentElement.appendChild(ul)
|
|
|
|
|
|
|
|
} else if (jresp.itemlist) {
|
|
|
|
jresp
|
|
|
|
.itemlist
|
|
|
|
.filter(item => item['@class'].includes('FolderItem'))
|
|
|
|
.forEach(item => {
|
|
|
|
const itemname = item.name
|
|
|
|
const itemid = item.id
|
|
|
|
const path = this.buildListUrl(itemid)
|
|
|
|
ul.appendChild(this.createListItem(itemname, itemid))
|
2022-03-30 11:42:08 +02:00
|
|
|
})
|
2022-04-27 19:40:27 +02:00
|
|
|
parentElement.appendChild(ul)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.error(jresp)
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
createListItem(label, id) {
|
|
|
|
var li = document.createElement('li')
|
|
|
|
li.setAttribute('data-id', id)
|
|
|
|
var child = document.createElement('span')
|
|
|
|
child.innerHTML = label
|
|
|
|
li.appendChild(child)
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
li.addEventListener('click', (ev) => {
|
|
|
|
ev.stopPropagation()
|
|
|
|
this.select(ev.currentTarget.getAttribute('data-id'))
|
|
|
|
})
|
|
|
|
return li
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
select(id) {
|
|
|
|
var li = this.shadowRoot.querySelector(`*[data-id='${id}']`)
|
|
|
|
this.displayPath(li)
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
// if it was already selected then do nothing
|
|
|
|
if (li.classList.contains(this.#selectedClass)) {
|
|
|
|
this.toggleULDisplay(li)
|
|
|
|
return
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
// switch selected and lists folder contents
|
|
|
|
if (!li.classList.contains(this.#selectedClass)) {
|
|
|
|
const selected = this.shadowRoot.querySelector('.' + this.#selectedClass)
|
|
|
|
if (selected) {
|
|
|
|
selected.classList.remove(this.#selectedClass)
|
|
|
|
}
|
|
|
|
li.classList.add(this.#selectedClass)
|
|
|
|
if (this.#storageFilelistId) {
|
|
|
|
var folder = document.getElementById(this.#storageFilelistId)
|
|
|
|
folder.join(this)
|
|
|
|
folder.list(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (li.classList.contains(this.#loadedClass)) {
|
|
|
|
return
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
this.boot.secureFetch(this.buildListUrl(id)).then(reply => {
|
|
|
|
if (reply.status !== 200) {
|
|
|
|
throw "Unable to load folder"
|
|
|
|
}
|
|
|
|
return reply.json()
|
|
|
|
|
|
|
|
}).then(data => {
|
|
|
|
this.parseResponse(li, data)
|
|
|
|
|
|
|
|
}).catch(err => console.error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
displayPath(li) {
|
|
|
|
var curr = li
|
|
|
|
while (curr.parentElement != null) {
|
|
|
|
if (curr.parentElement.tagName == 'UL') {
|
|
|
|
curr.parentElement.style.display = 'block'
|
|
|
|
}
|
|
|
|
curr = curr.parentElement
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
toggleULDisplay(li) {
|
|
|
|
const ul = li.querySelector('ul')
|
|
|
|
if (ul) {
|
|
|
|
ul.style.display = (ul.style.display === 'none') ? 'block' : 'none'
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
static get observedAttributes() {
|
|
|
|
return ["base-url", "filelist-id"]
|
|
|
|
}
|
|
|
|
|
|
|
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
|
|
if (oldValue !== newValue) {
|
|
|
|
switch (name) {
|
|
|
|
case "filelist-id":
|
|
|
|
this.#storageFilelistId = newValue
|
|
|
|
break
|
|
|
|
case "base-url":
|
|
|
|
this.baseUrl = newValue
|
|
|
|
break
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get filelistId() {
|
|
|
|
return this.#storageFilelistId
|
|
|
|
}
|
|
|
|
|
|
|
|
set filelistId(id) {
|
|
|
|
this.#storageFilelistId = id
|
|
|
|
this.setAttribute("filelist-id", id)
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists elements in selected folder
|
|
|
|
*
|
|
|
|
* <d4s-storage-folder
|
|
|
|
* /@id: specify the id to join this component with storage tree component
|
2022-03-30 16:40:55 +02:00
|
|
|
* /@base-url: the base endpoint url [defaults to: https://api.d4science.org/workspace]
|
2022-03-30 11:42:08 +02:00
|
|
|
*/
|
|
|
|
window.customElements.define('d4s-storage-folder', class extends D4SStorageHtmlElement {
|
|
|
|
|
2022-04-12 19:16:15 +02:00
|
|
|
#d4sstorageTree = null
|
|
|
|
#selectedbgcolor = 'lightgray'
|
2022-05-02 12:21:55 +02:00
|
|
|
#srcbaseurl = null
|
2022-04-12 19:16:15 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super()
|
2022-05-02 12:21:55 +02:00
|
|
|
if (D4S_STORAGE_SCRIPT) {
|
|
|
|
const d4ssrc = D4S_STORAGE_SCRIPT.src
|
|
|
|
this.#srcbaseurl = (d4ssrc) ? d4ssrc.substring(0, d4ssrc.lastIndexOf('/')) : ""
|
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
const shadowRoot = this.attachShadow({mode: 'open'})
|
|
|
|
this.appendStylesheets(shadowRoot)
|
|
|
|
const style = document.createElement('style')
|
|
|
|
style.innerHTML = `
|
2022-04-27 19:40:27 +02:00
|
|
|
span {
|
|
|
|
cursor: pointer;
|
|
|
|
user-select: none;
|
|
|
|
-moz-user-select: none;
|
|
|
|
-webkit-user-select: none;
|
|
|
|
-ms-user-select: none;
|
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
`
|
|
|
|
shadowRoot.appendChild(style)
|
|
|
|
this.createContainer()
|
|
|
|
}
|
|
|
|
|
|
|
|
createContainer() {
|
|
|
|
var div = document.createElement('div')
|
|
|
|
div.classList.add('d4s-folder')
|
|
|
|
var child = this.shadowRoot.querySelector('.d4s-folder')
|
|
|
|
if (child) {
|
2022-04-27 19:40:27 +02:00
|
|
|
this.shadowRoot.removeChild(child)
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
this.shadowRoot.appendChild(div)
|
|
|
|
return div
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-12 19:16:15 +02:00
|
|
|
join(tree) {
|
|
|
|
if (this.#d4sstorageTree == null) {
|
2022-04-27 19:40:27 +02:00
|
|
|
this.#d4sstorageTree = tree
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
list(folderId) {
|
2022-04-27 19:40:27 +02:00
|
|
|
this.boot.secureFetch(this.buildListUrl(folderId)).then(reply => {
|
|
|
|
if (reply.status !== 200) {
|
|
|
|
throw "Unable to build list url"
|
|
|
|
}
|
|
|
|
return reply.json()
|
|
|
|
|
|
|
|
}).then(data => {
|
2022-04-12 19:51:38 +02:00
|
|
|
this.parseResponse(data)
|
2022-04-27 19:40:27 +02:00
|
|
|
|
|
|
|
}).catch(err => console.error(err))
|
2022-04-12 19:16:15 +02:00
|
|
|
}
|
|
|
|
|
2022-04-12 19:51:38 +02:00
|
|
|
parseResponse(jresp) {
|
2022-04-12 19:16:15 +02:00
|
|
|
const root = this.createContainer()
|
|
|
|
if (jresp.itemlist) {
|
2022-04-27 19:40:27 +02:00
|
|
|
jresp.itemlist.forEach(item => root.appendChild(this.createFileRow(item)))
|
2022-04-12 19:16:15 +02:00
|
|
|
} else {
|
2022-04-27 19:40:27 +02:00
|
|
|
console.error(jresp)
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createFileRow(item) {
|
|
|
|
var div = document.createElement('div')
|
|
|
|
div.classList.add('row')
|
|
|
|
var filename = document.createElement('div')
|
|
|
|
filename.classList.add('col')
|
|
|
|
var name = `<span>${item.name}</span>`
|
|
|
|
filename.innerHTML = this.iconTag(item) + name
|
|
|
|
const isFolder = item['@class'].includes('FolderItem')
|
|
|
|
filename.addEventListener('click', (ev) => {
|
2022-04-27 19:40:27 +02:00
|
|
|
ev.stopPropagation()
|
|
|
|
if (isFolder) {
|
|
|
|
const span = ev.currentTarget.querySelector(':nth-child(2)')
|
|
|
|
if (span) {
|
|
|
|
span.style = 'background-color: ' + this.#selectedbgcolor
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
if (this.#d4sstorageTree != null) {
|
|
|
|
this.#d4sstorageTree.select(item.id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.info("Download of " + item.id)
|
|
|
|
this.download(this.buildDownloadUrl(item.id), item.name)
|
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
div.appendChild(filename)
|
|
|
|
return div
|
|
|
|
}
|
|
|
|
|
|
|
|
iconTag(item) {
|
2022-05-02 12:21:55 +02:00
|
|
|
var i = `<img src="${this.#srcbaseurl}/img/file-earmark.svg"></img>`
|
2022-04-12 19:16:15 +02:00
|
|
|
if (item['@class'].includes('FolderItem')) {
|
2022-05-02 12:21:55 +02:00
|
|
|
i = `<img src="${this.#srcbaseurl}/img/folder.svg"></img>`
|
2022-04-12 19:16:15 +02:00
|
|
|
} else if (item['@class'].includes('ImageFile')) {
|
2022-05-02 12:21:55 +02:00
|
|
|
i = `<img src="${this.#srcbaseurl}/img/image.svg"></img>`
|
2022-04-12 19:16:15 +02:00
|
|
|
} else if (item['@class'].includes('PDFFileItem')) {
|
2022-05-02 12:21:55 +02:00
|
|
|
i = `<img src="${this.#srcbaseurl}/img/filetype-pdf.svg"></img>`
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
return '<span class="px-2">' + i + '</span>'
|
|
|
|
}
|
|
|
|
|
|
|
|
buildDownloadUrl(id) {
|
|
|
|
return this.baseUrl+ '/items/' + id + '/download'
|
|
|
|
}
|
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
download(url, name) {
|
|
|
|
this.boot.secureFetch(url).then(reply => {
|
|
|
|
if (reply.status !== 200) {
|
|
|
|
throw "Unable to download"
|
|
|
|
}
|
|
|
|
return reply.blob()
|
|
|
|
|
|
|
|
}).then(blob => {
|
|
|
|
const objectURL = URL.createObjectURL(blob)
|
|
|
|
var tmplnk = document.createElement("a")
|
|
|
|
tmplnk.download = name
|
|
|
|
tmplnk.href = objectURL
|
|
|
|
//tmplnk.target="_blank"
|
|
|
|
document.body.appendChild(tmplnk)
|
|
|
|
tmplnk.click()
|
|
|
|
document.body.removeChild(tmplnk)
|
|
|
|
|
|
|
|
}).catch(err => console.error(err))
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:16:15 +02:00
|
|
|
static get observedAttributes() {
|
|
|
|
return ["base-url"];
|
|
|
|
}
|
|
|
|
|
|
|
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
|
|
if (oldValue !== newValue) {
|
2022-04-27 19:40:27 +02:00
|
|
|
switch (name) {
|
|
|
|
case "base-url":
|
|
|
|
this.baseUrl = newValue
|
|
|
|
break
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-22 20:00:15 +02:00
|
|
|
})
|