react-player
Version:
A react component for playing a variety of URLs, including file paths, YouTube, Facebook, SoundCloud, Streamable, Vidme, Vimeo and Wistia
189 lines (161 loc) • 7.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _Base2 = require('./Base');
var _Base3 = _interopRequireDefault(_Base2);
var _utils = require('../utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var SDK_URL = 'https://player.vimeo.com/api/player.js';
var SDK_GLOBAL = 'Vimeo';
var MATCH_URL = /https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/;
var BLANK_VIDEO_URL = 'https://vimeo.com/127250231';
var Vimeo = function (_Base) {
_inherits(Vimeo, _Base);
function Vimeo() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Vimeo);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Vimeo.__proto__ || Object.getPrototypeOf(Vimeo)).call.apply(_ref, [this].concat(args))), _this), _this.ref = function (container) {
_this.container = container;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Vimeo, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _props = this.props,
url = _props.url,
config = _props.config;
if (!url && config.vimeo.preload) {
this.preloading = true;
this.load(BLANK_VIDEO_URL);
}
_get(Vimeo.prototype.__proto__ || Object.getPrototypeOf(Vimeo.prototype), 'componentDidMount', this).call(this);
}
}, {
key: 'load',
value: function load(url) {
var _this2 = this;
var id = url.match(MATCH_URL)[3];
this.duration = null;
if (this.isReady) {
this.player.loadVideo(id);
return;
}
if (this.loadingSDK) {
this.loadOnReady = url;
return;
}
this.loadingSDK = true;
(0, _utils.getSDK)(SDK_URL, SDK_GLOBAL).then(function (Vimeo) {
_this2.player = new Vimeo.Player(_this2.container, _extends({}, _this2.props.config.vimeo.playerOptions, {
url: url,
loop: _this2.props.loop
}));
_this2.player.on('loaded', function () {
_this2.onReady();
var iframe = _this2.container.querySelector('iframe');
iframe.style.width = '100%';
iframe.style.height = '100%';
});
_this2.player.on('play', function (_ref2) {
var duration = _ref2.duration;
_this2.duration = duration;
_this2.onPlay();
});
_this2.player.on('pause', _this2.props.onPause);
_this2.player.on('seeked', function (e) {
return _this2.props.onSeek(e.seconds);
});
_this2.player.on('ended', _this2.props.onEnded);
_this2.player.on('error', _this2.props.onError);
_this2.player.on('timeupdate', function (_ref3) {
var percent = _ref3.percent;
_this2.fractionPlayed = percent;
});
_this2.player.on('progress', function (_ref4) {
var percent = _ref4.percent;
_this2.fractionLoaded = percent;
});
}, this.props.onError);
}
}, {
key: 'play',
value: function play() {
if (!this.isReady) return;
this.player.play();
}
}, {
key: 'pause',
value: function pause() {
if (!this.isReady) return;
this.player.pause();
}
}, {
key: 'stop',
value: function stop() {
if (!this.isReady) return;
this.player.unload();
}
}, {
key: 'seekTo',
value: function seekTo(amount) {
var seconds = _get(Vimeo.prototype.__proto__ || Object.getPrototypeOf(Vimeo.prototype), 'seekTo', this).call(this, amount);
if (!this.isReady || !this.player.setCurrentTime) return;
this.player.setCurrentTime(seconds);
}
}, {
key: 'setVolume',
value: function setVolume(fraction) {
this.player.setVolume(fraction);
}
}, {
key: 'setPlaybackRate',
value: function setPlaybackRate(rate) {
return null;
}
}, {
key: 'getDuration',
value: function getDuration() {
return this.duration;
}
}, {
key: 'getFractionPlayed',
value: function getFractionPlayed() {
return this.fractionPlayed || null;
}
}, {
key: 'getFractionLoaded',
value: function getFractionLoaded() {
return this.fractionLoaded || null;
}
}, {
key: 'render',
value: function render() {
var style = {
height: '100%',
display: this.props.url ? 'block' : 'none'
};
return _react2['default'].createElement('div', { style: style, ref: this.ref });
}
}], [{
key: 'canPlay',
value: function canPlay(url) {
return MATCH_URL.test(url);
}
}]);
return Vimeo;
}(_Base3['default']);
Vimeo.displayName = 'Vimeo';
exports['default'] = Vimeo;