Reformatting code. Refactor extended classes. Download method in d4s-storage-folder.
This commit is contained in:
parent
c2421ec5ab
commit
91d6e6abc6
|
@ -3,13 +3,24 @@
|
||||||
*/
|
*/
|
||||||
class D4SStorageHtmlElement extends HTMLElement {
|
class D4SStorageHtmlElement extends HTMLElement {
|
||||||
|
|
||||||
|
|
||||||
#d4smissmsg = 'Required d4s-boot-2 component not found'
|
#d4smissmsg = 'Required d4s-boot-2 component not found'
|
||||||
#baseurl = 'https://api.d4science.org/workspace'
|
#baseurl = 'https://api.d4science.org/workspace'
|
||||||
//#baseurl = 'https://workspace-repository.dev.d4science.org/storagehub/workspace'
|
//#baseurl = 'https://workspace-repository.dev.d4science.org/storagehub/workspace'
|
||||||
|
#boot = null
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
|
this.#boot = document.querySelector('d4s-boot-2')
|
||||||
|
}
|
||||||
|
|
||||||
|
get boot() {
|
||||||
|
if (this.#boot == null) {
|
||||||
|
this.#boot = document.querySelector('d4s-boot-2')
|
||||||
|
if (!this.#boot) {
|
||||||
|
throw this.#d4smissmsg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.#boot
|
||||||
}
|
}
|
||||||
|
|
||||||
appendStylesheets(root) {
|
appendStylesheets(root) {
|
||||||
|
@ -48,11 +59,9 @@ window.customElements.define('d4s-storage-tree', class extends D4SStorageHtmlEle
|
||||||
#selectedClass = 'selected'
|
#selectedClass = 'selected'
|
||||||
#selectedbgcolor = 'lightgray'
|
#selectedbgcolor = 'lightgray'
|
||||||
#shadowRoot
|
#shadowRoot
|
||||||
#boot = null
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
this.#boot = document.querySelector('d4s-boot-2')
|
|
||||||
this.#shadowRoot = this.attachShadow({mode: 'open'})
|
this.#shadowRoot = this.attachShadow({mode: 'open'})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,16 +95,17 @@ window.customElements.define('d4s-storage-tree', class extends D4SStorageHtmlEle
|
||||||
`
|
`
|
||||||
div.innerHTML = style
|
div.innerHTML = style
|
||||||
|
|
||||||
|
this.boot.secureFetch(this.baseUrl + '/vrefolder').then((reply) => {
|
||||||
this.#boot.secureFetch(this.baseUrl + '/vrefolder').then(
|
if (reply.status !== 200) {
|
||||||
(reply)=>{
|
throw "Unable to load folder"
|
||||||
if(reply.status !== 200) throw "Unable to load folder";
|
|
||||||
return reply.json()
|
|
||||||
}
|
}
|
||||||
).then(data => {
|
return reply.json()
|
||||||
|
|
||||||
|
}).then(data => {
|
||||||
this.parseResponse(div, data)
|
this.parseResponse(div, data)
|
||||||
this.#shadowRoot.appendChild(div)
|
this.#shadowRoot.appendChild(div)
|
||||||
}).catch(err=>alert(err))
|
|
||||||
|
}).catch(err => console.error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
parseResponse(parentElement, jresp) {
|
parseResponse(parentElement, jresp) {
|
||||||
|
@ -120,6 +130,7 @@ window.customElements.define('d4s-storage-tree', class extends D4SStorageHtmlEle
|
||||||
ul.appendChild(this.createListItem(itemname, itemid))
|
ul.appendChild(this.createListItem(itemname, itemid))
|
||||||
})
|
})
|
||||||
parentElement.appendChild(ul)
|
parentElement.appendChild(ul)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
console.error(jresp)
|
console.error(jresp)
|
||||||
}
|
}
|
||||||
|
@ -164,12 +175,17 @@ window.customElements.define('d4s-storage-tree', class extends D4SStorageHtmlEle
|
||||||
if (li.classList.contains(this.#loadedClass)) {
|
if (li.classList.contains(this.#loadedClass)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.#boot.secureFetch(this.buildListUrl(id)).then(reply=>{
|
|
||||||
if(reply.status !== 200) throw "Unable to load folder";
|
this.boot.secureFetch(this.buildListUrl(id)).then(reply => {
|
||||||
|
if (reply.status !== 200) {
|
||||||
|
throw "Unable to load folder"
|
||||||
|
}
|
||||||
return reply.json()
|
return reply.json()
|
||||||
|
|
||||||
}).then(data => {
|
}).then(data => {
|
||||||
this.parseResponse(li, data)
|
this.parseResponse(li, data)
|
||||||
}).catch(err=>alert(err))
|
|
||||||
|
}).catch(err => console.error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
displayPath(li) {
|
displayPath(li) {
|
||||||
|
@ -190,7 +206,7 @@ window.customElements.define('d4s-storage-tree', class extends D4SStorageHtmlEle
|
||||||
}
|
}
|
||||||
|
|
||||||
static get observedAttributes() {
|
static get observedAttributes() {
|
||||||
return ["base-url", "filelist-id"];
|
return ["base-url", "filelist-id"]
|
||||||
}
|
}
|
||||||
|
|
||||||
attributeChangedCallback(name, oldValue, newValue) {
|
attributeChangedCallback(name, oldValue, newValue) {
|
||||||
|
@ -227,11 +243,9 @@ window.customElements.define('d4s-storage-folder', class extends D4SStorageHtmlE
|
||||||
|
|
||||||
#d4sstorageTree = null
|
#d4sstorageTree = null
|
||||||
#selectedbgcolor = 'lightgray'
|
#selectedbgcolor = 'lightgray'
|
||||||
#boot = null;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
this.#boot = document.querySelector('d4s-boot-2')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
|
@ -270,12 +284,16 @@ window.customElements.define('d4s-storage-folder', class extends D4SStorageHtmlE
|
||||||
}
|
}
|
||||||
|
|
||||||
list(folderId) {
|
list(folderId) {
|
||||||
this.#boot.secureFetch(this.buildListUrl(folderId)).then(reply=>{
|
this.boot.secureFetch(this.buildListUrl(folderId)).then(reply => {
|
||||||
if(reply.status !== 200) throw "Unable to build list url";
|
if (reply.status !== 200) {
|
||||||
|
throw "Unable to build list url"
|
||||||
|
}
|
||||||
return reply.json()
|
return reply.json()
|
||||||
|
|
||||||
}).then(data => {
|
}).then(data => {
|
||||||
this.parseResponse(data)
|
this.parseResponse(data)
|
||||||
}).catch(err=>alert(err))
|
|
||||||
|
}).catch(err => console.error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
parseResponse(jresp) {
|
parseResponse(jresp) {
|
||||||
|
@ -307,7 +325,7 @@ window.customElements.define('d4s-storage-folder', class extends D4SStorageHtmlE
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.info("Download of " + item.id)
|
console.info("Download of " + item.id)
|
||||||
this.#boot.download(this.buildDownloadUrl(item.id), item.name)
|
this.download(this.buildDownloadUrl(item.id), item.name)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -331,6 +349,26 @@ window.customElements.define('d4s-storage-folder', class extends D4SStorageHtmlE
|
||||||
return this.baseUrl+ '/items/' + id + '/download'
|
return this.baseUrl+ '/items/' + id + '/download'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
static get observedAttributes() {
|
static get observedAttributes() {
|
||||||
return ["base-url"];
|
return ["base-url"];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue