dropdown.js 14.6 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
12
13
14
  isElement,
  makeArray,
  noop,
  typeCheckConfig
15
16
17
18
} from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
19
import Popper from 'popper.js'
20
import SelectorEngine from './dom/selector-engine'
21

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

XhmikosR's avatar
XhmikosR committed
28
29
30
31
32
33
34
35
36
37
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
38
const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse)
XhmikosR's avatar
XhmikosR committed
39
const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`)
Johann-S's avatar
Johann-S committed
40
41

const Event = {
XhmikosR's avatar
XhmikosR committed
42
43
44
45
46
47
48
49
  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
50
51
52
}

const ClassName = {
XhmikosR's avatar
XhmikosR committed
53
54
55
56
57
58
59
  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
60
61
62
}

const Selector = {
XhmikosR's avatar
XhmikosR committed
63
64
65
66
67
  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
68
69
70
}

const AttachmentMap = {
XhmikosR's avatar
XhmikosR committed
71
72
73
74
75
76
77
78
  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
79
80
81
}

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

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

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

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

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

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

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

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

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

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

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

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

140
    Dropdown.clearMenus()
141

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

146
147
148
149
150
151
152
153
154
    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
155
156
    const relatedTarget = {
      relatedTarget: this._element
Johann-S's avatar
Johann-S committed
157
    }
158

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
184
185
186
187
      // 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
188
        parent.classList.add(ClassName.POSITION_STATIC)
189
      }
XhmikosR's avatar
XhmikosR committed
190

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

Johann-S's avatar
Johann-S committed
194
195
196
197
198
    // 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 &&
199
200
      !makeArray(SelectorEngine.closest(parent, Selector.NAVBAR_NAV)).length) {
      makeArray(document.body.children)
XhmikosR's avatar
XhmikosR committed
201
        .forEach(elem => EventHandler.on(elem, 'mouseover', null, noop()))
fat's avatar
fat committed
202
203
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return config
  }

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

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

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

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

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

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

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

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

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

328
329
330
331
    return offset
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

410
411
412
      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
413
          parent.contains(event.target)) {
Johann-S's avatar
Johann-S committed
414
415
        continue
      }
fat's avatar
fat committed
416

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

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

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

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

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

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

445
  static dataApiKeydownHandler(event) {
Johann-S's avatar
Johann-S committed
446
447
448
449
450
451
452
    // 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
453
    if (/input|textarea/i.test(event.target.tagName) ?
454
455
456
      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
457
      !REGEXP_KEYDOWN.test(event.which)) {
Johann-S's avatar
Johann-S committed
458
459
      return
    }
fat's avatar
fat committed
460

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

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

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

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

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

480
    const items = makeArray(SelectorEngine.find(Selector.VISIBLE_ITEMS, parent))
fat's avatar
fat committed
481

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

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

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

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

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

    items[index].focus()
fat's avatar
fat committed
501
  }
502

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

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

514
515
516
517
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
518
519
520
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
  event.preventDefault()
  event.stopPropagation()
521
  Dropdown.dropdownInterface(this, 'toggle')
Johann-S's avatar
Johann-S committed
522
523
})
EventHandler
XhmikosR's avatar
XhmikosR committed
524
  .on(document, Event.CLICK_DATA_API, Selector.FORM_CHILD, e => e.stopPropagation())
Johann-S's avatar
Johann-S committed
525

526
527
const $ = getjQuery()

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

export default Dropdown