Blame view

node_modules/vue-video-player/src/ssr.js 5.43 KB
2a09d1a4   liuqimichale   添加宜春 天水 宣化
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  
  /*
  * VueVideoPlayer ssr.js
  * Author: surmon@foxmail.com
  * Github: https://github.com/surmon-china/vue-video-player
  */
  
  // Require sources
  import videojs from 'video.js'
  import objectAssign from 'object-assign'
  
  // as of videojs 6.6.0
  const DEFAULT_EVENTS = [
    'loadeddata',
    'canplay',
    'canplaythrough',
    'play',
    'pause',
    'waiting',
    'playing',
    'ended',
    'error'
  ]
  
  const videoPlayerDirective = globalOptions => {
  
    // globalOptions
    globalOptions.events = globalOptions.events || []
    globalOptions.options = globalOptions.options || {}
  
    // Get videojs instace name in directive
    const getInstanceName = (el, binding, vnode) => {
      let instanceName = null
      if (binding.arg) {
        instanceName = binding.arg
      } else if (vnode.data.attrs && (vnode.data.attrs.instanceName || vnode.data.attrs['instance-name'])) {
        instanceName = (vnode.data.attrs.instanceName || vnode.data.attrs['instance-name'])
      } else if (el.id) {
        instanceName = el.id
      }
      return instanceName || 'player'
    }
  
    // dom
    const repairDom = el => {
      if (!el.children.length) {
        const video = document.createElement('video')
        video.className = 'video-js'
        el.appendChild(video)
      }
    }
  
    // init
    const initPlayer = (el, binding, vnode) => {
  
      const self = vnode.context
      const attrs = vnode.data.attrs || {}
      const options = binding.value || {}
      const instanceName = getInstanceName(el, binding, vnode)
      const customEventName = attrs.customEventName || 'statechanged'
      let player = self[instanceName]
  
      // options
      const componentEvents = attrs.events || []
      const playsinline = attrs.playsinline || false
  
      // ios fullscreen
      if (playsinline) {
        el.children[0].setAttribute('playsinline', playsinline)
        el.children[0].setAttribute('webkit-playsinline', playsinline)
        el.children[0].setAttribute('x5-playsinline', playsinline)
        el.children[0].setAttribute('x5-video-player-type', 'h5')
        el.children[0].setAttribute('x5-video-player-fullscreen', false)
      }
  
      // cross origin
      if (attrs.crossOrigin) {
        el.children[0].crossOrigin = attrs.crossOrigin
        el.children[0].setAttribute('crossOrigin', attrs.crossOrigin)
      }
  
      // initialize
      if (!player) {
  
        // videoOptions
        const videoOptions = objectAssign({}, {
          controls: true,
          controlBar: {
            remainingTimeDisplay: false,
            playToggle: {},
            progressControl: {},
            fullscreenToggle: {},
            volumeMenuButton: {
              inline: false,
              vertical: true
            }
          },
          techOrder: ['html5'],
          plugins: {}
        }, globalOptions.options, options)
  
        // plugins
        if (videoOptions.plugins) {
          delete videoOptions.plugins.__ob__
        }
  
        // console.log('videoOptions', videoOptions)
  
        // eventEmit
        const eventEmit = (vnode, name, data) => {
          const handlers = (vnode.data && vnode.data.on) || 
                           (vnode.componentOptions && vnode.componentOptions.listeners)
          if (handlers && handlers[name]) handlers[name].fns(data)
        }
  
        // emit event
        const emitPlayerState = (event, value) => {
          if (event) {
            eventEmit(vnode, event, player)
          }
          if (value) {
            eventEmit(vnode, customEventName, { [event]: value })
          }
        }
        
        // instance
        player = self[instanceName] = videojs(el.children[0], videoOptions, function() {
  
          // events
          const events = DEFAULT_EVENTS.concat(componentEvents).concat(globalOptions.events)
  
          // watch events
          const onEdEvents = {}
          for (let i = 0; i < events.length; i++) {
            if (typeof events[i] === 'string' && onEdEvents[events[i]] === undefined) {
              (event => {
                onEdEvents[event] = null
                this.on(event, () => {
                  emitPlayerState(event, true)
                })
              })(events[i])
            }
          }
  
          // watch timeupdate
          this.on('timeupdate', function() {
            emitPlayerState('timeupdate', this.currentTime())
          })
  
          // player readied
          emitPlayerState('ready')
        })
      }
    }
  
    // dispose
    const disposePlayer = (el, binding, vnode) => {
      const self = vnode.context
      const instanceName = getInstanceName(el, binding, vnode)
      const player = self[instanceName]
      if (player && player.dispose) {
        if (player.techName_ !== 'Flash') {
          player.pause && player.pause()
        }
        player.dispose()
        repairDom(el)
        self[instanceName] = null
        delete self[instanceName]
      }
    }
  
    return {
      inserted: initPlayer,
  
      // Bind directive
      bind(el, binding, vnode) {
        repairDom(el)
      },
      
      // Parse text model change
      update(el, binding, vnode) {
        const options = binding.value || {}
        disposePlayer(el, binding, vnode)
        if (options && options.sources && options.sources.length) {
            initPlayer(el, binding, vnode)
          }
      },
      
      // Destroy this directive
      unbind: disposePlayer
    }
  }
  
  // videoPlayer
  const videoPlayer = videoPlayerDirective({})
  
  // Global quill default options
  const install = function (Vue, globalOptions = {
    options: {},
    events: []
  }) {
  
    // Mount quill directive for Vue global
    Vue.directive('video-player', videoPlayerDirective(globalOptions))
  }
  
  const VueVideoPlayer = { videojs, videoPlayer, install }
  
  export default VueVideoPlayer
  export { videojs, videoPlayer, install }