dropdown.js 15.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)
 * --------------------------------------------------------------------------
 */

Johann-S's avatar
Johann-S committed
8
9
10
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import Manipulator from './dom/manipulator'
11
import Popper from 'popper.js'
Johann-S's avatar
Johann-S committed
12
import SelectorEngine from './dom/selectorEngine'
13
14
import Util from './util'

Johann-S's avatar
Johann-S committed
15
16
17
18
19
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
20

Johann-S's avatar
Johann-S committed
21
const NAME                     = 'dropdown'
XhmikosR's avatar
XhmikosR committed
22
const VERSION                  = '4.3.1'
Johann-S's avatar
Johann-S committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse)
const REGEXP_KEYDOWN           = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`)

const Event = {
  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}`
}

const ClassName = {
46
47
48
49
50
51
52
  DISABLED        : 'disabled',
  SHOW            : 'show',
  DROPUP          : 'dropup',
  DROPRIGHT       : 'dropright',
  DROPLEFT        : 'dropleft',
  MENURIGHT       : 'dropdown-menu-right',
  MENULEFT        : 'dropdown-menu-left',
Johann-S's avatar
Johann-S committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  POSITION_STATIC : 'position-static'
}

const Selector = {
  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)'
}

const AttachmentMap = {
  TOP       : 'top-start',
  TOPEND    : 'top-end',
  BOTTOM    : 'bottom-start',
  BOTTOMEND : 'bottom-end',
  RIGHT     : 'right-start',
  RIGHTEND  : 'right-end',
  LEFT      : 'left-start',
  LEFTEND   : 'left-end'
}

const Default = {
76
77
78
79
80
  offset    : 0,
  flip      : true,
  boundary  : 'scrollParent',
  reference : 'toggle',
  display   : 'dynamic'
Johann-S's avatar
Johann-S committed
81
82
83
}

const DefaultType = {
84
85
86
87
88
  offset    : '(number|string|function)',
  flip      : 'boolean',
  boundary  : '(string|element)',
  reference : '(string|element)',
  display   : 'string'
Johann-S's avatar
Johann-S committed
89
90
91
92
93
94
95
}

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

Johann-S's avatar
Johann-S committed
97
98
99
100
101
102
103
104
105
class Dropdown {
  constructor(element, config) {
    this._element  = element
    this._popper   = null
    this._config   = this._getConfig(config)
    this._menu     = this._getMenuElement()
    this._inNavbar = this._detectNavbar()

    this._addEventListeners()
fat's avatar
fat committed
106
107
  }

Johann-S's avatar
Johann-S committed
108
109
110
111
  // Getters

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

Johann-S's avatar
Johann-S committed
114
115
  static get Default() {
    return Default
Johann-S's avatar
Johann-S committed
116
117
  }

Johann-S's avatar
Johann-S committed
118
119
  static get DefaultType() {
    return DefaultType
Johann-S's avatar
Johann-S committed
120
121
  }

Johann-S's avatar
Johann-S committed
122
123
124
  // Public

  toggle() {
Johann-S's avatar
Johann-S committed
125
    if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED)) {
Johann-S's avatar
Johann-S committed
126
      return
fat's avatar
fat committed
127
128
    }

Johann-S's avatar
Johann-S committed
129
    const parent   = Dropdown._getParentFromElement(this._element)
Johann-S's avatar
Johann-S committed
130
    const isActive = this._menu.classList.contains(ClassName.SHOW)
131

Johann-S's avatar
Johann-S committed
132
    Dropdown._clearMenus()
133

Johann-S's avatar
Johann-S committed
134
135
    if (isActive) {
      return
Johann-S's avatar
Johann-S committed
136
137
    }

Johann-S's avatar
Johann-S committed
138
139
    const relatedTarget = {
      relatedTarget: this._element
Johann-S's avatar
Johann-S committed
140
    }
Johann-S's avatar
Johann-S committed
141
    const showEvent = EventHandler.trigger(parent, Event.SHOW, relatedTarget)
Johann-S's avatar
Johann-S committed
142

Johann-S's avatar
Johann-S committed
143
    if (showEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
144
145
      return
    }
