modal-video.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * Modules in this bundle
  3. * @license
  4. *
  5. * modal-video:
  6. * license: appleple
  7. * author: appleple
  8. * homepage: http://developer.a-blogcms.jp
  9. * version: 2.4.1
  10. *
  11. * custom-event-polyfill:
  12. * license: MIT (http://opensource.org/licenses/MIT)
  13. * maintainers: krambuhl <[email protected]>
  14. * contributors: Frank Panetta, Mikhail Reenko <[email protected]>, Joscha Feth <[email protected]>
  15. * homepage: https://github.com/krambuhl/custom-event-polyfill#readme
  16. * version: 0.3.0
  17. *
  18. * es6-object-assign:
  19. * license: MIT (http://opensource.org/licenses/MIT)
  20. * author: Rubén Norte <[email protected]>
  21. * homepage: https://github.com/rubennorte/es6-object-assign
  22. * version: 1.1.0
  23. *
  24. * This header is generated by licensify (https://github.com/twada/licensify)
  25. */
  26. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ModalVideo = f()}})(function(){var define,module,exports;return (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(require,module,exports){
  27. // Polyfill for creating CustomEvents on IE9/10/11
  28. // code pulled from:
  29. // https://github.com/d4tocchini/customevent-polyfill
  30. // https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent#Polyfill
  31. try {
  32. var ce = new window.CustomEvent('test');
  33. ce.preventDefault();
  34. if (ce.defaultPrevented !== true) {
  35. // IE has problems with .preventDefault() on custom events
  36. // http://stackoverflow.com/questions/23349191
  37. throw new Error('Could not prevent default');
  38. }
  39. } catch(e) {
  40. var CustomEvent = function(event, params) {
  41. var evt, origPrevent;
  42. params = params || {
  43. bubbles: false,
  44. cancelable: false,
  45. detail: undefined
  46. };
  47. evt = document.createEvent("CustomEvent");
  48. evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
  49. origPrevent = evt.preventDefault;
  50. evt.preventDefault = function () {
  51. origPrevent.call(this);
  52. try {
  53. Object.defineProperty(this, 'defaultPrevented', {
  54. get: function () {
  55. return true;
  56. }
  57. });
  58. } catch(e) {
  59. this.defaultPrevented = true;
  60. }
  61. };
  62. return evt;
  63. };
  64. CustomEvent.prototype = window.Event.prototype;
  65. window.CustomEvent = CustomEvent; // expose definition to window
  66. }
  67. },{}],2:[function(require,module,exports){
  68. /**
  69. * Code refactored from Mozilla Developer Network:
  70. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  71. */
  72. 'use strict';
  73. function assign(target, firstSource) {
  74. if (target === undefined || target === null) {
  75. throw new TypeError('Cannot convert first argument to object');
  76. }
  77. var to = Object(target);
  78. for (var i = 1; i < arguments.length; i++) {
  79. var nextSource = arguments[i];
  80. if (nextSource === undefined || nextSource === null) {
  81. continue;
  82. }
  83. var keysArray = Object.keys(Object(nextSource));
  84. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  85. var nextKey = keysArray[nextIndex];
  86. var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
  87. if (desc !== undefined && desc.enumerable) {
  88. to[nextKey] = nextSource[nextKey];
  89. }
  90. }
  91. }
  92. return to;
  93. }
  94. function polyfill() {
  95. if (!Object.assign) {
  96. Object.defineProperty(Object, 'assign', {
  97. enumerable: false,
  98. configurable: true,
  99. writable: true,
  100. value: assign
  101. });
  102. }
  103. }
  104. module.exports = {
  105. assign: assign,
  106. polyfill: polyfill
  107. };
  108. },{}],3:[function(require,module,exports){
  109. 'use strict';
  110. Object.defineProperty(exports, "__esModule", {
  111. value: true
  112. });
  113. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  114. require('custom-event-polyfill');
  115. var _util = require('../lib/util');
  116. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  117. var assign = require('es6-object-assign').assign;
  118. var defaults = {
  119. channel: 'youtube',
  120. facebook: {},
  121. youtube: {
  122. autoplay: 1,
  123. cc_load_policy: 1,
  124. color: null,
  125. controls: 1,
  126. disablekb: 0,
  127. enablejsapi: 0,
  128. end: null,
  129. fs: 1,
  130. h1: null,
  131. iv_load_policy: 1,
  132. list: null,
  133. listType: null,
  134. loop: 0,
  135. modestbranding: null,
  136. origin: null,
  137. playlist: null,
  138. playsinline: null,
  139. rel: 0,
  140. showinfo: 1,
  141. start: 0,
  142. wmode: 'transparent',
  143. theme: 'dark',
  144. nocookie: false
  145. },
  146. ratio: '16:9',
  147. vimeo: {
  148. api: false,
  149. autopause: true,
  150. autoplay: true,
  151. byline: true,
  152. callback: null,
  153. color: null,
  154. height: null,
  155. loop: false,
  156. maxheight: null,
  157. maxwidth: null,
  158. player_id: null,
  159. portrait: true,
  160. title: true,
  161. width: null,
  162. xhtml: false
  163. },
  164. allowFullScreen: true,
  165. animationSpeed: 300,
  166. classNames: {
  167. modalVideo: 'modal-video',
  168. modalVideoClose: 'modal-video-close',
  169. modalVideoBody: 'modal-video-body',
  170. modalVideoInner: 'modal-video-inner',
  171. modalVideoIframeWrap: 'modal-video-movie-wrap',
  172. modalVideoCloseBtn: 'modal-video-close-btn'
  173. },
  174. aria: {
  175. openMessage: 'You just openned the modal video',
  176. dismissBtnMessage: 'Close the modal by clicking here'
  177. }
  178. };
  179. var ModalVideo = function () {
  180. function ModalVideo(ele, option) {
  181. var _this = this;
  182. _classCallCheck(this, ModalVideo);
  183. var opt = assign({}, defaults, option);
  184. var selectors = typeof ele === 'string' ? document.querySelectorAll(ele) : ele;
  185. var body = document.querySelector('body');
  186. var classNames = opt.classNames;
  187. var speed = opt.animationSpeed;
  188. [].forEach.call(selectors, function (selector) {
  189. selector.addEventListener('click', function (event) {
  190. if (selector.tagName === 'A') {
  191. event.preventDefault();
  192. }
  193. var videoId = selector.dataset.videoId;
  194. var channel = selector.dataset.channel || opt.channel;
  195. var id = (0, _util.getUniqId)();
  196. var videoUrl = selector.dataset.videoUrl || _this.getVideoUrl(opt, channel, videoId);
  197. var html = _this.getHtml(opt, videoUrl, id);
  198. (0, _util.append)(body, html);
  199. var modal = document.getElementById(id);
  200. var btn = modal.querySelector('.js-modal-video-dismiss-btn');
  201. modal.focus();
  202. modal.addEventListener('click', function () {
  203. (0, _util.addClass)(modal, classNames.modalVideoClose);
  204. setTimeout(function () {
  205. (0, _util.remove)(modal);
  206. selector.focus();
  207. }, speed);
  208. });
  209. modal.addEventListener('keydown', function (e) {
  210. if (e.which === 9) {
  211. e.preventDefault();
  212. if (document.activeElement === modal) {
  213. btn.focus();
  214. } else {
  215. modal.setAttribute('aria-label', '');
  216. modal.focus();
  217. }
  218. }
  219. });
  220. btn.addEventListener('click', function () {
  221. (0, _util.triggerEvent)(modal, 'click');
  222. });
  223. });
  224. });
  225. }
  226. _createClass(ModalVideo, [{
  227. key: 'getPadding',
  228. value: function getPadding(ratio) {
  229. var arr = ratio.split(':');
  230. var width = Number(arr[0]);
  231. var height = Number(arr[1]);
  232. var padding = height * 100 / width;
  233. return padding + '%';
  234. }
  235. }, {
  236. key: 'getQueryString',
  237. value: function getQueryString(obj) {
  238. var url = '';
  239. Object.keys(obj).forEach(function (key) {
  240. url += key + '=' + obj[key] + '&';
  241. });
  242. return url.substr(0, url.length - 1);
  243. }
  244. }, {
  245. key: 'getVideoUrl',
  246. value: function getVideoUrl(opt, channel, videoId) {
  247. if (channel === 'youtube') {
  248. return this.getYoutubeUrl(opt.youtube, videoId);
  249. } else if (channel === 'vimeo') {
  250. return this.getVimeoUrl(opt.vimeo, videoId);
  251. } else if (channel === 'facebook') {
  252. return this.getFacebookUrl(opt.facebook, videoId);
  253. }
  254. return '';
  255. }
  256. }, {
  257. key: 'getVimeoUrl',
  258. value: function getVimeoUrl(vimeo, videoId) {
  259. var query = this.getQueryString(vimeo);
  260. return '//player.vimeo.com/video/' + videoId + '?' + query;
  261. }
  262. }, {
  263. key: 'getYoutubeUrl',
  264. value: function getYoutubeUrl(youtube, videoId) {
  265. var query = this.getQueryString(youtube);
  266. if (youtube.nocookie === true) {
  267. return '//www.youtube-nocookie.com/embed/' + videoId + '?' + query;
  268. }
  269. return '//www.youtube.com/embed/' + videoId + '?' + query;
  270. }
  271. }, {
  272. key: 'getFacebookUrl',
  273. value: function getFacebookUrl(facebook, videoId) {
  274. return '//www.facebook.com/v2.10/plugins/video.php?href=https://www.facebook.com/facebook/videos/' + videoId + '&' + this.getQueryString(facebook);
  275. }
  276. }, {
  277. key: 'getHtml',
  278. value: function getHtml(opt, videoUrl, id) {
  279. var padding = this.getPadding(opt.ratio);
  280. var classNames = opt.classNames;
  281. return '\n <div class="' + classNames.modalVideo + '" tabindex="-1" role="dialog" aria-label="' + opt.aria.openMessage + '" id="' + id + '">\n <div class="' + classNames.modalVideoBody + '">\n <div class="' + classNames.modalVideoInner + '">\n <div class="' + classNames.modalVideoIframeWrap + '" style="padding-bottom:' + padding + '">\n <button class="' + classNames.modalVideoCloseBtn + ' js-modal-video-dismiss-btn" aria-label="' + opt.aria.dismissBtnMessage + '"></button>\n <iframe width=\'460\' height=\'230\' src="' + videoUrl + '" frameborder=\'0\' allowfullscreen=' + opt.allowFullScreen + ' tabindex="-1"/>\n </div>\n </div>\n </div>\n </div>\n ';
  282. }
  283. }]);
  284. return ModalVideo;
  285. }();
  286. exports.default = ModalVideo;
  287. module.exports = exports['default'];
  288. },{"../lib/util":5,"custom-event-polyfill":1,"es6-object-assign":2}],4:[function(require,module,exports){
  289. 'use strict';
  290. module.exports = require('./core/');
  291. },{"./core/":3}],5:[function(require,module,exports){
  292. 'use strict';
  293. Object.defineProperty(exports, "__esModule", {
  294. value: true
  295. });
  296. var append = exports.append = function append(element, string) {
  297. var div = document.createElement('div');
  298. div.innerHTML = string;
  299. while (div.children.length > 0) {
  300. element.appendChild(div.children[0]);
  301. }
  302. };
  303. var getUniqId = exports.getUniqId = function getUniqId() {
  304. return (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase();
  305. };
  306. var remove = exports.remove = function remove(element) {
  307. if (element && element.parentNode) {
  308. element.parentNode.removeChild(element);
  309. }
  310. };
  311. var addClass = exports.addClass = function addClass(element, className) {
  312. if (element.classList) {
  313. element.classList.add(className);
  314. } else {
  315. element.className += ' ' + className;
  316. }
  317. };
  318. var triggerEvent = exports.triggerEvent = function triggerEvent(el, eventName, options) {
  319. var event = void 0;
  320. if (window.CustomEvent) {
  321. event = new CustomEvent(eventName, { cancelable: true });
  322. } else {
  323. event = document.createEvent('CustomEvent');
  324. event.initCustomEvent(eventName, false, false, options);
  325. }
  326. el.dispatchEvent(event);
  327. };
  328. },{}]},{},[4])(4)
  329. });