modal.js 16.9 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
import {
9
  getjQuery,
10
11
  TRANSITION_END,
  emulateTransitionEnd,
12
  getElementFromSelector,
13
14
15
16
17
  getTransitionDurationFromElement,
  isVisible,
  makeArray,
  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
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
  HIDE: `hide${EVENT_KEY}`,
53
  HIDE_PREVENTED: `hidePrevented${EVENT_KEY}`,
XhmikosR's avatar
XhmikosR committed
54
55
56
57
58
59
60
61
62
63
  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
64
65
66
}

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

const Selector = {
XhmikosR's avatar
XhmikosR committed
77
78
79
80
81
82
  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
83
}
fat's avatar
fat committed
84

Johann-S's avatar
Johann-S committed
85
86
87
88
89
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
90

Johann-S's avatar
Johann-S committed
91
92
class Modal {
  constructor(element, config) {
XhmikosR's avatar
XhmikosR committed
93
94
95
96
97
98
    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
99
    this._ignoreBackdropClick = false
XhmikosR's avatar
XhmikosR committed
100
101
    this._isTransitioning = false
    this._scrollbarWidth = 0
102
    Data.setData(element, DATA_KEY, this)
103
104
  }

Johann-S's avatar
Johann-S committed
105
106
107
108
  // Getters

  static get VERSION() {
    return VERSION
109
110
  }

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

Johann-S's avatar
Johann-S committed
115
  // Public
116

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

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

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

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

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

Johann-S's avatar
Johann-S committed
138
    this._isShown = true
139

Johann-S's avatar
Johann-S committed
140
141
    this._checkScrollbar()
    this._setScrollbar()
142

Johann-S's avatar
Johann-S committed
143
    this._adjustDialog()
144

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

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

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

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

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

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

174
    const hideEvent = EventHandler.trigger(this._element, Event.HIDE)
175

Johann-S's avatar
Johann-S committed
176
    if (hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
177
178
      return
    }
179

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

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

Johann-S's avatar
Johann-S committed
187
188
    this._setEscapeEvent()
    this._setResizeEvent()
189

190
    EventHandler.off(document, Event.FOCUSIN)
191

192
    this._element.classList.remove(ClassName.SHOW)
193

194
195
    EventHandler.off(this._element, Event.CLICK_DISMISS)
    EventHandler.off(this._dialog, Event.MOUSEDOWN_DISMISS)
196

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

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

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

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

218
    Data.removeData(this._element, DATA_KEY)
219

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

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

Johann-S's avatar
Johann-S committed
235
  // Private
fat's avatar
fat committed
236

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

Johann-S's avatar
Johann-S committed
246
  _showElement(relatedTarget) {
247
    const transition = this._element.classList.contains(ClassName.FADE)
248
    const modalBody = SelectorEngine.findOne(Selector.MODAL_BODY, this._dialog)
249

Johann-S's avatar
Johann-S committed
250
251
252
253
    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
254
255
    }

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

260
261
    if (this._dialog.classList.contains(ClassName.SCROLLABLE) && modalBody) {
      modalBody.scrollTop = 0
Shohei Yoshida's avatar
Shohei Yoshida committed
262
263
264
    } else {
      this._element.scrollTop = 0
    }
265

Johann-S's avatar
Johann-S committed
266
    if (transition) {
267
      reflow(this._element)
Johann-S's avatar
Johann-S committed
268
    }
269

270
    this._element.classList.add(ClassName.SHOW)
271

Johann-S's avatar
Johann-S committed
272
273
274
    if (this._config.focus) {
      this._enforceFocus()
    }
275

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

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

Johann-S's avatar
Johann-S committed
287
    if (transition) {
XhmikosR's avatar
XhmikosR committed
288
      const transitionDuration = getTransitionDurationFromElement(this._dialog)
289

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

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

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

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

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

  _removeBackdrop() {
Johann-S's avatar
Johann-S committed
342
343
    this._backdrop.parentNode.removeChild(this._backdrop)
    this._backdrop = null
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

371
        this._triggerBackdropTransition()
372
373
      })

Johann-S's avatar
Johann-S committed
374
      if (animate) {
375
        reflow(this._backdrop)
376
377
      }

378
      this._backdrop.classList.add(ClassName.SHOW)
379

Johann-S's avatar
Johann-S committed
380
381
382
383
      if (!animate) {
        callback()
        return
      }
384

385
      const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)
