a61fa07d31595f4b42599f804cd187255bbccacf.svn-base 16 KB

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