|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +Object.defineProperty(exports, "__esModule", { |
| 4 | + value: true |
| 5 | +}); |
| 6 | +exports.install = exports.videoPlayer = exports.videojs = undefined; |
| 7 | + |
| 8 | +var _video = require('video.js'); |
| 9 | + |
| 10 | +var _video2 = _interopRequireDefault(_video); |
| 11 | + |
| 12 | +var _objectAssign = require('object-assign'); |
| 13 | + |
| 14 | +var _objectAssign2 = _interopRequireDefault(_objectAssign); |
| 15 | + |
| 16 | +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| 17 | + |
| 18 | +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } |
| 19 | + |
| 20 | +var DEFAULT_EVENTS = ['loadeddata', 'canplay', 'canplaythrough', 'play', 'pause', 'waiting', 'playing', 'ended', 'error']; |
| 21 | + |
| 22 | +var videoPlayerDirective = function videoPlayerDirective(globalOptions) { |
| 23 | + globalOptions.events = globalOptions.events || []; |
| 24 | + globalOptions.options = globalOptions.options || {}; |
| 25 | + |
| 26 | + var getInstanceName = function getInstanceName(el, binding, vnode) { |
| 27 | + var instanceName = null; |
| 28 | + if (binding.arg) { |
| 29 | + instanceName = binding.arg; |
| 30 | + } else if (vnode.data.attrs && (vnode.data.attrs.instanceName || vnode.data.attrs['instance-name'])) { |
| 31 | + instanceName = vnode.data.attrs.instanceName || vnode.data.attrs['instance-name']; |
| 32 | + } else if (el.id) { |
| 33 | + instanceName = el.id; |
| 34 | + } |
| 35 | + return instanceName || 'player'; |
| 36 | + }; |
| 37 | + |
| 38 | + var repairDom = function repairDom(el) { |
| 39 | + if (!el.children.length) { |
| 40 | + var video = document.createElement('video'); |
| 41 | + video.className = 'video-js'; |
| 42 | + el.appendChild(video); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + var initPlayer = function initPlayer(el, binding, vnode) { |
| 47 | + |
| 48 | + var self = vnode.context; |
| 49 | + var attrs = vnode.data.attrs || {}; |
| 50 | + var options = binding.value || {}; |
| 51 | + var instanceName = getInstanceName(el, binding, vnode); |
| 52 | + var customEventName = attrs.customEventName || 'statechanged'; |
| 53 | + var player = self[instanceName]; |
| 54 | + |
| 55 | + var componentEvents = attrs.events || []; |
| 56 | + var playsinline = attrs.playsinline || false; |
| 57 | + |
| 58 | + if (playsinline) { |
| 59 | + el.children[0].setAttribute('playsinline', playsinline); |
| 60 | + el.children[0].setAttribute('webkit-playsinline', playsinline); |
| 61 | + el.children[0].setAttribute('x5-playsinline', playsinline); |
| 62 | + el.children[0].setAttribute('x5-video-player-type', 'h5'); |
| 63 | + el.children[0].setAttribute('x5-video-player-fullscreen', false); |
| 64 | + } |
| 65 | + |
| 66 | + if (!!attrs.crossOrigin) { |
| 67 | + el.children[0].crossOrigin = attrs.crossOrigin; |
| 68 | + el.children[0].setAttribute('crossOrigin', attrs.crossOrigin); |
| 69 | + } |
| 70 | + |
| 71 | + if (!player) { |
| 72 | + var videoOptions = (0, _objectAssign2.default)({}, { |
| 73 | + controls: true, |
| 74 | + controlBar: { |
| 75 | + remainingTimeDisplay: false, |
| 76 | + playToggle: {}, |
| 77 | + progressControl: {}, |
| 78 | + fullscreenToggle: {}, |
| 79 | + volumeMenuButton: { |
| 80 | + inline: false, |
| 81 | + vertical: true |
| 82 | + } |
| 83 | + }, |
| 84 | + techOrder: ['html5'], |
| 85 | + plugins: {} |
| 86 | + }, globalOptions.options, options); |
| 87 | + |
| 88 | + if (videoOptions.plugins) { |
| 89 | + delete videoOptions.plugins.__ob__; |
| 90 | + } |
| 91 | + |
| 92 | + var eventEmit = function eventEmit(vnode, name, data) { |
| 93 | + var handlers = vnode.data && vnode.data.on || vnode.componentOptions && vnode.componentOptions.listeners; |
| 94 | + if (handlers && handlers[name]) handlers[name].fns(data); |
| 95 | + }; |
| 96 | + |
| 97 | + var emitPlayerState = function emitPlayerState(event, value) { |
| 98 | + if (event) { |
| 99 | + eventEmit(vnode, event, player); |
| 100 | + } |
| 101 | + if (value) { |
| 102 | + eventEmit(vnode, customEventName, _defineProperty({}, event, value)); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + player = self[instanceName] = (0, _video2.default)(el.children[0], videoOptions, function () { |
| 107 | + var _this = this; |
| 108 | + |
| 109 | + var events = DEFAULT_EVENTS.concat(componentEvents).concat(globalOptions.events); |
| 110 | + |
| 111 | + var onEdEvents = {}; |
| 112 | + for (var i = 0; i < events.length; i++) { |
| 113 | + if (typeof events[i] === 'string' && onEdEvents[events[i]] === undefined) { |
| 114 | + (function (event) { |
| 115 | + onEdEvents[event] = null; |
| 116 | + _this.on(event, function () { |
| 117 | + emitPlayerState(event, true); |
| 118 | + }); |
| 119 | + })(events[i]); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + this.on('timeupdate', function () { |
| 124 | + emitPlayerState('timeupdate', this.currentTime()); |
| 125 | + }); |
| 126 | + |
| 127 | + emitPlayerState('ready'); |
| 128 | + }); |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + var disposePlayer = function disposePlayer(el, binding, vnode) { |
| 133 | + var self = vnode.context; |
| 134 | + var instanceName = getInstanceName(el, binding, vnode); |
| 135 | + var player = self[instanceName]; |
| 136 | + if (player && player.dispose) { |
| 137 | + if (player.techName_ !== 'Flash') { |
| 138 | + player.pause && player.pause(); |
| 139 | + } |
| 140 | + player.dispose(); |
| 141 | + repairDom(el); |
| 142 | + self[instanceName] = null; |
| 143 | + delete self[instanceName]; |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + return { |
| 148 | + inserted: initPlayer, |
| 149 | + |
| 150 | + bind: function bind(el, binding, vnode) { |
| 151 | + repairDom(el); |
| 152 | + }, |
| 153 | + update: function update(el, binding, vnode) { |
| 154 | + var options = binding.value || {}; |
| 155 | + disposePlayer(el, binding, vnode); |
| 156 | + if (options && options.sources && options.sources.length) { |
| 157 | + initPlayer(el, binding, vnode); |
| 158 | + } |
| 159 | + }, |
| 160 | + |
| 161 | + unbind: disposePlayer |
| 162 | + }; |
| 163 | +}; |
| 164 | + |
| 165 | +var videoPlayer = videoPlayerDirective({}); |
| 166 | + |
| 167 | +var install = function install(Vue) { |
| 168 | + var globalOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { |
| 169 | + options: {}, |
| 170 | + events: [] |
| 171 | + }; |
| 172 | + |
| 173 | + Vue.directive('video-player', videoPlayerDirective(globalOptions)); |
| 174 | +}; |
| 175 | + |
| 176 | +var VueVideoPlayer = { videojs: _video2.default, videoPlayer: videoPlayer, install: install }; |
| 177 | + |
| 178 | +exports.default = VueVideoPlayer; |
| 179 | +exports.videojs = _video2.default; |
| 180 | +exports.videoPlayer = videoPlayer; |
| 181 | +exports.install = install; |
0 commit comments