modal.js 25.1 KB
Newer Older
XhmikosR's avatar
Dist    
XhmikosR committed
1
/*!
XhmikosR's avatar
XhmikosR committed
2
  * Bootstrap modal.js v4.3.1 (https://getbootstrap.com/)
Mark Otto's avatar
Mark Otto committed
3
  * Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
XhmikosR's avatar
Dist    
XhmikosR committed
4
5
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  */
Mark Otto's avatar
dist    
Mark Otto committed
6
(function (global, factory) {
XhmikosR's avatar
XhmikosR committed
7
8
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./dom/selector-engine.js')) :
  typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/event-handler.js', './dom/manipulator.js', './dom/selector-engine.js'], factory) :
XhmikosR's avatar
XhmikosR committed
9
10
  (global = global || self, global.Modal = factory(global.Data, global.EventHandler, global.Manipulator, global.SelectorEngine));
}(this, function (Data, EventHandler, Manipulator, SelectorEngine) { 'use strict';
Mark Otto's avatar
dist    
Mark Otto committed
11

XhmikosR's avatar
XhmikosR committed
12
13
14
15
  Data = Data && Data.hasOwnProperty('default') ? Data['default'] : Data;
  EventHandler = EventHandler && EventHandler.hasOwnProperty('default') ? EventHandler['default'] : EventHandler;
  Manipulator = Manipulator && Manipulator.hasOwnProperty('default') ? Manipulator['default'] : Manipulator;
  SelectorEngine = SelectorEngine && SelectorEngine.hasOwnProperty('default') ? SelectorEngine['default'] : SelectorEngine;
Mark Otto's avatar
dist    
Mark Otto committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

  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);
    }
  }

  function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
  }

  function _defineProperty(obj, key, value) {
    if (key in obj) {
      Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
      });
    } else {
      obj[key] = value;
    }
Mark Otto's avatar
dist    
Mark Otto committed
44

Mark Otto's avatar
dist    
Mark Otto committed
45
46
    return obj;
  }
Mark Otto's avatar
dist    
Mark Otto committed
47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);

    if (Object.getOwnPropertySymbols) {
      var symbols = Object.getOwnPropertySymbols(object);
      if (enumerableOnly) symbols = symbols.filter(function (sym) {
        return Object.getOwnPropertyDescriptor(object, sym).enumerable;
      });
      keys.push.apply(keys, symbols);
    }

    return keys;
  }

  function _objectSpread2(target) {
Mark Otto's avatar
dist    
Mark Otto committed
63
64
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i] != null ? arguments[i] : {};
Mark Otto's avatar
dist    
Mark Otto committed
65

66
67
68
69
70
71
72
73
74
75
      if (i % 2) {
        ownKeys(source, true).forEach(function (key) {
          _defineProperty(target, key, source[key]);
        });
      } else if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
      } else {
        ownKeys(source).forEach(function (key) {
          Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
Mark Otto's avatar
dist    
Mark Otto committed
76
77
78
79
80
      }
    }

    return target;
  }
81

XhmikosR's avatar
XhmikosR committed
82
83
84
85
86
87
88
  /**
   * --------------------------------------------------------------------------
   * Bootstrap (v4.3.1): util/index.js
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
   * --------------------------------------------------------------------------
   */
  var MILLISECONDS_MULTIPLIER = 1000;
XhmikosR's avatar
XhmikosR committed
89
  var TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
XhmikosR's avatar
XhmikosR committed
90
91
92
93
94

  var toType = function toType(obj) {
    return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
  };

XhmikosR's avatar
XhmikosR committed
95
  var getSelector = function getSelector(element) {
XhmikosR's avatar
XhmikosR committed
96
97
98
99
    var selector = element.getAttribute('data-target');

    if (!selector || selector === '#') {
      var hrefAttr = element.getAttribute('href');
XhmikosR's avatar
XhmikosR committed
100
      selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
XhmikosR's avatar
XhmikosR committed
101
102
    }

XhmikosR's avatar
XhmikosR committed
103
104
105
106
107
108
    return selector;
  };

  var getElementFromSelector = function getElementFromSelector(element) {
    var selector = getSelector(element);
    return selector ? document.querySelector(selector) : null;
XhmikosR's avatar
XhmikosR committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  };

  var getTransitionDurationFromElement = function getTransitionDurationFromElement(element) {
    if (!element) {
      return 0;
    } // Get transition-duration of the element


    var _window$getComputedSt = window.getComputedStyle(element),
        transitionDuration = _window$getComputedSt.transitionDuration,
        transitionDelay = _window$getComputedSt.transitionDelay;

    var floatTransitionDuration = parseFloat(transitionDuration);
    var floatTransitionDelay = parseFloat(transitionDelay); // Return 0 if element or transition duration is not found

    if (!floatTransitionDuration && !floatTransitionDelay) {
      return 0;
    } // If multiple durations are defined, take the first


    transitionDuration = transitionDuration.split(',')[0];
    transitionDelay = transitionDelay.split(',')[0];
    return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
  };

  var triggerTransitionEnd = function triggerTransitionEnd(element) {
XhmikosR's avatar
XhmikosR committed
135
136
137
    var evt = document.createEvent('HTMLEvents');
    evt.initEvent(TRANSITION_END, true, true);
    element.dispatchEvent(evt);
XhmikosR's avatar
XhmikosR committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  };

  var isElement = function isElement(obj) {
    return (obj[0] || obj).nodeType;
  };

  var emulateTransitionEnd = function emulateTransitionEnd(element, duration) {
    var called = false;
    var durationPadding = 5;
    var emulatedDuration = duration + durationPadding;

    function listener() {
      called = true;
      element.removeEventListener(TRANSITION_END, listener);
    }

    element.addEventListener(TRANSITION_END, listener);
    setTimeout(function () {
      if (!called) {
        triggerTransitionEnd(element);
      }
    }, emulatedDuration);
  };

  var typeCheckConfig = function typeCheckConfig(componentName, config, configTypes) {
    Object.keys(configTypes).forEach(function (property) {
      var expectedTypes = configTypes[property];
      var value = config[property];
      var valueType = value && isElement(value) ? 'element' : toType(value);

      if (!new RegExp(expectedTypes).test(valueType)) {
        throw new Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
      }
    });
  };

  var makeArray = function makeArray(nodeList) {
    if (!nodeList) {
      return [];
    }

    return [].slice.call(nodeList);
  };

  var isVisible = function isVisible(element) {
    if (!element) {
      return false;
    }

    if (element.style && element.parentNode && element.parentNode.style) {
      return element.style.display !== 'none' && element.parentNode.style.display !== 'none' && element.style.visibility !== 'hidden';
    }

    return false;
  };

  var reflow = function reflow(element) {
    return element.offsetHeight;
  };

