modal.js 16.5 KB
Newer Older
1
2
/**
 * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
3
 * Bootstrap (v4.3.1): modal.js
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

8
9
10
11
12
13
14
15
16
17
18
import {
  jQuery as $,
  TRANSITION_END,
  emulateTransitionEnd,
  getSelectorFromElement,
  getTransitionDurationFromElement,
  isVisible,
  makeArray,
  reflow,
  typeCheckConfig
} from './util/index'
19
20
21
22
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selectorEngine'
23

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

XhmikosR's avatar
XhmikosR committed
30
31
32
33
34
35
const NAME = 'modal'
const VERSION = '4.3.1'
const DATA_KEY = 'bs.modal'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
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
51
}

const Event = {
XhmikosR's avatar
XhmikosR committed
52
53
54
55
56
57
58
59
60
61
62
  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}`
Johann-S's avatar
Johann-S committed
63
64
65
}

const ClassName = {
XhmikosR's avatar
XhmikosR committed
66
67
68
69
70
71
  SCROLLABLE: 'modal-dialog-scrollable',
  SCROLLBAR_MEASURER: 'modal-scrollbar-measure',
  BACKDROP: 'modal-backdrop',
  OPEN: 'modal-open',
  FADE: 'fade',
  SHOW: 'show'
Johann-S's avatar
Johann-S committed
72
73
74
}

const Selector = {
XhmikosR's avatar
XhmikosR committed
75
76
77
78
79
80
  DIALOG: '.modal-dialog',
  MODAL_BODY: '.modal-body',
  DATA_TOGGLE: '[data-toggle="modal"]',
  DATA_DISMISS: '[data-dismiss="modal"]',
  FIXED_CONTENT: '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top',
  STICKY_CONTENT: '.sticky-top'
Johann-S's avatar
Johann-S committed
81
}
fat's avatar
fat committed
82

Johann-S's avatar
Johann-S committed
83
84
85
86
87
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
88

Johann-S's avatar
Johann-S committed
89
90
class Modal {
  constructor(element, config) {
XhmikosR's avatar
XhmikosR committed
91
92
93
94
95
96
    this._config = this._getConfig(config)
    this._element = element
    this._dialog = SelectorEngine.findOne(Selector.DIALOG, element)
    this._backdrop = null
    this._isShown = false
    this._isBodyOverflowing = false
Johann-S's avatar
Johann-S committed
97
    this._ignoreBackdropClick = false
XhmikosR's avatar
XhmikosR committed
98
99
    this._isTransitioning = false
    this._scrollbarWidth = 0
100
    Data.setData(element, DATA_KEY, this)
101
102
  }

Johann-S's avatar
Johann-S committed
103
104
105
106
  // Getters

  static get VERSION() {
    return VERSION
107
108
  }

Johann-S's avatar
Johann-S committed
109
110
111
  static get Default() {
    return Default
  }
112

Johann-S's avatar
Johann-S committed
113
  // Public
114

Johann-S's avatar
Johann-S committed
115
116
117
  toggle(relatedTarget) {
    return this._isShown ? this.hide() : this.show(relatedTarget)
  }
118

Johann-S's avatar
Johann-S committed
119
  show(relatedTarget) {
120
    if (this._isShown || this._isTransitioning) {
Johann-S's avatar
Johann-S committed
121
      return
122
123
    }

124
    if (this._element.classList.contains(ClassName.FADE)) {
Johann-S's avatar
Johann-S committed
125
      this._isTransitioning = true
126
127
    }

128
    const showEvent = EventHandler.trigger(this._element, Event.SHOW, {
Johann-S's avatar
Johann-S committed
129
130
      relatedTarget
    })
131

Johann-S's avatar
Johann-S committed
132
    if (this._isShown || showEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
133
134
      return
    }
135

Johann-S's avatar
Johann-S committed
136
    this._isShown = true
137

Johann-S's avatar
Johann-S committed
138
139
    this._checkScrollbar()
    this._setScrollbar()
140

Johann-S's avatar
Johann-S committed
141
    this._adjustDialog()
142

Johann-S's avatar
Johann-S committed
143
144
    this._setEscapeEvent()
    this._setResizeEvent()
David Bailey's avatar
David Bailey committed
145

146
    EventHandler.on(this._element,
Johann-S's avatar
Johann-S committed
147
148
      Event.CLICK_DISMISS,
      Selector.DATA_DISMISS,
XhmikosR's avatar
XhmikosR committed
149
      event => this.hide(event)
Johann-S's avatar
Johann-S committed
150
    )
151

152
    EventHandler.on(this._dialog, Event.MOUSEDOWN_DISMISS, () => {
XhmikosR's avatar
XhmikosR committed
153
      EventHandler.one(this._element, Event.MOUSEUP_DISMISS, event => {
154
        if (event.target === this._element) {
Johann-S's avatar
Johann-S committed
155
156
157
158
          this._ignoreBackdropClick = true
        }
      })
    })
159

Johann-S's avatar
Johann-S committed
160
161
    this._showBackdrop(() => this._showElement(relatedTarget))
  }
162

Johann-S's avatar
Johann-S committed
163
164
165
166
  hide(event) {
    if (event) {
      event.preventDefault()
    }
167

168
    if (!this._isShown || this._isTransitioning) {
Johann-S's avatar
Johann-S committed
169
      return
170
171
    }

172
    const hideEvent = EventHandler.trigger(this._element, Event.HIDE)
173

Johann-S's avatar
Johann-S committed
174
    if (!this._isShown || hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
175
176
      return
    }
177

Johann-S's avatar
Johann-S committed
178
    this._isShown = false
179
    const transition = this._element.classList.contains(ClassName.FADE)
180

Johann-S's avatar
Johann-S committed
181
182
183
    if (transition) {
      this._isTransitioning = true
    }
184

Johann-S's avatar
Johann-S committed
185
186
    this._setEscapeEvent()
    this._setResizeEvent()
187

188
    EventHandler.off(document, Event.FOCUSIN)
189

190
    this._element.classList.remove(ClassName.SHOW)
191

192
193
    EventHandler.off(this._element, Event.CLICK_DISMISS)
    EventHandler.off(this._dialog, Event.MOUSEDOWN_DISMISS)
194

Johann-S's avatar
Johann-S committed
195
    if (transition) {
196
      const transitionDuration = getTransitionDurationFromElement(this._element)
197

XhmikosR's avatar
XhmikosR committed
198
      EventHandler.one(this._element, TRANSITION_END, event => this._hideModal(event))
199
      emulateTransitionEnd(this._element, transitionDuration)
Johann-S's avatar
Johann-S committed
200
201
202
203
    } else {
      this._hideModal()
    }
  }
204

Johann-S's avatar
Johann-S committed
205
  dispose() {
206
    [window, this._element, this._dialog]
XhmikosR's avatar
XhmikosR committed
207
      .forEach(htmlElement => EventHandler.off(htmlElement, EVENT_KEY))
208
209
210
211
212
213

    /**
     * `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
     */
