modal.js 17.7 KB
Newer Older
1
2
/**
 * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
3
 * Bootstrap (v5.0.0-alpha3): modal.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
6
7
 * --------------------------------------------------------------------------
 */

8
import {
9
  getjQuery,
10
  onDOMContentLoaded,
11
12
  TRANSITION_END,
  emulateTransitionEnd,
13
  getElementFromSelector,
14
15
16
17
  getTransitionDurationFromElement,
  isVisible,
  reflow,
  typeCheckConfig
18
19
20
21
22
} from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
23

Johann-S's avatar
Johann-S committed
24
25
26
27
28
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */
29

XhmikosR's avatar
XhmikosR committed
30
const NAME = 'modal'
XhmikosR's avatar
XhmikosR committed
31
const VERSION = '5.0.0-alpha3'
XhmikosR's avatar
XhmikosR committed
32
33
34
const DATA_KEY = 'bs.modal'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
35
const ESCAPE_KEY = 'Escape'
Johann-S's avatar
Johann-S committed
36
37

const Default = {
XhmikosR's avatar
XhmikosR committed
38
39
40
41
  backdrop: true,
  keyboard: true,
  focus: true,
  show: true
Johann-S's avatar
Johann-S committed
42
43
44
}

const DefaultType = {
XhmikosR's avatar
XhmikosR committed
45
46
47
48
  backdrop: '(boolean|string)',
  keyboard: 'boolean',
  focus: 'boolean',
  show: 'boolean'
Johann-S's avatar
Johann-S committed
49
50
}