fat's avatar
fat committed
146

Johann-S's avatar
Johann-S committed
147
148
149
150
151
152
153
    // Disable totally Popper.js for Dropdown in Navbar
    if (!this._inNavbar) {
      /**
       * Check for Popper dependency
       * Popper - https://popper.js.org
       */
      if (typeof Popper === 'undefined') {
154
        throw new TypeError('Bootstrap\'s dropdowns require Popper.js (https://popper.js.org/)')
fat's avatar
fat committed
155
156
      }

Johann-S's avatar
Johann-S committed
157
      let referenceElement = this._element
fat's avatar
fat committed
158

Johann-S's avatar
Johann-S committed
159
160
161
162
      if (this._config.reference === 'parent') {
        referenceElement = parent
      } else if (Util.isElement(this._config.reference)) {
        referenceElement = this._config.reference
fat's avatar
fat committed
163

Johann-S's avatar
Johann-S committed
164
165
166
        // Check if it's jQuery element
        if (typeof this._config.reference.jquery !== 'undefined') {
          referenceElement = this._config.reference[0]
167
        }
168
      }
169

Johann-S's avatar
Johann-S committed
170
171
172
173
      // 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
174
        parent.classList.add(ClassName.POSITION_STATIC)
175
      }
Johann-S's avatar
Johann-S committed
176
      this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig())
fat's avatar
fat committed
177
178
    }

Johann-S's avatar
Johann-S committed
179
180
181
182
183
    // 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 &&
Johann-S's avatar
Johann-S committed
184
185
186
      !Util.makeArray(SelectorEngine.closest(parent, Selector.NAVBAR_NAV)).length) {
      Util.makeArray(document.body.children)
        .forEach((elem) => EventHandler.on(elem, 'mouseover', null, Util.noop()))
fat's avatar
fat committed
187
188
    }

Johann-S's avatar
Johann-S committed
189
190
    this._element.focus()
    this._element.setAttribute('aria-expanded', true)
fat's avatar
fat committed
191

Johann-S's avatar
Johann-S committed
192
193
194
    Manipulator.toggleClass(this._menu, ClassName.SHOW)
    Manipulator.toggleClass(parent, ClassName.SHOW)
    EventHandler.trigger(parent, Event.SHOWN, relatedTarget)
Johann-S's avatar
Johann-S committed
195
  }
fat's avatar
fat committed
196

197
  show() {
Johann-S's avatar
Johann-S committed
198
    if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || this._menu.classList.contains(ClassName.SHOW)) {
199
200
201
      return
    }

Johann-S's avatar
Johann-S committed
202
    const parent = Dropdown._getParentFromElement(this._element)
203
204
205
    const relatedTarget = {
      relatedTarget: this._element
    }
206

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

Johann-S's avatar
Johann-S committed
209
    if (showEvent.defaultPrevented) {
210
211
212
      return
    }

Johann-S's avatar
Johann-S committed
213
214
215
    Manipulator.toggleClass(this._menu, ClassName.SHOW)
    Manipulator.toggleClass(parent, ClassName.SHOW)
    EventHandler.trigger(parent, Event.SHOWN, relatedTarget)
216
217
218
  }

  hide() {
Johann-S's avatar
Johann-S committed
219
    if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || !this._menu.classList.contains(ClassName.SHOW)) {
220
221
222
      return
    }

Johann-S's avatar
Johann-S committed
223
    const parent = Dropdown._getParentFromElement(this._element)
224
225
226
    const relatedTarget = {
      relatedTarget: this._element
    }
227

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

Johann-S's avatar
Johann-S committed
230
    if (hideEvent.defaultPrevented) {
231
232
233
      return
    }