214
    EventHandler.off(document, Event.FOCUSIN)
215

216
    Data.removeData(this._element, DATA_KEY)
217

XhmikosR's avatar
XhmikosR committed
218
219
220
221
222
223
    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
224
    this._ignoreBackdropClick = null
XhmikosR's avatar
XhmikosR committed
225
226
    this._isTransitioning = null
    this._scrollbarWidth = null
Johann-S's avatar
Johann-S committed
227
  }
fat's avatar
fat committed
228

Johann-S's avatar
Johann-S committed
229
230
231
  handleUpdate() {
    this._adjustDialog()
  }
fat's avatar
fat committed
232

Johann-S's avatar
Johann-S committed
233
  // Private
fat's avatar
fat committed
234

Johann-S's avatar
Johann-S committed
235
236
237
238
  _getConfig(config) {
    config = {
      ...Default,
      ...config
239
    }
240
    typeCheckConfig(NAME, config, DefaultType)
Johann-S's avatar
Johann-S committed
241
242
    return config
  }
243

Johann-S's avatar
Johann-S committed
244
  _showElement(relatedTarget) {
245
    const transition = this._element.classList.contains(ClassName.FADE)
246

Johann-S's avatar
Johann-S committed
247
248
249
250
    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
251
252
    }

Johann-S's avatar
Johann-S committed
253
254
    this._element.style.display = 'block'
    this._element.removeAttribute('aria-hidden')
255
    this._element.setAttribute('aria-modal', true)
Shohei Yoshida's avatar
Shohei Yoshida committed
256

257
    if (this._dialog.classList.contains(ClassName.SCROLLABLE)) {
258
      SelectorEngine.findOne(Selector.MODAL_BODY, this._dialog).scrollTop = 0
Shohei Yoshida's avatar
Shohei Yoshida committed
259
260
261
    } else {
      this._element.scrollTop = 0
    }
262

Johann-S's avatar
Johann-S committed
263
    if (transition) {
264
      reflow(this._element)
Johann-S's avatar
Johann-S committed
265
    }
266

267
    this._element.classList.add(ClassName.SHOW)
268

Johann-S's avatar
Johann-S committed
269
270
271
    if (this._config.focus) {
      this._enforceFocus()
    }
