Skip to content
Snippets Groups Projects
Unverified Commit d25cfd94 authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire:
Browse files

Add more webdav file handling and table columns (#26)

parent b86b3fb6
No related branches found
No related tags found
No related merge requests found
Pipeline #11855 passed with warnings
...@@ -24,8 +24,8 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) { ...@@ -24,8 +24,8 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) {
this.loginWindow = null; this.loginWindow = null;
this.isPickerActive = false; this.isPickerActive = false;
this.statusText = ""; this.statusText = "";
this.lastDirectoryPath = "/";
this.directoryPath = "/"; this.directoryPath = "/";
this.directoryContents = [];
this.webDavClient = null; this.webDavClient = null;
this.tabulatorTable = null; this.tabulatorTable = null;
...@@ -49,7 +49,6 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) { ...@@ -49,7 +49,6 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) {
isPickerActive: { type: Boolean, attribute: false }, isPickerActive: { type: Boolean, attribute: false },
statusText: { type: String, attribute: false }, statusText: { type: String, attribute: false },
directoryPath: { type: String, attribute: false }, directoryPath: { type: String, attribute: false },
directoryContents: { type: Array, attribute: false },
}; };
} }
...@@ -78,11 +77,38 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) { ...@@ -78,11 +77,38 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) {
window.addEventListener('message', this._onReceiveWindowMessage); window.addEventListener('message', this._onReceiveWindowMessage);
// http://tabulator.info/docs/4.7 // http://tabulator.info/docs/4.7
this.tabulatorTable = new Tabulator(this._("#directory-content-table"), {}); // TODO: format size and lastmod
// TODO: translation of column headers
// TODO: mime type icon
this.tabulatorTable = new Tabulator(this._("#directory-content-table"), {
layout: "fitDataStretch",
selectable: true,
columns: [
{title: "Filename", field: "basename"},
{title: "Size", field: "size", formatter: (cell, formatterParams, onRendered) => {
return cell.getRow().getData().type === "directory" ? "" : humanFileSize(cell.getValue());}},
{title: "Type", field: "type"},
{title: "Mime", field: "mime"},
{title: "Last modified", field: "lastmod", sorter: "date"},
],
rowClick: (e, row) => {
const data = row.getData();
switch(data.type) {
case "directory":
this.directoryClicked(e, data);
break;
case "file":
console.log("file selected", data);
break;
}
},
});
}); });
} }
openFilePicker() { openFilePicker() {
// TODO: translation
this.statusText = "Auth in progress"; this.statusText = "Auth in progress";
this.loginWindow = window.open(this.authUrl, "Nextcloud Login", this.loginWindow = window.open(this.authUrl, "Nextcloud Login",
"width=400,height=400,menubar=no,scrollbars=no,status=no,titlebar=no,toolbar=no"); "width=400,height=400,menubar=no,scrollbars=no,status=no,titlebar=no,toolbar=no");
...@@ -107,7 +133,7 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) { ...@@ -107,7 +133,7 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) {
} }
); );
this.loadDirectory(""); this.loadDirectory("/");
} }
} }
...@@ -117,20 +143,18 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) { ...@@ -117,20 +143,18 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) {
* @param path * @param path
*/ */
loadDirectory(path) { loadDirectory(path) {
// TODO: translation
this.statusText = "Loading directory from Nextcloud: " + path; this.statusText = "Loading directory from Nextcloud: " + path;
this.lastDirectoryPath = this.directoryPath;
this.directoryPath = path; this.directoryPath = path;
this.directoryContents = [];
// https://github.com/perry-mitchell/webdav-client#getdirectorycontents // https://github.com/perry-mitchell/webdav-client#getdirectorycontents
this.webDavClient this.webDavClient
.getDirectoryContents(path) .getDirectoryContents(path, {details: true})
.then(contents => { .then(contents => {
console.log("contents", contents); console.log("contents", contents);
this.statusText = ""; this.statusText = "";
this.directoryContents = contents; this.tabulatorTable.setData(contents.data);
this.tabulatorTable.setData(contents);
this.isPickerActive = true; this.isPickerActive = true;
}).catch(error => { }).catch(error => {
console.error(error.message); console.error(error.message);
...@@ -139,41 +163,53 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) { ...@@ -139,41 +163,53 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) {
}); });
} }
static get styles() { directoryClicked(event, file) {
// language=css this.loadDirectory(file.filename);
return css` event.preventDefault();
${commonStyles.getGeneralCSS()} }
${commonStyles.getButtonCSS()}
.block { downloadFiles(files) {
margin-bottom: 10px; files.forEach((file) => this.downloadFile(file));
} }
`;
downloadFile(file) {
this.statusText = "Loading " + file.filename + "...";
// https://github.com/perry-mitchell/webdav-client#getfilecontents
this.webDavClient
.getFileContents(file.filename)
.then(contents => {
console.log("contents", contents);
// TODO: Generate file and send event
this.statusText = "";
}).catch(error => {
console.error(error.message);
this.statusText = error.message;
});
} }
/** /**
* Returns the list of files in a directory * Returns the parent directory path
* *
* @returns {*[]} * @returns {string} parent directory path
*/ */
getDirectoryContentsHtml() { getParentDirectoryPath() {
let results = []; let path = this.directoryPath.replace(/\/$/, "");
path = path.replace(path.split("/").pop(), "").replace(/\/$/, "");
// this.directoryContents.forEach((content) => {
// results.push(html`
// <tr>
// <td><a href="#" @click="${(e) => { this.fileClicked(content, e); }}">${content.filename}</a></td>
// <td>${content.size}</td>
// </tr>
// `);
// });
return results; return (path === "") ? "/" : path;
} }
fileClicked(file, event) { static get styles() {
this.loadDirectory(this.directoryPath + file.filename); // language=css
event.preventDefault(); return css`
${commonStyles.getGeneralCSS()}
${commonStyles.getButtonCSS()}
.block {
margin-bottom: 10px;
}
`;
} }
render() { render() {
...@@ -194,13 +230,16 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) { ...@@ -194,13 +230,16 @@ export class NextcloudFilePicker extends ScopedElementsMixin(VPULitElement) {
</div> </div>
<div class="block ${classMap({hidden: !this.isPickerActive})}"> <div class="block ${classMap({hidden: !this.isPickerActive})}">
<h2>${this.directoryPath}</h2> <h2>${this.directoryPath}</h2>
<table id="directory-content-table"> <button class="button is-small"
<thead> title="${i18n.t('nextcloud-file-picker.folder-last')}"
<th>Filename</th> @click="${() => { this.loadDirectory(this.lastDirectoryPath); }}">&#8678;</button>
<th>Size</th> <button class="button is-small"
</thead> title="${i18n.t('nextcloud-file-picker.folder-up')}"
<tbody>${this.getDirectoryContentsHtml()}</tbody> @click="${() => { this.loadDirectory(this.getParentDirectoryPath()); }}">&#8679;</button>
</table> <table id="directory-content-table"></table>
<button class="button"
title="${i18n.t('nextcloud-file-picker.folder-up')}"
@click="${() => { this.downloadFiles(this.tabulatorTable.getSelectedData()); }}">${i18n.t('nextcloud-file-picker.select-files')}</button>
</div> </div>
`; `;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment