ssr.js 4.4 KB

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