272

Johann-S's avatar
Johann-S committed
273
    const transitionComplete = () => {
Jacob Thornton's avatar
Jacob Thornton committed
274
      if (this._config.focus) {
Johann-S's avatar
Johann-S committed
275
        this._element.focus()
Jacob Thornton's avatar
Jacob Thornton committed
276
      }
XhmikosR's avatar
XhmikosR committed
277

Johann-S's avatar
Johann-S committed
278
      this._isTransitioning = false
279
280
281
      EventHandler.trigger(this._element, Event.SHOWN, {
        relatedTarget
      })
Johann-S's avatar
Johann-S committed
282
    }
283

Johann-S's avatar
Johann-S committed
284
    if (transition) {
XhmikosR's avatar
XhmikosR committed
285
      const transitionDuration = getTransitionDurationFromElement(this._dialog)
286

287
288
      EventHandler.one(this._dialog, TRANSITION_END, transitionComplete)
      emulateTransitionEnd(this._dialog, transitionDuration)
Johann-S's avatar
Johann-S committed
289
290
291
292
293
294
    } else {
      transitionComplete()
    }
  }

  _enforceFocus() {
Johann-S's avatar
Johann-S committed
295
    EventHandler.off(document, Event.FOCUSIN) // guard against infinite focus loop
XhmikosR's avatar
XhmikosR committed
296
    EventHandler.on(document, Event.FOCUSIN, event => {
Johann-S's avatar
Johann-S committed
297
298
299
300
301
302
      if (document !== event.target &&
          this._element !== event.target &&
          !this._element.contains(event.target)) {
        this._element.focus()
      }
    })
Johann-S's avatar
Johann-S committed
303
  }
304

Johann-S's avatar
Johann-S committed
305
306
  _setEscapeEvent() {
    if (this._isShown && this._config.keyboard) {
XhmikosR's avatar
XhmikosR committed
307
      EventHandler.on(this._element, Event.KEYDOWN_DISMISS, event => {
Johann-S's avatar
Johann-S committed
308
309
310
311
312
313
        if (event.which === ESCAPE_KEYCODE) {
          event.preventDefault()
          this.hide()
        }
      })
    } else if (!this._isShown) {
314
      EventHandler.off(this._element, Event.KEYDOWN_DISMISS)
315
    }
Johann-S's avatar
Johann-S committed
316
  }
317

Johann-S's avatar
Johann-S committed
318
319
  _setResizeEvent() {
    if (this._isShown) {
XhmikosR's avatar
XhmikosR committed
320
      EventHandler.on(window, Event.RESIZE, event => this.handleUpdate(event))
Johann-S's avatar
Johann-S committed
321
    } else {
322
      EventHandler.off(window, Event.RESIZE)
323
    }
Johann-S's avatar
Johann-S committed
324
  }
325

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

  _removeBackdrop() {
    if (this._backdrop) {
341
      this._backdrop.parentNode.removeChild(this._backdrop)
Johann-S's avatar
Johann-S committed
342
      this._backdrop = null
343
    }
Johann-S's avatar
Johann-S committed
344
  }
345

Johann-S's avatar
Johann-S committed
346
  _showBackdrop(callback) {
XhmikosR's avatar
XhmikosR committed
347
348
349
    const animate = this._element.classList.contains(ClassName.FADE) ?
      ClassName.FADE :
      ''
Johann-S's avatar
Johann-S committed
350
351
352
353
354
355
356

    if (this._isShown && this._config.backdrop) {
      this._backdrop = document.createElement('div')
      this._backdrop.className = ClassName.BACKDROP

      if (animate) {
        this._backdrop.classList.add(animate)
357
358
      }

359
      document.body.appendChild(this._backdrop)
Johann-S's avatar
Johann-S committed
360

XhmikosR's avatar
XhmikosR committed
361
      EventHandler.on(this._element, Event.CLICK_DISMISS, event => {
Johann-S's avatar
Johann-S committed
362
363
364
365
        if (this._ignoreBackdropClick) {
          this._ignoreBackdropClick = false
          return
        }
XhmikosR's avatar
XhmikosR committed
366

Johann-S's avatar
Johann-S committed
367
368
369
        if (event.target !== event.currentTarget) {
          return
        }
XhmikosR's avatar
XhmikosR committed
370

Johann-S's avatar
Johann-S committed
371
372
373
374
375
        if (this._config.backdrop === 'static') {
          this._element.focus()
        } else {
          this.hide()
        }
376
377
      })

Johann-S's avatar
Johann-S committed
378
      if (animate) {
379
        reflow(this._backdrop)
380
381
      }

382
      this._backdrop.classList.add(ClassName.SHOW)
383

Johann-S's avatar
Johann-S committed
384
385
386
      if (!callback) {
        return
      }
387

Johann-S's avatar
Johann-S committed
388
389
390
391
      if (!animate) {
        callback()
        return
      }
392

393
      const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)
394

395
396
      EventHandler.one(this._backdrop, TRANSITION_END, callback)
      emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
Johann-S's avatar
Johann-S committed
397
    } else if (!this._isShown && this._backdrop) {
398
      this._backdrop.classList.remove(ClassName.SHOW)
399

Johann-S's avatar
Johann-S committed
400
401
402
      const callbackRemove = () => {
        this._removeBackdrop()
        if (callback) {
403
404
          callback()
        }
Johann-S's avatar
Johann-S committed
405
      }
406

407
      if (this._element.classList.contains(ClassName.FADE)) {
408
409
410
        const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)
        EventHandler.one(this._backdrop, TRANSITION_END, callbackRemove)
        emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
Johann-S's avatar
Johann-S committed
411
412
      } else {
        callbackRemove()
413
      }