XhmikosR's avatar
XhmikosR committed
198
199
200
201
202
203
204
205
206
207
208
  var getjQuery = function getjQuery() {
    var _window = window,
        jQuery = _window.jQuery;

    if (jQuery && !document.body.hasAttribute('data-no-jquery')) {
      return jQuery;
    }

    return null;
  };

XhmikosR's avatar
Dist    
XhmikosR committed
209
210
211
212
213
214
215
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  var NAME = 'modal';
XhmikosR's avatar
XhmikosR committed
216
  var VERSION = '4.3.1';
XhmikosR's avatar
Dist    
XhmikosR committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  var DATA_KEY = 'bs.modal';
  var EVENT_KEY = "." + DATA_KEY;
  var DATA_API_KEY = '.data-api';
  var ESCAPE_KEYCODE = 27; // KeyboardEvent.which value for Escape (Esc) key

  var Default = {
    backdrop: true,
    keyboard: true,
    focus: true,
    show: true
  };
  var DefaultType = {
    backdrop: '(boolean|string)',
    keyboard: 'boolean',
    focus: 'boolean',
    show: 'boolean'
  };
XhmikosR's avatar
XhmikosR committed
234
  var Event = {
XhmikosR's avatar
Dist    
XhmikosR committed
235
236
237
238
239
240
241
242
243
244
245
246
247
    HIDE: "hide" + EVENT_KEY,
    HIDDEN: "hidden" + EVENT_KEY,
    SHOW: "show" + EVENT_KEY,
    SHOWN: "shown" + EVENT_KEY,
    FOCUSIN: "focusin" + EVENT_KEY,
    RESIZE: "resize" + EVENT_KEY,
    CLICK_DISMISS: "click.dismiss" + EVENT_KEY,
    KEYDOWN_DISMISS: "keydown.dismiss" + EVENT_KEY,
    MOUSEUP_DISMISS: "mouseup.dismiss" + EVENT_KEY,
    MOUSEDOWN_DISMISS: "mousedown.dismiss" + EVENT_KEY,
    CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
  };
  var ClassName = {
Mark Otto's avatar
Mark Otto committed
248
    SCROLLABLE: 'modal-dialog-scrollable',
XhmikosR's avatar
Dist    
XhmikosR committed
249
250
251
252
253
254
255
256
    SCROLLBAR_MEASURER: 'modal-scrollbar-measure',
    BACKDROP: 'modal-backdrop',
    OPEN: 'modal-open',
    FADE: 'fade',
    SHOW: 'show'
  };
  var Selector = {
    DIALOG: '.modal-dialog',
Mark Otto's avatar
Mark Otto committed
257
    MODAL_BODY: '.modal-body',
XhmikosR's avatar
Dist    
XhmikosR committed
258
259
260
261
262
    DATA_TOGGLE: '[data-toggle="modal"]',
    DATA_DISMISS: '[data-dismiss="modal"]',
    FIXED_CONTENT: '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top',
    STICKY_CONTENT: '.sticky-top'
  };
XhmikosR's avatar
XhmikosR committed
263
264
265
266
267
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
XhmikosR's avatar
Dist    
XhmikosR committed
268
269
270
271
272
273
274

  var Modal =
  /*#__PURE__*/
  function () {
    function Modal(element, config) {
      this._config = this._getConfig(config);
      this._element = element;
XhmikosR's avatar
XhmikosR committed
275
      this._dialog = SelectorEngine.findOne(Selector.DIALOG, element);
XhmikosR's avatar
Dist    
XhmikosR committed
276
277
278
279
280
281
      this._backdrop = null;
      this._isShown = false;
      this._isBodyOverflowing = false;
      this._ignoreBackdropClick = false;
      this._isTransitioning = false;
      this._scrollbarWidth = 0;
XhmikosR's avatar
XhmikosR committed
282
      Data.setData(element, DATA_KEY, this);
XhmikosR's avatar
Dist    
XhmikosR committed
283
284
285
286
287
288
289
290
    } // Getters


    var _proto = Modal.prototype;

    // Public
    _proto.toggle = function toggle(relatedTarget) {
      return this._isShown ? this.hide() : this.show(relatedTarget);
Mark Otto's avatar
dist    
Mark Otto committed
291
    };
292

XhmikosR's avatar
Dist    
XhmikosR committed
293
294
    _proto.show = function show(relatedTarget) {
      var _this = this;
Jacob Thornton's avatar
Jacob Thornton committed
295

XhmikosR's avatar
Dist    
XhmikosR committed
296
297
298
      if (this._isShown || this._isTransitioning) {
        return;
      }
299

XhmikosR's avatar
XhmikosR committed
300
      if (this._element.classList.contains(ClassName.FADE)) {
XhmikosR's avatar
Dist    
XhmikosR committed
301
302
        this._isTransitioning = true;
      }
303

XhmikosR's avatar
XhmikosR committed
304
      var showEvent = EventHandler.trigger(this._element, Event.SHOW, {
XhmikosR's avatar
Dist    
XhmikosR committed
305
306
        relatedTarget: relatedTarget
      });
Mark Otto's avatar
grunt    
Mark Otto committed
307

XhmikosR's avatar
XhmikosR committed
308
      if (this._isShown || showEvent.defaultPrevented) {
XhmikosR's avatar
Dist    
XhmikosR committed
309
310
        return;
      }
311

XhmikosR's avatar
Dist    
XhmikosR committed
312
      this._isShown = true;
Mark Otto's avatar
grunt    
Mark Otto committed
313

XhmikosR's avatar
Dist    
XhmikosR committed
314
      this._checkScrollbar();
315

XhmikosR's avatar
Dist    
XhmikosR committed
316
      this._setScrollbar();
Mark Otto's avatar
grunt    
Mark Otto committed
317

XhmikosR's avatar
Dist    
XhmikosR committed
318
      this._adjustDialog();
Mark Otto's avatar
grunt    
Mark Otto committed
319

XhmikosR's avatar
Dist    
XhmikosR committed
320
      this._setEscapeEvent();
321

XhmikosR's avatar
Dist    
XhmikosR committed
322
      this._setResizeEvent();
Mark Otto's avatar
grunt    
Mark Otto committed
323

XhmikosR's avatar
XhmikosR committed
324
      EventHandler.on(this._element, Event.CLICK_DISMISS, Selector.DATA_DISMISS, function (event) {
XhmikosR's avatar
Dist    
XhmikosR committed
325
326
        return _this.hide(event);
      });
XhmikosR's avatar
XhmikosR committed
327
328
      EventHandler.on(this._dialog, Event.MOUSEDOWN_DISMISS, function () {
        EventHandler.one(_this._element, Event.MOUSEUP_DISMISS, function (event) {
XhmikosR's avatar
XhmikosR committed
329
          if (event.target === _this._element) {
XhmikosR's avatar
Dist    
XhmikosR committed
330
331
            _this._ignoreBackdropClick = true;
          }
Mark Otto's avatar
dist    
Mark Otto committed
332
        });
XhmikosR's avatar
Dist    
XhmikosR committed
333
      });
334

XhmikosR's avatar
Dist    
XhmikosR committed
335
336
337
338
      this._showBackdrop(function () {
        return _this._showElement(relatedTarget);
      });
    };
Mark Otto's avatar
dist    
Mark Otto committed
339

XhmikosR's avatar
Dist    
XhmikosR committed
340
341
    _proto.hide = function hide(event) {
      var _this2 = this;
342

XhmikosR's avatar
Dist    
XhmikosR committed
343
344
345
      if (event) {
        event.preventDefault();
      }
346

XhmikosR's avatar
Dist    
XhmikosR committed
347
348
349
      if (!this._isShown || this._isTransitioning) {
        return;
      }
350

XhmikosR's avatar
XhmikosR committed
351
      var hideEvent = EventHandler.trigger(this._element, Event.HIDE);
352

353
      if (hideEvent.defaultPrevented) {
XhmikosR's avatar
Dist    
XhmikosR committed
354
355
        return;
      }
Mark Otto's avatar
dist    
Mark Otto committed
356

XhmikosR's avatar
Dist    
XhmikosR committed
357
      this._isShown = false;
XhmikosR's avatar
XhmikosR committed
358
359

      var transition = this._element.classList.contains(ClassName.FADE);
Mark Otto's avatar
grunt    
Mark Otto committed
360

XhmikosR's avatar
Dist    
XhmikosR committed
361
362
363
      if (transition) {
        this._isTransitioning = true;
      }
364

XhmikosR's avatar
Dist    
XhmikosR committed
365
      this._setEscapeEvent();
366

XhmikosR's avatar
Dist    
XhmikosR committed
367
      this._setResizeEvent();
Mark Otto's avatar
grunt    
Mark Otto committed
368

XhmikosR's avatar
XhmikosR committed
369
      EventHandler.off(document, Event.FOCUSIN);
XhmikosR's avatar
XhmikosR committed
370
371
372

      this._element.classList.remove(ClassName.SHOW);

XhmikosR's avatar
XhmikosR committed
373
374
      EventHandler.off(this._element, Event.CLICK_DISMISS);
      EventHandler.off(this._dialog, Event.MOUSEDOWN_DISMISS);
Mark Otto's avatar
dist    
Mark Otto committed
375

XhmikosR's avatar
Dist    
XhmikosR committed
376
      if (transition) {
XhmikosR's avatar
XhmikosR committed
377
378
        var transitionDuration = getTransitionDurationFromElement(this._element);
        EventHandler.one(this._element, TRANSITION_END, function (event) {
XhmikosR's avatar
Dist    
XhmikosR committed
379
          return _this2._hideModal(event);
XhmikosR's avatar
XhmikosR committed
380
381
        });
        emulateTransitionEnd(this._element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
382
383
384
385
      } else {
        this._hideModal();
      }
    };
Mark Otto's avatar
grunt    
Mark Otto committed
386

XhmikosR's avatar
Dist    
XhmikosR committed
387
388
    _proto.dispose = function dispose() {
      [window, this._element, this._dialog].forEach(function (htmlElement) {
XhmikosR's avatar
XhmikosR committed
389
        return EventHandler.off(htmlElement, EVENT_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
390
391
392
393
394
395
      });
      /**
       * `document` has 2 events `Event.FOCUSIN` and `Event.CLICK_DATA_API`
       * Do not move `document` in `htmlElements` array
       * It will remove `Event.CLICK_DATA_API` event that should remain
       */
Mark Otto's avatar
grunt    
Mark Otto committed
396

XhmikosR's avatar
XhmikosR committed
397
      EventHandler.off(document, Event.FOCUSIN);
XhmikosR's avatar
XhmikosR committed
398
      Data.removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
399
400
401
402
403
404
405
406
407
408
      this._config = null;
      this._element = null;
      this._dialog = null;
      this._backdrop = null;
      this._isShown = null;
      this._isBodyOverflowing = null;
      this._ignoreBackdropClick = null;
      this._isTransitioning = null;
      this._scrollbarWidth = null;
    };
Mark Otto's avatar
grunt    
Mark Otto committed
409

XhmikosR's avatar
Dist    
XhmikosR committed
410
411
    _proto.handleUpdate = function handleUpdate() {
      this._adjustDialog();
Mark Otto's avatar
Mark Otto committed
412
413
    } // Private
    ;
Mark Otto's avatar
grunt    
Mark Otto committed
414

XhmikosR's avatar
Dist    
XhmikosR committed
415
    _proto._getConfig = function _getConfig(config) {
416
      config = _objectSpread2({}, Default, {}, config);
XhmikosR's avatar
XhmikosR committed
417
      typeCheckConfig(NAME, config, DefaultType);
XhmikosR's avatar
Dist    
XhmikosR committed
418
419
      return config;
    };
Mark Otto's avatar
grunt    
Mark Otto committed
420

XhmikosR's avatar
Dist    
XhmikosR committed
421
422
    _proto._showElement = function _showElement(relatedTarget) {
      var _this3 = this;
Mark Otto's avatar
dist    
Mark Otto committed
423

XhmikosR's avatar
XhmikosR committed
424
      var transition = this._element.classList.contains(ClassName.FADE);
Mark Otto's avatar
dist    
Mark Otto committed
425

XhmikosR's avatar
XhmikosR committed
426
427
      var modalBody = SelectorEngine.findOne(Selector.MODAL_BODY, this._dialog);

XhmikosR's avatar
Dist    
XhmikosR committed
428
429
430
431
      if (!this._element.parentNode || this._element.parentNode.nodeType !== Node.ELEMENT_NODE) {
        // Don't move modal's DOM position
        document.body.appendChild(this._element);
      }
Mark Otto's avatar
dist    
Mark Otto committed
432

XhmikosR's avatar
Dist    
XhmikosR committed
433
      this._element.style.display = 'block';
Mark Otto's avatar
dist    
Mark Otto committed
434

XhmikosR's avatar
Dist    
XhmikosR committed
435
      this._element.removeAttribute('aria-hidden');
436

Mark Otto's avatar
dist    
Mark Otto committed
437
438
      this._element.setAttribute('aria-modal', true);

XhmikosR's avatar
XhmikosR committed
439
440
      if (this._dialog.classList.contains(ClassName.SCROLLABLE) && modalBody) {
        modalBody.scrollTop = 0;
Mark Otto's avatar
Mark Otto committed
441
442
443
      } else {
        this._element.scrollTop = 0;
      }
444

XhmikosR's avatar
Dist    
XhmikosR committed
445
      if (transition) {
XhmikosR's avatar
XhmikosR committed
446
        reflow(this._element);
XhmikosR's avatar
Dist    
XhmikosR committed
447
      }
448

XhmikosR's avatar
XhmikosR committed
449
      this._element.classList.add(ClassName.SHOW);
Mark Otto's avatar
dist    
Mark Otto committed
450

XhmikosR's avatar
Dist    
XhmikosR committed
451
452
453
      if (this._config.focus) {
        this._enforceFocus();
      }
454

XhmikosR's avatar
Dist    
XhmikosR committed
455
456
457
      var transitionComplete = function transitionComplete() {
        if (_this3._config.focus) {
          _this3._element.focus();
Mark Otto's avatar
dist    
Mark Otto committed
458
        }
459

XhmikosR's avatar
Dist    
XhmikosR committed
460
        _this3._isTransitioning = false;
XhmikosR's avatar
XhmikosR committed
461
        EventHandler.trigger(_this3._element, Event.SHOWN, {
XhmikosR's avatar
XhmikosR committed
462
463
          relatedTarget: relatedTarget
        });
Mark Otto's avatar
dist    
Mark Otto committed
464
      };
465

XhmikosR's avatar
Dist    
XhmikosR committed
466
      if (transition) {
XhmikosR's avatar
XhmikosR committed
467
468
469
        var transitionDuration = getTransitionDurationFromElement(this._dialog);
        EventHandler.one(this._dialog, TRANSITION_END, transitionComplete);
        emulateTransitionEnd(this._dialog, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
470
471
472
473
      } else {
        transitionComplete();
      }
    };
474

XhmikosR's avatar
Dist    
XhmikosR committed
475
476
    _proto._enforceFocus = function _enforceFocus() {
      var _this4 = this;
477

XhmikosR's avatar
XhmikosR committed
478
      EventHandler.off(document, Event.FOCUSIN); // guard against infinite focus loop
XhmikosR's avatar
XhmikosR committed
479

XhmikosR's avatar
XhmikosR committed
480
      EventHandler.on(document, Event.FOCUSIN, function (event) {
XhmikosR's avatar
XhmikosR committed
481
        if (document !== event.target && _this4._element !== event.target && !_this4._element.contains(event.target)) {
XhmikosR's avatar
Dist    
XhmikosR committed
482
          _this4._element.focus();
483
        }
XhmikosR's avatar
Dist    
XhmikosR committed
484
485
      });
    };
Mark Otto's avatar
dist    
Mark Otto committed
486

XhmikosR's avatar
Dist    
XhmikosR committed
487
488
    _proto._setEscapeEvent = function _setEscapeEvent() {
      var _this5 = this;
Mark Otto's avatar
grunt    
Mark Otto committed
489

XhmikosR's avatar
Dist    
XhmikosR committed
490
      if (this._isShown && this._config.keyboard) {
XhmikosR's avatar
XhmikosR committed
491
        EventHandler.on(this._element, Event.KEYDOWN_DISMISS, function (event) {
XhmikosR's avatar
Dist    
XhmikosR committed
492
493
          if (event.which === ESCAPE_KEYCODE) {
            event.preventDefault();
Mark Otto's avatar
dist    
Mark Otto committed
494

XhmikosR's avatar
Dist    
XhmikosR committed
495
496
497
            _this5.hide();
          }
        });
498
      } else {
XhmikosR's avatar
XhmikosR committed
499
        EventHandler.off(this._element, Event.KEYDOWN_DISMISS);
XhmikosR's avatar
Dist    
XhmikosR committed
500
501
      }
    };
Mark Otto's avatar
dist    
Mark Otto committed
502

XhmikosR's avatar
Dist    
XhmikosR committed
503
504
    _proto._setResizeEvent = function _setResizeEvent() {
      var _this6 = this;
505

XhmikosR's avatar
Dist    
XhmikosR committed
506
      if (this._isShown) {
507
508
        EventHandler.on(window, Event.RESIZE, function () {
          return _this6._adjustDialog();
XhmikosR's avatar
Dist    
XhmikosR committed
509
510
        });
      } else {
XhmikosR's avatar
XhmikosR committed
511
        EventHandler.off(window, Event.RESIZE);
XhmikosR's avatar
Dist    
XhmikosR committed
512
513
      }
    };
514

XhmikosR's avatar
Dist    
XhmikosR committed
515
516
    _proto._hideModal = function _hideModal() {
      var _this7 = this;
517

XhmikosR's avatar
Dist    
XhmikosR committed
518
      this._element.style.display = 'none';
519

XhmikosR's avatar
Dist    
XhmikosR committed
520
      this._element.setAttribute('aria-hidden', true);
521

Mark Otto's avatar
dist    
Mark Otto committed
522
523
      this._element.removeAttribute('aria-modal');

XhmikosR's avatar
Dist    
XhmikosR committed
524
      this._isTransitioning = false;
Mark Otto's avatar
dist    
Mark Otto committed
525

XhmikosR's avatar
Dist    
XhmikosR committed
526
      this._showBackdrop(function () {
XhmikosR's avatar
XhmikosR committed
527
        document.body.classList.remove(ClassName.OPEN);
528

XhmikosR's avatar
Dist    
XhmikosR committed
529
        _this7._resetAdjustments();
Mark Otto's avatar
dist    
Mark Otto committed
530

XhmikosR's avatar
Dist    
XhmikosR committed
531
        _this7._resetScrollbar();
Mark Otto's avatar
dist    
Mark Otto committed
532

XhmikosR's avatar
XhmikosR committed
533
        EventHandler.trigger(_this7._element, Event.HIDDEN);
XhmikosR's avatar
Dist    
XhmikosR committed
534
535
      });
    };
Mark Otto's avatar
dist    
Mark Otto committed
536

XhmikosR's avatar
Dist    
XhmikosR committed
537
    _proto._removeBackdrop = function _removeBackdrop() {
538
      this._backdrop.parentNode.removeChild(this._backdrop);
XhmikosR's avatar
XhmikosR committed
539

540
      this._backdrop = null;
XhmikosR's avatar
Dist    
XhmikosR committed
541
    };
Mark Otto's avatar
dist    
Mark Otto committed
542

XhmikosR's avatar
Dist    
XhmikosR committed
543
544
    _proto._showBackdrop = function _showBackdrop(callback) {
      var _this8 = this;
Mark Otto's avatar
dist    
Mark Otto committed
545

XhmikosR's avatar
XhmikosR committed
546
      var animate = this._element.classList.contains(ClassName.FADE) ? ClassName.FADE : '';
XhmikosR's avatar
Dist    
XhmikosR committed
547
548
549
550

      if (this._isShown && this._config.backdrop) {
        this._backdrop = document.createElement('div');
        this._backdrop.className = ClassName.BACKDROP;
Mark Otto's avatar
dist    
Mark Otto committed
551

XhmikosR's avatar
Dist    
XhmikosR committed
552
553
554
        if (animate) {
          this._backdrop.classList.add(animate);
        }
555

XhmikosR's avatar
XhmikosR committed
556
        document.body.appendChild(this._backdrop);
XhmikosR's avatar
XhmikosR committed
557
        EventHandler.on(this._element, Event.CLICK_DISMISS, function (event) {
XhmikosR's avatar
Dist    
XhmikosR committed
558
559
          if (_this8._ignoreBackdropClick) {
            _this8._ignoreBackdropClick = false;
560
561
            return;
          }
Mark Otto's avatar
dist    
Mark Otto committed
562

XhmikosR's avatar
Dist    
XhmikosR committed
563
          if (event.target !== event.currentTarget) {
564
565
566
            return;
          }

XhmikosR's avatar
Dist    
XhmikosR committed
567
568
569
570
571
572
          if (_this8._config.backdrop === 'static') {
            _this8._element.focus();
          } else {
            _this8.hide();
          }
        });
Mark Otto's avatar
dist    
Mark Otto committed
573

XhmikosR's avatar
Dist    
XhmikosR committed
574
        if (animate) {
XhmikosR's avatar
XhmikosR committed
575
          reflow(this._backdrop);
XhmikosR's avatar
Dist    
XhmikosR committed
576
        }
577

XhmikosR's avatar
XhmikosR committed
578
        this._backdrop.classList.add(ClassName.SHOW);
579

XhmikosR's avatar
Dist    
XhmikosR committed
580
        if (!animate) {
Mark Otto's avatar
dist    
Mark Otto committed
581
          callback();
XhmikosR's avatar
Dist    
XhmikosR committed
582
          return;
583
        }
Mark Otto's avatar
grunt    
Mark Otto committed
584

XhmikosR's avatar
XhmikosR committed
585
586
587
        var backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop);
        EventHandler.one(this._backdrop, TRANSITION_END, callback);
        emulateTransitionEnd(this._backdrop, backdropTransitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
588
      } else if (!this._isShown && this._backdrop) {
XhmikosR's avatar
XhmikosR committed
589
        this._backdrop.classList.remove(ClassName.SHOW);
Mark Otto's avatar
dist    
Mark Otto committed
590

XhmikosR's avatar
Dist    
XhmikosR committed
591
592
        var callbackRemove = function callbackRemove() {
          _this8._removeBackdrop();
Mark Otto's avatar
dist    
Mark Otto committed
593

594
          callback();
XhmikosR's avatar
Dist    
XhmikosR committed
595
        };
Mark Otto's avatar
dist    
Mark Otto committed
596

XhmikosR's avatar
XhmikosR committed
597
598
        if (this._element.classList.contains(ClassName.FADE)) {
          var _backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop);
XhmikosR's avatar
Dist    
XhmikosR committed
599

XhmikosR's avatar
XhmikosR committed
600
601
          EventHandler.one(this._backdrop, TRANSITION_END, callbackRemove);
          emulateTransitionEnd(this._backdrop, _backdropTransitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
602
603
        } else {
          callbackRemove();
Mark Otto's avatar
dist    
Mark Otto committed
604
        }
605
      } else {
XhmikosR's avatar
Dist    
XhmikosR committed
606
607
        callback();
      }
Mark Otto's avatar
Mark Otto committed
608
    } // ----------------------------------------------------------------------
XhmikosR's avatar
Dist    
XhmikosR committed
609
610
    // the following methods are used to handle overflowing modals
    // ----------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
611
    ;
Mark Otto's avatar
dist    
Mark Otto committed
612

XhmikosR's avatar
Dist    
XhmikosR committed
613
614
    _proto._adjustDialog = function _adjustDialog() {
      var isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight;
Mark Otto's avatar
dist    
Mark Otto committed
615

XhmikosR's avatar
Dist    
XhmikosR committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
      if (!this._isBodyOverflowing && isModalOverflowing) {
        this._element.style.paddingLeft = this._scrollbarWidth + "px";
      }

      if (this._isBodyOverflowing && !isModalOverflowing) {
        this._element.style.paddingRight = this._scrollbarWidth + "px";
      }
    };

    _proto._resetAdjustments = function _resetAdjustments() {
      this._element.style.paddingLeft = '';
      this._element.style.paddingRight = '';
    };

    _proto._checkScrollbar = function _checkScrollbar() {
      var rect = document.body.getBoundingClientRect();
      this._isBodyOverflowing = rect.left + rect.right < window.innerWidth;
      this._scrollbarWidth = this._getScrollbarWidth();
    };

    _proto._setScrollbar = function _setScrollbar() {
      var _this9 = this;
Mark Otto's avatar
dist    
Mark Otto committed
638

XhmikosR's avatar
Dist    
XhmikosR committed
639
640
641
      if (this._isBodyOverflowing) {
        // Note: DOMNode.style.paddingRight returns the actual value or '' if not set
        //   while $(DOMNode).css('padding-right') returns the calculated value or 0 if not set
XhmikosR's avatar
XhmikosR committed
642
643
        // Adjust fixed content padding
        makeArray(SelectorEngine.find(Selector.FIXED_CONTENT)).forEach(function (element) {
XhmikosR's avatar
Dist    
XhmikosR committed
644
          var actualPadding = element.style.paddingRight;
XhmikosR's avatar
XhmikosR committed
645
646
647
          var calculatedPadding = window.getComputedStyle(element)['padding-right'];
          Manipulator.setDataAttribute(element, 'padding-right', actualPadding);
          element.style.paddingRight = parseFloat(calculatedPadding) + _this9._scrollbarWidth + "px";
XhmikosR's avatar
Dist    
XhmikosR committed
648
649
        }); // Adjust sticky content margin

XhmikosR's avatar
XhmikosR committed
650
        makeArray(SelectorEngine.find(Selector.STICKY_CONTENT)).forEach(function (element) {
XhmikosR's avatar
Dist    
XhmikosR committed
651
          var actualMargin = element.style.marginRight;
XhmikosR's avatar
XhmikosR committed
652
653
654
          var calculatedMargin = window.getComputedStyle(element)['margin-right'];
          Manipulator.setDataAttribute(element, 'margin-right', actualMargin);
          element.style.marginRight = parseFloat(calculatedMargin) - _this9._scrollbarWidth + "px";
XhmikosR's avatar
Dist    
XhmikosR committed
655
656
657
        }); // Adjust body padding

        var actualPadding = document.body.style.paddingRight;
XhmikosR's avatar
XhmikosR committed
658
659
660
        var calculatedPadding = window.getComputedStyle(document.body)['padding-right'];
        Manipulator.setDataAttribute(document.body, 'padding-right', actualPadding);
        document.body.style.paddingRight = parseFloat(calculatedPadding) + this._scrollbarWidth + "px";
XhmikosR's avatar
Dist    
XhmikosR committed
661
      }
XhmikosR's avatar
Dist    
XhmikosR committed
662

XhmikosR's avatar
XhmikosR committed
663
      document.body.classList.add(ClassName.OPEN);
XhmikosR's avatar
Dist    
XhmikosR committed
664
    };
Mark Otto's avatar
grunt    
Mark Otto committed
665

XhmikosR's avatar
Dist    
XhmikosR committed
666
667
    _proto._resetScrollbar = function _resetScrollbar() {
      // Restore fixed content padding
XhmikosR's avatar
XhmikosR committed
668
669
      makeArray(SelectorEngine.find(Selector.FIXED_CONTENT)).forEach(function (element) {
        var padding = Manipulator.getDataAttribute(element, 'padding-right');
XhmikosR's avatar
Dist    
XhmikosR committed
670

XhmikosR's avatar
XhmikosR committed
671
672
673
674
675
676
677
678
        if (typeof padding !== 'undefined') {
          Manipulator.removeDataAttribute(element, 'padding-right');
          element.style.paddingRight = padding;
        }
      }); // Restore sticky content and navbar-toggler margin

      makeArray(SelectorEngine.find("" + Selector.STICKY_CONTENT)).forEach(function (element) {
        var margin = Manipulator.getDataAttribute(element, 'margin-right');
XhmikosR's avatar
Dist    
XhmikosR committed
679
680

        if (typeof margin !== 'undefined') {
XhmikosR's avatar
XhmikosR committed
681
682
          Manipulator.removeDataAttribute(element, 'margin-right');
          element.style.marginRight = margin;
XhmikosR's avatar
Dist    
XhmikosR committed
683
684
        }
      }); // Restore body padding
Mark Otto's avatar
dist    
Mark Otto committed
685

XhmikosR's avatar
XhmikosR committed
686
687
      var padding = Manipulator.getDataAttribute(document.body, 'padding-right');

XhmikosR's avatar
Dist.    
XhmikosR committed
688
689
690
      if (typeof padding === 'undefined') {
        document.body.style.paddingRight = '';
      } else {
XhmikosR's avatar
XhmikosR committed
691
692
693
        Manipulator.removeDataAttribute(document.body, 'padding-right');
        document.body.style.paddingRight = padding;
      }
XhmikosR's avatar
Dist    
XhmikosR committed
694
    };
Mark Otto's avatar
grunt    
Mark Otto committed
695

XhmikosR's avatar
Dist    
XhmikosR committed
696
697
698
699
700
701
702
703
    _proto._getScrollbarWidth = function _getScrollbarWidth() {
      // thx d.walsh
      var scrollDiv = document.createElement('div');
      scrollDiv.className = ClassName.SCROLLBAR_MEASURER;
      document.body.appendChild(scrollDiv);
      var scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth;
      document.body.removeChild(scrollDiv);
      return scrollbarWidth;
Mark Otto's avatar
Mark Otto committed
704
705
    } // Static
    ;
Mark Otto's avatar
grunt    
Mark Otto committed
706

XhmikosR's avatar
XhmikosR committed
707
    Modal.jQueryInterface = function jQueryInterface(config, relatedTarget) {
XhmikosR's avatar
Dist    
XhmikosR committed
708
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
709
        var data = Data.getData(this, DATA_KEY);
Mark Otto's avatar
grunt    
Mark Otto committed
710

711
        var _config = _objectSpread2({}, Default, {}, Manipulator.getDataAttributes(this), {}, typeof config === 'object' && config ? config : {});
Mark Otto's avatar
dist    
Mark Otto committed
712

XhmikosR's avatar
Dist    
XhmikosR committed
713
714
715
        if (!data) {
          data = new Modal(this, _config);
        }
Mark Otto's avatar
dist    
Mark Otto committed
716

XhmikosR's avatar
Dist    
XhmikosR committed
717
718
719
        if (typeof config === 'string') {
          if (typeof data[config] === 'undefined') {
            throw new TypeError("No method named \"" + config + "\"");
Mark Otto's avatar
dist    
Mark Otto committed
720
          }
Mark Otto's avatar
dist    
Mark Otto committed
721

XhmikosR's avatar
Dist    
XhmikosR committed
722
723
724
          data[config](relatedTarget);
        } else if (_config.show) {
          data.show(relatedTarget);
Mark Otto's avatar
dist    
Mark Otto committed
725
        }
XhmikosR's avatar
Dist    
XhmikosR committed
726
727
      });
    };
728

XhmikosR's avatar
XhmikosR committed
729
    Modal.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
730
731
732
      return Data.getData(element, DATA_KEY);
    };

XhmikosR's avatar
Dist    
XhmikosR committed
733
734
735
736
737
738
739
740
741
742
743
    _createClass(Modal, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }, {
      key: "Default",
      get: function get() {
        return Default;
      }
    }]);
