import * as React from "react"; import "./file-browser.css"; import Dropzone from "react-dropzone"; import Icon from "./icon"; export class FileBrowser extends React.Component { constructor(props) { super(props); this.state = { api: props.api, files: props.files, token: props.token, filesToUpload: [], } } svgMiddle(indentation, size=64) { let style = (indentation > 1 ? { marginLeft: ((indentation-1)*size) + "px" } : {}); return ; } svgEnd(indentation, size=64) { let style = (indentation > 1 ? { marginLeft: ((indentation-1)*size) + "px" } : {}); return ; } formatSize(size) { const suffixes = ["B","KiB","MiB","GiB","TiB"]; let i = 0; for (; i < suffixes.length && size >= 1024; i++) { size /= 1024.0; } return size.toFixed(1) + " " + suffixes[i]; } canUpload() { return this.state.api.loggedIn || (this.state.token.valid && this.state.token.type === "upload"); } onAddUploadFiles(acceptedFiles) { let files = this.state.filesToUpload.slice(); files.push(...acceptedFiles); this.setState({ ...this.state, filesToUpload: files }); } getSelectedIds(items = null, recursive = true) { let ids = []; items = items || this.state.files; for (const fileItem of Object.values(items)) { if (fileItem.selected) { ids.push(fileItem.uid); } if (recursive && fileItem.isDirectory) { ids.push(...this.getSelectedIds(fileItem.items)); } } return ids; } onSelectAll(selected, items) { for (const fileElement of Object.values(items)) { fileElement.selected = selected; if (fileElement.isDirectory) { this.onSelectAll(selected, fileElement.items); } } } onSelectFile(e, uid, items=null) { let found = false; let updatedFiles = (items === null) ? {...this.state.files} : items; if (updatedFiles.hasOwnProperty(uid)) { let fileElement = updatedFiles[uid]; found = true; fileElement.selected = e.target.checked; if (fileElement.isDirectory) { this.onSelectAll(fileElement.selected, fileElement.items); } } else { for (const fileElement of Object.values(updatedFiles)) { if (fileElement.isDirectory) { if (this.onSelectFile(e, uid, fileElement.items)) { if (!e.target.checked) { fileElement.selected = false; } else if (this.getSelectedIds(fileElement.items, false).length === Object.values(fileElement.items).length) { fileElement.selected = true; } found = true; break; } } } } if (items === null) { this.setState({ ...this.state, files: updatedFiles }); } return found; } createFileList(elements, indentation=0) { let rows = []; let i = 0; const values = Object.values(elements); for (const fileElement of values) { let name = fileElement.name; let uid = fileElement.uid; let type = (fileElement.isDirectory ? "Directory" : fileElement.mimeType); let size = (fileElement.isDirectory ? "" : this.formatSize(fileElement.size)); // let iconUrl = (fileElement.directory ? "/img/icon/") let iconUrl = ""; let token = (this.state.token && this.state.token.valid ? "&token=" + this.token.state.value : ""); let svg = <>>; if (indentation > 0) { if (i === values.length - 1) { svg = this.svgEnd(indentation, 48); } else { svg = this.svgMiddle(indentation, 48); } } rows.push(
Drag 'n' drop some files here, or click to select files
{ uploadedFiles.length === 0 ?Name | Type | Size |
---|