modal.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. 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; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * --------------------------------------------------------------------------
  6. * Bootstrap (v4.0.0-alpha.4): modal.js
  7. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  8. * --------------------------------------------------------------------------
  9. */
  10. var Modal = function ($) {
  11. /**
  12. * ------------------------------------------------------------------------
  13. * Constants
  14. * ------------------------------------------------------------------------
  15. */
  16. var NAME = 'modal';
  17. var VERSION = '4.0.0-alpha.4';
  18. var DATA_KEY = 'bs.modal';
  19. var EVENT_KEY = '.' + DATA_KEY;
  20. var DATA_API_KEY = '.data-api';
  21. var JQUERY_NO_CONFLICT = $.fn[NAME];
  22. var TRANSITION_DURATION = 300;
  23. var BACKDROP_TRANSITION_DURATION = 150;
  24. var ESCAPE_KEYCODE = 27; // KeyboardEvent.which value for Escape (Esc) key
  25. var Default = {
  26. backdrop: true,
  27. keyboard: true,
  28. focus: true,
  29. show: true
  30. };
  31. var DefaultType = {
  32. backdrop: '(boolean|string)',
  33. keyboard: 'boolean',
  34. focus: 'boolean',
  35. show: 'boolean'
  36. };
  37. var Event = {
  38. HIDE: 'hide' + EVENT_KEY,
  39. HIDDEN: 'hidden' + EVENT_KEY,
  40. SHOW: 'show' + EVENT_KEY,
  41. SHOWN: 'shown' + EVENT_KEY,
  42. FOCUSIN: 'focusin' + EVENT_KEY,
  43. RESIZE: 'resize' + EVENT_KEY,
  44. CLICK_DISMISS: 'click.dismiss' + EVENT_KEY,
  45. KEYDOWN_DISMISS: 'keydown.dismiss' + EVENT_KEY,
  46. MOUSEUP_DISMISS: 'mouseup.dismiss' + EVENT_KEY,
  47. MOUSEDOWN_DISMISS: 'mousedown.dismiss' + EVENT_KEY,
  48. CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY
  49. };
  50. var ClassName = {
  51. SCROLLBAR_MEASURER: 'modal-scrollbar-measure',
  52. BACKDROP: 'modal-backdrop',
  53. OPEN: 'modal-open',
  54. FADE: 'fade',
  55. IN: 'in'
  56. };
  57. var Selector = {
  58. DIALOG: '.modal-dialog',
  59. DATA_TOGGLE: '[data-toggle="modal"]',
  60. DATA_DISMISS: '[data-dismiss="modal"]',
  61. FIXED_CONTENT: '.navbar-fixed-top, .navbar-fixed-bottom, .is-fixed'
  62. };
  63. /**
  64. * ------------------------------------------------------------------------
  65. * Class Definition
  66. * ------------------------------------------------------------------------
  67. */
  68. var Modal = function () {
  69. function Modal(element, config) {
  70. _classCallCheck(this, Modal);
  71. this._config = this._getConfig(config);
  72. this._element = element;
  73. this._dialog = $(element).find(Selector.DIALOG)[0];
  74. this._backdrop = null;
  75. this._isShown = false;
  76. this._isBodyOverflowing = false;
  77. this._ignoreBackdropClick = false;
  78. this._originalBodyPadding = 0;
  79. this._scrollbarWidth = 0;
  80. }
  81. // getters
  82. // public
  83. Modal.prototype.toggle = function toggle(relatedTarget) {
  84. return this._isShown ? this.hide() : this.show(relatedTarget);
  85. };
  86. Modal.prototype.show = function show(relatedTarget) {
  87. var _this = this;
  88. var showEvent = $.Event(Event.SHOW, {
  89. relatedTarget: relatedTarget
  90. });
  91. $(this._element).trigger(showEvent);
  92. if (this._isShown || showEvent.isDefaultPrevented()) {
  93. return;
  94. }
  95. this._isShown = true;
  96. this._checkScrollbar();
  97. this._setScrollbar();
  98. $(document.body).addClass(ClassName.OPEN);
  99. this._setEscapeEvent();
  100. this._setResizeEvent();
  101. $(this._element).on(Event.CLICK_DISMISS, Selector.DATA_DISMISS, $.proxy(this.hide, this));
  102. $(this._dialog).on(Event.MOUSEDOWN_DISMISS, function () {
  103. $(_this._element).one(Event.MOUSEUP_DISMISS, function (event) {
  104. if ($(event.target).is(_this._element)) {
  105. _this._ignoreBackdropClick = true;
  106. }
  107. });
  108. });
  109. this._showBackdrop($.proxy(this._showElement, this, relatedTarget));
  110. };
  111. Modal.prototype.hide = function hide(event) {
  112. if (event) {
  113. event.preventDefault();
  114. }
  115. var hideEvent = $.Event(Event.HIDE);
  116. $(this._element).trigger(hideEvent);
  117. if (!this._isShown || hideEvent.isDefaultPrevented()) {
  118. return;
  119. }
  120. this._isShown = false;
  121. this._setEscapeEvent();
  122. this._setResizeEvent();
  123. $(document).off(Event.FOCUSIN);
  124. $(this._element).removeClass(ClassName.IN);
  125. $(this._element).off(Event.CLICK_DISMISS);
  126. $(this._dialog).off(Event.MOUSEDOWN_DISMISS);
  127. if (Util.supportsTransitionEnd() && $(this._element).hasClass(ClassName.FADE)) {
  128. $(this._element).one(Util.TRANSITION_END, $.proxy(this._hideModal, this)).emulateTransitionEnd(TRANSITION_DURATION);
  129. } else {
  130. this._hideModal();
  131. }
  132. };
  133. Modal.prototype.dispose = function dispose() {
  134. $.removeData(this._element, DATA_KEY);
  135. $(window).off(EVENT_KEY);
  136. $(document).off(EVENT_KEY);
  137. $(this._element).off(EVENT_KEY);
  138. $(this._backdrop).off(EVENT_KEY);
  139. this._config = null;
  140. this._element = null;
  141. this._dialog = null;
  142. this._backdrop = null;
  143. this._isShown = null;
  144. this._isBodyOverflowing = null;
  145. this._ignoreBackdropClick = null;
  146. this._originalBodyPadding = null;
  147. this._scrollbarWidth = null;
  148. };
  149. // private
  150. Modal.prototype._getConfig = function _getConfig(config) {
  151. config = $.extend({}, Default, config);
  152. Util.typeCheckConfig(NAME, config, DefaultType);
  153. return config;
  154. };
  155. Modal.prototype._showElement = function _showElement(relatedTarget) {
  156. var _this2 = this;
  157. var transition = Util.supportsTransitionEnd() && $(this._element).hasClass(ClassName.FADE);
  158. if (!this._element.parentNode || this._element.parentNode.nodeType !== Node.ELEMENT_NODE) {
  159. // don't move modals dom position
  160. document.body.appendChild(this._element);
  161. }
  162. this._element.style.display = 'block';
  163. this._element.removeAttribute('aria-hidden');
  164. this._element.scrollTop = 0;
  165. if (transition) {
  166. Util.reflow(this._element);
  167. }
  168. $(this._element).addClass(ClassName.IN);
  169. if (this._config.focus) {
  170. this._enforceFocus();
  171. }
  172. var shownEvent = $.Event(Event.SHOWN, {
  173. relatedTarget: relatedTarget
  174. });
  175. var transitionComplete = function transitionComplete() {
  176. if (_this2._config.focus) {
  177. _this2._element.focus();
  178. }
  179. $(_this2._element).trigger(shownEvent);
  180. };
  181. if (transition) {
  182. $(this._dialog).one(Util.TRANSITION_END, transitionComplete).emulateTransitionEnd(TRANSITION_DURATION);
  183. } else {
  184. transitionComplete();
  185. }
  186. };
  187. Modal.prototype._enforceFocus = function _enforceFocus() {
  188. var _this3 = this;
  189. $(document).off(Event.FOCUSIN) // guard against infinite focus loop
  190. .on(Event.FOCUSIN, function (event) {
  191. if (document !== event.target && _this3._element !== event.target && !$(_this3._element).has(event.target).length) {
  192. _this3._element.focus();
  193. }
  194. });
  195. };
  196. Modal.prototype._setEscapeEvent = function _setEscapeEvent() {
  197. var _this4 = this;
  198. if (this._isShown && this._config.keyboard) {
  199. $(this._element).on(Event.KEYDOWN_DISMISS, function (event) {
  200. if (event.which === ESCAPE_KEYCODE) {
  201. _this4.hide();
  202. }
  203. });
  204. } else if (!this._isShown) {
  205. $(this._element).off(Event.KEYDOWN_DISMISS);
  206. }
  207. };
  208. Modal.prototype._setResizeEvent = function _setResizeEvent() {
  209. if (this._isShown) {
  210. $(window).on(Event.RESIZE, $.proxy(this._handleUpdate, this));
  211. } else {
  212. $(window).off(Event.RESIZE);
  213. }
  214. };
  215. Modal.prototype._hideModal = function _hideModal() {
  216. var _this5 = this;
  217. this._element.style.display = 'none';
  218. this._element.setAttribute('aria-hidden', 'true');
  219. this._showBackdrop(function () {
  220. $(document.body).removeClass(ClassName.OPEN);
  221. _this5._resetAdjustments();
  222. _this5._resetScrollbar();
  223. $(_this5._element).trigger(Event.HIDDEN);
  224. });
  225. };
  226. Modal.prototype._removeBackdrop = function _removeBackdrop() {
  227. if (this._backdrop) {
  228. $(this._backdrop).remove();
  229. this._backdrop = null;
  230. }
  231. };
  232. Modal.prototype._showBackdrop = function _showBackdrop(callback) {
  233. var _this6 = this;
  234. var animate = $(this._element).hasClass(ClassName.FADE) ? ClassName.FADE : '';
  235. if (this._isShown && this._config.backdrop) {
  236. var doAnimate = Util.supportsTransitionEnd() && animate;
  237. this._backdrop = document.createElement('div');
  238. this._backdrop.className = ClassName.BACKDROP;
  239. if (animate) {
  240. $(this._backdrop).addClass(animate);
  241. }
  242. $(this._backdrop).appendTo(document.body);
  243. $(this._element).on(Event.CLICK_DISMISS, function (event) {
  244. if (_this6._ignoreBackdropClick) {
  245. _this6._ignoreBackdropClick = false;
  246. return;
  247. }
  248. if (event.target !== event.currentTarget) {
  249. return;
  250. }
  251. if (_this6._config.backdrop === 'static') {
  252. _this6._element.focus();
  253. } else {
  254. _this6.hide();
  255. }
  256. });
  257. if (doAnimate) {
  258. Util.reflow(this._backdrop);
  259. }
  260. $(this._backdrop).addClass(ClassName.IN);
  261. if (!callback) {
  262. return;
  263. }
  264. if (!doAnimate) {
  265. callback();
  266. return;
  267. }
  268. $(this._backdrop).one(Util.TRANSITION_END, callback).emulateTransitionEnd(BACKDROP_TRANSITION_DURATION);
  269. } else if (!this._isShown && this._backdrop) {
  270. $(this._backdrop).removeClass(ClassName.IN);
  271. var callbackRemove = function callbackRemove() {
  272. _this6._removeBackdrop();
  273. if (callback) {
  274. callback();
  275. }
  276. };
  277. if (Util.supportsTransitionEnd() && $(this._element).hasClass(ClassName.FADE)) {
  278. $(this._backdrop).one(Util.TRANSITION_END, callbackRemove).emulateTransitionEnd(BACKDROP_TRANSITION_DURATION);
  279. } else {
  280. callbackRemove();
  281. }
  282. } else if (callback) {
  283. callback();
  284. }
  285. };
  286. // ----------------------------------------------------------------------
  287. // the following methods are used to handle overflowing modals
  288. // todo (fat): these should probably be refactored out of modal.js
  289. // ----------------------------------------------------------------------
  290. Modal.prototype._handleUpdate = function _handleUpdate() {
  291. this._adjustDialog();
  292. };
  293. Modal.prototype._adjustDialog = function _adjustDialog() {
  294. var isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight;
  295. if (!this._isBodyOverflowing && isModalOverflowing) {
  296. this._element.style.paddingLeft = this._scrollbarWidth + 'px';
  297. }
  298. if (this._isBodyOverflowing && !isModalOverflowing) {
  299. this._element.style.paddingRight = this._scrollbarWidth + 'px';
  300. }
  301. };
  302. Modal.prototype._resetAdjustments = function _resetAdjustments() {
  303. this._element.style.paddingLeft = '';
  304. this._element.style.paddingRight = '';
  305. };
  306. Modal.prototype._checkScrollbar = function _checkScrollbar() {
  307. this._isBodyOverflowing = document.body.clientWidth < window.innerWidth;
  308. this._scrollbarWidth = this._getScrollbarWidth();
  309. };
  310. Modal.prototype._setScrollbar = function _setScrollbar() {
  311. var bodyPadding = parseInt($(Selector.FIXED_CONTENT).css('padding-right') || 0, 10);
  312. this._originalBodyPadding = document.body.style.paddingRight || '';
  313. if (this._isBodyOverflowing) {
  314. document.body.style.paddingRight = bodyPadding + this._scrollbarWidth + 'px';
  315. }
  316. };
  317. Modal.prototype._resetScrollbar = function _resetScrollbar() {
  318. document.body.style.paddingRight = this._originalBodyPadding;
  319. };
  320. Modal.prototype._getScrollbarWidth = function _getScrollbarWidth() {
  321. // thx d.walsh
  322. var scrollDiv = document.createElement('div');
  323. scrollDiv.className = ClassName.SCROLLBAR_MEASURER;
  324. document.body.appendChild(scrollDiv);
  325. var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
  326. document.body.removeChild(scrollDiv);
  327. return scrollbarWidth;
  328. };
  329. // static
  330. Modal._jQueryInterface = function _jQueryInterface(config, relatedTarget) {
  331. return this.each(function () {
  332. var data = $(this).data(DATA_KEY);
  333. var _config = $.extend({}, Modal.Default, $(this).data(), (typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object' && config);
  334. if (!data) {
  335. data = new Modal(this, _config);
  336. $(this).data(DATA_KEY, data);
  337. }
  338. if (typeof config === 'string') {
  339. if (data[config] === undefined) {
  340. throw new Error('No method named "' + config + '"');
  341. }
  342. data[config](relatedTarget);
  343. } else if (_config.show) {
  344. data.show(relatedTarget);
  345. }
  346. });
  347. };
  348. _createClass(Modal, null, [{
  349. key: 'VERSION',
  350. get: function get() {
  351. return VERSION;
  352. }
  353. }, {
  354. key: 'Default',
  355. get: function get() {
  356. return Default;
  357. }
  358. }]);
  359. return Modal;
  360. }();
  361. /**
  362. * ------------------------------------------------------------------------
  363. * Data Api implementation
  364. * ------------------------------------------------------------------------
  365. */
  366. $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
  367. var _this7 = this;
  368. var target = void 0;
  369. var selector = Util.getSelectorFromElement(this);
  370. if (selector) {
  371. target = $(selector)[0];
  372. }
  373. var config = $(target).data(DATA_KEY) ? 'toggle' : $.extend({}, $(target).data(), $(this).data());
  374. if (this.tagName === 'A') {
  375. event.preventDefault();
  376. }
  377. var $target = $(target).one(Event.SHOW, function (showEvent) {
  378. if (showEvent.isDefaultPrevented()) {
  379. // only register focus restorer if modal will actually get shown
  380. return;
  381. }
  382. $target.one(Event.HIDDEN, function () {
  383. if ($(_this7).is(':visible')) {
  384. _this7.focus();
  385. }
  386. });
  387. });
  388. Modal._jQueryInterface.call($(target), config, this);
  389. });
  390. /**
  391. * ------------------------------------------------------------------------
  392. * jQuery
  393. * ------------------------------------------------------------------------
  394. */
  395. $.fn[NAME] = Modal._jQueryInterface;
  396. $.fn[NAME].Constructor = Modal;
  397. $.fn[NAME].noConflict = function () {
  398. $.fn[NAME] = JQUERY_NO_CONFLICT;
  399. return Modal._jQueryInterface;
  400. };
  401. return Modal;
  402. }(jQuery);
  403. //# sourceMappingURL=modal.js.map