Johann-S's avatar
Johann-S committed
414
415
    } else if (callback) {
      callback()
416
    }
Johann-S's avatar
Johann-S committed
417
  }
418

Johann-S's avatar
Johann-S committed
419
420
421
  // ----------------------------------------------------------------------
  // the following methods are used to handle overflowing modals
  // ----------------------------------------------------------------------
422

Johann-S's avatar
Johann-S committed
423
424
425
  _adjustDialog() {
    const isModalOverflowing =
      this._element.scrollHeight > document.documentElement.clientHeight
426

Johann-S's avatar
Johann-S committed
427
428
    if (!this._isBodyOverflowing && isModalOverflowing) {
      this._element.style.paddingLeft = `${this._scrollbarWidth}px`
429
430
    }

Johann-S's avatar
Johann-S committed
431
432
    if (this._isBodyOverflowing && !isModalOverflowing) {
      this._element.style.paddingRight = `${this._scrollbarWidth}px`
433
    }
Johann-S's avatar
Johann-S committed
434
  }
435

Johann-S's avatar
Johann-S committed
436
437
438
439
  _resetAdjustments() {
    this._element.style.paddingLeft = ''
    this._element.style.paddingRight = ''
  }
440

Johann-S's avatar
Johann-S committed
441
442
443
444
445
  _checkScrollbar() {
    const rect = document.body.getBoundingClientRect()
    this._isBodyOverflowing = rect.left + rect.right < window.innerWidth
    this._scrollbarWidth = this._getScrollbarWidth()
  }
446

Johann-S's avatar
Johann-S committed
447
448
449
450
451
452
  _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
453
      makeArray(SelectorEngine.find(Selector.FIXED_CONTENT))
XhmikosR's avatar
XhmikosR committed
454
        .forEach(element => {
455
456
457
458
459
          const actualPadding = element.style.paddingRight
          const calculatedPadding = window.getComputedStyle(element)['padding-right']
          Manipulator.setDataAttribute(element, 'padding-right', actualPadding)
          element.style.paddingRight = `${parseFloat(calculatedPadding) + this._scrollbarWidth}px`
        })
460

Johann-S's avatar
Johann-S committed
461
      // Adjust sticky content margin
462
      makeArray(SelectorEngine.find(Selector.STICKY_CONTENT))
XhmikosR's avatar
XhmikosR committed
463
        .forEach(element => {
464
465
466
467
468
          const actualMargin = element.style.marginRight
          const calculatedMargin = window.getComputedStyle(element)['margin-right']
          Manipulator.setDataAttribute(element, 'margin-right', actualMargin)
          element.style.marginRight = `${parseFloat(calculatedMargin) - this._scrollbarWidth}px`
        })
469

Johann-S's avatar
Johann-S committed
470
471
      // Adjust body padding
      const actualPadding = document.body.style.paddingRight
472
473
474
475
      const calculatedPadding = window.getComputedStyle(document.body)['padding-right']

      Manipulator.setDataAttribute(document.body, 'padding-right', actualPadding)
      document.body.style.paddingRight = `${parseFloat(calculatedPadding) + this._scrollbarWidth}px`
476
    }
477

478
    document.body.classList.add(ClassName.OPEN)
Johann-S's avatar
Johann-S committed
479
  }
480