386

387
388
      EventHandler.one(this._backdrop, TRANSITION_END, callback)
      emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
Johann-S's avatar
Johann-S committed
389
    } else if (!this._isShown && this._backdrop) {
390
      this._backdrop.classList.remove(ClassName.SHOW)
391

Johann-S's avatar
Johann-S committed
392
393
      const callbackRemove = () => {
        this._removeBackdrop()
Johann-S's avatar
Johann-S committed
394
        callback()
Johann-S's avatar
Johann-S committed
395
      }
396

397
      if (this._element.classList.contains(ClassName.FADE)) {
398
399
400
        const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)
        EventHandler.one(this._backdrop, TRANSITION_END, callbackRemove)
        emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
Johann-S's avatar
Johann-S committed
401
402
      } else {
        callbackRemove()
403
      }
Johann-S's avatar
Johann-S committed
404
    } else {
Johann-S's avatar
Johann-S committed
405
      callback()
406
    }
Johann-S's avatar
Johann-S committed
407
  }
408

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  _triggerBackdropTransition() {
    if (this._config.backdrop === 'static') {
      const hideEvent = EventHandler.trigger(this._element, Event.HIDE_PREVENTED)
      if (hideEvent.defaultPrevented) {
        return
      }

      this._element.classList.add(ClassName.STATIC)
      const modalTransitionDuration = getTransitionDurationFromElement(this._element)
      EventHandler.one(this._element, TRANSITION_END, () => {
        this._element.classList.remove(ClassName.STATIC)
      })
      emulateTransitionEnd(this._element, modalTransitionDuration)
      this._element.focus()
    } else {
      this.hide()
    }
  }

Johann-S's avatar
Johann-S committed
428
429
430
  // ----------------------------------------------------------------------
  // the following methods are used to handle overflowing modals
  // ----------------------------------------------------------------------
431

Johann-S's avatar
Johann-S committed
432
433
434
  _adjustDialog() {
    const isModalOverflowing =
      this._element.scrollHeight > document.documentElement.clientHeight
435

Johann-S's avatar
Johann-S committed
436
437
    if (!this._isBodyOverflowing && isModalOverflowing) {
      this._element.style.paddingLeft = `${this._scrollbarWidth}px`
438
439
    }

Johann-S's avatar
Johann-S committed
440
441
    if (this._isBodyOverflowing && !isModalOverflowing) {
      this._element.style.paddingRight = `${this._scrollbarWidth}px`
442
    }
Johann-S's avatar
Johann-S committed
443
  }
444

Johann-S's avatar
Johann-S committed
445
446
447
448
  _resetAdjustments() {
    this._element.style.paddingLeft = ''
    this._element.style.paddingRight = ''
  }
449

Johann-S's avatar
Johann-S committed
450
451
452
453
454
  _checkScrollbar() {
    const rect = document.body.getBoundingClientRect()
    this._isBodyOverflowing = rect.left + rect.right < window.innerWidth
    this._scrollbarWidth = this._getScrollbarWidth()
  }
455

Johann-S's avatar
Johann-S committed
456
457
458
459
460
461
  _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
462
      makeArray(SelectorEngine.find(Selector.FIXED_CONTENT))
XhmikosR's avatar
XhmikosR committed
463
        .forEach(element => {
464
465
466
467
468
          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`
        })
469

Johann-S's avatar
Johann-S committed
470
      // Adjust sticky content margin
471
      makeArray(SelectorEngine.find(Selector.STICKY_CONTENT))
XhmikosR's avatar
XhmikosR committed
472
        .forEach(element => {
473
474
475
476
477
          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`
        })
478

Johann-S's avatar
Johann-S committed
479
480
      // Adjust body padding
      const actualPadding = document.body.style.paddingRight
481
482
483
484
      const calculatedPadding = window.getComputedStyle(document.body)['padding-right']

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

487
    document.body.classList.add(ClassName.OPEN)
Johann-S's avatar
Johann-S committed
488
  }
