modal.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*!
  2. * Bootstrap modal.js v4.3.1 (https://getbootstrap.com/)
  3. * Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
  8. typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) :
  9. (global = global || self, global.Modal = factory(global.jQuery, global.Util));
  10. }(this, function ($, Util) { 'use strict';
  11. $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
  12. Util = Util && Util.hasOwnProperty('default') ? Util['default'] : Util;
  13. function _defineProperties(target, props) {
  14. for (var i = 0; i < props.length; i++) {
  15. var descriptor = props[i];
  16. descriptor.enumerable = descriptor.enumerable || false;
  17. descriptor.configurable = true;
  18. if ("value" in descriptor) descriptor.writable = true;
  19. Object.defineProperty(target, descriptor.key, descriptor);
  20. }
  21. }
  22. function _createClass(Constructor, protoProps, staticProps) {
  23. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  24. if (staticProps) _defineProperties(Constructor, staticProps);
  25. return Constructor;
  26. }
  27. function _defineProperty(obj, key, value) {
  28. if (key in obj) {
  29. Object.defineProperty(obj, key, {
  30. value: value,
  31. enumerable: true,
  32. configurable: true,
  33. writable: true
  34. });
  35. } else {
  36. obj[key] = value;
  37. }
  38. return obj;
  39. }
  40. function _objectSpread(target) {
  41. for (var i = 1; i < arguments.length; i++) {
  42. var source = arguments[i] != null ? arguments[i] : {};
  43. var ownKeys = Object.keys(source);
  44. if (typeof Object.getOwnPropertySymbols === 'function') {
  45. ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
  46. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  47. }));
  48. }
  49. ownKeys.forEach(function (key) {
  50. _defineProperty(target, key, source[key]);
  51. });
  52. }
  53. return target;
  54. }
  55. /**
  56. * ------------------------------------------------------------------------
  57. * Constants
  58. * ------------------------------------------------------------------------
  59. */
  60. var NAME = 'modal';
  61. var VERSION = '4.3.1';
  62. var DATA_KEY = 'bs.modal';
  63. var EVENT_KEY = "." + DATA_KEY;
  64. var DATA_API_KEY = '.data-api';
  65. var JQUERY_NO_CONFLICT = $.fn[NAME];
  66. var ESCAPE_KEYCODE = 27; // KeyboardEvent.which value for Escape (Esc) key
  67. var Default = {
  68. backdrop: true,
  69. keyboard: true,
  70. focus: true,
  71. show: true
  72. };
  73. var DefaultType = {
  74. backdrop: '(boolean|string)',
  75. keyboard: 'boolean',
  76. focus: 'boolean',
  77. show: 'boolean'
  78. };
  79. var Event = {
  80. HIDE: "hide" + EVENT_KEY,
  81. HIDDEN: "hidden" + EVENT_KEY,
  82. SHOW: "show" + EVENT_KEY,
  83. SHOWN: "shown" + EVENT_KEY,
  84. FOCUSIN: "focusin" + EVENT_KEY,
  85. RESIZE: "resize" + EVENT_KEY,
  86. CLICK_DISMISS: "click.dismiss" + EVENT_KEY,
  87. KEYDOWN_DISMISS: "keydown.dismiss" + EVENT_KEY,
  88. MOUSEUP_DISMISS: "mouseup.dismiss" + EVENT_KEY,
  89. MOUSEDOWN_DISMISS: "mousedown.dismiss" + EVENT_KEY,
  90. CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
  91. };
  92. var ClassName = {
  93. SCROLLABLE: 'modal-dialog-scrollable',
  94. SCROLLBAR_MEASURER: 'modal-scrollbar-measure',
  95. BACKDROP: 'modal-backdrop',
  96. OPEN: 'modal-open',
  97. FADE: 'fade',
  98. SHOW: 'show'
  99. };
  100. var Selector = {
  101. DIALOG: '.modal-dialog',
  102. MODAL_BODY: '.modal-body',
  103. DATA_TOGGLE: '[data-toggle="modal"]',
  104. DATA_DISMISS: '[data-dismiss="modal"]',
  105. FIXED_CONTENT: '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top',
  106. STICKY_CONTENT: '.sticky-top'
  107. /**
  108. * ------------------------------------------------------------------------
  109. * Class Definition
  110. * ------------------------------------------------------------------------
  111. */
  112. };
  113. var Modal =
  114. /*#__PURE__*/
  115. function () {
  116. function Modal(element, config) {
  117. this._config = this._getConfig(config);
  118. this._element = element;
  119. this._dialog = element.querySelector(Selector.DIALOG);
  120. this._backdrop = null;
  121. this._isShown = false;
  122. this._isBodyOverflowing = false;
  123. this._ignoreBackdropClick = false;
  124. this._isTransitioning = false;
  125. this._scrollbarWidth = 0;
  126. } // Getters
  127. var _proto = Modal.prototype;
  128. // Public
  129. _proto.toggle = function toggle(relatedTarget) {
  130. return this._isShown ? this.hide() : this.show(relatedTarget);
  131. };
  132. _proto.show = function show(relatedTarget) {
  133. var _this = this;
  134. if (this._isShown || this._isTransitioning) {
  135. return;
  136. }
  137. if ($(this._element).hasClass(ClassName.FADE)) {
  138. this._isTransitioning = true;
  139. }
  140. var showEvent = $.Event(Event.SHOW, {
  141. relatedTarget: relatedTarget
  142. });
  143. $(this._element).trigger(showEvent);
  144. if (this._isShown || showEvent.isDefaultPrevented()) {
  145. return;
  146. }
  147. this._isShown = true;
  148. this._checkScrollbar();
  149. this._setScrollbar();
  150. this._adjustDialog();
  151. this._setEscapeEvent();
  152. this._setResizeEvent();
  153. $(this._element).on(Event.CLICK_DISMISS, Selector.DATA_DISMISS, function (event) {
  154. return _this.hide(event);
  155. });
  156. $(this._dialog).on(Event.MOUSEDOWN_DISMISS, function () {
  157. $(_this._element).one(Event.MOUSEUP_DISMISS, function (event) {
  158. if ($(event.target).is(_this._element)) {
  159. _this._ignoreBackdropClick = true;
  160. }
  161. });
  162. });
  163. this._showBackdrop(function () {
  164. return _this._showElement(relatedTarget);
  165. });
  166. };
  167. _proto.hide = function hide(event) {
  168. var _this2 = this;
  169. if (event) {
  170. event.preventDefault();
  171. }
  172. if (!this._isShown || this._isTransitioning) {
  173. return;
  174. }
  175. var hideEvent = $.Event(Event.HIDE);
  176. $(this._element).trigger(hideEvent);
  177. if (!this._isShown || hideEvent.isDefaultPrevented()) {
  178. return;
  179. }
  180. this._isShown = false;
  181. var transition = $(this._element).hasClass(ClassName.FADE);
  182. if (transition) {
  183. this._isTransitioning = true;
  184. }
  185. this._setEscapeEvent();
  186. this._setResizeEvent();
  187. $(document).off(Event.FOCUSIN);
  188. $(this._element).removeClass(ClassName.SHOW);
  189. $(this._element).off(Event.CLICK_DISMISS);
  190. $(this._dialog).off(Event.MOUSEDOWN_DISMISS);
  191. if (transition) {
  192. var transitionDuration = Util.getTransitionDurationFromElement(this._element);
  193. $(this._element).one(Util.TRANSITION_END, function (event) {
  194. return _this2._hideModal(event);
  195. }).emulateTransitionEnd(transitionDuration);
  196. } else {
  197. this._hideModal();
  198. }
  199. };
  200. _proto.dispose = function dispose() {
  201. [window, this._element, this._dialog].forEach(function (htmlElement) {
  202. return $(htmlElement).off(EVENT_KEY);
  203. });
  204. /**
  205. * `document` has 2 events `Event.FOCUSIN` and `Event.CLICK_DATA_API`
  206. * Do not move `document` in `htmlElements` array
  207. * It will remove `Event.CLICK_DATA_API` event that should remain
  208. */
  209. $(document).off(Event.FOCUSIN);
  210. $.removeData(this._element, DATA_KEY);
  211. this._config = null;
  212. this._element = null;
  213. this._dialog = null;
  214. this._backdrop = null;
  215. this._isShown = null;
  216. this._isBodyOverflowing = null;
  217. this._ignoreBackdropClick = null;
  218. this._isTransitioning = null;
  219. this._scrollbarWidth = null;
  220. };
  221. _proto.handleUpdate = function handleUpdate() {
  222. this._adjustDialog();
  223. } // Private
  224. ;
  225. _proto._getConfig = function _getConfig(config) {
  226. config = _objectSpread({}, Default, config);
  227. Util.typeCheckConfig(NAME, config, DefaultType);
  228. return config;
  229. };
  230. _proto._showElement = function _showElement(relatedTarget) {
  231. var _this3 = this;
  232. var transition = $(this._element).hasClass(ClassName.FADE);
  233. if (!this._element.parentNode || this._element.parentNode.nodeType !== Node.ELEMENT_NODE) {
  234. // Don't move modal's DOM position
  235. document.body.appendChild(this._element);
  236. }
  237. this._element.style.display = 'block';
  238. this._element.removeAttribute('aria-hidden');
  239. this._element.setAttribute('aria-modal', true);
  240. if ($(this._dialog).hasClass(ClassName.SCROLLABLE)) {
  241. this._dialog.querySelector(Selector.MODAL_BODY).scrollTop = 0;
  242. } else {
  243. this._element.scrollTop = 0;
  244. }
  245. if (transition) {
  246. Util.reflow(this._element);
  247. }
  248. $(this._element).addClass(ClassName.SHOW);
  249. if (this._config.focus) {
  250. this._enforceFocus();
  251. }
  252. var shownEvent = $.Event(Event.SHOWN, {
  253. relatedTarget: relatedTarget
  254. });
  255. var transitionComplete = function transitionComplete() {
  256. if (_this3._config.focus) {
  257. _this3._element.focus();
  258. }
  259. _this3._isTransitioning = false;
  260. $(_this3._element).trigger(shownEvent);
  261. };
  262. if (transition) {
  263. var transitionDuration = Util.getTransitionDurationFromElement(this._dialog);
  264. $(this._dialog).one(Util.TRANSITION_END, transitionComplete).emulateTransitionEnd(transitionDuration);
  265. } else {
  266. transitionComplete();
  267. }
  268. };
  269. _proto._enforceFocus = function _enforceFocus() {
  270. var _this4 = this;
  271. $(document).off(Event.FOCUSIN) // Guard against infinite focus loop
  272. .on(Event.FOCUSIN, function (event) {
  273. if (document !== event.target && _this4._element !== event.target && $(_this4._element).has(event.target).length === 0) {
  274. _this4._element.focus();
  275. }
  276. });
  277. };
  278. _proto._setEscapeEvent = function _setEscapeEvent() {
  279. var _this5 = this;
  280. if (this._isShown && this._config.keyboard) {
  281. $(this._element).on(Event.KEYDOWN_DISMISS, function (event) {
  282. if (event.which === ESCAPE_KEYCODE) {
  283. event.preventDefault();
  284. _this5.hide();
  285. }
  286. });
  287. } else if (!this._isShown) {
  288. $(this._element).off(Event.KEYDOWN_DISMISS);
  289. }
  290. };
  291. _proto._setResizeEvent = function _setResizeEvent() {
  292. var _this6 = this;
  293. if (this._isShown) {
  294. $(window).on(Event.RESIZE, function (event) {
  295. return _this6.handleUpdate(event);
  296. });
  297. } else {
  298. $(window).off(Event.RESIZE);
  299. }
  300. };
  301. _proto._hideModal = function _hideModal() {
  302. var _this7 = this;
  303. this._element.style.display = 'none';
  304. this._element.setAttribute('aria-hidden', true);
  305. this._element.removeAttribute('aria-modal');
  306. this._isTransitioning = false;
  307. this._showBackdrop(function () {
  308. $(document.body).removeClass(ClassName.OPEN);
  309. _this7._resetAdjustments();
  310. _this7._resetScrollbar();
  311. $(_this7._element).trigger(Event.HIDDEN);
  312. });
  313. };
  314. _proto._removeBackdrop = function _removeBackdrop() {
  315. if (this._backdrop) {
  316. $(this._backdrop).remove();
  317. this._backdrop = null;
  318. }
  319. };
  320. _proto._showBackdrop = function _showBackdrop(callback) {
  321. var _this8 = this;
  322. var animate = $(this._element).hasClass(ClassName.FADE) ? ClassName.FADE : '';
  323. if (this._isShown && this._config.backdrop) {
  324. this._backdrop = document.createElement('div');
  325. this._backdrop.className = ClassName.BACKDROP;
  326. if (animate) {
  327. this._backdrop.classList.add(animate);
  328. }
  329. $(this._backdrop).appendTo(document.body);
  330. $(this._element).on(Event.CLICK_DISMISS, function (event) {
  331. if (_this8._ignoreBackdropClick) {
  332. _this8._ignoreBackdropClick = false;
  333. return;
  334. }
  335. if (event.target !== event.currentTarget) {
  336. return;
  337. }
  338. if (_this8._config.backdrop === 'static') {
  339. _this8._element.focus();
  340. } else {
  341. _this8.hide();
  342. }
  343. });
  344. if (animate) {
  345. Util.reflow(this._backdrop);
  346. }
  347. $(this._backdrop).addClass(ClassName.SHOW);
  348. if (!callback) {
  349. return;
  350. }
  351. if (!animate) {
  352. callback();
  353. return;
  354. }
  355. var backdropTransitionDuration = Util.getTransitionDurationFromElement(this._backdrop);
  356. $(this._backdrop).one(Util.TRANSITION_END, callback).emulateTransitionEnd(backdropTransitionDuration);
  357. } else if (!this._isShown && this._backdrop) {
  358. $(this._backdrop).removeClass(ClassName.SHOW);
  359. var callbackRemove = function callbackRemove() {
  360. _this8._removeBackdrop();
  361. if (callback) {
  362. callback();
  363. }
  364. };
  365. if ($(this._element).hasClass(ClassName.FADE)) {
  366. var _backdropTransitionDuration = Util.getTransitionDurationFromElement(this._backdrop);
  367. $(this._backdrop).one(Util.TRANSITION_END, callbackRemove).emulateTransitionEnd(_backdropTransitionDuration);
  368. } else {
  369. callbackRemove();
  370. }
  371. } else if (callback) {
  372. callback();
  373. }
  374. } // ----------------------------------------------------------------------
  375. // the following methods are used to handle overflowing modals
  376. // todo (fat): these should probably be refactored out of modal.js
  377. // ----------------------------------------------------------------------
  378. ;
  379. _proto._adjustDialog = function _adjustDialog() {
  380. var isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight;
  381. if (!this._isBodyOverflowing && isModalOverflowing) {
  382. this._element.style.paddingLeft = this._scrollbarWidth + "px";
  383. }
  384. if (this._isBodyOverflowing && !isModalOverflowing) {
  385. this._element.style.paddingRight = this._scrollbarWidth + "px";
  386. }
  387. };
  388. _proto._resetAdjustments = function _resetAdjustments() {
  389. this._element.style.paddingLeft = '';
  390. this._element.style.paddingRight = '';
  391. };
  392. _proto._checkScrollbar = function _checkScrollbar() {
  393. var rect = document.body.getBoundingClientRect();
  394. this._isBodyOverflowing = rect.left + rect.right < window.innerWidth;
  395. this._scrollbarWidth = this._getScrollbarWidth();
  396. };
  397. _proto._setScrollbar = function _setScrollbar() {
  398. var _this9 = this;
  399. if (this._isBodyOverflowing) {
  400. // Note: DOMNode.style.paddingRight returns the actual value or '' if not set
  401. // while $(DOMNode).css('padding-right') returns the calculated value or 0 if not set
  402. var fixedContent = [].slice.call(document.querySelectorAll(Selector.FIXED_CONTENT));
  403. var stickyContent = [].slice.call(document.querySelectorAll(Selector.STICKY_CONTENT)); // Adjust fixed content padding
  404. $(fixedContent).each(function (index, element) {
  405. var actualPadding = element.style.paddingRight;
  406. var calculatedPadding = $(element).css('padding-right');
  407. $(element).data('padding-right', actualPadding).css('padding-right', parseFloat(calculatedPadding) + _this9._scrollbarWidth + "px");
  408. }); // Adjust sticky content margin
  409. $(stickyContent).each(function (index, element) {
  410. var actualMargin = element.style.marginRight;
  411. var calculatedMargin = $(element).css('margin-right');
  412. $(element).data('margin-right', actualMargin).css('margin-right', parseFloat(calculatedMargin) - _this9._scrollbarWidth + "px");
  413. }); // Adjust body padding
  414. var actualPadding = document.body.style.paddingRight;
  415. var calculatedPadding = $(document.body).css('padding-right');
  416. $(document.body).data('padding-right', actualPadding).css('padding-right', parseFloat(calculatedPadding) + this._scrollbarWidth + "px");
  417. }
  418. $(document.body).addClass(ClassName.OPEN);
  419. };
  420. _proto._resetScrollbar = function _resetScrollbar() {
  421. // Restore fixed content padding
  422. var fixedContent = [].slice.call(document.querySelectorAll(Selector.FIXED_CONTENT));
  423. $(fixedContent).each(function (index, element) {
  424. var padding = $(element).data('padding-right');
  425. $(element).removeData('padding-right');
  426. element.style.paddingRight = padding ? padding : '';
  427. }); // Restore sticky content
  428. var elements = [].slice.call(document.querySelectorAll("" + Selector.STICKY_CONTENT));
  429. $(elements).each(function (index, element) {
  430. var margin = $(element).data('margin-right');
  431. if (typeof margin !== 'undefined') {
  432. $(element).css('margin-right', margin).removeData('margin-right');
  433. }
  434. }); // Restore body padding
  435. var padding = $(document.body).data('padding-right');
  436. $(document.body).removeData('padding-right');
  437. document.body.style.paddingRight = padding ? padding : '';
  438. };
  439. _proto._getScrollbarWidth = function _getScrollbarWidth() {
  440. // thx d.walsh
  441. var scrollDiv = document.createElement('div');
  442. scrollDiv.className = ClassName.SCROLLBAR_MEASURER;
  443. document.body.appendChild(scrollDiv);
  444. var scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth;
  445. document.body.removeChild(scrollDiv);
  446. return scrollbarWidth;
  447. } // Static
  448. ;
  449. Modal._jQueryInterface = function _jQueryInterface(config, relatedTarget) {
  450. return this.each(function () {
  451. var data = $(this).data(DATA_KEY);
  452. var _config = _objectSpread({}, Default, $(this).data(), typeof config === 'object' && config ? config : {});
  453. if (!data) {
  454. data = new Modal(this, _config);
  455. $(this).data(DATA_KEY, data);
  456. }
  457. if (typeof config === 'string') {
  458. if (typeof data[config] === 'undefined') {
  459. throw new TypeError("No method named \"" + config + "\"");
  460. }
  461. data[config](relatedTarget);
  462. } else if (_config.show) {
  463. data.show(relatedTarget);
  464. }
  465. });
  466. };
  467. _createClass(Modal, null, [{
  468. key: "VERSION",
  469. get: function get() {
  470. return VERSION;
  471. }
  472. }, {
  473. key: "Default",
  474. get: function get() {
  475. return Default;
  476. }
  477. }]);
  478. return Modal;
  479. }();
  480. /**
  481. * ------------------------------------------------------------------------
  482. * Data Api implementation
  483. * ------------------------------------------------------------------------
  484. */
  485. $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
  486. var _this10 = this;
  487. var target;
  488. var selector = Util.getSelectorFromElement(this);
  489. if (selector) {
  490. target = document.querySelector(selector);
  491. }
  492. var config = $(target).data(DATA_KEY) ? 'toggle' : _objectSpread({}, $(target).data(), $(this).data());
  493. if (this.tagName === 'A' || this.tagName === 'AREA') {
  494. event.preventDefault();
  495. }
  496. var $target = $(target).one(Event.SHOW, function (showEvent) {
  497. if (showEvent.isDefaultPrevented()) {
  498. // Only register focus restorer if modal will actually get shown
  499. return;
  500. }
  501. $target.one(Event.HIDDEN, function () {
  502. if ($(_this10).is(':visible')) {
  503. _this10.focus();
  504. }
  505. });
  506. });
  507. Modal._jQueryInterface.call($(target), config, this);
  508. });
  509. /**
  510. * ------------------------------------------------------------------------
  511. * jQuery
  512. * ------------------------------------------------------------------------
  513. */
  514. $.fn[NAME] = Modal._jQueryInterface;
  515. $.fn[NAME].Constructor = Modal;
  516. $.fn[NAME].noConflict = function () {
  517. $.fn[NAME] = JQUERY_NO_CONFLICT;
  518. return Modal._jQueryInterface;
  519. };
  520. return Modal;
  521. }));
  522. //# sourceMappingURL=modal.js.map