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 {
|
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
#srcbaseurl = null
|
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
constructor() {
|
|
|
|
super()
|
2023-08-08 20:16:26 +02:00
|
|
|
if (D4S_STORAGE_SCRIPT && D4S_STORAGE_SCRIPT.src) {
|
|
|
|
this.#srcbaseurl = D4S_STORAGE_SCRIPT.src.substring(0, D4S_STORAGE_SCRIPT.src.lastIndexOf('/'));
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
this.attachShadow({mode: 'open'})
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-09 12:39:18 +02:00
|
|
|
connectedCallback() {
|
2023-08-08 20:16:26 +02:00
|
|
|
let linkElem = document.createElement('link');
|
|
|
|
linkElem.setAttribute('rel', 'stylesheet');
|
|
|
|
linkElem.setAttribute('href', 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css');
|
2023-08-09 12:39:18 +02:00
|
|
|
this.shadowRoot.appendChild(linkElem);
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
get srcBaseURL() {
|
|
|
|
return this.#srcbaseurl;
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
|
2023-08-09 12:39:18 +02:00
|
|
|
class D4SStorageToolbar extends D4SStorageHtmlElement {
|
2023-07-26 18:14:11 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
static toolbar_event_name = "d4s-toolbar";
|
2023-07-26 18:14:11 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
static refresh_folder = "refresh-folder";
|
|
|
|
static create_folder = "create-folder";
|
|
|
|
static delete_folder = "delete-folder";
|
|
|
|
static download = "download";
|
|
|
|
static upload_file = "upload-file";
|
|
|
|
static upload_archive = "upload-archive";
|
|
|
|
|
|
|
|
#last_action = null;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super()
|
2023-07-26 18:14:11 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
connectedCallback() {
|
2023-08-09 12:39:18 +02:00
|
|
|
super.connectedCallback();
|
2023-08-08 20:16:26 +02:00
|
|
|
|
|
|
|
var div = document.createElement('div');
|
|
|
|
div.innerHTML = /*html*/`
|
|
|
|
<div class="border">
|
2023-08-09 12:39:18 +02:00
|
|
|
<span class="px-2" id="refresh"><img src="${this.srcBaseURL}/img/arrow-clockwise.svg" alt="Refresh folder contents"></img></span>
|
|
|
|
<span class="px-2" id="create"><img src="${this.srcBaseURL}/img/folder-plus.svg" alt="Create new folder"></img></span>
|
|
|
|
<span class="px-2" id="delete"><img src="${this.srcBaseURL}/img/folder-x.svg" alt="Delete folder"></img></span>
|
|
|
|
<span class="px-2" id="download"><img src="${this.srcBaseURL}/img/box-arrow-down.svg" alt="Download folder as ZIP"></img></span>
|
|
|
|
<span class="px-2" id="upload-file"><img src="${this.srcBaseURL}/img/box-arrow-in-up.svg" alt="Upload file"></img></span>
|
|
|
|
<span class="px-2" id="upload-archive"><img src="${this.srcBaseURL}/img/file-earmark-zip.svg" alt="Upload archive"></img></span>
|
2023-08-08 20:16:26 +02:00
|
|
|
</div>`;
|
|
|
|
div.querySelector("#refresh").addEventListener('click', (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.actionPerformed(D4SStorageToolbar.refresh_folder);
|
|
|
|
});
|
|
|
|
div.querySelector("#create").addEventListener('click', (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.actionPerformed(D4SStorageToolbar.create_folder);
|
|
|
|
});
|
|
|
|
div.querySelector("#delete").addEventListener('click', (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.actionPerformed(D4SStorageToolbar.delete_folder);
|
|
|
|
});
|
|
|
|
div.querySelector("#download").addEventListener('click', (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.actionPerformed(D4SStorageToolbar.download);
|
|
|
|
});
|
|
|
|
div.querySelector("#upload-file").addEventListener('click', (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.actionPerformed(D4SStorageToolbar.upload_file);
|
|
|
|
});
|
|
|
|
div.querySelector("#upload-archive").addEventListener('click', (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.actionPerformed(D4SStorageToolbar.upload_archive);
|
|
|
|
});
|
|
|
|
this.shadowRoot.appendChild(div);
|
|
|
|
}
|
|
|
|
|
|
|
|
actionPerformed(action) {
|
|
|
|
this.#last_action = action;
|
2023-08-09 12:39:18 +02:00
|
|
|
this.dispatchEvent(new CustomEvent(D4SStorageToolbar.toolbar_event_name, {detail: {action: action}}));
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get lastAction() {
|
|
|
|
return this.#last_action;
|
2023-07-26 18:14:11 +02:00
|
|
|
}
|
|
|
|
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
class D4SStorageTree extends D4SStorageHtmlElement {
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
static tree_event_name = "d4s-toolbar";
|
|
|
|
static folder_data_event_name = "d4s-folder-data";
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
static dataid_attr = 'data-id';
|
|
|
|
static dataname_attr = 'data-name';
|
|
|
|
static parentid_attr = 'parent-id';
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
#loadedClass = 'loaded'
|
|
|
|
#selectedClass = 'selected'
|
|
|
|
#selectedbgcolor = 'lightgray'
|
2023-08-08 20:16:26 +02:00
|
|
|
|
|
|
|
#boot = null
|
|
|
|
#baseurl = 'https://api.d4science.org/workspace'
|
2023-08-09 18:25:25 +02:00
|
|
|
#filedownloadenabled = false;
|
2023-10-05 18:34:58 +02:00
|
|
|
#showfiles = true;
|
2023-10-06 10:24:24 +02:00
|
|
|
#allowdrag = false;
|
2023-08-08 20:16:26 +02:00
|
|
|
#d4sworkspace = null;
|
|
|
|
#foldersMap = {};
|
|
|
|
#currentid = null;
|
2023-08-09 18:25:25 +02:00
|
|
|
#pendingIdRequests = new Set();
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
constructor() {
|
|
|
|
super()
|
2023-08-08 20:16:26 +02:00
|
|
|
this.#boot = document.querySelector('d4s-boot-2');
|
|
|
|
this.#d4sworkspace = new D4SWorkspace(this.#baseurl, this.#boot);
|
|
|
|
}
|
|
|
|
|
|
|
|
get boot() {
|
|
|
|
return this.#boot;
|
|
|
|
}
|
2023-08-09 18:25:25 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
get baseUrl() {
|
|
|
|
return this.#baseurl;
|
|
|
|
}
|
2023-10-05 18:34:58 +02:00
|
|
|
|
|
|
|
get showFiles() {
|
|
|
|
return this.#showfiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
set showFiles(value){
|
|
|
|
this.#showfiles = value
|
|
|
|
}
|
2023-10-06 10:24:24 +02:00
|
|
|
|
|
|
|
get allowDrag() {
|
|
|
|
return this.#allowdrag;
|
|
|
|
}
|
|
|
|
|
|
|
|
set allowDrag(value){
|
|
|
|
this.#allowdrag = value
|
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
|
|
|
|
set baseUrl(url) {
|
|
|
|
this.#baseurl = url;
|
|
|
|
// this.setAttribute("baseurl", url)
|
|
|
|
this.#d4sworkspace = new D4SWorkspace(this.#baseurl, this.#boot);
|
|
|
|
}
|
|
|
|
|
2023-08-09 18:25:25 +02:00
|
|
|
set fileDownloadEnabled(value) {
|
|
|
|
this.#filedownloadenabled = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get fileDownloadEnabled() {
|
|
|
|
return this.#filedownloadenabled;
|
|
|
|
}
|
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
get d4sWorkspace() {
|
|
|
|
return this.#d4sworkspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
set currentId(id) {
|
|
|
|
this.#currentid = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentId() {
|
|
|
|
return this.#currentid;
|
|
|
|
}
|
|
|
|
|
2023-08-09 12:39:18 +02:00
|
|
|
setFolderItems(id, items, fireEvent = true) {
|
2023-08-08 20:16:26 +02:00
|
|
|
this.#foldersMap[id] = items;
|
2023-08-09 18:25:25 +02:00
|
|
|
this.#pendingIdRequests.delete(id);
|
2023-08-09 12:39:18 +02:00
|
|
|
if (fireEvent) {
|
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(
|
|
|
|
D4SStorageTree.folder_data_event_name,
|
|
|
|
{detail: {id: id}}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getFolderItems(id) {
|
|
|
|
return this.#foldersMap[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelectedFolderItems() {
|
|
|
|
this.getFolderItems(this.currentId);
|
|
|
|
}
|
|
|
|
|
2023-08-09 18:25:25 +02:00
|
|
|
static get observedAttributes() {
|
2023-10-06 10:24:24 +02:00
|
|
|
return ["base-url", "file-download-enabled", "show-files", "allow-drag"];
|
2023-08-09 18:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
|
|
if (oldValue !== newValue) {
|
|
|
|
switch (name) {
|
|
|
|
case "base-url":
|
|
|
|
this.baseUrl = newValue;
|
|
|
|
break;
|
|
|
|
case "file-download-enabled":
|
|
|
|
this.fileDownloadEnabled = (newValue.toLowerCase() === 'true');
|
|
|
|
break;
|
2023-10-05 18:34:58 +02:00
|
|
|
case "show-files":
|
|
|
|
this.showFiles = (newValue.toLowerCase() === 'true');
|
|
|
|
break;
|
2023-10-06 10:24:24 +02:00
|
|
|
case "allow-drag":
|
|
|
|
this.allowDrag = (newValue.toLowerCase() === 'true');
|
|
|
|
break;
|
2023-08-09 18:25:25 +02:00
|
|
|
default:
|
|
|
|
console.warn("Unexpected attribute changed: " + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
connectedCallback() {
|
2023-08-09 12:39:18 +02:00
|
|
|
super.connectedCallback();
|
2022-04-27 19:40:27 +02:00
|
|
|
|
2023-08-09 18:25:25 +02:00
|
|
|
const div = document.createElement('div')
|
2023-08-08 20:16:26 +02:00
|
|
|
div.innerHTML = /*css*/`
|
2022-04-27 19:40:27 +02:00
|
|
|
<style>
|
2023-10-06 10:24:24 +02:00
|
|
|
.selected > span {
|
2022-04-27 19:40:27 +02:00
|
|
|
background-color: ${this.#selectedbgcolor};
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
ul.root {
|
|
|
|
padding-left: 0;
|
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
ul, li {
|
|
|
|
list-style-type: none;
|
2023-08-08 20:16:26 +02:00
|
|
|
margin-bottom: 0;
|
|
|
|
padding-left: 0.6em;
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
|
|
|
li {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
2023-08-09 18:25:25 +02:00
|
|
|
.dragover {
|
|
|
|
background-color: purple;
|
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
</style>`;
|
2022-04-27 19:40:27 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
this.shadowRoot.appendChild(div);
|
2023-08-09 18:25:25 +02:00
|
|
|
const workspaceDIV = document.createElement('div');
|
|
|
|
const vreFolderDIV = document.createElement('div');
|
|
|
|
div.appendChild(workspaceDIV);
|
|
|
|
div.appendChild(vreFolderDIV);
|
2023-08-08 20:16:26 +02:00
|
|
|
|
|
|
|
this.d4sWorkspace.getWorkspace().then(data => {
|
2023-08-09 18:25:25 +02:00
|
|
|
this.parseItemsData(workspaceDIV, data);
|
2023-08-08 20:16:26 +02:00
|
|
|
}).catch(err => console.error(err));
|
|
|
|
|
|
|
|
this.d4sWorkspace.getVREFolder().then(data => {
|
2023-08-09 18:25:25 +02:00
|
|
|
this.parseItemsData(vreFolderDIV, data);
|
2022-04-27 19:40:27 +02:00
|
|
|
}).catch(err => console.error(err))
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
this.addEventListener(D4SStorageTree.folder_data_event_name, this.folderDataEventHandler);
|
|
|
|
if (document.readyState === "loading") {
|
|
|
|
document.addEventListener("DOMContentLoaded", this.registerOtherListeners.bind(this));
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
disconnectedCallback() {
|
|
|
|
this.removeEventListener(D4SStorageTree.folder_data_event_name);
|
|
|
|
const toolbar = document.querySelector("d4s-storage-tool");
|
|
|
|
if (toolbar) toolbar.removeEventListener(D4SStorageToolbar.toolbar_event_name);
|
|
|
|
const folder = document.querySelector("d4s-storage-folder");
|
|
|
|
if (folder) folder.removeEventListener(D4SStorageFolder.selected_event_name);
|
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
registerOtherListeners(event) {
|
|
|
|
const toolbar = document.querySelector("d4s-storage-tool");
|
|
|
|
if (toolbar) toolbar.addEventListener(D4SStorageToolbar.toolbar_event_name, this.toolbarEventHandler.bind(this));
|
|
|
|
const folder = document.querySelector("d4s-storage-folder");
|
|
|
|
if (folder) folder.addEventListener(D4SStorageFolder.selected_event_name, this.folderEventHandler.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
folderDataEventHandler(event) {
|
2023-08-09 18:25:25 +02:00
|
|
|
this.fillWithContent(this.shadowRoot.querySelector(`*[${D4SStorageTree.dataid_attr}='${event.detail.id}']`))
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toolbarEventHandler(event) {
|
2023-08-09 12:39:18 +02:00
|
|
|
switch (event.detail.action) {
|
2023-08-08 20:16:26 +02:00
|
|
|
case D4SStorageToolbar.refresh_folder:
|
|
|
|
if (this.currentId) {
|
|
|
|
this.refreshFolder(this.currentId);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case D4SStorageToolbar.create_folder:
|
|
|
|
if (this.currentId) {
|
|
|
|
this.createNewFolderIn(this.currentId);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case D4SStorageToolbar.delete_folder:
|
|
|
|
if (this.currentId) {
|
|
|
|
this.delete(this.currentId);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case D4SStorageToolbar.download:
|
|
|
|
if (this.currentId) {
|
|
|
|
const name = this.shadowRoot.querySelector(`*[${D4SStorageTree.dataid_attr}='${this.currentId}']`).getAttribute(D4SStorageTree.dataname_attr);
|
|
|
|
this.d4sWorkspace.download(this.currentId, name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case D4SStorageToolbar.upload_file:
|
|
|
|
var file = document.createElement('input');
|
|
|
|
file.type = 'file';
|
|
|
|
file.addEventListener('change', (ev) => {
|
|
|
|
const selectedFile = file.files[0];
|
|
|
|
if (selectedFile) {
|
|
|
|
this.uploadFile(this.currentId, selectedFile);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
file.click();
|
|
|
|
break;
|
|
|
|
case D4SStorageToolbar.upload_archive:
|
|
|
|
var file = document.createElement('input');
|
|
|
|
file.type = 'file';
|
|
|
|
file.addEventListener('change', (ev) => {
|
|
|
|
const selectedFile = file.files[0];
|
|
|
|
if (selectedFile) {
|
|
|
|
this.uploadArchive(this.currentId, selectedFile);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
file.click();
|
|
|
|
break;
|
|
|
|
default:
|
2023-08-09 12:39:18 +02:00
|
|
|
console.error('Unexpected event action: ' + event.detail.action);
|
2023-08-08 20:16:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
folderEventHandler(event) {
|
|
|
|
switch (event.detail.itemtype) {
|
|
|
|
case D4SStorageFolder.file_selected_detail_itemtype:
|
2023-08-09 18:25:25 +02:00
|
|
|
if (this.fileDownloadEnabled) {
|
|
|
|
console.info("Download of file: " + event.detail.name + " [" + event.detail.id + "]");
|
|
|
|
this.d4sWorkspace.download(event.detail.id, event.detail.name).catch(err => {alert(err)});
|
|
|
|
} else {
|
|
|
|
console.log("File download is disabled");
|
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
break;
|
|
|
|
case D4SStorageFolder.folder_selected_detail_itemtype:
|
|
|
|
this.select(event.detail.id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn("Unexpected folder event: " + event.detail.itemtype);
|
|
|
|
break;
|
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
select(id) {
|
2023-08-08 20:16:26 +02:00
|
|
|
this.currentId = id;
|
2023-08-09 18:25:25 +02:00
|
|
|
const li = this.shadowRoot.querySelector(`*[${D4SStorageTree.dataid_attr}='${id}']`);
|
2023-08-08 20:16:26 +02:00
|
|
|
this.displayPath(li);
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-27 19:40:27 +02:00
|
|
|
if (li.classList.contains(this.#selectedClass)) {
|
2023-08-08 20:16:26 +02:00
|
|
|
this.toggleULDisplay(li);
|
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)
|
|
|
|
}
|
2023-08-09 12:39:18 +02:00
|
|
|
if (!li.classList.contains(this.#loadedClass)) {
|
|
|
|
this.fillWithContent(li);
|
2022-03-30 11:42:08 +02:00
|
|
|
}
|
|
|
|
|
2023-08-09 12:39:18 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(
|
|
|
|
D4SStorageTree.tree_event_name,
|
|
|
|
{detail: {id: id}}
|
|
|
|
)
|
|
|
|
);
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
refreshContents(element) {
|
|
|
|
[...element.querySelectorAll(`ul`)].forEach(ul => {
|
|
|
|
ul.remove();
|
|
|
|
});
|
|
|
|
this.fillWithContent(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
fillWithContent(element) {
|
|
|
|
const id = element.getAttribute(D4SStorageTree.dataid_attr);
|
2023-08-09 12:39:18 +02:00
|
|
|
const contents = this.getFolderItems(id);
|
|
|
|
if (contents) {
|
|
|
|
this.parseItemsData(element, contents);
|
2023-08-08 20:16:26 +02:00
|
|
|
} else {
|
2023-08-09 18:25:25 +02:00
|
|
|
if (!this.#pendingIdRequests.has(id)) {
|
|
|
|
this.#pendingIdRequests.add(id);
|
|
|
|
this.d4sWorkspace.listFolder(id).then(data => {
|
|
|
|
this.setFolderItems(id, data);
|
|
|
|
}).catch(err => console.error(err));
|
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parseItemsData(parentElement, data) {
|
|
|
|
parentElement.classList.add(this.#loadedClass);
|
|
|
|
const parentId = parentElement.getAttribute(D4SStorageTree.dataid_attr);
|
2023-08-09 18:25:25 +02:00
|
|
|
const ul = document.createElement('ul');
|
2023-08-08 20:16:26 +02:00
|
|
|
ul.classList.add('nested');
|
|
|
|
if (!parentId) {
|
|
|
|
ul.classList.add('root');
|
|
|
|
}
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
data
|
2023-10-05 18:34:58 +02:00
|
|
|
.filter(item => this.showFiles || item['@class'].includes('FolderItem'))
|
2023-08-08 20:16:26 +02:00
|
|
|
.forEach(item => {
|
2023-10-05 18:34:58 +02:00
|
|
|
ul.appendChild(this.createListItem(item.name, item.id, item['@class'], parentId));
|
2023-08-08 20:16:26 +02:00
|
|
|
})
|
|
|
|
} else {
|
2023-10-05 18:34:58 +02:00
|
|
|
ul.appendChild(this.createListItem(data.displayName ? data.displayName : data.name, data.id, data['@class'], parentId));
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
parentElement.appendChild(ul);
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:34:58 +02:00
|
|
|
createListItem(label, id, type, parentId) {
|
2023-08-08 20:16:26 +02:00
|
|
|
const li = document.createElement('li');
|
|
|
|
li.setAttribute(D4SStorageTree.dataname_attr, label);
|
|
|
|
li.setAttribute(D4SStorageTree.dataid_attr, id);
|
2023-10-06 10:24:24 +02:00
|
|
|
li.style.userSelect = "none"
|
2023-10-06 11:41:32 +02:00
|
|
|
li.title = li.alt = label
|
2023-08-08 20:16:26 +02:00
|
|
|
if (parentId) {
|
|
|
|
li.setAttribute(D4SStorageTree.parentid_attr, parentId);
|
|
|
|
}
|
2023-10-05 18:34:58 +02:00
|
|
|
const icon = type.includes('FolderItem') ? "folder.svg" : "file-earmark.svg"
|
2023-10-06 10:24:24 +02:00
|
|
|
li.innerHTML = `
|
|
|
|
<img class="px-1" src="${this.srcBaseURL}/img/${icon}"</img>
|
|
|
|
<span>${label}</span>
|
|
|
|
`
|
2023-08-08 20:16:26 +02:00
|
|
|
|
|
|
|
li.addEventListener('click', (ev) => {
|
|
|
|
ev.stopPropagation();
|
2023-08-09 12:39:18 +02:00
|
|
|
this.select(ev.currentTarget.getAttribute(D4SStorageTree.dataid_attr));
|
|
|
|
});
|
2023-10-06 10:24:24 +02:00
|
|
|
|
|
|
|
if(this.allowDrag){
|
|
|
|
li.setAttribute("draggable", true)
|
|
|
|
|
|
|
|
li.addEventListener("dragstart", ev=>{
|
|
|
|
const id = ev.currentTarget.getAttribute(D4SStorageTree.dataid_attr)
|
|
|
|
ev.dataTransfer.effectAllowed = 'copy'
|
|
|
|
ev.dataTransfer.setData('text/html', ev.currentTarget.querySelector("span").innerHTML)
|
|
|
|
ev.dataTransfer.setData('text/plain+publiclink', this.#d4sworkspace.getPublicLink(id))
|
|
|
|
ev.dataTransfer.setData('text/plain+downloadlink', this.#d4sworkspace.getDownloadLink(id))
|
|
|
|
ev.stopPropagation()
|
|
|
|
})
|
|
|
|
|
|
|
|
li.addEventListener("dragend", ev=>{
|
|
|
|
ev.preventDefault()
|
|
|
|
ev.stopPropagation()
|
|
|
|
})
|
|
|
|
}
|
2023-08-09 18:25:25 +02:00
|
|
|
// li.addEventListener("dragenter", dee =>{
|
|
|
|
// dee.stopPropagation();
|
|
|
|
// dee.preventDefault();
|
|
|
|
// if (li == dee.target) {
|
|
|
|
// dee.target.classList.add("dragover");
|
|
|
|
// }
|
|
|
|
// }, false);
|
|
|
|
// li.addEventListener("dragleave", dle =>{
|
|
|
|
// dle.stopPropagation();
|
|
|
|
// dle.preventDefault();
|
|
|
|
// if (li == dle.target) {
|
|
|
|
// dle.target.classList.remove("dragover");
|
|
|
|
// }
|
|
|
|
// }, false);
|
|
|
|
// li.addEventListener("dragover", doe => {
|
|
|
|
// doe.stopPropagation();
|
|
|
|
// doe.preventDefault();
|
|
|
|
// }, false);
|
|
|
|
// li.addEventListener("drop", de => {
|
|
|
|
// de.stopPropagation();
|
|
|
|
// de.preventDefault();
|
|
|
|
|
|
|
|
// const dt = de.dataTransfer;
|
|
|
|
// const files = dt.files;
|
|
|
|
|
|
|
|
// console.log(files);
|
|
|
|
// }, false);
|
2023-08-08 20:16:26 +02:00
|
|
|
return li;
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
displayPath(li) {
|
2023-08-09 18:25:25 +02:00
|
|
|
let curr = li
|
2022-04-27 19:40:27 +02:00
|
|
|
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
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
refreshFolder(id) {
|
|
|
|
const folder = this.shadowRoot.querySelector(`*[${D4SStorageTree.dataid_attr}='${id}']`);
|
|
|
|
console.log("Refreshing folder: " + folder.getAttribute(D4SStorageTree.dataname_attr));
|
2023-08-09 12:39:18 +02:00
|
|
|
this.setFolderItems(id, null, false);
|
2023-08-08 20:16:26 +02:00
|
|
|
this.refreshContents(folder);
|
2023-08-09 12:39:18 +02:00
|
|
|
// this.select(id);
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
createNewFolderIn(id) {
|
|
|
|
const newFolderName = window.prompt("Enter the new folder name");
|
|
|
|
if (newFolderName) {
|
|
|
|
this.d4sWorkspace.getOrCreateFolder(id, newFolderName, "Created from JS UI").then(newId => {
|
|
|
|
window.setTimeout(()=>{
|
|
|
|
this.refreshContents(this.shadowRoot.querySelector(`*[${D4SStorageTree.dataid_attr}='${id}']`));
|
|
|
|
this.select(id);
|
|
|
|
}, 2000);
|
|
|
|
}).catch(err => {
|
|
|
|
alert(err);
|
|
|
|
})
|
|
|
|
}
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
delete(id) {
|
|
|
|
const currentLI = this.shadowRoot.querySelector(`*[${D4SStorageTree.dataid_attr}='${id}']`);
|
|
|
|
const name = currentLI.getAttribute(D4SStorageTree.dataname_attr);
|
|
|
|
if (currentLI.getAttribute(D4SStorageTree.parentid_attr)) {
|
|
|
|
if (confirm(`Do you really want to delete folder '${name}'?`)) {
|
|
|
|
if(this.d4sWorkspace.deleteItem(id)) {
|
|
|
|
window.setTimeout(()=>{
|
|
|
|
const parentFolderId = currentLI.getAttribute(D4SStorageTree.parentid_attr);
|
|
|
|
this.refreshContents(this.shadowRoot.querySelector(`*[${D4SStorageTree.dataid_attr}='${parentFolderId}']`));
|
|
|
|
this.select(parentFolderId);
|
|
|
|
}, 2000);
|
|
|
|
} else {
|
|
|
|
alert(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alert("You cannot delete ROOT folder: "+ name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadFile(targetFolderId, file) {
|
|
|
|
if (this.d4sWorkspace.uploadFile(targetFolderId, file.name, "", file, file.type)) {
|
|
|
|
window.setTimeout(()=>{
|
|
|
|
this.select(targetFolderId);
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadArchive(targetFolderId, file) {
|
|
|
|
if (this.d4sWorkspace.uploadArchive(targetFolderId, file.name.substring(0, file.name.lastIndexOf(".")), file)) {
|
|
|
|
window.setTimeout(()=>{
|
|
|
|
this.refreshContents(this.shadowRoot.querySelector(`*[${D4SStorageTree.dataid_attr}='${targetFolderId}']`));
|
|
|
|
this.select(targetFolderId);
|
|
|
|
}, 3000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class D4SStorageFolder extends D4SStorageHtmlElement {
|
|
|
|
|
|
|
|
static selected_event_name = "d4s-folder-selected";
|
|
|
|
|
|
|
|
static folder_selected_detail_itemtype = "folder_selected";
|
|
|
|
static file_selected_detail_itemtype = "file_selected";
|
2022-03-30 11:42:08 +02:00
|
|
|
|
2022-04-12 19:16:15 +02:00
|
|
|
#selectedbgcolor = 'lightgray'
|
2023-08-09 18:25:25 +02:00
|
|
|
|
2022-04-12 19:16:15 +02:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback() {
|
2023-08-09 12:39:18 +02:00
|
|
|
super.connectedCallback();
|
|
|
|
|
2022-04-12 19:16:15 +02:00
|
|
|
const style = document.createElement('style')
|
2023-08-08 20:16:26 +02:00
|
|
|
style.innerHTML = /*css*/`
|
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
|
|
|
`
|
2023-08-08 20:16:26 +02:00
|
|
|
this.shadowRoot.appendChild(style);
|
|
|
|
this.createContainer();
|
|
|
|
|
|
|
|
if (document.readyState === "loading") {
|
|
|
|
document.addEventListener("DOMContentLoaded", this.registerOtherListeners.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback() {
|
|
|
|
const tree = document.querySelector("d4s-storage-tree");
|
|
|
|
if (tree) {
|
|
|
|
tree.removeEventListener(D4SStorageTree.folder_data_event_name);
|
2023-08-09 12:39:18 +02:00
|
|
|
tree.removeEventListener(D4SStorageTree.tree_event_name);
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerOtherListeners(event) {
|
|
|
|
const tree = document.querySelector("d4s-storage-tree");
|
|
|
|
if (tree) {
|
|
|
|
tree.addEventListener(D4SStorageTree.folder_data_event_name, this.folderDataEventHandler.bind(this));
|
2023-08-09 12:39:18 +02:00
|
|
|
tree.addEventListener(D4SStorageTree.tree_event_name, this.treeEventHandler.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
folderDataEventHandler(event) {
|
|
|
|
if (event.detail.id === event.target.currentId) {
|
|
|
|
const folderItems = event.target.getFolderItems(event.detail.id);
|
|
|
|
if (folderItems) {
|
|
|
|
this.parseItemsData(folderItems);
|
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
treeEventHandler(event) {
|
2023-08-09 12:39:18 +02:00
|
|
|
const folderItems = event.target.getFolderItems(event.detail.id);
|
2023-08-08 20:16:26 +02:00
|
|
|
if (folderItems) {
|
|
|
|
// It's first time folder is selected, data is stil not loaded,
|
|
|
|
// they will be loaded by the folderDataEventHandler() function when available
|
|
|
|
this.parseItemsData(folderItems);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:16:15 +02:00
|
|
|
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
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
parseItemsData(data) {
|
2023-07-26 18:14:11 +02:00
|
|
|
const root = this.createContainer();
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
data.forEach(item => root.appendChild(this.createFileRow(item)));
|
2022-04-12 19:16:15 +02:00
|
|
|
} else {
|
2023-07-26 18:14:11 +02:00
|
|
|
console.error("Returned data is not an array: " + data);
|
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')
|
2023-08-08 20:16:26 +02:00
|
|
|
var spanAndName = /*html*/`<span>${item.name}</span>`
|
|
|
|
filename.innerHTML = this.iconTag(item) + spanAndName
|
2022-04-12 19:16:15 +02:00
|
|
|
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
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(
|
|
|
|
D4SStorageFolder.selected_event_name,
|
|
|
|
{detail: {itemtype: D4SStorageFolder.folder_selected_detail_itemtype, id: item.id, name: item.name}}
|
|
|
|
)
|
|
|
|
);
|
2022-04-27 19:40:27 +02:00
|
|
|
} else {
|
2023-08-08 20:16:26 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(
|
|
|
|
D4SStorageFolder.selected_event_name,
|
|
|
|
{detail: {itemtype: D4SStorageFolder.file_selected_detail_itemtype, id: item.id, name: item.name}}
|
|
|
|
)
|
|
|
|
);
|
2022-04-27 19:40:27 +02:00
|
|
|
}
|
2022-04-12 19:16:15 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
div.appendChild(filename)
|
|
|
|
return div
|
|
|
|
}
|
|
|
|
|
|
|
|
iconTag(item) {
|
2023-08-08 20:16:26 +02:00
|
|
|
var i = /*html*/`<img src="${this.srcBaseURL}/img/file-earmark.svg"></img>`
|
2022-04-12 19:16:15 +02:00
|
|
|
if (item['@class'].includes('FolderItem')) {
|
2023-08-08 20:16:26 +02:00
|
|
|
i = /*html*/`<img src="${this.srcBaseURL}/img/folder.svg"></img>`
|
2022-04-12 19:16:15 +02:00
|
|
|
} else if (item['@class'].includes('ImageFile')) {
|
2023-08-08 20:16:26 +02:00
|
|
|
i = /*html*/`<img src="${this.srcBaseURL}/img/image.svg"></img>`
|
2022-04-12 19:16:15 +02:00
|
|
|
} else if (item['@class'].includes('PDFFileItem')) {
|
2023-08-08 20:16:26 +02:00
|
|
|
i = /*html*/`<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>'
|
|
|
|
}
|
|
|
|
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
2023-07-26 18:14:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that helps the interaction with the D4Science's Workspace via API and user's token as bearer authentication
|
|
|
|
*/
|
|
|
|
class D4SWorkspace {
|
|
|
|
|
|
|
|
#d4sboot = null;
|
|
|
|
#workspaceURL = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the new D4SWorkspace object, the d4sboot parameter can be null, in this case it is searched in the DOM document via <code>querySelector()</code> method
|
|
|
|
* @param {string} workspaceURL the WS API base URL
|
|
|
|
* @param {d4s-boot-2} d4sboot the d4s-boot-2 instance
|
|
|
|
*/
|
|
|
|
constructor(workspaceURL, d4sboot) {
|
|
|
|
this.setWorkspaceURL(workspaceURL)
|
|
|
|
if (d4sboot) {
|
|
|
|
this.setD4SBoot(d4sboot)
|
|
|
|
} else {
|
|
|
|
this.setD4SBoot(document.querySelector("d4s-boot-2"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Workspace API base URL
|
|
|
|
* @param {string} workspaceURL the WS API base URL
|
|
|
|
*/
|
|
|
|
setWorkspaceURL(workspaceURL) {
|
|
|
|
this.#workspaceURL = workspaceURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the d4s-boot-2 instance to be used to perform the OIDC/UMA autenticated secure fetches
|
|
|
|
* @param {string} d4sboot the d4s-boot-2 instance
|
|
|
|
*/
|
|
|
|
setD4SBoot(d4sboot) {
|
|
|
|
this.#d4sboot = d4sboot;
|
|
|
|
}
|
|
|
|
|
2023-10-06 10:24:24 +02:00
|
|
|
getPublicLink(itemId){
|
|
|
|
return this.#workspaceURL + "/items/" + itemId + "/publiclink"
|
|
|
|
}
|
|
|
|
|
|
|
|
getDownloadLink(itemId){
|
|
|
|
return this.#workspaceURL + "/items/" + itemId + "/download"
|
|
|
|
}
|
|
|
|
|
2023-07-26 18:14:11 +02:00
|
|
|
/**
|
|
|
|
* Calls with a secure fetch the workspace with provided data.
|
|
|
|
* To upload data by using a <code>FormData</code> object you have to leave the <code>mime</code> attribute as <code>null</code>.
|
|
|
|
* @param {*} method the method to use for the call, default to <code>GET</code>
|
|
|
|
* @param {*} uri the uri to invoke, relative to <code>workspaceURL</code> provided in the object's constructor
|
|
|
|
* @param {*} body the payload to send
|
|
|
|
* @param {*} mime the mime type of the payload
|
|
|
|
* @param {*} extraHeaders extra HTTP headers to send
|
2023-08-08 20:16:26 +02:00
|
|
|
* @returns the reponse payload as a Blob() promise
|
2023-07-26 18:14:11 +02:00
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
#callWorkspace(method, uri, body, mime, extraHeaders) {
|
|
|
|
let req = { };
|
|
|
|
if (method) {
|
|
|
|
req.method = method;
|
|
|
|
}
|
|
|
|
if (body) {
|
|
|
|
req.body = body;
|
|
|
|
}
|
|
|
|
if (extraHeaders) {
|
|
|
|
req.headers = extraHeaders;
|
|
|
|
}
|
|
|
|
if (mime) {
|
|
|
|
if (req.headers) {
|
|
|
|
req.headers["Content-Type"] = mime;
|
|
|
|
} else {
|
|
|
|
req.headers = { "Content-Type" : mime };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let url = this.#workspaceURL;
|
|
|
|
if (uri) {
|
|
|
|
url += uri.startsWith('/') ? uri : '/' + uri;
|
|
|
|
}
|
|
|
|
console.log("Invoking WS API with: " + url);
|
|
|
|
return this.#d4sboot.secureFetch(url, req)
|
|
|
|
.then(resp => {
|
|
|
|
if (resp.ok) {
|
2023-08-08 20:16:26 +02:00
|
|
|
return resp.blob();
|
2023-07-26 18:14:11 +02:00
|
|
|
} else {
|
|
|
|
throw "Cannot invoke workspace via secure fetch for URL: " + url;
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user's Workspace root folder JSON item
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the WS root folder JSON item object
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
getWorkspace(extraHeaders) {
|
|
|
|
return this.#callWorkspace("GET", null, null, null, extraHeaders)
|
2023-08-08 20:16:26 +02:00
|
|
|
.then(blob => {
|
|
|
|
return blob.text().then(text => {
|
|
|
|
return JSON.parse(text).item;
|
|
|
|
})
|
2023-07-26 18:14:11 +02:00
|
|
|
}).catch(err => {
|
|
|
|
const msg = "Cannot get workspace root ID. Error message: " + err;
|
|
|
|
console.error(msg);
|
|
|
|
throw msg;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user's Workspace root folder id (as string)
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the id of the WS root folder
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
getWorkspaceId(extraHeaders) {
|
|
|
|
return this.getWorkspace(extraHeaders)
|
|
|
|
.then(json => {
|
|
|
|
return json.item.id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the contents of a folder, all sub-folders and files.
|
|
|
|
* @param {string} folderId the WS folder id to list
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the contents of the WS folder as json objects
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
listFolder(folderId, extraHeaders) {
|
|
|
|
const uri = "/items/" + folderId + "/children";
|
|
|
|
return this.#callWorkspace("GET", uri, null, null, extraHeaders)
|
2023-08-08 20:16:26 +02:00
|
|
|
.then(blob => {
|
|
|
|
return blob.text().then(text => {
|
|
|
|
return JSON.parse(text).itemlist;
|
|
|
|
});
|
2023-07-26 18:14:11 +02:00
|
|
|
}).catch(err => {
|
|
|
|
const msg = "Cannot get folder's content list. Error message: " + err;
|
|
|
|
console.error(msg);
|
|
|
|
throw msg;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the VRE folder JSON item that belongs to the context where the access token has been issued.
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the id of the WS VRE folder
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
getVREFolder(extraHeaders) {
|
|
|
|
return this.#callWorkspace("GET", "/vrefolder", null, null, extraHeaders)
|
2023-08-08 20:16:26 +02:00
|
|
|
.then(blob => {
|
|
|
|
return blob.text().then(text => {
|
|
|
|
return JSON.parse(text).item;
|
|
|
|
});
|
2023-07-26 18:14:11 +02:00
|
|
|
}).catch(err => {
|
|
|
|
const msg = "Cannot get VRE folder ID. Error message: " + err;
|
|
|
|
console.error(msg);
|
|
|
|
throw msg;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the VRE folder id (as string) that belongs to the context where the access token has been issued.
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the id of the WS VRE folder
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
getVREFolderId(extraHeaders) {
|
|
|
|
return this.getVREFolder(extraHeaders)
|
|
|
|
.then(item => {
|
|
|
|
return item.id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated please use getOrCreateFolder() function instead
|
|
|
|
* @param {string} parentFolderId the id of the parent folder where search fo the folder
|
|
|
|
* @param {string} name the name of the folder (characters not allowed: ":", [others still to be found])
|
|
|
|
* @param {string} description the description of the folder if it should be creaded
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the id of the folder, if it exists or it has been just created
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
checkOrCreateFolder(parentFolderId, name, description, extraHeaders) {
|
|
|
|
return this.getOrCreateFolder(parentFolderId, name, description, extraHeaders);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the id of the folder represented by the name parameter if exists, a new folder is created if it doesn't not exist
|
|
|
|
* @param {string} parentFolderId the id of the parent folder where search fo the folder
|
|
|
|
* @param {string} name the name of the folder (characters not allowed: ":", [others still to be found])
|
|
|
|
* @param {string} description the description of the folder if it should be creaded
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the id of the folder, if it exists or it has been just created
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
getOrCreateFolder(parentFolderId, name, description, extraHeaders) {
|
|
|
|
const baseURI = "/items/" + parentFolderId;
|
|
|
|
console.log("Checking existance of folder: " + name);
|
|
|
|
let uri = baseURI + "/items/" + name;
|
|
|
|
return this.#callWorkspace("GET", uri, null, null, extraHeaders)
|
2023-08-08 20:16:26 +02:00
|
|
|
.then(blob => {
|
|
|
|
return blob.text().then(text => {
|
|
|
|
const json = JSON.parse(text);
|
|
|
|
if (json.itemlist[0]) {
|
|
|
|
const id = json.itemlist[0].id;
|
|
|
|
console.log("'" + name + "' folder exists with and has id: " + id);
|
|
|
|
return id;
|
|
|
|
} else {
|
|
|
|
console.info("'" + name + "' folder doesn't exist, creating it: " + name);
|
|
|
|
uri = baseURI + "/create/FOLDER";
|
|
|
|
const params = new URLSearchParams({ name : name, description : description, hidden : false });
|
|
|
|
return this.#callWorkspace("POST", uri, params.toString(), "application/x-www-form-urlencoded", extraHeaders).then(id => {
|
|
|
|
console.log("New '" + name + "' folder successfully created with id: " + id);
|
|
|
|
return id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-07-26 18:14:11 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a Workspace item (folder or file) represented by its id.
|
|
|
|
* @param {string} itemId the id of the item do delete
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns <code>true</code> if the file has been correctly created, <code>false<c/ode> otherwise
|
|
|
|
*/
|
|
|
|
deleteItem(itemId, extraHeaders) {
|
2023-08-08 20:16:26 +02:00
|
|
|
return this.#callWorkspace("DELETE", "/items/" + itemId, null, null, extraHeaders)
|
2023-07-26 18:14:11 +02:00
|
|
|
.then(results => {
|
|
|
|
return true;
|
|
|
|
}).catch(err => {
|
|
|
|
console.error("Cannot DELETE workspace item with ID: " + itemId + ". Error message: " + err);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads a Workspace item (folder or file) represented by its id.
|
|
|
|
* @param {string} itemId the WS item to download
|
|
|
|
* @param {string} name the name of the file to use for the download, if null the id is used
|
|
|
|
* @param {boolean} skipLocalDownload if provided and <code>true</code> returns the data to function caller, otherwise it also downloads it locally.
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
2023-08-08 20:16:26 +02:00
|
|
|
* @returns the file's content, if item is a file, or a ZIP compressed archive if the item represents a folder, as a Blob object
|
2023-07-26 18:14:11 +02:00
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
download(itemId, name, skipLocalDownload, extraHeaders) {
|
|
|
|
return this.#callWorkspace("GET", "/items/" + itemId + "/download", null, null, extraHeaders)
|
|
|
|
.then(data => {
|
|
|
|
if (!skipLocalDownload) {
|
|
|
|
console.log("Donwloads item also locally to browser");
|
|
|
|
const objectURL = URL.createObjectURL(data);
|
|
|
|
var tmplnk = document.createElement("a");
|
|
|
|
tmplnk.download = name ? name : itemId;
|
|
|
|
tmplnk.href = objectURL;
|
|
|
|
//tmplnk.target="_blank";
|
|
|
|
document.body.appendChild(tmplnk);
|
|
|
|
tmplnk.click();
|
|
|
|
document.body.removeChild(tmplnk);
|
|
|
|
} else {
|
|
|
|
console.log("Skipping local download");
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}).catch(err => {
|
|
|
|
const msg = "Cannot download item with ID: " + itemId + ". Error message: " + err;
|
|
|
|
console.error(msg);
|
|
|
|
throw msg;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads a new file into the folder represented by its id
|
|
|
|
* @param {string} folderId the folder where to create the new file
|
|
|
|
* @param {string} name the name of the file to crete (characters not allowed: ":", [others still to be found])
|
|
|
|
* @param {object} data the archive data
|
|
|
|
* @param {string} contentType the content type of the file
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns <code>true</code> if the file has been correctly created, <code>false<c/ode> otherwise
|
|
|
|
*/
|
|
|
|
uploadFile(folderId, name, description, data, contentType, extraHeaders) {
|
|
|
|
const uri = "/items/" + folderId + "/create/FILE";
|
|
|
|
const request = new FormData();
|
|
|
|
request.append("name", name);
|
|
|
|
request.append("description", description);
|
|
|
|
request.append(
|
|
|
|
"file",
|
|
|
|
new Blob([data], {
|
|
|
|
type: contentType
|
|
|
|
})
|
|
|
|
);
|
|
|
|
return this.#callWorkspace("POST", uri, request, null, extraHeaders)
|
2023-08-08 20:16:26 +02:00
|
|
|
.then(blob => {
|
|
|
|
return blob.text().then(id => {
|
|
|
|
console.info("File '" + name + "' successfully uploaded and its id is: " + id);
|
|
|
|
return true;
|
|
|
|
})
|
2023-07-26 18:14:11 +02:00
|
|
|
}).catch(err => {
|
|
|
|
console.error("Cannot upload file '" + name + "'. Error: " + err);
|
|
|
|
return false;
|
|
|
|
});
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
2023-07-26 18:14:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads a new archive and stores its content into the folder represented by its id
|
|
|
|
* @param {string} folderId the folder where to create the new file
|
|
|
|
* @param {string} parentFolderName the name of the folder to create (characters not allowed: ":", [others still to be found])
|
|
|
|
* @param {object} data the file's contents
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns <code>true</code> if the file has been correctly created, <code>false<c/ode> otherwise
|
|
|
|
*/
|
2023-08-08 20:16:26 +02:00
|
|
|
uploadArchive(folderId, parentFolderName, data, extraHeaders) {
|
2023-07-26 18:14:11 +02:00
|
|
|
const uri = "/items/" + folderId + "/create/ARCHIVE";
|
|
|
|
const request = new FormData();
|
|
|
|
request.append("parentFolderName", parentFolderName);
|
|
|
|
request.append(
|
|
|
|
"file",
|
|
|
|
new Blob([data], {
|
|
|
|
type: "application/octet-stream"
|
|
|
|
})
|
|
|
|
);
|
|
|
|
return this.#callWorkspace("POST", uri, request, null, extraHeaders)
|
2023-08-08 20:16:26 +02:00
|
|
|
.then(blob => {
|
|
|
|
return blob.text().then(id => {
|
|
|
|
console.info("Archive file successfully uploaded and extracted into the '" + parentFolderName + "' folder, its id is: " + id);
|
|
|
|
return true;
|
|
|
|
})
|
2023-07-26 18:14:11 +02:00
|
|
|
}).catch(err => {
|
|
|
|
console.error("Cannot upload archive file. Error: " + err);
|
|
|
|
return false;
|
|
|
|
});
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
2023-07-26 18:14:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the public URL of a file
|
|
|
|
* @param {string} itemId the id of the item
|
|
|
|
* @param {int} version the optional file version
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the public URL of the item
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
getPublicURL(itemId, version, extraHeaders) {
|
2023-10-06 10:24:24 +02:00
|
|
|
let uri = "/items/" + itemId + "/publiclink";
|
2023-07-26 18:14:11 +02:00
|
|
|
if (version) {
|
|
|
|
uri += "version=" + version;
|
|
|
|
}
|
|
|
|
return this.#callWorkspace("GET", uri, null, null, extraHeaders)
|
2023-08-08 20:16:26 +02:00
|
|
|
.then(blob => {
|
|
|
|
return blob.text().then(publicURL => {
|
|
|
|
return publicURL;
|
|
|
|
})
|
2023-07-26 18:14:11 +02:00
|
|
|
}).catch(err => {
|
|
|
|
const message = "Cannot retrieve the item's public URL. Error message: " + err;
|
|
|
|
console.error(message);
|
|
|
|
throw message;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds into a folder all the file files having the name than matches a specific pattern.
|
|
|
|
* Wildcard (*) can be used at the start or the end of the pattern (eg. starts with *{name}, ends with = {name}*)
|
|
|
|
* @param {string} folderId the WS folder id where to search
|
|
|
|
* @param {string} pattern the pattern to use for the search. Wildcard '*' can be used at the start or the end of the pattern (eg. starts with *{name}, ends with = {name}*)
|
|
|
|
* @param {map} extraHeaders extra HTTP headers to be used for the request
|
|
|
|
* @returns the contents of the WS folder as json objects
|
|
|
|
* @throws the error as string, if occurred
|
|
|
|
*/
|
|
|
|
findByName(folderId, pattern, extraHeaders) {
|
|
|
|
const uri = "/items/" + folderId + "/items/" + pattern;
|
|
|
|
return this.#callWorkspace("GET", uri, null, null, extraHeaders)
|
2023-08-08 20:16:26 +02:00
|
|
|
.then(blob => {
|
|
|
|
return blob.text().then(text => {
|
|
|
|
return JSON.parse(text).itemlist;
|
|
|
|
});
|
2023-07-26 18:14:11 +02:00
|
|
|
}).catch(err => {
|
|
|
|
const msg = "Cannot get folder's content list. Error message: " + err;
|
|
|
|
console.error(msg);
|
|
|
|
throw msg;
|
|
|
|
});
|
|
|
|
}
|
2023-08-08 20:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds storagehub toolbar and raises an event for each selected tool item
|
|
|
|
* <d4s-storage-tool>
|
|
|
|
*/
|
|
|
|
window.customElements.define('d4s-storage-tool', D4SStorageToolbar);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds storagehub VRE folder or Workspace root tree, linked to toolbar and foldr list by events
|
|
|
|
* <d4s-storage-tree
|
2023-08-09 18:25:25 +02:00
|
|
|
* /@base-url: the base endpoint url string [defaults to: https://api.d4science.org/workspace]
|
|
|
|
* /@file-download-enabled: true/false to enable/disable the file download on d4s-storage-tool click event (false by default)
|
2023-08-08 20:16:26 +02:00
|
|
|
*/
|
|
|
|
window.customElements.define('d4s-storage-tree', D4SStorageTree);
|
|
|
|
/**
|
|
|
|
* Lists elements in selected folder linked to tree and model by events
|
|
|
|
* <d4s-storage-folder>
|
|
|
|
*/
|
|
|
|
window.customElements.define('d4s-storage-folder', D4SStorageFolder);
|