Johann-S's avatar
Johann-S committed
234
235
236
    Manipulator.toggleClass(this._menu, ClassName.SHOW)
    Manipulator.toggleClass(parent, ClassName.SHOW)
    EventHandler.trigger(parent, Event.SHOWN, 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
244
245
246
    this._element = null
    this._menu = null
    if (this._popper !== null) {
      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
252
253
254
255
  update() {
    this._inNavbar = this._detectNavbar()
    if (this._popper !== null) {
      this._popper.scheduleUpdate()
    }
  }
Johann-S's avatar
Johann-S committed
256

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

  _addEventListeners() {
Johann-S's avatar
Johann-S 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,
Johann-S's avatar
Johann-S committed
270
      ...Util.getDataAttributes(this._element),
Johann-S's avatar
Johann-S committed
271
      ...config
Johann-S's avatar
Johann-S committed
272
273
    }

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

    return config
  }

  _getMenuElement() {
    if (!this._menu) {
      const parent = Dropdown._getParentFromElement(this._element)
286

Johann-S's avatar
Johann-S committed
287
288
      if (parent) {
        this._menu = parent.querySelector(Selector.MENU)
Johann-S's avatar
Johann-S committed
289
290
      }
    }
Johann-S's avatar
Johann-S committed
291
292
    return this._menu
  }
fat's avatar
fat committed
293

Johann-S's avatar
Johann-S committed
294
  _getPlacement() {
Johann-S's avatar
Johann-S committed
295
296
    const parentDropdown = this._element.parentNode
    let placement        = AttachmentMap.BOTTOM
297

Johann-S's avatar
Johann-S committed
298
    // Handle dropup
Johann-S's avatar
Johann-S committed
299
    if (parentDropdown.classList.contains(ClassName.DROPUP)) {
Johann-S's avatar
Johann-S committed
300
      placement = AttachmentMap.TOP
Johann-S's avatar
Johann-S committed
301
      if (this._menu.classList.contains(ClassName.MENURIGHT)) {
Johann-S's avatar
Johann-S committed
302
        placement = AttachmentMap.TOPEND
303
      }
Johann-S's avatar
Johann-S committed
304
    } else if (parentDropdown.classList.contains(ClassName.DROPRIGHT)) {
Johann-S's avatar
Johann-S committed
305
      placement = AttachmentMap.RIGHT
Johann-S's avatar
Johann-S committed
306
    } else if (parentDropdown.classList.contains(ClassName.DROPLEFT)) {
Johann-S's avatar
Johann-S committed
307
      placement = AttachmentMap.LEFT
Johann-S's avatar
Johann-S committed
308
    } else if (this._menu.classList.contains(ClassName.MENURIGHT)) {
Johann-S's avatar
Johann-S committed
309
      placement = AttachmentMap.BOTTOMEND
310
    }
Johann-S's avatar
Johann-S committed
311
312
    return placement
  }
313

Johann-S's avatar
Johann-S committed
314
  _detectNavbar() {
Johann-S's avatar
Johann-S committed
315
    return Util.makeArray(SelectorEngine.closest(this._element, '.navbar')).length > 0
Johann-S's avatar
Johann-S committed
316
  }
317

318
319
320
  _getOffset() {
    const offset = {}

Johann-S's avatar
Johann-S committed
321
    if (typeof this._config.offset === 'function') {
322
      offset.fn = (data) => {
Johann-S's avatar
Johann-S committed
323
324
        data.offsets = {
          ...data.offsets,
325
          ...this._config.offset(data.offsets, this._element) || {}
326
        }
327

Johann-S's avatar
Johann-S committed
328
        return data
329
      }
Johann-S's avatar
Johann-S committed
330
    } else {
331
      offset.offset = this._config.offset
Johann-S's avatar
Johann-S committed
332
    }
333

334
335
336
337
    return offset
  }

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

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

Johann-S's avatar
Johann-S committed
358
359
    return popperConfig
  }
360

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

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

Johann-S's avatar
Johann-S committed
367
368
369
370
    if (!data) {
      data = new Dropdown(element, _config)
      Data.setData(element, DATA_KEY, data)
    }
fat's avatar
fat committed
371