XhmikosR's avatar
XhmikosR committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const EVENT_HIDE = `hide${EVENT_KEY}`
const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}`
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
const EVENT_SHOW = `show${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
const EVENT_FOCUSIN = `focusin${EVENT_KEY}`
const EVENT_RESIZE = `resize${EVENT_KEY}`
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
const EVENT_MOUSEUP_DISMISS = `mouseup.dismiss${EVENT_KEY}`
const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`

const CLASS_NAME_SCROLLBAR_MEASURER = 'modal-scrollbar-measure'
const CLASS_NAME_BACKDROP = 'modal-backdrop'
const CLASS_NAME_OPEN = 'modal-open'
const CLASS_NAME_FADE = 'fade'
const CLASS_NAME_SHOW = 'show'
const CLASS_NAME_STATIC = 'modal-static'

const SELECTOR_DIALOG = '.modal-dialog'
const SELECTOR_MODAL_BODY = '.modal-body'
Rohit Sharma's avatar
Rohit Sharma committed
73
74
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="modal"]'
const SELECTOR_DATA_DISMISS = '[data-bs-dismiss="modal"]'
XhmikosR's avatar
XhmikosR committed
75
76
const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top'
const SELECTOR_STICKY_CONTENT = '.sticky-top'
fat's avatar
fat committed
77

Johann-S's avatar
Johann-S committed
78
79
80
81
82
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
83

Johann-S's avatar
Johann-S committed
84
85
class Modal {
  constructor(element, config) {
XhmikosR's avatar
XhmikosR committed
86
87
    this._config = this._getConfig(config)
    this._element = element
XhmikosR's avatar
XhmikosR committed
88
    this._dialog = SelectorEngine.findOne(SELECTOR_DIALOG, element)
XhmikosR's avatar
XhmikosR committed
89
90
91
    this._backdrop = null
    this._isShown = false
    this._isBodyOverflowing = false
Johann-S's avatar
Johann-S committed
92
    this._ignoreBackdropClick = false
XhmikosR's avatar
XhmikosR committed
93
94
    this._isTransitioning = false
    this._scrollbarWidth = 0
95
    Data.setData(element, DATA_KEY, this)
96
97
  }

Johann-S's avatar
Johann-S committed
98
99
100
101
  // Getters

  static get VERSION() {
    return VERSION
102
103
  }

Johann-S's avatar
Johann-S committed
104
105
106
  static get Default() {
    return Default
  }
107

Johann-S's avatar
Johann-S committed
108
  // Public
109

Johann-S's avatar
Johann-S committed
110
111
112
  toggle(relatedTarget) {
    return this._isShown ? this.hide() : this.show(relatedTarget)
  }
113

Johann-S's avatar
Johann-S committed
114
  show(relatedTarget) {
115
    if (this._isShown || this._isTransitioning) {
Johann-S's avatar
Johann-S committed
116
      return
117
118
    }

XhmikosR's avatar
XhmikosR committed
119
    if (this._element.classList.contains(CLASS_NAME_FADE)) {
Johann-S's avatar
Johann-S committed
120
      this._isTransitioning = true
121
122
    }

XhmikosR's avatar
XhmikosR committed
123
    const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, {
Johann-S's avatar
Johann-S committed
124
125
      relatedTarget
    })
126

Johann-S's avatar
Johann-S committed
127
    if (this._isShown || showEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
128
129
      return
    }
130

Johann-S's avatar
Johann-S committed
131
    this._isShown = true
132

Johann-S's avatar
Johann-S committed
133
134
    this._checkScrollbar()
    this._setScrollbar()
135

Johann-S's avatar
Johann-S committed
136
    this._adjustDialog()
137

Johann-S's avatar
Johann-S committed
138
139
    this._setEscapeEvent()
    this._setResizeEvent()
David Bailey's avatar
David Bailey committed
140

141
    EventHandler.on(this._element,
XhmikosR's avatar
XhmikosR committed
142
143
      EVENT_CLICK_DISMISS,
      SELECTOR_DATA_DISMISS,
XhmikosR's avatar
XhmikosR committed
144
      event => this.hide(event)
Johann-S's avatar
Johann-S committed
145
    )
146

XhmikosR's avatar
XhmikosR committed
147
148
    EventHandler.on(this._dialog, EVENT_MOUSEDOWN_DISMISS, () => {
      EventHandler.one(this._element, EVENT_MOUSEUP_DISMISS, event => {
149
        if (event.target === this._element) {
Johann-S's avatar
Johann-S committed
150
151
152
153
          this._ignoreBackdropClick = true
        }
      })
    })
154

Johann-S's avatar
Johann-S committed
155
156
    this._showBackdrop(() => this._showElement(relatedTarget))
  }
157

Johann-S's avatar
Johann-S committed
158
159
160
161
  hide(event) {
    if (event) {
      event.preventDefault()
    }
162

163
    if (!this._isShown || this._isTransitioning) {
Johann-S's avatar
Johann-S committed
164
      return
165
166
    }

XhmikosR's avatar
XhmikosR committed
167
    const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE)
168

Johann-S's avatar
Johann-S committed
169
    if (hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
170
171
      return
    }
172

Johann-S's avatar
Johann-S committed
173
    this._isShown = false
XhmikosR's avatar
XhmikosR committed
174
    const transition = this._element.classList.contains(CLASS_NAME_FADE)
175

Johann-S's avatar
Johann-S committed
176
177
178
    if (transition) {
      this._isTransitioning = true
    }
179

Johann-S's avatar
Johann-S committed
180
181
    this._setEscapeEvent()
    this._setResizeEvent()
182

XhmikosR's avatar
XhmikosR committed
183
    EventHandler.off(document, EVENT_FOCUSIN)
184

XhmikosR's avatar
XhmikosR committed
185
    this._element.classList.remove(CLASS_NAME_SHOW)
186

XhmikosR's avatar
XhmikosR committed
187
188
    EventHandler.off(this._element, EVENT_CLICK_DISMISS)
    EventHandler.off(this._dialog, EVENT_MOUSEDOWN_DISMISS)
189

Johann-S's avatar
Johann-S committed
190
    if (transition) {
191
      const transitionDuration = getTransitionDurationFromElement(this._element)
192

XhmikosR's avatar
XhmikosR committed
193
      EventHandler.one(this._element, TRANSITION_END, event => this._hideModal(event))
194
      emulateTransitionEnd(this._element, transitionDuration)
Johann-S's avatar
Johann-S committed
195
196
197
198
    } else {
      this._hideModal()
    }
  }
199

Johann-S's avatar
Johann-S committed
200
  dispose() {
201
    [window, this._element, this._dialog]
XhmikosR's avatar
XhmikosR committed
202
      .forEach(htmlElement => EventHandler.off(htmlElement, EVENT_KEY))
203
204

    /**
XhmikosR's avatar
XhmikosR committed
205
     * `document` has 2 events `EVENT_FOCUSIN` and `EVENT_CLICK_DATA_API`
206
     * Do not move `document` in `htmlElements` array
XhmikosR's avatar
XhmikosR committed
207
     * It will remove `EVENT_CLICK_DATA_API` event that should remain
208
     */
XhmikosR's avatar
XhmikosR committed
209
    EventHandler.off(document, EVENT_FOCUSIN)
210

211
    Data.removeData(this._element, DATA_KEY)
212

XhmikosR's avatar
XhmikosR committed
213
214
215
216
217
218
    this._config = null
    this._element = null
    this._dialog = null
    this._backdrop = null
    this._isShown = null
    this._isBodyOverflowing = null
Johann-S's avatar
Johann-S committed
219
    this._ignoreBackdropClick = null
XhmikosR's avatar
XhmikosR committed
220
221
    this._isTransitioning = null
    this._scrollbarWidth = null
Johann-S's avatar
Johann-S committed
222
  }
fat's avatar
fat committed
223

Johann-S's avatar
Johann-S committed
224
225
226
  handleUpdate() {
    this._adjustDialog()
  }
fat's avatar
fat committed
227

Johann-S's avatar
Johann-S committed
228
  // Private
fat's avatar
fat committed
229

Johann-S's avatar
Johann-S committed
230
231
232
233
  _getConfig(config) {
    config = {
      ...Default,
      ...config
234
    }
235
    typeCheckConfig(NAME, config, DefaultType)
Johann-S's avatar
Johann-S committed
236
237
    return config
  }
238

Johann-S's avatar
Johann-S committed
239
  _showElement(relatedTarget) {
XhmikosR's avatar
XhmikosR committed
240
241
    const transition = this._element.classList.contains(CLASS_NAME_FADE)
    const modalBody = SelectorEngine.findOne(SELECTOR_MODAL_BODY, this._dialog)
242

Johann-S's avatar
Johann-S committed
243
244
245
246
    if (!this._element.parentNode ||
        this._element.parentNode.nodeType !== Node.ELEMENT_NODE) {
      // Don't move modal's DOM position
      document.body.appendChild(this._element)
fat's avatar
fat committed
247
248
    }

Johann-S's avatar
Johann-S committed
249
250
    this._element.style.display = 'block'
    this._element.removeAttribute('aria-hidden')
251
    this._element.setAttribute('aria-modal', true)
252
    this._element.setAttribute('role', 'dialog')
ysds's avatar
ysds committed
253
    this._element.scrollTop = 0
Shohei Yoshida's avatar
Shohei Yoshida committed
254

ysds's avatar
ysds committed
255
    if (modalBody) {
256
      modalBody.scrollTop = 0
Shohei Yoshida's avatar
Shohei Yoshida committed
257
    }
258

Johann-S's avatar
Johann-S committed
259
    if (transition) {
260
      reflow(this._element)
Johann-S's avatar
Johann-S committed
261
    }
262

XhmikosR's avatar
XhmikosR committed
263
    this._element.classList.add(CLASS_NAME_SHOW)
264

Johann-S's avatar
Johann-S committed
265
266
267
    if (this._config.focus) {
      this._enforceFocus()
    }
268

Johann-S's avatar
Johann-S committed
269
    const transitionComplete = () => {
Jacob Thornton's avatar
Jacob Thornton committed
270
      if (this._config.focus) {
Johann-S's avatar
Johann-S committed
271
        this._element.focus()
Jacob Thornton's avatar
Jacob Thornton committed
272
      }
XhmikosR's avatar
XhmikosR committed
273

Johann-S's avatar
Johann-S committed
274
      this._isTransitioning = false
XhmikosR's avatar
XhmikosR committed
275
      EventHandler.trigger(this._element, EVENT_SHOWN, {
276
277
        relatedTarget
      })
Johann-S's avatar
Johann-S committed
278
    }
279

Johann-S's avatar
Johann-S committed
280
    if (transition) {
XhmikosR's avatar
XhmikosR committed
281
      const transitionDuration = getTransitionDurationFromElement(this._dialog)
282

283
284
      EventHandler.one(this._dialog, TRANSITION_END, transitionComplete)
      emulateTransitionEnd(this._dialog, transitionDuration)
Johann-S's avatar
Johann-S committed
285
286
287
288
289
290
    } else {
      transitionComplete()
    }
  }

  _enforceFocus() {
XhmikosR's avatar
XhmikosR committed
291
292
    EventHandler.off(document, EVENT_FOCUSIN) // guard against infinite focus loop
    EventHandler.on(document, EVENT_FOCUSIN, event => {
Johann-S's avatar
Johann-S committed
293
294
295
296
297
298
      if (document !== event.target &&
          this._element !== event.target &&
          !this._element.contains(event.target)) {
        this._element.focus()
      }
    })
Johann-S's avatar
Johann-S committed
299
  }
300

Johann-S's avatar
Johann-S committed
301
  _setEscapeEvent() {
302
    if (this._isShown) {
XhmikosR's avatar
XhmikosR committed
303
      EventHandler.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
304
        if (this._config.keyboard && event.key === ESCAPE_KEY) {
305
306
          event.preventDefault()
          this.hide()
307
        } else if (!this._config.keyboard && event.key === ESCAPE_KEY) {
308
          this._triggerBackdropTransition()
Johann-S's avatar
Johann-S committed
309
310
        }
      })
Johann-S's avatar
Johann-S committed
311
    } else {
XhmikosR's avatar
XhmikosR committed
312
      EventHandler.off(this._element, EVENT_KEYDOWN_DISMISS)
313
    }
Johann-S's avatar
Johann-S committed
314
  }
315

Johann-S's avatar
Johann-S committed
316
317
  _setResizeEvent() {
    if (this._isShown) {
XhmikosR's avatar
XhmikosR committed
318
      EventHandler.on(window, EVENT_RESIZE, () => this._adjustDialog())
Johann-S's avatar
Johann-S committed
319
    } else {
XhmikosR's avatar
XhmikosR committed
320
      EventHandler.off(window, EVENT_RESIZE)
321
    }
Johann-S's avatar
Johann-S committed
322
  }
323

Johann-S's avatar
Johann-S committed
324
325
326
  _hideModal() {
    this._element.style.display = 'none'
    this._element.setAttribute('aria-hidden', true)
327
    this._element.removeAttribute('aria-modal')
328
    this._element.removeAttribute('role')
Johann-S's avatar
Johann-S committed
329
330
    this._isTransitioning = false
    this._showBackdrop(() => {
XhmikosR's avatar
XhmikosR committed
331
      document.body.classList.remove(CLASS_NAME_OPEN)
Johann-S's avatar
Johann-S committed
332
333
      this._resetAdjustments()
      this._resetScrollbar()
XhmikosR's avatar
XhmikosR committed
334
      EventHandler.trigger(this._element, EVENT_HIDDEN)
Johann-S's avatar
Johann-S committed
335
336
337
338
    })
  }

  _removeBackdrop() {
Johann-S's avatar
Johann-S committed
339
340
    this._backdrop.parentNode.removeChild(this._backdrop)
    this._backdrop = null
Johann-S's avatar
Johann-S committed
341
  }
342

Johann-S's avatar
Johann-S committed
343
  _showBackdrop(callback) {
XhmikosR's avatar
XhmikosR committed
344
345
    const animate = this._element.classList.contains(CLASS_NAME_FADE) ?
      CLASS_NAME_FADE :
XhmikosR's avatar
XhmikosR committed
346
      ''
Johann-S's avatar
Johann-S committed
347
348
349

    if (this._isShown && this._config.backdrop) {
      this._backdrop = document.createElement('div')
XhmikosR's avatar
XhmikosR committed
350
      this._backdrop.className = CLASS_NAME_BACKDROP
Johann-S's avatar
Johann-S committed
351
352
353

      if (animate) {
        this._backdrop.classList.add(animate)
354
355
      }

356
      document.body.appendChild(this._backdrop)
Johann-S's avatar
Johann-S committed
357

XhmikosR's avatar
XhmikosR committed
358
      EventHandler.on(this._element, EVENT_CLICK_DISMISS, event => {
Johann-S's avatar
Johann-S committed
359
360
361
362
        if (this._ignoreBackdropClick) {
          this._ignoreBackdropClick = false
          return
        }
XhmikosR's avatar
XhmikosR committed
363

Johann-S's avatar
Johann-S committed
364
365
366
        if (event.target !== event.currentTarget) {
          return
        }
XhmikosR's avatar
XhmikosR committed
367

368
        this._triggerBackdropTransition()
369
370
      })

Johann-S's avatar
Johann-S committed
371
      if (animate) {
372
        reflow(this._backdrop)
373
374
      }

XhmikosR's avatar
XhmikosR committed
375
      this._backdrop.classList.add(CLASS_NAME_SHOW)
376

Johann-S's avatar
Johann-S committed
377
378
379
380
      if (!animate) {
        callback()
        return
      }
381

382
      const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)
383

384
385
      EventHandler.one(this._backdrop, TRANSITION_END, callback)
      emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
Johann-S's avatar
Johann-S committed
386
    } else if (!this._isShown && this._backdrop) {
XhmikosR's avatar
XhmikosR committed
387
      this._backdrop.classList.remove(CLASS_NAME_SHOW)
388

Johann-S's avatar
Johann-S committed
389
390
      const callbackRemove = () => {
        this._removeBackdrop()
Johann-S's avatar
Johann-S committed
391
        callback()
Johann-S's avatar
Johann-S committed
392
      }
393

XhmikosR's avatar
XhmikosR committed
394
      if (this._element.classList.contains(CLASS_NAME_FADE)) {
395
396
397
        const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)
        EventHandler.one(this._backdrop, TRANSITION_END, callbackRemove)
        emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
Johann-S's avatar
Johann-S committed
398
399
      } else {
        callbackRemove()
400
      }
Johann-S's avatar
Johann-S committed
401
    } else {
Johann-S's avatar
Johann-S committed
402
      callback()
403
    }
Johann-S's avatar
Johann-S committed
404
  }
405

406
407
  _triggerBackdropTransition() {
    if (this._config.backdrop === 'static') {
XhmikosR's avatar
XhmikosR committed
408
      const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED)
409
410
411
412
      if (hideEvent.defaultPrevented) {
        return
      }

413
414
415
416
417
418
      const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight

      if (!isModalOverflowing) {
        this._element.style.overflowY = 'hidden'
      }

XhmikosR's avatar
XhmikosR committed
419
      this._element.classList.add(CLASS_NAME_STATIC)
420
421
      const modalTransitionDuration = getTransitionDurationFromElement(this._dialog)
      EventHandler.off(this._element, TRANSITION_END)
422
      EventHandler.one(this._element, TRANSITION_END, () => {
XhmikosR's avatar
XhmikosR committed
423
        this._element.classList.remove(CLASS_NAME_STATIC)
424
425
426
427
428
429
        if (!isModalOverflowing) {
          EventHandler.one(this._element, TRANSITION_END, () => {
            this._element.style.overflowY = ''
          })
          emulateTransitionEnd(this._element, modalTransitionDuration)
        }
430
431
432
433
434
435
436
437
      })
      emulateTransitionEnd(this._element, modalTransitionDuration)
      this._element.focus()
    } else {
      this.hide()
    }
  }

Johann-S's avatar
Johann-S committed
438
439
440
  // ----------------------------------------------------------------------
  // the following methods are used to handle overflowing modals
  // ----------------------------------------------------------------------
441

Johann-S's avatar
Johann-S committed
442
443
444
  _adjustDialog() {
    const isModalOverflowing =
      this._element.scrollHeight > document.documentElement.clientHeight
445

Johann-S's avatar
Johann-S committed
446
447
    if (!this._isBodyOverflowing && isModalOverflowing) {
      this._element.style.paddingLeft = `${this._scrollbarWidth}px`
448
449
    }

Johann-S's avatar
Johann-S committed
450
451
    if (this._isBodyOverflowing && !isModalOverflowing) {
      this._element.style.paddingRight = `${this._scrollbarWidth}px`
452
    }
Johann-S's avatar
Johann-S committed
453
  }
454

Johann-S's avatar
Johann-S committed
455
456
457
458
  _resetAdjustments() {
    this._element.style.paddingLeft = ''
    this._element.style.paddingRight = ''
  }
459

Johann-S's avatar
Johann-S committed
460
  _checkScrollbar() {
461
    const rect = document.body.getBoundingClientRect()
462
    this._isBodyOverflowing = Math.round(rect.left + rect.right) < window.innerWidth
Johann-S's avatar
Johann-S committed
463
464
    this._scrollbarWidth = this._getScrollbarWidth()
  }
465

Johann-S's avatar
Johann-S committed
466
467
468
469
470
471
  _setScrollbar() {
    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

      // Adjust fixed content padding
472
      SelectorEngine.find(SELECTOR_FIXED_CONTENT)
XhmikosR's avatar
XhmikosR committed
473
        .forEach(element => {
474
475
          const actualPadding = element.style.paddingRight
          const calculatedPadding = window.getComputedStyle(element)['padding-right']
476
          Manipulator.setDataAttribute(element, 'padding-right', actualPadding)
XhmikosR's avatar
XhmikosR committed
477
          element.style.paddingRight = `${Number.parseFloat(calculatedPadding) + this._scrollbarWidth}px`
478
        })
479

Johann-S's avatar
Johann-S committed
480
      // Adjust sticky content margin
481
      SelectorEngine.find(SELECTOR_STICKY_CONTENT)
XhmikosR's avatar
XhmikosR committed
482
        .forEach(element => {
483
484
          const actualMargin = element.style.marginRight
          const calculatedMargin = window.getComputedStyle(element)['margin-right']
485
          Manipulator.setDataAttribute(element, 'margin-right', actualMargin)
XhmikosR's avatar
XhmikosR committed
486
          element.style.marginRight = `${Number.parseFloat(calculatedMargin) - this._scrollbarWidth}px`
487
        })
488

Johann-S's avatar
Johann-S committed
489
490
      // Adjust body padding
      const actualPadding = document.body.style.paddingRight
491
492
      const calculatedPadding = window.getComputedStyle(document.body)['padding-right']

493
      Manipulator.setDataAttribute(document.body, 'padding-right', actualPadding)
XhmikosR's avatar
XhmikosR committed
494
      document.body.style.paddingRight = `${Number.parseFloat(calculatedPadding) + this._scrollbarWidth}px`
495
    }
496

XhmikosR's avatar
XhmikosR committed
497
    document.body.classList.add(CLASS_NAME_OPEN)
Johann-S's avatar
Johann-S committed
498
  }
499

Johann-S's avatar
Johann-S committed
500
501
  _resetScrollbar() {
    // Restore fixed content padding
502
    SelectorEngine.find(SELECTOR_FIXED_CONTENT)
XhmikosR's avatar
XhmikosR committed
503
      .forEach(element => {
504
        const padding = Manipulator.getDataAttribute(element, 'padding-right')
505
        if (typeof padding !== 'undefined') {
506
          Manipulator.removeDataAttribute(element, 'padding-right')
507
508
509
          element.style.paddingRight = padding
        }
      })
Johann-S's avatar
Johann-S committed
510

511
    // Restore sticky content and navbar-toggler margin
512
    SelectorEngine.find(`${SELECTOR_STICKY_CONTENT}`)
XhmikosR's avatar
XhmikosR committed
513
      .forEach(element => {
514
        const margin = Manipulator.getDataAttribute(element, 'margin-right')
515
        if (typeof margin !== 'undefined') {
516
          Manipulator.removeDataAttribute(element, 'margin-right')
517
518
519
          element.style.marginRight = margin
        }
      })
520

Johann-S's avatar
Johann-S committed
521
    // Restore body padding
522
    const padding = Manipulator.getDataAttribute(document.body, 'padding-right')
XhmikosR's avatar
XhmikosR committed
523
524
525
    if (typeof padding === 'undefined') {
      document.body.style.paddingRight = ''
    } else {
526
      Manipulator.removeDataAttribute(document.body, 'padding-right')
527
528
      document.body.style.paddingRight = padding
    }
Johann-S's avatar
Johann-S committed
529
  }
530

Johann-S's avatar
Johann-S committed
531
532
  _getScrollbarWidth() { // thx d.walsh
    const scrollDiv = document.createElement('div')
XhmikosR's avatar
XhmikosR committed
533
    scrollDiv.className = CLASS_NAME_SCROLLBAR_MEASURER
Johann-S's avatar
Johann-S committed
534
535
536
537
538
    document.body.appendChild(scrollDiv)
    const scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth
    document.body.removeChild(scrollDiv)
    return scrollbarWidth
  }
539

Johann-S's avatar
Johann-S committed
540
541
  // Static

542
  static jQueryInterface(config, relatedTarget) {
Johann-S's avatar
Johann-S committed
543
    return this.each(function () {
544
      let data = Data.getData(this, DATA_KEY)
Johann-S's avatar
Johann-S committed
545
546
      const _config = {
        ...Default,
547
        ...Manipulator.getDataAttributes(this),
548
        ...(typeof config === 'object' && config ? config : {})
Johann-S's avatar
Johann-S committed
549
550
551
552
553
      }

      if (!data) {
        data = new Modal(this, _config)
      }
554

Johann-S's avatar
Johann-S committed
555
556
557
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
558
        }
XhmikosR's avatar
XhmikosR committed
559

Johann-S's avatar
Johann-S committed
560
561
562
563
564
        data[config](relatedTarget)
      } else if (_config.show) {
        data.show(relatedTarget)
      }
    })
565
  }
566

567
  static getInstance(element) {
568
569
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
570
571
572
573
574
575
576
}

/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
577

XhmikosR's avatar
XhmikosR committed
578
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
579
  const target = getElementFromSelector(this)
580

Johann-S's avatar
Johann-S committed
581
582
583
  if (this.tagName === 'A' || this.tagName === 'AREA') {
    event.preventDefault()
  }
584

XhmikosR's avatar
XhmikosR committed
585
  EventHandler.one(target, EVENT_SHOW, showEvent => {
586
587
    if (showEvent.defaultPrevented) {
      // only register focus restorer if modal will actually get shown
Johann-S's avatar
Johann-S committed
588
      return
589
590
    }

XhmikosR's avatar
XhmikosR committed
591
    EventHandler.one(target, EVENT_HIDDEN, () => {
592
      if (isVisible(this)) {
Johann-S's avatar
Johann-S committed
593
        this.focus()
594
595
596
597
      }
    })
  })

598
599
  let data = Data.getData(target, DATA_KEY)
  if (!data) {
Johann-S's avatar
Johann-S committed
600
601
602
603
604
    const config = {
      ...Manipulator.getDataAttributes(target),
      ...Manipulator.getDataAttributes(this)
    }

605
606
607
608
    data = new Modal(target, config)
  }

  data.show(this)
Johann-S's avatar
Johann-S committed
609
610
611
612
613
614
})

/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
615
 * add .Modal to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
616
 */
617
618
619
620
621
622
623
624
625
626
627
628

onDOMContentLoaded(() => {
  const $ = getjQuery()
  /* istanbul ignore if */
  if ($) {
    const JQUERY_NO_CONFLICT = $.fn[NAME]
    $.fn[NAME] = Modal.jQueryInterface
    $.fn[NAME].Constructor = Modal
    $.fn[NAME].noConflict = () => {
      $.fn[NAME] = JQUERY_NO_CONFLICT
      return Modal.jQueryInterface
    }
629
  }
630
})
631
632

export default Modal