UNPKG

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