Johann-S's avatar
Johann-S committed
372
373
374
    if (typeof config === 'string') {
      if (typeof data[config] === 'undefined') {
        throw new Error(`No method named "${config}"`)
Johann-S's avatar
Johann-S committed
375
      }
Johann-S's avatar
Johann-S committed
376
377
378
379
380
381
382
      data[config]()
    }
  }

  static _jQueryInterface(config) {
    return this.each(function () {
      Dropdown._dropdownInterface(this, config)
Johann-S's avatar
Johann-S committed
383
384
385
386
387
388
389
    })
  }

  static _clearMenus(event) {
    if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH ||
      event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
      return
fat's avatar
fat committed
390
391
    }

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

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

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

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
440
441
442
  static _getParentFromElement(element) {
    let parent
    const selector = Util.getSelectorFromElement(element)
fat's avatar
fat committed
443

Johann-S's avatar
Johann-S committed
444
445
    if (selector) {
      parent = document.querySelector(selector)
fat's avatar
fat committed
446
447
    }

Johann-S's avatar
Johann-S committed
448
449
    return parent || element.parentNode
  }
fat's avatar
fat committed
450

Johann-S's avatar
Johann-S committed
451
452
453
454
455
456
457
458
459
460
461
462
  // eslint-disable-next-line complexity
  static _dataApiKeydownHandler(event) {
    // 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
    if (/input|textarea/i.test(event.target.tagName)
      ? event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE &&
      (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE ||
463
        SelectorEngine.closest(event.target, Selector.MENU)) : !REGEXP_KEYDOWN.test(event.which)) {
Johann-S's avatar
Johann-S committed
464
465
      return
    }
fat's avatar
fat committed
466

Johann-S's avatar
Johann-S committed
467
468
    event.preventDefault()
    event.stopPropagation()
fat's avatar
fat committed
469

Johann-S's avatar
Johann-S committed
470
    if (this.disabled || this.classList.contains(ClassName.DISABLED)) {
Johann-S's avatar
Johann-S committed
471
472
      return
    }
fat's avatar
fat committed
473

Johann-S's avatar
Johann-S committed
474
    const parent   = Dropdown._getParentFromElement(this)
Johann-S's avatar
Johann-S committed
475
    const isActive = parent.classList.contains(ClassName.SHOW)
fat's avatar
fat committed
476

477
    if (!isActive || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
Johann-S's avatar
Johann-S committed
478
      if (event.which === ESCAPE_KEYCODE) {
Johann-S's avatar
Johann-S committed
479
        EventHandler.trigger(SelectorEngine.findOne(Selector.DATA_TOGGLE, parent), 'focus')
fat's avatar
fat committed
480
481
      }

Johann-S's avatar
Johann-S committed
482
      EventHandler.trigger(this, 'click')
Johann-S's avatar
Johann-S committed
483
484
      return
    }
fat's avatar
fat committed
485

Johann-S's avatar
Johann-S committed
486
    const items = [].slice.call(parent.querySelectorAll(Selector.VISIBLE_ITEMS))
fat's avatar
fat committed
487

Johann-S's avatar
Johann-S committed
488
    if (!items.length) {
Johann-S's avatar
Johann-S committed
489
490
      return
    }
fat's avatar
fat committed
491

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

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

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

Johann-S's avatar
Johann-S committed
502
503
    if (index < 0) {
      index = 0
fat's avatar
fat committed
504
    }
Johann-S's avatar
Johann-S committed
505
506

    items[index].focus()
fat's avatar
fat committed
507
  }
508
509
510
511

  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
512
}
fat's avatar
fat committed
513

Johann-S's avatar
Johann-S committed
514
515
516
517
518
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
519

Johann-S's avatar
Johann-S committed
520
521
522
523
524
525
526
527
528
529
530
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)
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
  event.preventDefault()
  event.stopPropagation()
  Dropdown._dropdownInterface(this, 'toggle')
})
EventHandler
  .on(document, Event.CLICK_DATA_API, Selector.FORM_CHILD, (e) => e.stopPropagation())
Johann-S's avatar
Johann-S committed
531
532
533
534
535

/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
Johann-S's avatar
Johann-S committed
536
 * add .dropdown to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
537
538
 */

Johann-S's avatar
Johann-S committed
539
540
541
542
543
544
545
546
547
const $ = Util.jQuery
if (typeof $ !== 'undefined') {
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME]               = Dropdown._jQueryInterface
  $.fn[NAME].Constructor   = Dropdown
  $.fn[NAME].noConflict    = () => {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Dropdown._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
548
}
fat's avatar
fat committed
549
550

export default Dropdown