From 2263f412817d6d2d36372e7617feb0d97fa57af8 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 4 Jul 2017 02:11:44 +0200 Subject: break out browser and tasks --- .../components/Browser/Folder/FileUploadButton.jsx | 35 ++++++++++ client/components/Browser/Folder/Folder.jsx | 77 ++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 client/components/Browser/Folder/FileUploadButton.jsx create mode 100644 client/components/Browser/Folder/Folder.jsx (limited to 'client/components/Browser/Folder') diff --git a/client/components/Browser/Folder/FileUploadButton.jsx b/client/components/Browser/Folder/FileUploadButton.jsx new file mode 100644 index 0000000..136cd45 --- /dev/null +++ b/client/components/Browser/Folder/FileUploadButton.jsx @@ -0,0 +1,35 @@ +import { h, Component } from 'preact' + +import client from '../../../client.js' + +export default class Folder extends Component { + constructor(props) { + super() + this.state = { + } + this.updateFiles = this.updateFiles.bind(this) + } + updateFiles(event){ + const name = event.target.name + const files = event.target.files + client.upload(this.props.folder.id, files).then( got_files => { + this.props.addFiles(got_files) + }) + } + + render() { + const files = (this.props.folder.files || []).map( (file, i) => { + return ( +
+ {file.name} +
+ ) + }) + return ( +
+ + + upload +
+ ) + } +} diff --git a/client/components/Browser/Folder/Folder.jsx b/client/components/Browser/Folder/Folder.jsx new file mode 100644 index 0000000..3ed3ddb --- /dev/null +++ b/client/components/Browser/Folder/Folder.jsx @@ -0,0 +1,77 @@ +import { h, Component } from 'preact' + +import client from '../../../client.js' + +import FileUploadButton from './FileUploadButton.jsx' + +export default class Folder extends Component { + constructor(props) { + super() + this.state = { + files: props.folder.files || [], + } + this.addFiles = this.addFiles.bind(this) + if (! props.folder.files) { + client.file.index({ folder_id: props.folder.id }).then( files => this.setState({ files }) ) + } + this.audio = document.createElement('audio') + } + addFiles(newFiles) { + if (! newFiles) return + const files = this.state.files.concat(newFiles).sort( (a,b) => { return b.id - a.id } ) + this.setState({ files }) + } + handleClick(file) { + if (file.type === 'audio') { + this.audio.setAttribute('src', mp3path(file)) + this.audio.play() + document.body.style.backgroundImage = 'url(' + pngpath(file) + ')' + } + } + render() { + const files = (this.state.files).map(toFilenamePair).sort(sortByFilename).map(fromPair).map( (file, i) => { + if (! file) return + return ( +
+ this.handleClick(file)}>{file.name} + {file.mime} + + this.props.setContent(file)}>content > + this.props.setStyle(file)}>style > + +
+ ) + }) + return ( +
+
+ {this.props.folder.name} +
+ + +
+
+
+ {files} +
+
+ ) + } +} + +function toFilenamePair (file) { return [file.name.toLowerCase(), file] } +function sortByFilename (a,b) { return a[0] < b[0] ? -1 : a[0] == b[0] ? 0 : 1 } +function fromPair (pair) { return pair[1] } + +function filepath (file) { + return '/data/' + file.folder_id + '/' + encodeURIComponent(file.name) +} +function mp3path (file) { + if (file.mime !== 'audio/mp3') { + return filepath(file) + '.mp3' + } + return filepath(file) +} +function pngpath (file) { + return filepath(file) + '.png' +} \ No newline at end of file -- cgit v1.2.3-70-g09d2