Mark Otto's avatar
dist    
Mark Otto committed
744

XhmikosR's avatar
Dist    
XhmikosR committed
745
746
747
748
749
750
751
    return Modal;
  }();
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
752
753


XhmikosR's avatar
XhmikosR committed
754
  EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
XhmikosR's avatar
Dist    
XhmikosR committed
755
    var _this10 = this;
756

XhmikosR's avatar
XhmikosR committed
757
    var target = getElementFromSelector(this);
XhmikosR's avatar
Dist    
XhmikosR committed
758
759
760
761
762

    if (this.tagName === 'A' || this.tagName === 'AREA') {
      event.preventDefault();
    }

XhmikosR's avatar
XhmikosR committed
763
    EventHandler.one(target, Event.SHOW, function (showEvent) {
XhmikosR's avatar
XhmikosR committed
764
765
      if (showEvent.defaultPrevented) {
        // only register focus restorer if modal will actually get shown
XhmikosR's avatar
Dist    
XhmikosR committed
766
        return;
767
768
      }

XhmikosR's avatar
XhmikosR committed
769
      EventHandler.one(target, Event.HIDDEN, function () {
XhmikosR's avatar
XhmikosR committed
770
        if (isVisible(_this10)) {
XhmikosR's avatar
Dist    
XhmikosR committed
771
          _this10.focus();
772
773
774
        }
      });
    });
