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

8
import {
9
  getjQuery,
10
  getElementFromSelector,
11
  isElement,
12
  isVisible,
13
14
15
  makeArray,
  noop,
  typeCheckConfig
16
17
18
19
} from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
20
import Popper from 'popper.js'
21
import SelectorEngine from './dom/selector-engine'
22

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

XhmikosR's avatar
XhmikosR committed
29
30
31
32
33
34
35
36
37
38
const NAME = 'dropdown'
const VERSION = '4.3.1'
const DATA_KEY = 'bs.dropdown'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
const SPACE_KEYCODE = 32 // KeyboardEvent.which value for space key
const TAB_KEYCODE = 9 // KeyboardEvent.which value for tab key
const ARROW_UP_KEYCODE = 38 // KeyboardEvent.which value for up arrow key
const ARROW_DOWN_KEYCODE = 40 // KeyboardEvent.which value for down arrow key
Johann-S's avatar
Johann-S committed
39
const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse)
XhmikosR's avatar
XhmikosR committed
40
const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`)
Johann-S's avatar
Johann-S committed
41
42

const Event = {
XhmikosR's avatar
XhmikosR committed
43
44
45
46
47
48
49
50
  HIDE: `hide${EVENT_KEY}`,
  HIDDEN: `hidden${EVENT_KEY}`,
  SHOW: `show${EVENT_KEY}`,
  SHOWN: `shown${EVENT_KEY}`,
  CLICK: `click${EVENT_KEY}`,
  CLICK_DATA_API: `click${EVENT_KEY}${DATA_API_KEY}`,
  KEYDOWN_DATA_API: `keydown${EVENT_KEY}${DATA_API_KEY}`,
  KEYUP_DATA_API: `keyup${EVENT_KEY}${DATA_API_KEY}`
Johann-S's avatar
Johann-S committed
51
52
53
}

const ClassName = {
XhmikosR's avatar
XhmikosR committed
54
55
56
57
58
59
60
  DISABLED: 'disabled',
  SHOW: 'show',
  DROPUP: 'dropup',
  DROPRIGHT: 'dropright',
  DROPLEFT: 'dropleft',
  MENURIGHT: 'dropdown-menu-right',
  POSITION_STATIC: 'position-static'
Johann-S's avatar
Johann-S committed
61
62
63
}

const Selector = {
XhmikosR's avatar
XhmikosR committed
64
65
66
67
68
  DATA_TOGGLE: '[data-toggle="dropdown"]',
  FORM_CHILD: '.dropdown form',
  MENU: '.dropdown-menu',
  NAVBAR_NAV: '.navbar-nav',
  VISIBLE_ITEMS: '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)'
Johann-S's avatar
Johann-S committed
69
70
71
}

const AttachmentMap = {
XhmikosR's avatar
XhmikosR committed
72
73
74
75
76
77
78
79
  TOP: 'top-start',
  TOPEND: 'top-end',
  BOTTOM: 'bottom-start',
  BOTTOMEND: 'bottom-end',
  RIGHT: 'right-start',
  RIGHTEND: 'right-end',
  LEFT: 'left-start',
  LEFTEND: 'left-end'
Johann-S's avatar
Johann-S committed
80
81
82
}

const Default = {
XhmikosR's avatar
XhmikosR committed
83
84
85
86
  offset: 0,
  flip: true,
  boundary: 'scrollParent',
  reference: 'toggle',
87
88
  display: 'dynamic',
  popperConfig: null
Johann-S's avatar
Johann-S committed
89
90
91
}

const DefaultType = {
XhmikosR's avatar
XhmikosR committed
92
93
94
95
  offset: '(number|string|function)',
  flip: 'boolean',
  boundary: '(string|element)',
  reference: '(string|element)',
96
97
  display: 'string',
  popperConfig: '(null|object)'
Johann-S's avatar
Johann-S committed
98
99
100
101
102
103
104
}

/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
105

Johann-S's avatar
Johann-S committed
106
107
class Dropdown {
  constructor(element, config) {
XhmikosR's avatar
XhmikosR committed
108
109
110
111
    this._element = element
    this._popper = null
    this._config = this._getConfig(config)
    this._menu = this._getMenuElement()
Johann-S's avatar
Johann-S committed
112
113
114
    this._inNavbar = this._detectNavbar()

    this._addEventListeners()
115
    Data.setData(element, DATA_KEY, this)
fat's avatar
fat committed
116
117
  }

Johann-S's avatar
Johann-S committed
118
119
120
121
  // Getters

  static get VERSION() {
    return VERSION
Johann-S's avatar
Johann-S committed
122
123
  }

Johann-S's avatar
Johann-S committed
124
125
  static get Default() {
    return Default
Johann-S's avatar
Johann-S committed
126
127
  }

Johann-S's avatar
Johann-S committed
128
129
  static get DefaultType() {
    return DefaultType
Johann-S's avatar
Johann-S committed
130
131
  }

Johann-S's avatar
Johann-S committed
132
133
134
  // Public

  toggle() {
Johann-S's avatar
Johann-S committed
135
    if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED)) {
Johann-S's avatar
Johann-S committed
136
      return
fat's avatar
fat committed
137
138
    }

Johann-S's avatar
Johann-S committed
139
    const isActive = this._menu.classList.contains(ClassName.SHOW)
140

141
    Dropdown.clearMenus()
142

Johann-S's avatar
Johann-S committed
143
144
    if (isActive) {
      return
Johann-S's avatar
Johann-S committed
145
146
    }

147
148
149
150
151
152
153
154
155
    this.show()
  }

  show() {
    if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || this._menu.classList.contains(ClassName.SHOW)) {
      return
    }

    const parent = Dropdown.getParentFromElement(this._element)
Johann-S's avatar
Johann-S committed
156
157
    const relatedTarget = {
      relatedTarget: this._element
Johann-S's avatar
Johann-S committed
158
    }
159

Johann-S's avatar
Johann-S committed
160
    const showEvent = EventHandler.trigger(parent, Event.SHOW, relatedTarget)
Johann-S's avatar
Johann-S committed
161

Johann-S's avatar
Johann-S committed
162
    if (showEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
163
164
      return
    }
fat's avatar
fat committed
165

Johann-S's avatar
Johann-S committed
166
167
168
    // Disable totally Popper.js for Dropdown in Navbar
    if (!this._inNavbar) {
      if (typeof Popper === 'undefined') {
169
        throw new TypeError('Bootstrap\'s dropdowns require Popper.js (https://popper.js.org)')
fat's avatar
fat committed
170
171
      }

Johann-S's avatar
Johann-S committed
172
      let referenceElement = this._element
fat's avatar
fat committed
173

Johann-S's avatar
Johann-S committed
174
175
      if (this._config.reference === 'parent') {
        referenceElement = parent
176
      } else if (isElement(this._config.reference)) {
Johann-S's avatar
Johann-S committed
177
        referenceElement = this._config.reference
fat's avatar
fat committed
178

Johann-S's avatar
Johann-S committed
179
180
181
        // Check if it's jQuery element
        if (typeof this._config.reference.jquery !== 'undefined') {
          referenceElement = this._config.reference[0]
182
        }
183
      }
184

Johann-S's avatar
Johann-S committed
185
186
187
188
      // If boundary is not `scrollParent`, then set position to `static`
      // to allow the menu to "escape" the scroll parent's boundaries
      // https://github.com/twbs/bootstrap/issues/24251
      if (this._config.boundary !== 'scrollParent') {
Johann-S's avatar
Johann-S committed
189
        parent.classList.add(ClassName.POSITION_STATIC)
190
      }
XhmikosR's avatar
XhmikosR committed
191

Johann-S's avatar
Johann-S committed
192
      this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig())
fat's avatar
fat committed
193
194
    }

Johann-S's avatar
Johann-S committed
195
196
197
198
199
    // If this is a touch-enabled device we add extra
    // empty mouseover listeners to the body's immediate children;
    // only needed because of broken event delegation on iOS
    // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
    if ('ontouchstart' in document.documentElement &&
200
201
      !makeArray(SelectorEngine.closest(parent, Selector.NAVBAR_NAV)).length) {
      makeArray(document.body.children)
XhmikosR's avatar
XhmikosR committed
202
        .forEach(elem => EventHandler.on(elem, 'mouseover', null, noop()))
fat's avatar
fat committed
203
204
    }

Johann-S's avatar
Johann-S committed
205
206
    this._element.focus()
    this._element.setAttribute('aria-expanded', true)
fat's avatar
fat committed
207

Johann-S's avatar
Johann-S committed
208
209
210
    Manipulator.toggleClass(this._menu, ClassName.SHOW)
    Manipulator.toggleClass(parent, ClassName.SHOW)
    EventHandler.trigger(parent, Event.SHOWN, relatedTarget)
Johann-S's avatar
Johann-S committed
211
  }
fat's avatar
fat committed
212

213
  hide() {
Johann-S's avatar
Johann-S committed
214
    if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || !this._menu.classList.contains(ClassName.SHOW)) {
215
216
217
      return
    }

218
    const parent = Dropdown.getParentFromElement(this._element)
219
220
221
    const relatedTarget = {
      relatedTarget: this._element
    }
222

Johann-S's avatar
Johann-S committed
223
    const hideEvent = EventHandler.trigger(parent, Event.HIDE, relatedTarget)
224

Johann-S's avatar
Johann-S committed
225
    if (hideEvent.defaultPrevented) {
226
227
228
      return
    }

229
230
231
232
    if (this._popper) {
      this._popper.destroy()
    }

Johann-S's avatar
Johann-S committed
233
234
    Manipulator.toggleClass(this._menu, ClassName.SHOW)
    Manipulator.toggleClass(parent, ClassName.SHOW)
Johann-S's avatar
Johann-S committed
235
    EventHandler.trigger(parent, Event.HIDDEN, relatedTarget)
236
237
  }

Johann-S's avatar
Johann-S committed
238
  dispose() {
Johann-S's avatar
Johann-S committed
239
240
    Data.removeData(this._element, DATA_KEY)
    EventHandler.off(this._element, EVENT_KEY)
Johann-S's avatar
Johann-S committed
241
242
    this._element = null
    this._menu = null
243
    if (this._popper) {
Johann-S's avatar
Johann-S committed
244
245
      this._popper.destroy()
      this._popper = null
fat's avatar
fat committed
246
    }
Johann-S's avatar
Johann-S committed
247
  }
fat's avatar
fat committed
248

Johann-S's avatar
Johann-S committed
249
250
  update() {
    this._inNavbar = this._detectNavbar()
251
    if (this._popper) {
Johann-S's avatar
Johann-S committed
252
253
254
      this._popper.scheduleUpdate()
    }
  }
Johann-S's avatar
Johann-S committed
255

Johann-S's avatar
Johann-S committed
256
257
258
  // Private

  _addEventListeners() {
XhmikosR's avatar
XhmikosR committed
259
    EventHandler.on(this._element, Event.CLICK, event => {
Johann-S's avatar
Johann-S committed
260
261
262
263
264
      event.preventDefault()
      event.stopPropagation()
      this.toggle()
    })
  }
Johann-S's avatar
Johann-S committed
265

Johann-S's avatar
Johann-S committed
266
267
268
  _getConfig(config) {
    config = {
      ...this.constructor.Default,
269
      ...Manipulator.getDataAttributes(this._element),
Johann-S's avatar
Johann-S committed
270
      ...config
Johann-S's avatar
Johann-S committed
271
272
    }

273
    typeCheckConfig(
Johann-S's avatar
Johann-S committed
274
275
276
277
278
279
280
281
282
      NAME,
      config,
      this.constructor.DefaultType
    )

    return config
  }

  _getMenuElement() {
283
    const parent = Dropdown.getParentFromElement(this._element)
XhmikosR's avatar
XhmikosR committed
284

Johann-S's avatar
Johann-S committed
285
    return SelectorEngine.findOne(Selector.MENU, parent)
Johann-S's avatar
Johann-S committed
286
  }
fat's avatar
fat committed
287

Johann-S's avatar
Johann-S committed
288
  _getPlacement() {
Johann-S's avatar
Johann-S committed
289
    const parentDropdown = this._element.parentNode
XhmikosR's avatar
XhmikosR committed
290
    let placement = AttachmentMap.BOTTOM
291

Johann-S's avatar
Johann-S committed
292
    // Handle dropup
Johann-S's avatar
Johann-S committed
293
    if (parentDropdown.classList.contains(ClassName.DROPUP)) {
Johann-S's avatar
Johann-S committed
294
      placement = AttachmentMap.TOP
Johann-S's avatar
Johann-S committed
295
      if (this._menu.classList.contains(ClassName.MENURIGHT)) {
Johann-S's avatar
Johann-S committed
296
        placement = AttachmentMap.TOPEND
297
      }
Johann-S's avatar
Johann-S committed
298
    } else if (parentDropdown.classList.contains(ClassName.DROPRIGHT)) {
Johann-S's avatar
Johann-S committed
299
      placement = AttachmentMap.RIGHT
Johann-S's avatar
Johann-S committed
300
    } else if (parentDropdown.classList.contains(ClassName.DROPLEFT)) {
Johann-S's avatar
Johann-S committed
301
      placement = AttachmentMap.LEFT
Johann-S's avatar
Johann-S committed
302
    } else if (this._menu.classList.contains(ClassName.MENURIGHT)) {
Johann-S's avatar
Johann-S committed
303
      placement = AttachmentMap.BOTTOMEND
304
    }
XhmikosR's avatar
XhmikosR committed
305

Johann-S's avatar
Johann-S committed
306
307
    return placement
  }
308

Johann-S's avatar
Johann-S committed
309
  _detectNavbar() {
310
    return Boolean(SelectorEngine.closest(this._element, '.navbar'))
Johann-S's avatar
Johann-S committed
311
  }
312

313
314
315
  _getOffset() {
    const offset = {}

Johann-S's avatar
Johann-S committed
316
    if (typeof this._config.offset === 'function') {
XhmikosR's avatar
XhmikosR committed
317
      offset.fn = data => {
Johann-S's avatar
Johann-S committed
318
319
        data.offsets = {
          ...data.offsets,
320
          ...this._config.offset(data.offsets, this._element) || {}
321
        }
322

Johann-S's avatar
Johann-S committed
323
        return data
324
      }
Johann-S's avatar
Johann-S committed
325
    } else {
326
      offset.offset = this._config.offset
Johann-S's avatar
Johann-S committed
327
    }
328

329
330
331
332
    return offset
  }

  _getPopperConfig() {
333
    const popperConfig = {
Johann-S's avatar
Johann-S committed
334
335
      placement: this._getPlacement(),
      modifiers: {
336
        offset: this._getOffset(),
Johann-S's avatar
Johann-S committed
337
338
339
340
341
        flip: {
          enabled: this._config.flip
        },
        preventOverflow: {
          boundariesElement: this._config.boundary
342
343
        }
      }
Johann-S's avatar
Johann-S committed
344
    }
345

Johann-S's avatar
Johann-S committed
346
347
348
349
    // Disable Popper.js if we have a static display
    if (this._config.display === 'static') {
      popperConfig.modifiers.applyStyle = {
        enabled: false
350
      }
351
    }
352

353
354
355
    return {
      ...popperConfig,
      ...this._config.popperConfig
356
    }
Johann-S's avatar
Johann-S committed
357
  }
358

Johann-S's avatar
Johann-S committed
359
  // Static
fat's avatar
fat committed
360

361
  static dropdownInterface(element, config) {
Johann-S's avatar
Johann-S committed
362
363
    let data = Data.getData(element, DATA_KEY)
    const _config = typeof config === 'object' ? config : null
fat's avatar
fat committed
364

Johann-S's avatar
Johann-S committed
365
366
367
    if (!data) {
      data = new Dropdown(element, _config)
    }
fat's avatar
fat committed
368

Johann-S's avatar
Johann-S committed
369
370
    if (typeof config === 'string') {
      if (typeof data[config] === 'undefined') {
XhmikosR's avatar
XhmikosR committed
371
        throw new TypeError(`No method named "${config}"`)
Johann-S's avatar
Johann-S committed
372
      }
XhmikosR's avatar
XhmikosR committed
373

Johann-S's avatar
Johann-S committed
374
375
376
377
      data[config]()
    }
  }

378
  static jQueryInterface(config) {
Johann-S's avatar
Johann-S committed
379
    return this.each(function () {
380
      Dropdown.dropdownInterface(this, config)
Johann-S's avatar
Johann-S committed
381
382
383
    })
  }

384
  static clearMenus(event) {
Johann-S's avatar
Johann-S committed
385
    if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH ||
386
      (event.type === 'keyup' && event.which !== TAB_KEYCODE))) {
Johann-S's avatar
Johann-S committed
387
      return
fat's avatar
fat committed
388
389
    }

390
    const toggles = makeArray(SelectorEngine.find(Selector.DATA_TOGGLE))
Johann-S's avatar
Johann-S committed
391
    for (let i = 0, len = toggles.length; i < len; i++) {
392
      const parent = Dropdown.getParentFromElement(toggles[i])
XhmikosR's avatar
XhmikosR committed
393
      const context = Data.getData(toggles[i], DATA_KEY)
Johann-S's avatar
Johann-S committed
394
395
      const relatedTarget = {
        relatedTarget: toggles[i]
fat's avatar
fat committed
396
397
      }

Johann-S's avatar
Johann-S committed
398
399
400
      if (event && event.type === 'click') {
        relatedTarget.clickEvent = event
      }
401

Johann-S's avatar
Johann-S committed
402
403
404
      if (!context) {
        continue
      }
Johann-S's avatar
Johann-S committed
405

Johann-S's avatar
Johann-S committed
406
      const dropdownMenu = context._menu
Johann-S's avatar
Johann-S committed
407
      if (!parent.classList.contains(ClassName.SHOW)) {
Johann-S's avatar
Johann-S committed
408
409
        continue
      }
fat's avatar
fat committed
410

411
412
413
      if (event && ((event.type === 'click' &&
          /input|textarea/i.test(event.target.tagName)) ||
          (event.type === 'keyup' && event.which === TAB_KEYCODE)) &&
Johann-S's avatar
Johann-S committed
414
          parent.contains(event.target)) {
Johann-S's avatar
Johann-S committed
415
416
        continue
      }
fat's avatar
fat committed
417

Johann-S's avatar
Johann-S committed
418
419
      const hideEvent = EventHandler.trigger(parent, Event.HIDE, relatedTarget)
      if (hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
420
421
        continue
      }
fat's avatar
fat committed
422

Johann-S's avatar
Johann-S committed
423
424
425
      // If this is a touch-enabled device we remove the extra
      // empty mouseover listeners we added for iOS support
      if ('ontouchstart' in document.documentElement) {
426
        makeArray(document.body.children)
XhmikosR's avatar
XhmikosR committed
427
          .forEach(elem => EventHandler.off(elem, 'mouseover', null, noop()))
Johann-S's avatar
Johann-S committed
428
      }
429

Johann-S's avatar
Johann-S committed
430
      toggles[i].setAttribute('aria-expanded', 'false')
fat's avatar
fat committed
431

432
433
434
435
      if (context._popper) {
        context._popper.destroy()
      }

Johann-S's avatar
Johann-S committed
436
437
438
      dropdownMenu.classList.remove(ClassName.SHOW)
      parent.classList.remove(ClassName.SHOW)
      EventHandler.trigger(parent, Event.HIDDEN, relatedTarget)
fat's avatar
fat committed
439
    }
Johann-S's avatar
Johann-S committed
440
  }
fat's avatar
fat committed
441

442
  static getParentFromElement(element) {
443
    return getElementFromSelector(element) || element.parentNode
Johann-S's avatar
Johann-S committed
444
  }
fat's avatar
fat committed
445

446
  static dataApiKeydownHandler(event) {
Johann-S's avatar
Johann-S committed
447
448
449
450
451
452
453
    // If not input/textarea:
    //  - And not a key in REGEXP_KEYDOWN => not a dropdown command
    // If input/textarea:
    //  - If space key => not a dropdown command
    //  - If key is other than escape
    //    - If key is not up or down => not a dropdown command
    //    - If trigger inside the menu => not a dropdown command
XhmikosR's avatar
XhmikosR committed
454
    if (/input|textarea/i.test(event.target.tagName) ?
455
456
457
      event.which === SPACE_KEYCODE || (event.which !== ESCAPE_KEYCODE &&
      ((event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE) ||
        SelectorEngine.closest(event.target, Selector.MENU))) :
XhmikosR's avatar
XhmikosR committed
458
      !REGEXP_KEYDOWN.test(event.which)) {
Johann-S's avatar
Johann-S committed
459
460
      return
    }
fat's avatar
fat committed
461

Johann-S's avatar
Johann-S committed
462
463
    event.preventDefault()
    event.stopPropagation()
fat's avatar
fat committed
464

Johann-S's avatar
Johann-S committed
465
    if (this.disabled || this.classList.contains(ClassName.DISABLED)) {
Johann-S's avatar
Johann-S committed
466
467
      return
    }
fat's avatar
fat committed
468

469
    const parent = Dropdown.getParentFromElement(this)
Johann-S's avatar
Johann-S committed
470
    const isActive = parent.classList.contains(ClassName.SHOW)
fat's avatar
fat committed
471

472
    if (!isActive || (isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE))) {
Johann-S's avatar
Johann-S committed
473
      if (event.which === ESCAPE_KEYCODE) {
474
        SelectorEngine.findOne(Selector.DATA_TOGGLE, parent).focus()
fat's avatar
fat committed
475
476
      }

477
      Dropdown.clearMenus()
Johann-S's avatar
Johann-S committed
478
479
      return
    }
fat's avatar
fat committed
480

481
    const items = makeArray(SelectorEngine.find(Selector.VISIBLE_ITEMS, parent))
482
      .filter(isVisible)
fat's avatar
fat committed
483

Johann-S's avatar
Johann-S committed
484
    if (!items.length) {
Johann-S's avatar
Johann-S committed
485
486
      return
    }
fat's avatar
fat committed
487

Johann-S's avatar
Johann-S committed
488
    let index = items.indexOf(event.target)
Jacob Thornton's avatar
Jacob Thornton committed
489

Johann-S's avatar
Johann-S committed
490
491
492
    if (event.which === ARROW_UP_KEYCODE && index > 0) { // Up
      index--
    }
Jacob Thornton's avatar
Jacob Thornton committed
493

Johann-S's avatar
Johann-S committed
494
495
496
    if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { // Down
      index++
    }
fat's avatar
fat committed
497

Johann-S's avatar
Johann-S committed
498
499
    if (index < 0) {
      index = 0
fat's avatar
fat committed
500
    }
Johann-S's avatar
Johann-S committed
501
502

    items[index].focus()
fat's avatar
fat committed
503
  }
504

505
  static getInstance(element) {
506
507
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
508
}
fat's avatar
fat committed
509

Johann-S's avatar
Johann-S committed
510
511
512
513
514
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
515

516
517
518
519
EventHandler.on(document, Event.KEYDOWN_DATA_API, Selector.DATA_TOGGLE, Dropdown.dataApiKeydownHandler)
EventHandler.on(document, Event.KEYDOWN_DATA_API, Selector.MENU, Dropdown.dataApiKeydownHandler)
EventHandler.on(document, Event.CLICK_DATA_API, Dropdown.clearMenus)
EventHandler.on(document, Event.KEYUP_DATA_API, Dropdown.clearMenus)
Johann-S's avatar
Johann-S committed
520
521
522
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
  event.preventDefault()
  event.stopPropagation()
523
  Dropdown.dropdownInterface(this, 'toggle')
Johann-S's avatar
Johann-S committed
524
525
})
EventHandler
XhmikosR's avatar
XhmikosR committed
526
  .on(document, Event.CLICK_DATA_API, Selector.FORM_CHILD, e => e.stopPropagation())
Johann-S's avatar
Johann-S committed
527

528
529
const $ = getjQuery()

Johann-S's avatar
Johann-S committed
530
531
532
533
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
Johann-S's avatar
Johann-S committed
534
 * add .dropdown to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
535
 */
Johann-S's avatar
Johann-S committed
536
/* istanbul ignore if */
537
if ($) {
Johann-S's avatar
Johann-S committed
538
  const JQUERY_NO_CONFLICT = $.fn[NAME]
539
  $.fn[NAME] = Dropdown.jQueryInterface
XhmikosR's avatar
XhmikosR committed
540
541
  $.fn[NAME].Constructor = Dropdown
  $.fn[NAME].noConflict = () => {
Johann-S's avatar
Johann-S committed
542
    $.fn[NAME] = JQUERY_NO_CONFLICT
543
    return Dropdown.jQueryInterface
Johann-S's avatar
Johann-S committed
544
  }
Johann-S's avatar
Johann-S committed
545
}
fat's avatar
fat committed
546
547

export default Dropdown