From b89147ecd38b0f95a2e4917aba7f44bf3bb70327 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 7 Jul 2017 18:43:24 +0200 Subject: refactor audioplayer --- client/components/Browser/Folder/Files.jsx | 83 +++++++++++++++++++++++++++++ client/components/Browser/Folder/Folder.jsx | 79 --------------------------- 2 files changed, 83 insertions(+), 79 deletions(-) create mode 100644 client/components/Browser/Folder/Files.jsx delete mode 100644 client/components/Browser/Folder/Folder.jsx (limited to 'client/components/Browser/Folder') diff --git a/client/components/Browser/Folder/Files.jsx b/client/components/Browser/Folder/Files.jsx new file mode 100644 index 0000000..7d439c5 --- /dev/null +++ b/client/components/Browser/Folder/Files.jsx @@ -0,0 +1,83 @@ +import { h, Component } from 'preact' + +import { audioPlayFile } fromĀ '../../../actions' + +import client from '../../../client.js' + +import FileUploadButton from './FileUploadButton.jsx' +import FileLink from '../../../containers/fileLink.js' +import TaskContentLink from '../../../containers/taskContentLink.js' +import TaskStyleLink from '../../../containers/taskStyleLink.js' + +export default class Files extends Component { + constructor(props) { + super() + this.state = { + files: props.folder.files || [], + selected: null, + } + this.addFiles = this.addFiles.bind(this) + if (! props.folder.files) { + client.file.index({ folder_id: props.folder.id }).then( files => this.setState({ files }) ) + } + } + 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') { + audioPlayFile(file) + document.body.style.backgroundImage = 'url(' + pngpath(file) + ')' + } + this.setState({ selected: file }) + } + render() { + const files = (this.state.files).map(toFilenamePair).sort(sortByFilename).map(fromPair).map( (file, i) => { + if (! file) return + return ( +
+ {file.name} + {file.mime} + {file.duration.toFixed(1) + 's'} + + content + 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 diff --git a/client/components/Browser/Folder/Folder.jsx b/client/components/Browser/Folder/Folder.jsx deleted file mode 100644 index a3588f1..0000000 --- a/client/components/Browser/Folder/Folder.jsx +++ /dev/null @@ -1,79 +0,0 @@ -import { h, Component } from 'preact' - -import client from '../../../client.js' - -import FileUploadButton from './FileUploadButton.jsx' -import TaskContentLink from '../../../containers/taskContentLink.js' -import TaskStyleLink from '../../../containers/taskStyleLink.js' - -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} - - content - 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