forked from surmon-china/videojs-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssr.js
executable file
·143 lines (128 loc) · 4.36 KB
/
ssr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
window.videojs = require('video.js')
videojs = videojs.default || videojs
var videoPlayer = {
videojs: videojs,
install: function(Vue) {
Vue.directive('video-player', {
bind: function(el, binding, vnode) {
if (!el.children.length) {
var video = document.createElement('video')
video.className = 'video-js'
el.appendChild(video)
}
},
inserted: function (el, binding, vnode) {
var _this = vnode.context
var attrs = vnode.data.attrs || {}
var customInstanceName = attrs.playerInstanceName || binding.arg
var instanceName = customInstanceName || 'player'
var options = binding.value || {}
var player = _this[instanceName]
var playsinline = attrs.playsinline || false
var customEventName = attrs.customEventName || 'statechanged'
// playsinline
if (playsinline) {
el.children[0].setAttribute('playsinline', playsinline)
el.children[0].setAttribute('webkit-playsinline', playsinline)
}
// initialize
if (!player) {
var optionsKey
var defaultOptions = {
// videojs options
autoplay: false,
controls: true,
preload: 'auto',
fluid: false,
muted: false,
width: '100%',
height: '360',
language: 'en',
controlBar: {
remainingTimeDisplay: false,
playToggle: {},
progressControl: {},
fullscreenToggle: {},
volumeMenuButton: {
inline: false,
vertical: true
}
},
techOrder: ['html5'],
playbackRates: []
}
// assign options
for (optionsKey in defaultOptions) {
if (options[optionsKey] === undefined && defaultOptions[optionsKey] != undefined) {
options[optionsKey] = defaultOptions[optionsKey]
}
}
if (options.plugins) {
delete options.plugins.__ob__
}
// console.log(options)
var eventEmit = function (vnode, name, data) {
var handlers = (vnode.data && vnode.data.on) || (vnode.componentOptions && vnode.componentOptions.listeners)
if (handlers && handlers[name]) {
handlers[name].fns(data)
}
}
// emit event
var emitPlayerState = function (event, value) {
if (event) {
eventEmit(vnode, event, player)
}
if (value) {
var values = {}
values[event] = value
eventEmit(vnode, customEventName, values)
}
}
// instance
player = _this[instanceName] = videojs(el.children[0], options, function() {
// player ready
var self = this
emitPlayerState('ready')
// events
var events = ['loadeddata',
'canplay',
'canplaythrough',
'play',
'pause',
'waiting',
'playing',
'ended']
for (var i = 0; i < events.length; i++) {
(function(event) {
self.on(event, function() {
emitPlayerState(event, true)
})
})(events[i])
}
this.on('timeupdate', function() {
emitPlayerState('timeupdate', this.currentTime())
})
})
}
},
unbind: function (el, binding, vnode) {
var _this = vnode.context
var customInstanceName = vnode.data.attrs ? vnode.data.attrs.playerInstanceName : binding.arg
var instanceName = customInstanceName || 'player'
var player = _this[instanceName]
if (player && videojs) {
player.pause && player.pause()
videojs(el.children[0]).dispose()
if (!el.children.length) {
var video = document.createElement('video')
video.className = 'video-js'
el.appendChild(video)
}
_this[instanceName] = null
delete _this[instanceName]
}
}
})
}
}
module.exports = videoPlayer