489

Johann-S's avatar
Johann-S committed
490
491
  _resetScrollbar() {
    // Restore fixed content padding
492
    makeArray(SelectorEngine.find(Selector.FIXED_CONTENT))
XhmikosR's avatar
XhmikosR committed
493
      .forEach(element => {
494
        const padding = Manipulator.getDataAttribute(element, 'padding-right')
495
496
497
498
499
        if (typeof padding !== 'undefined') {
          Manipulator.removeDataAttribute(element, 'padding-right')
          element.style.paddingRight = padding
        }
      })
Johann-S's avatar
Johann-S committed
500

501
    // Restore sticky content and navbar-toggler margin
502
    makeArray(SelectorEngine.find(`${Selector.STICKY_CONTENT}`))
XhmikosR's avatar
XhmikosR committed
503
      .forEach(element => {
504
        const margin = Manipulator.getDataAttribute(element, 'margin-right')
505
506
507
508
509
        if (typeof margin !== 'undefined') {
          Manipulator.removeDataAttribute(element, 'margin-right')
          element.style.marginRight = margin
        }
      })
510

Johann-S's avatar
Johann-S committed
511
    // Restore body padding
512
    const padding = Manipulator.getDataAttribute(document.body, 'padding-right')
XhmikosR's avatar
XhmikosR committed
513
514
515
    if (typeof padding === 'undefined') {
      document.body.style.paddingRight = ''
    } else {
516
517
518
      Manipulator.removeDataAttribute(document.body, 'padding-right')
      document.body.style.paddingRight = padding
    }
Johann-S's avatar
Johann-S committed
519
  }
520

Johann-S's avatar
Johann-S committed
521
522
523
524
525
526
527
528
  _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
  }
529

Johann-S's avatar
Johann-S committed
530
531
  // Static

532
  static jQueryInterface(config, relatedTarget) {
Johann-S's avatar
Johann-S committed
533
    return this.each(function () {
534
      let data = Data.getData(this, DATA_KEY)
Johann-S's avatar
Johann-S committed
535
536
      const _config = {
        ...Default,
537
        ...Manipulator.getDataAttributes(this),
Johann-S's avatar
Johann-S committed
538
539
540
541
542
543
        ...typeof config === 'object' && config ? config : {}
      }

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

Johann-S's avatar
Johann-S committed
545
546
547
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
548
        }
XhmikosR's avatar
XhmikosR committed
549

Johann-S's avatar
Johann-S committed
550
551
552
553
554
        data[config](relatedTarget)
      } else if (_config.show) {
        data.show(relatedTarget)
      }
    })
555
  }
556

557
  static getInstance(element) {
558
559
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
560
561
562
563
564
565
566
}

/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
567

568
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
569
  const target = getElementFromSelector(this)
570

Johann-S's avatar
Johann-S committed
571
572
573
  if (this.tagName === 'A' || this.tagName === 'AREA') {
    event.preventDefault()
  }
574

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

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

588
589
  let data = Data.getData(target, DATA_KEY)
  if (!data) {
Johann-S's avatar
Johann-S committed
590
591
592
593
594
    const config = {
      ...Manipulator.getDataAttributes(target),
      ...Manipulator.getDataAttributes(this)
    }

595
596
597
598
    data = new Modal(target, config)
  }

  data.show(this)
Johann-S's avatar
Johann-S committed
599
600
})

601
602
const $ = getjQuery()

Johann-S's avatar
Johann-S committed
603
604
605
606
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
Johann-S's avatar
Johann-S committed
607
 * add .modal to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
608
 */
Johann-S's avatar
Johann-S committed
609
/* istanbul ignore if */
610
if ($) {
611
  const JQUERY_NO_CONFLICT = $.fn[NAME]
612
  $.fn[NAME] = Modal.jQueryInterface
XhmikosR's avatar
XhmikosR committed
613
614
  $.fn[NAME].Constructor = Modal
  $.fn[NAME].noConflict = () => {
615
    $.fn[NAME] = JQUERY_NO_CONFLICT
616
    return Modal.jQueryInterface
617
  }
Johann-S's avatar
Johann-S committed
618
}
619
620

export default Modal