1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 09:35:10 +01:00
onion/js/components/ascribe_media/media_player.js

134 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-06-05 14:14:59 +02:00
'use strict';
2015-05-27 17:34:15 +02:00
import React from 'react';
import InjectInHeadMixin from '../../mixins/inject_in_head_mixin';
2015-06-04 17:37:46 +02:00
import Panel from 'react-bootstrap/lib/Panel';
2015-06-16 14:42:36 +02:00
import AppConstants from '../../constants/application_constants.js';
2015-05-27 17:34:15 +02:00
/**
* This is the component that implements display-specific functionality.
*
* ResourceViewer handles the following mimetypes:
* - image
* - video
* - audio
* - pdf
* - other
*/
2015-06-04 17:37:46 +02:00
let Other = React.createClass({
2015-06-05 14:14:59 +02:00
propTypes: {
url: React.PropTypes.string.isRequired
},
2015-06-04 17:37:46 +02:00
render() {
let ext = this.props.url.split('.').pop();
return (
<Panel className="media-other">
<p className="text-center">
.{ext}
</p>
</Panel>
);
}
});
2015-06-03 16:53:27 +02:00
let Image = React.createClass({
2015-06-05 14:14:59 +02:00
propTypes: {
url: React.PropTypes.string.isRequired,
preview: React.PropTypes.string.isRequired
},
2015-06-05 13:15:31 +02:00
mixins: [InjectInHeadMixin],
componentDidMount() {
this.inject('http://code.jquery.com/jquery-2.1.4.min.js')
.then(() =>
Promise.all([
2015-06-16 14:42:36 +02:00
this.inject(AppConstants.baseUrl + 'static/thirdparty/shmui/shmui.css'),
this.inject(AppConstants.baseUrl + 'static/thirdparty/shmui/jquery.shmui.js')
2015-06-05 14:14:59 +02:00
]).then(() => { window.jQuery('.shmui-ascribe').shmui(); }));
2015-06-05 13:15:31 +02:00
},
2015-06-03 16:53:27 +02:00
render() {
2015-06-04 17:17:39 +02:00
return (
2015-06-05 13:15:31 +02:00
<img className="shmui-ascribe" src={this.props.preview} data-large-src={this.props.url}/>
2015-06-04 17:37:46 +02:00
);
2015-06-03 16:53:27 +02:00
}
});
let Video = React.createClass({
2015-05-27 17:34:15 +02:00
propTypes: {
2015-06-03 16:53:27 +02:00
preview: React.PropTypes.string.isRequired,
url: React.PropTypes.string.isRequired,
extraData: React.PropTypes.array.isRequired
2015-05-27 17:34:15 +02:00
},
mixins: [InjectInHeadMixin],
2015-06-03 16:53:27 +02:00
getInitialState() {
return { ready: false };
},
2015-05-27 17:34:15 +02:00
componentDidMount() {
2015-06-03 16:53:27 +02:00
this.inject('http://code.jquery.com/jquery-2.1.4.min.js')
.then(() =>
Promise.all([
this.inject('https://cdnjs.cloudflare.com/ajax/libs/mediaelement/2.17.0/mediaelement-and-player.min.js'),
this.inject('https://cdnjs.cloudflare.com/ajax/libs/mediaelement/2.17.0/mediaelementplayer.min.css')
2015-06-05 14:14:59 +02:00
]).then(this.ready));
},
ready() {
this.setState({ready: true});
2015-06-03 16:53:27 +02:00
},
render() {
if (this.state.ready) {
return (
<video poster={this.props.preview} controls="controls" preload="none">
{this.props.extraData.map((data, i) =>
<source key={i} type={'video/' + data.type} src={data.url} />
)}
</video>
);
} else {
2015-06-05 14:14:59 +02:00
return (
2015-06-03 16:53:27 +02:00
<Image src={this.props.preview} />
);
}
}
});
let resourceMap = {
'image': Image,
2015-06-04 17:37:46 +02:00
'video': Video,
'other': Other
2015-06-05 14:14:59 +02:00
};
2015-06-03 16:53:27 +02:00
2015-06-04 16:15:59 +02:00
let MediaPlayer = React.createClass({
2015-06-03 16:53:27 +02:00
propTypes: {
mimetype: React.PropTypes.oneOf(['image', 'video', 'audio', 'pdf', 'other']).isRequired,
preview: React.PropTypes.string.isRequired,
2015-06-05 14:14:59 +02:00
url: React.PropTypes.string.isRequired,
extraData: React.PropTypes.array
2015-05-27 17:34:15 +02:00
},
render() {
2015-06-04 17:37:46 +02:00
let Component = resourceMap[this.props.mimetype] || Other;
2015-06-03 16:53:27 +02:00
2015-05-27 17:34:15 +02:00
return (
2015-06-04 16:15:59 +02:00
<div className="ascribe-media-player">
2015-06-03 16:53:27 +02:00
<Component preview={this.props.preview}
url={this.props.url}
extraData={this.props.extraData} />
2015-05-27 17:34:15 +02:00
</div>
);
}
});
2015-06-04 16:15:59 +02:00
export default MediaPlayer;