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
  DISABLED: 'disabled',
  SHOW: 'show',
  DROPUP: 'dropup',
  DROPRIGHT: 'dropright',
  DROPLEFT: 'dropleft',
  MENURIGHT: 'dropdown-menu-right',
60
  NAVBAR: 'navbar',
XhmikosR's avatar
XhmikosR committed
61
  POSITION_STATIC: 'position-static'
Johann-S's avatar
Johann-S committed
62
63
64
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

142
    Dropdown.clearMenus()
143

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return config
  }

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

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

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

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

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

Johann-S's avatar
Johann-S committed
310
  _detectNavbar() {
311
    return Boolean(SelectorEngine.closest(this._element, `.${ClassName.NAVBAR}`))
Johann-S's avatar
Johann-S committed
312
  }
313

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

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

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

330
331
332
333
    return offset
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

529
530
const $ = getjQuery()

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

export default Dropdown