XhmikosR's avatar
XhmikosR committed
775
776
777
    var data = Data.getData(target, DATA_KEY);

    if (!data) {
778
779
      var config = _objectSpread2({}, Manipulator.getDataAttributes(target), {}, Manipulator.getDataAttributes(this));

XhmikosR's avatar
XhmikosR committed
780
781
      data = new Modal(target, config);
    }
782

XhmikosR's avatar
XhmikosR committed
783
    data.show(this);
XhmikosR's avatar
Dist    
XhmikosR committed
784
  });
XhmikosR's avatar
XhmikosR committed
785
  var $ = getjQuery();
XhmikosR's avatar
Dist    
XhmikosR committed
786
787
788
789
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
790
   * add .modal to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
791
   */
792

793
794
  /* istanbul ignore if */

XhmikosR's avatar
XhmikosR committed
795
796
797
798
  if ($) {
    var JQUERY_NO_CONFLICT = $.fn[NAME];
    $.fn[NAME] = Modal.jQueryInterface;
    $.fn[NAME].Constructor = Modal;
Mark Otto's avatar
dist    
Mark Otto committed
799

XhmikosR's avatar
XhmikosR committed
800
801
802
    $.fn[NAME].noConflict = function () {
      $.fn[NAME] = JQUERY_NO_CONFLICT;
      return Modal.jQueryInterface;
XhmikosR's avatar
XhmikosR committed
803
804
    };
  }
805
806

  return Modal;
Mark Otto's avatar
dist    
Mark Otto committed
807

Mark Otto's avatar
Mark Otto committed
808
}));
Mark Otto's avatar
dist    
Mark Otto committed
809
//# sourceMappingURL=modal.js.map