dailymotion.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*!
  2. * MediaElement.js
  3. * http://www.mediaelementjs.com/
  4. *
  5. * Wrapper that mimics native HTML5 MediaElement (audio and video)
  6. * using a variety of technologies (pure JavaScript, Flash, iframe)
  7. *
  8. * Copyright 2010-2017, John Dyer (http://j.hn/)
  9. * License: MIT
  10. *
  11. */(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  12. 'use strict';
  13. var DailyMotionApi = {
  14. isSDKStarted: false,
  15. isSDKLoaded: false,
  16. iframeQueue: [],
  17. enqueueIframe: function enqueueIframe(settings) {
  18. if (DailyMotionApi.isLoaded) {
  19. DailyMotionApi.createIframe(settings);
  20. } else {
  21. DailyMotionApi.loadIframeApi();
  22. DailyMotionApi.iframeQueue.push(settings);
  23. }
  24. },
  25. loadIframeApi: function loadIframeApi() {
  26. if (!DailyMotionApi.isSDKStarted) {
  27. mejs.Utils.loadScript('https://api.dmcdn.net/all.js');
  28. DailyMotionApi.isSDKStarted = true;
  29. }
  30. },
  31. apiReady: function apiReady() {
  32. DailyMotionApi.isLoaded = true;
  33. DailyMotionApi.isSDKLoaded = true;
  34. while (DailyMotionApi.iframeQueue.length > 0) {
  35. var settings = DailyMotionApi.iframeQueue.pop();
  36. DM.init({
  37. apiKey: settings.apiKey,
  38. status: settings.status,
  39. cookie: settings.cookie
  40. });
  41. DailyMotionApi.createIframe(settings);
  42. }
  43. },
  44. createIframe: function createIframe(settings) {
  45. var player = DM.player(settings.container, {
  46. height: settings.height || '100%',
  47. width: settings.width || '100%',
  48. video: settings.videoId,
  49. params: Object.assign({ api: true }, settings.params),
  50. origin: location.host
  51. });
  52. player.addEventListener('apiready', function () {
  53. window['__ready__' + settings.id](player, { paused: true, ended: false });
  54. });
  55. },
  56. getDailyMotionId: function getDailyMotionId(url) {
  57. var parts = url.split('/'),
  58. lastPart = parts[parts.length - 1],
  59. dashParts = lastPart.split('_');
  60. return dashParts[0];
  61. }
  62. };
  63. var DailyMotionIframeRenderer = {
  64. name: 'dailymotion_iframe',
  65. options: {
  66. prefix: 'dailymotion_iframe',
  67. dailymotion: {
  68. width: '100%',
  69. height: '100%',
  70. params: {
  71. autoplay: false,
  72. chromeless: 1,
  73. info: 0,
  74. logo: 0,
  75. related: 0
  76. },
  77. apiKey: null,
  78. status: true,
  79. cookie: true
  80. }
  81. },
  82. canPlayType: function canPlayType(type) {
  83. return ~['video/dailymotion', 'video/x-dailymotion'].indexOf(type.toLowerCase());
  84. },
  85. create: function create(mediaElement, options, mediaFiles) {
  86. var dm = {},
  87. apiStack = [],
  88. readyState = 4;
  89. var events = void 0,
  90. dmPlayer = null,
  91. dmIframe = null,
  92. muted = mediaElement.originalNode.muted;
  93. dm.options = options;
  94. dm.id = mediaElement.id + '_' + options.prefix;
  95. dm.mediaElement = mediaElement;
  96. var props = mejs.html5media.properties,
  97. assignGettersSetters = function assignGettersSetters(propName) {
  98. var capName = '' + propName.substring(0, 1).toUpperCase() + propName.substring(1);
  99. dm['get' + capName] = function () {
  100. if (dmPlayer !== null) {
  101. var value = null;
  102. switch (propName) {
  103. case 'currentTime':
  104. return dmPlayer.currentTime;
  105. case 'duration':
  106. return isNaN(dmPlayer.duration) ? 0 : dmPlayer.duration;
  107. case 'volume':
  108. return dmPlayer.volume;
  109. case 'paused':
  110. return dmPlayer.paused;
  111. case 'ended':
  112. return dmPlayer.ended;
  113. case 'muted':
  114. muted = dmPlayer.muted;
  115. return muted;
  116. case 'buffered':
  117. var percentLoaded = dmPlayer.bufferedTime,
  118. duration = dmPlayer.duration;
  119. return {
  120. start: function start() {
  121. return 0;
  122. },
  123. end: function end() {
  124. return percentLoaded / duration;
  125. },
  126. length: 1
  127. };
  128. case 'src':
  129. return mediaElement.originalNode.getAttribute('src');
  130. case 'readyState':
  131. return readyState;
  132. }
  133. return value;
  134. } else {
  135. return null;
  136. }
  137. };
  138. dm['set' + capName] = function (value) {
  139. if (dmPlayer !== null) {
  140. switch (propName) {
  141. case 'src':
  142. var url = typeof value === 'string' ? value : value[0].src;
  143. dmPlayer.load(DailyMotionApi.getDailyMotionId(url));
  144. break;
  145. case 'currentTime':
  146. dmPlayer.seek(value);
  147. break;
  148. case 'muted':
  149. if (value === true) {
  150. dmPlayer.setVolume(0);
  151. }
  152. dmPlayer.setMuted(value);
  153. muted = value;
  154. setTimeout(function () {
  155. var event = mejs.Utils.createEvent('volumechange', dm);
  156. mediaElement.dispatchEvent(event);
  157. }, 50);
  158. break;
  159. case 'volume':
  160. dmPlayer.setVolume(value);
  161. if (value === 0 && !dmPlayer.muted) {
  162. dmPlayer.setMuted(true);
  163. muted = true;
  164. } else if (value > 0 && dmPlayer.muted) {
  165. dmPlayer.setMuted(false);
  166. muted = false;
  167. }
  168. setTimeout(function () {
  169. var event = mejs.Utils.createEvent('volumechange', dm);
  170. mediaElement.dispatchEvent(event);
  171. }, 50);
  172. break;
  173. case 'readyState':
  174. var event = mejs.Utils.createEvent('canplay', dm);
  175. mediaElement.dispatchEvent(event);
  176. break;
  177. default:
  178. break;
  179. }
  180. } else {
  181. apiStack.push({ type: 'set', propName: propName, value: value });
  182. }
  183. };
  184. };
  185. for (var i = 0, total = props.length; i < total; i++) {
  186. assignGettersSetters(props[i]);
  187. }
  188. var methods = mejs.html5media.methods,
  189. assignMethods = function assignMethods(methodName) {
  190. dm[methodName] = function () {
  191. if (dmPlayer !== null) {
  192. switch (methodName) {
  193. case 'play':
  194. return dmPlayer.play();
  195. case 'pause':
  196. return dmPlayer.pause();
  197. case 'load':
  198. return null;
  199. }
  200. } else {
  201. apiStack.push({ type: 'call', methodName: methodName });
  202. }
  203. };
  204. };
  205. for (var _i = 0, _total = methods.length; _i < _total; _i++) {
  206. assignMethods(methods[_i]);
  207. }
  208. window['__ready__' + dm.id] = function (_dmPlayer) {
  209. mediaElement.dmPlayer = dmPlayer = _dmPlayer;
  210. if (apiStack.length) {
  211. for (var _i2 = 0, _total2 = apiStack.length; _i2 < _total2; _i2++) {
  212. var stackItem = apiStack[_i2];
  213. if (stackItem.type === 'set') {
  214. var propName = stackItem.propName,
  215. capName = '' + propName.substring(0, 1).toUpperCase() + propName.substring(1);
  216. dm['set' + capName](stackItem.value);
  217. } else if (stackItem.type === 'call') {
  218. dm[stackItem.methodName]();
  219. }
  220. }
  221. }
  222. dmIframe = document.getElementById(dm.id);
  223. events = ['mouseover', 'mouseout'];
  224. var assignEvents = function assignEvents(e) {
  225. var event = mejs.Utils.createEvent(e.type, dm);
  226. mediaElement.dispatchEvent(event);
  227. };
  228. for (var _i3 = 0, _total3 = events.length; _i3 < _total3; _i3++) {
  229. dmIframe.addEventListener(events[_i3], assignEvents, false);
  230. }
  231. if (mediaElement.originalNode.muted) {
  232. dmPlayer.setVolume(0);
  233. dmPlayer.setMuted(true);
  234. } else {
  235. dmPlayer.setVolume(dmPlayer.volume);
  236. dmPlayer.setMuted(false);
  237. }
  238. events = mejs.html5media.events;
  239. events = events.concat(['click', 'mouseover', 'mouseout']);
  240. var assignNativeEvents = function assignNativeEvents(eventName) {
  241. if (eventName !== 'ended') {
  242. dmPlayer.addEventListener(eventName, function (e) {
  243. var event = mejs.Utils.createEvent(e.type, dm);
  244. mediaElement.dispatchEvent(event);
  245. });
  246. }
  247. };
  248. for (var _i4 = 0, _total4 = events.length; _i4 < _total4; _i4++) {
  249. assignNativeEvents(events[_i4]);
  250. }
  251. dmPlayer.addEventListener('ad_start', function () {
  252. var event = mejs.Utils.createEvent('play', dm);
  253. mediaElement.dispatchEvent(event);
  254. event = mejs.Utils.createEvent('progress', dm);
  255. mediaElement.dispatchEvent(event);
  256. event = mejs.Utils.createEvent('timeupdate', dm);
  257. mediaElement.dispatchEvent(event);
  258. });
  259. dmPlayer.addEventListener('ad_timeupdate', function () {
  260. var event = mejs.Utils.createEvent('timeupdate', dm);
  261. mediaElement.dispatchEvent(event);
  262. });
  263. dmPlayer.addEventListener('ad_pause', function () {
  264. var event = mejs.Utils.createEvent('pause', dm);
  265. mediaElement.dispatchEvent(event);
  266. });
  267. dmPlayer.addEventListener('start', function () {
  268. if (dmPlayer.muted) {
  269. var event = mejs.Utils.createEvent('volumechange', dm);
  270. mediaElement.dispatchEvent(event);
  271. }
  272. });
  273. dmPlayer.addEventListener('video_start', function () {
  274. var event = mejs.Utils.createEvent('play', dm);
  275. mediaElement.dispatchEvent(event);
  276. var playingEvent = mejs.Utils.createEvent('playing', dm);
  277. mediaElement.dispatchEvent(playingEvent);
  278. });
  279. dmPlayer.addEventListener('ad_timeupdate', function () {
  280. var event = mejs.Utils.createEvent('timeupdate', dm);
  281. mediaElement.dispatchEvent(event);
  282. });
  283. dmPlayer.addEventListener('video_end', function () {
  284. var event = mejs.Utils.createEvent('ended', dm);
  285. mediaElement.dispatchEvent(event);
  286. if (mediaElement.originalNode.getAttribute('loop')) {
  287. dmPlayer.play();
  288. }
  289. });
  290. var initEvents = ['rendererready', 'loadedmetadata', 'loadeddata', 'canplay'];
  291. for (var _i5 = 0, _total5 = initEvents.length; _i5 < _total5; _i5++) {
  292. var event = mejs.Utils.createEvent(initEvents[_i5], dm);
  293. mediaElement.dispatchEvent(event);
  294. }
  295. };
  296. var dmContainer = document.createElement('div');
  297. dmContainer.id = dm.id;
  298. mediaElement.appendChild(dmContainer);
  299. if (mediaElement.originalNode) {
  300. dmContainer.style.width = mediaElement.originalNode.style.width;
  301. dmContainer.style.height = mediaElement.originalNode.style.height;
  302. }
  303. mediaElement.originalNode.style.display = 'none';
  304. var videoId = DailyMotionApi.getDailyMotionId(mediaFiles[0].src),
  305. dmSettings = {
  306. id: dm.id,
  307. container: dmContainer,
  308. videoId: videoId
  309. };
  310. dmSettings.params = Object.assign({}, dm.options.dailymotion);
  311. dmSettings.params.controls = !!mediaElement.originalNode.controls;
  312. if (mediaElement.originalNode.autoplay) {
  313. dmSettings.params.autoplay = true;
  314. }
  315. if (mediaElement.originalNode.muted) {
  316. dmSettings.params.mute = true;
  317. }
  318. dmSettings.params.api = '1';
  319. DailyMotionApi.enqueueIframe(dmSettings);
  320. dm.hide = function () {
  321. dm.pause();
  322. if (dmIframe) {
  323. dmIframe.style.display = 'none';
  324. }
  325. };
  326. dm.show = function () {
  327. if (dmIframe) {
  328. dmIframe.style.display = '';
  329. }
  330. };
  331. dm.setSize = function (width, height) {
  332. if (dmIframe) {
  333. dmIframe.width = width;
  334. dmIframe.height = height;
  335. }
  336. };
  337. dm.destroy = function () {
  338. dmPlayer.destroy();
  339. };
  340. return dm;
  341. }
  342. };
  343. mejs.Utils.typeChecks.push(function (url) {
  344. return (/\/\/((www\.)?dailymotion\.com|dai\.ly)/i.test(url) ? 'video/x-dailymotion' : null
  345. );
  346. });
  347. window.dmAsyncInit = function () {
  348. DailyMotionApi.apiReady();
  349. };
  350. mejs.Renderers.add(DailyMotionIframeRenderer);
  351. },{}]},{},[1]);