blueimp-gallery-vimeo.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * blueimp Gallery Vimeo Video Factory JS
  3. * https://github.com/blueimp/Gallery
  4. *
  5. * Copyright 2013, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global define, window, document, $f */
  12. ;(function (factory) {
  13. 'use strict'
  14. if (typeof define === 'function' && define.amd) {
  15. // Register as an anonymous AMD module:
  16. define(['./blueimp-helper', './blueimp-gallery-video'], factory)
  17. } else {
  18. // Browser globals:
  19. factory(window.blueimp.helper || window.jQuery, window.blueimp.Gallery)
  20. }
  21. })(function ($, Gallery) {
  22. 'use strict'
  23. if (!window.postMessage) {
  24. return Gallery
  25. }
  26. $.extend(Gallery.prototype.options, {
  27. // The list object property (or data attribute) with the Vimeo video id:
  28. vimeoVideoIdProperty: 'vimeo',
  29. // The URL for the Vimeo video player, can be extended with custom parameters:
  30. // https://developer.vimeo.com/player/embedding
  31. vimeoPlayerUrl:
  32. '//player.vimeo.com/video/VIDEO_ID?api=1&player_id=PLAYER_ID',
  33. // The prefix for the Vimeo video player ID:
  34. vimeoPlayerIdPrefix: 'vimeo-player-',
  35. // Require a click on the native Vimeo player for the initial playback:
  36. vimeoClickToPlay: true
  37. })
  38. var textFactory =
  39. Gallery.prototype.textFactory || Gallery.prototype.imageFactory
  40. var VimeoPlayer = function (url, videoId, playerId, clickToPlay) {
  41. this.url = url
  42. this.videoId = videoId
  43. this.playerId = playerId
  44. this.clickToPlay = clickToPlay
  45. this.element = document.createElement('div')
  46. this.listeners = {}
  47. }
  48. var counter = 0
  49. $.extend(VimeoPlayer.prototype, {
  50. canPlayType: function () {
  51. return true
  52. },
  53. on: function (type, func) {
  54. this.listeners[type] = func
  55. return this
  56. },
  57. loadAPI: function () {
  58. var that = this
  59. var apiUrl = '//f.vimeocdn.com/js/froogaloop2.min.js'
  60. var scriptTags = document.getElementsByTagName('script')
  61. var i = scriptTags.length
  62. var scriptTag
  63. var called
  64. function callback () {
  65. if (!called && that.playOnReady) {
  66. that.play()
  67. }
  68. called = true
  69. }
  70. while (i) {
  71. i -= 1
  72. if (scriptTags[i].src === apiUrl) {
  73. scriptTag = scriptTags[i]
  74. break
  75. }
  76. }
  77. if (!scriptTag) {
  78. scriptTag = document.createElement('script')
  79. scriptTag.src = apiUrl
  80. }
  81. $(scriptTag).on('load', callback)
  82. scriptTags[0].parentNode.insertBefore(scriptTag, scriptTags[0])
  83. // Fix for cached scripts on IE 8:
  84. if (/loaded|complete/.test(scriptTag.readyState)) {
  85. callback()
  86. }
  87. },
  88. onReady: function () {
  89. var that = this
  90. this.ready = true
  91. this.player.addEvent('play', function () {
  92. that.hasPlayed = true
  93. that.onPlaying()
  94. })
  95. this.player.addEvent('pause', function () {
  96. that.onPause()
  97. })
  98. this.player.addEvent('finish', function () {
  99. that.onPause()
  100. })
  101. if (this.playOnReady) {
  102. this.play()
  103. }
  104. },
  105. onPlaying: function () {
  106. if (this.playStatus < 2) {
  107. this.listeners.playing()
  108. this.playStatus = 2
  109. }
  110. },
  111. onPause: function () {
  112. this.listeners.pause()
  113. delete this.playStatus
  114. },
  115. insertIframe: function () {
  116. var iframe = document.createElement('iframe')
  117. iframe.src = this.url
  118. .replace('VIDEO_ID', this.videoId)
  119. .replace('PLAYER_ID', this.playerId)
  120. iframe.id = this.playerId
  121. this.element.parentNode.replaceChild(iframe, this.element)
  122. this.element = iframe
  123. },
  124. play: function () {
  125. var that = this
  126. if (!this.playStatus) {
  127. this.listeners.play()
  128. this.playStatus = 1
  129. }
  130. if (this.ready) {
  131. if (
  132. !this.hasPlayed &&
  133. (this.clickToPlay ||
  134. (window.navigator &&
  135. /iP(hone|od|ad)/.test(window.navigator.platform)))
  136. ) {
  137. // Manually trigger the playing callback if clickToPlay
  138. // is enabled and to workaround a limitation in iOS,
  139. // which requires synchronous user interaction to start
  140. // the video playback:
  141. this.onPlaying()
  142. } else {
  143. this.player.api('play')
  144. }
  145. } else {
  146. this.playOnReady = true
  147. if (!window.$f) {
  148. this.loadAPI()
  149. } else if (!this.player) {
  150. this.insertIframe()
  151. this.player = $f(this.element)
  152. this.player.addEvent('ready', function () {
  153. that.onReady()
  154. })
  155. }
  156. }
  157. },
  158. pause: function () {
  159. if (this.ready) {
  160. this.player.api('pause')
  161. } else if (this.playStatus) {
  162. delete this.playOnReady
  163. this.listeners.pause()
  164. delete this.playStatus
  165. }
  166. }
  167. })
  168. $.extend(Gallery.prototype, {
  169. VimeoPlayer: VimeoPlayer,
  170. textFactory: function (obj, callback) {
  171. var options = this.options
  172. var videoId = this.getItemProperty(obj, options.vimeoVideoIdProperty)
  173. if (videoId) {
  174. if (this.getItemProperty(obj, options.urlProperty) === undefined) {
  175. obj[options.urlProperty] = '//vimeo.com/' + videoId
  176. }
  177. counter += 1
  178. return this.videoFactory(
  179. obj,
  180. callback,
  181. new VimeoPlayer(
  182. options.vimeoPlayerUrl,
  183. videoId,
  184. options.vimeoPlayerIdPrefix + counter,
  185. options.vimeoClickToPlay
  186. )
  187. )
  188. }
  189. return textFactory.call(this, obj, callback)
  190. }
  191. })
  192. return Gallery
  193. })