Johann-S's avatar
Johann-S committed
481
482
  _resetScrollbar() {
    // Restore fixed content padding
483
    makeArray(SelectorEngine.find(Selector.FIXED_CONTENT))
XhmikosR's avatar
XhmikosR committed
484
      .forEach(element => {
485
        const padding = Manipulator.getDataAttribute(element, 'padding-right')
486
487
488
489
490
        if (typeof padding !== 'undefined') {
          Manipulator.removeDataAttribute(element, 'padding-right')
          element.style.paddingRight = padding
        }
      })
Johann-S's avatar
Johann-S committed
491

492
    // Restore sticky content and navbar-toggler margin
493
    makeArray(SelectorEngine.find(`${Selector.STICKY_CONTENT}`))
XhmikosR's avatar
XhmikosR committed
494
      .forEach(element => {
495
        const margin = Manipulator.getDataAttribute(element, 'margin-right')
496
497
498
499
500
        if (typeof margin !== 'undefined') {
          Manipulator.removeDataAttribute(element, 'margin-right')
          element.style.marginRight = margin
        }
      })
501

Johann-S's avatar
Johann-S committed
502
    // Restore body padding
503
    const padding = Manipulator.getDataAttribute(document.body, 'padding-right')
XhmikosR's avatar
XhmikosR committed
504
505
506
    if (typeof padding === 'undefined') {
      document.body.style.paddingRight = ''
    } else {
507
508
509
      Manipulator.removeDataAttribute(document.body, 'padding-right')
      document.body.style.paddingRight = padding
    }
Johann-S's avatar
Johann-S committed
510
  }
511

Johann-S's avatar
Johann-S committed
512
513
514
515
516
517
518
519
  _getScrollbarWidth() { // thx d.walsh
    const scrollDiv = document.createElement('div')
    scrollDiv.className = ClassName.SCROLLBAR_MEASURER
    document.body.appendChild(scrollDiv)
    const scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth
    document.body.removeChild(scrollDiv)
    return scrollbarWidth
  }
520

Johann-S's avatar
Johann-S committed
521
522
523
524
  // Static

  static _jQueryInterface(config, relatedTarget) {
    return this.each(function () {
525
      let data = Data.getData(this, DATA_KEY)
Johann-S's avatar
Johann-S committed
526
527
      const _config = {
        ...Default,
528
        ...Manipulator.getDataAttributes(this),
Johann-S's avatar
Johann-S committed
529
530
531
532
533
534
        ...typeof config === 'object' && config ? config : {}
      }

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

Johann-S's avatar
Johann-S committed
536
537
538
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
539
        }
XhmikosR's avatar
XhmikosR committed
540

Johann-S's avatar
Johann-S committed
541
542
543
544
545
        data[config](relatedTarget)
      } else if (_config.show) {
        data.show(relatedTarget)
      }
    })
546
  }
547
548
549
550

  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
551
552
553
554
555
556
557
}

/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
558

559
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
Johann-S's avatar
Johann-S committed
560
  let target
561
  const selector = getSelectorFromElement(this)
562

Johann-S's avatar
Johann-S committed
563
  if (selector) {
564
    target = SelectorEngine.findOne(selector)
Johann-S's avatar
Johann-S committed
565
  }
566

XhmikosR's avatar
XhmikosR committed
567
568
569
  const config = Data.getData(target, DATA_KEY) ?
    'toggle' :
    {
570
571
      ...Manipulator.getDataAttributes(target),
      ...Manipulator.getDataAttributes(this)
572
573
    }

Johann-S's avatar
Johann-S committed
574
575
576
  if (this.tagName === 'A' || this.tagName === 'AREA') {
    event.preventDefault()
  }
577

XhmikosR's avatar
XhmikosR committed
578
  EventHandler.one(target, Event.SHOW, showEvent => {
579
580
    if (showEvent.defaultPrevented) {
      // only register focus restorer if modal will actually get shown
Johann-S's avatar
Johann-S committed
581
      return
582
583
    }

584
    EventHandler.one(target, Event.HIDDEN, () => {
585
      if (isVisible(this)) {
Johann-S's avatar
Johann-S committed
586
        this.focus()
587
588
589
590
      }
    })
  })

591
592
593
594
595
596
  let data = Data.getData(target, DATA_KEY)
  if (!data) {
    data = new Modal(target, config)
  }

  data.show(this)
Johann-S's avatar
Johann-S committed
597
598
599
600
601
602
603
})

/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
 */
604

605
606
if (typeof $ !== 'undefined') {
  const JQUERY_NO_CONFLICT = $.fn[NAME]
XhmikosR's avatar
XhmikosR committed
607
608
609
  $.fn[NAME] = Modal._jQueryInterface
  $.fn[NAME].Constructor = Modal
  $.fn[NAME].noConflict = () => {
610
611
612
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Modal._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
613
}
614
615

export default Modal