UNPKG

react-player-custom

Version:

A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion

169 lines (144 loc) 6.38 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Facebook = undefined; 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _utils = require('../utils'); var _singlePlayer = require('../singlePlayer'); var _singlePlayer2 = _interopRequireDefault(_singlePlayer); 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 = '//connect.facebook.net/en_US/sdk.js'; var SDK_GLOBAL = 'FB'; var SDK_GLOBAL_READY = 'fbAsyncInit'; var MATCH_URL = /facebook\.com\/([^/?].+\/)?video(s|\.php)[/?].*$/; var PLAYER_ID_PREFIX = 'facebook-player-'; var Facebook = exports.Facebook = function (_Component) { _inherits(Facebook, _Component); function Facebook() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Facebook); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Facebook.__proto__ || Object.getPrototypeOf(Facebook)).call.apply(_ref, [this].concat(args))), _this), _this.callPlayer = _utils.callPlayer, _this.playerID = PLAYER_ID_PREFIX + (0, _utils.randomString)(), _this.mute = function () { _this.callPlayer('mute'); }, _this.unmute = function () { _this.callPlayer('unmute'); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Facebook, [{ key: 'load', value: function load(url, isReady) { var _this2 = this; if (isReady) { (0, _utils.getSDK)(SDK_URL, SDK_GLOBAL, SDK_GLOBAL_READY).then(function (FB) { return FB.XFBML.parse(); }); return; } (0, _utils.getSDK)(SDK_URL, SDK_GLOBAL, SDK_GLOBAL_READY).then(function (FB) { FB.init({ appId: _this2.props.config.facebook.appId, xfbml: true, version: 'v2.5' }); FB.Event.subscribe('xfbml.render', function (msg) { // Here we know the SDK has loaded, even if onReady/onPlay // is not called due to a video that cannot be embedded _this2.props.onLoaded(); }); FB.Event.subscribe('xfbml.ready', function (msg) { if (msg.type === 'video' && msg.id === _this2.playerID) { _this2.player = msg.instance; _this2.player.subscribe('startedPlaying', _this2.props.onPlay); _this2.player.subscribe('paused', _this2.props.onPause); _this2.player.subscribe('finishedPlaying', _this2.props.onEnded); _this2.player.subscribe('startedBuffering', _this2.props.onBuffer); _this2.player.subscribe('finishedBuffering', _this2.props.onBufferEnd); _this2.player.subscribe('error', _this2.props.onError); if (!_this2.props.muted) { // Player is muted by default _this2.callPlayer('unmute'); } _this2.props.onReady(); // For some reason Facebook have added `visibility: hidden` // to the iframe when autoplay fails, so here we set it back document.getElementById(_this2.playerID).querySelector('iframe').style.visibility = 'visible'; } }); }); } }, { key: 'play', value: function play() { this.callPlayer('play'); } }, { key: 'pause', value: function pause() { this.callPlayer('pause'); } }, { key: 'stop', value: function stop() { // Nothing to do } }, { key: 'seekTo', value: function seekTo(seconds) { this.callPlayer('seek', seconds); } }, { key: 'setVolume', value: function setVolume(fraction) { this.callPlayer('setVolume', fraction); } }, { key: 'getDuration', value: function getDuration() { return this.callPlayer('getDuration'); } }, { key: 'getCurrentTime', value: function getCurrentTime() { return this.callPlayer('getCurrentPosition'); } }, { key: 'getSecondsLoaded', value: function getSecondsLoaded() { return null; } }, { key: 'render', value: function render() { var style = { width: '100%', height: '100%', backgroundColor: 'black' }; return _react2['default'].createElement('div', { style: style, id: this.playerID, className: 'fb-video', 'data-href': this.props.url, 'data-autoplay': this.props.playing ? 'true' : 'false', 'data-allowfullscreen': 'true', 'data-controls': this.props.controls ? 'true' : 'false' }); } }]); return Facebook; }(_react.Component); Facebook.displayName = 'Facebook'; Facebook.canPlay = function (url) { return MATCH_URL.test(url); }; Facebook.loopOnEnded = true; exports['default'] = (0, _singlePlayer2['default'])(Facebook);