dropdown.js 15.1 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
9
10
11
12
13
14
15
import {
  jQuery as $,
  getSelectorFromElement,
  isElement,
  makeArray,
  noop,
  typeCheckConfig
} from './util/index'
Johann-S's avatar
Johann-S committed
16
import Data from './dom/data'
17
import EventHandler from './dom/event-handler'
Johann-S's avatar
Johann-S committed
18
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
86
  offset: 0,
  flip: true,
  boundary: 'scrollParent',
  reference: 'toggle',
  display: 'dynamic'
Johann-S's avatar
Johann-S committed
87
88
89
}

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

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

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

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

Johann-S's avatar
Johann-S committed
115
116
117
118
  // Getters

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

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

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

Johann-S's avatar
Johann-S committed
129
130
131
  // Public

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

XhmikosR's avatar
XhmikosR committed
136
    const parent = Dropdown._getParentFromElement(this._element)
Johann-S's avatar
Johann-S committed
137
    const isActive = this._menu.classList.contains(ClassName.SHOW)
138

Johann-S's avatar
Johann-S committed
139
    Dropdown._clearMenus()
140

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

Johann-S's avatar
Johann-S committed
145
146
    const relatedTarget = {
      relatedTarget: this._element
Johann-S's avatar
Johann-S committed
147
    }
Johann-S's avatar
Johann-S committed
148
    const showEvent = EventHandler.trigger(parent, Event.SHOW, relatedTarget)
Johann-S's avatar
Johann-S committed
149

Johann-S's avatar
Johann-S committed
150
    if (showEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
151
152
      return
    }
fat's avatar
fat committed
153

Johann-S's avatar
Johann-S committed
154
155
156
157
158
159
160
    // Disable totally Popper.js for Dropdown in Navbar
    if (!this._inNavbar) {
      /**
       * Check for Popper dependency
       * Popper - https://popper.js.org
       */
      if (typeof Popper === 'undefined') {
161
        throw new TypeError('Bootstrap\'s dropdowns require Popper.js (https://popper.js.org)')
fat's avatar
fat committed
162
163
      }

Johann-S's avatar
Johann-S committed
164
      let referenceElement = this._element
fat's avatar
fat committed
165

Johann-S's avatar
Johann-S committed
166
167
      if (this._config.reference === 'parent') {
        referenceElement = parent
168
      } else if (isElement(this._config.reference)) {
Johann-S's avatar
Johann-S committed
169
        referenceElement = this._config.reference
fat's avatar
fat committed
170

Johann-S's avatar
Johann-S committed
171
172
173
        // Check if it's jQuery element
        if (typeof this._config.reference.jquery !== 'undefined') {
          referenceElement = this._config.reference[0]
174
        }
175
      }
176

Johann-S's avatar
Johann-S committed
177
178
179
180
      // 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
181
        parent.classList.add(ClassName.POSITION_STATIC)
182
      }
XhmikosR's avatar
XhmikosR committed
183

Johann-S's avatar
Johann-S committed
184
      this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig())
fat's avatar
fat committed
185
186
    }

Johann-S's avatar
Johann-S committed
187
188
189
190
191
    // 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 &&
192
193
      !makeArray(SelectorEngine.closest(parent, Selector.NAVBAR_NAV)).length) {
      makeArray(document.body.children)
XhmikosR's avatar
XhmikosR committed
194
        .forEach(elem => EventHandler.on(elem, 'mouseover', null, noop()))
fat's avatar
fat committed
195
196
    }

Johann-S's avatar
Johann-S committed
197
198
    this._element.focus()
    this._element.setAttribute('aria-expanded', true)
fat's avatar
fat committed
199

Johann-S's avatar
Johann-S committed
200
201
202
    Manipulator.toggleClass(this._menu, ClassName.SHOW)
    Manipulator.toggleClass(parent, ClassName.SHOW)
    EventHandler.trigger(parent, Event.SHOWN, relatedTarget)
Johann-S's avatar
Johann-S committed
203
  }
fat's avatar
fat committed
204

205
  show() {
Johann-S's avatar
Johann-S committed
206
    if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || this._menu.classList.contains(ClassName.SHOW)) {
207
208
209
      return
    }

Johann-S's avatar
Johann-S committed
210
    const parent = Dropdown._getParentFromElement(this._element)
211
212
213
    const relatedTarget = {
      relatedTarget: this._element
    }
214

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

Johann-S's avatar
Johann-S committed
217
    if (showEvent.defaultPrevented) {
218
219
220
      return
    }

Johann-S's avatar
Johann-S committed
221
222
223
    Manipulator.toggleClass(this._menu, ClassName.SHOW)
    Manipulator.toggleClass(parent, ClassName.SHOW)
    EventHandler.trigger(parent, Event.SHOWN, relatedTarget)
224
225
226
  }

  hide() {
Johann-S's avatar
Johann-S committed
227
    if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || !this._menu.classList.contains(ClassName.SHOW)) {
228
229
230
      return
    }

Johann-S's avatar
Johann-S committed
231
    const parent = Dropdown._getParentFromElement(this._element)
232
233
234
    const relatedTarget = {
      relatedTarget: this._element
    }
235

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

Johann-S's avatar
Johann-S committed
238
    if (hideEvent.defaultPrevented) {
239
240
241
      return
    }

Johann-S's avatar
Johann-S committed
242
243
    Manipulator.toggleClass(this._menu, ClassName.SHOW)
    Manipulator.toggleClass(parent, ClassName.SHOW)
Johann-S's avatar
Johann-S committed
244
    EventHandler.trigger(parent, Event.HIDDEN, relatedTarget)
245
246
  }

Johann-S's avatar
Johann-S committed
247
  dispose() {
Johann-S's avatar
Johann-S committed
248
249
    Data.removeData(this._element, DATA_KEY)
    EventHandler.off(this._element, EVENT_KEY)
Johann-S's avatar
Johann-S committed
250
251
252
253
254
    this._element = null
    this._menu = null
    if (this._popper !== null) {
      this._popper.destroy()
      this._popper = null
fat's avatar
fat committed
255
    }
Johann-S's avatar
Johann-S committed
256
  }
fat's avatar
fat committed
257

Johann-S's avatar
Johann-S committed
258
259
260
261
262
263
  update() {
    this._inNavbar = this._detectNavbar()
    if (this._popper !== null) {
      this._popper.scheduleUpdate()
    }
  }
Johann-S's avatar
Johann-S committed
264

Johann-S's avatar
Johann-S committed
265
266
267
  // Private

  _addEventListeners() {
XhmikosR's avatar
XhmikosR committed
268
    EventHandler.on(this._element, Event.CLICK, event => {
Johann-S's avatar
Johann-S committed
269
270
271
272
273
      event.preventDefault()
      event.stopPropagation()
      this.toggle()
    })
  }
Johann-S's avatar
Johann-S committed
274

Johann-S's avatar
Johann-S committed
275
276
277
  _getConfig(config) {
    config = {
      ...this.constructor.Default,
278
      ...Manipulator.getDataAttributes(this._element),
Johann-S's avatar
Johann-S committed
279
      ...config
Johann-S's avatar
Johann-S committed
280
281
    }

282
    typeCheckConfig(
Johann-S's avatar
Johann-S committed
283
284
285
286
287
288
289
290
291
292
293
      NAME,
      config,
      this.constructor.DefaultType
    )

    return config
  }

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

Johann-S's avatar
Johann-S committed
295
      if (parent) {
296
        this._menu = SelectorEngine.findOne(Selector.MENU, parent)
Johann-S's avatar
Johann-S committed
297
298
      }
    }
XhmikosR's avatar
XhmikosR committed
299

Johann-S's avatar
Johann-S committed
300
301
    return this._menu
  }
fat's avatar
fat committed
302

Johann-S's avatar
Johann-S committed
303
  _getPlacement() {
Johann-S's avatar
Johann-S committed
304
    const parentDropdown = this._element.parentNode
XhmikosR's avatar
XhmikosR committed
305
    let placement = AttachmentMap.BOTTOM
306

Johann-S's avatar
Johann-S committed
307
    // Handle dropup
Johann-S's avatar
Johann-S committed
308
    if (parentDropdown.classList.contains(ClassName.DROPUP)) {
Johann-S's avatar
Johann-S committed
309
      placement = AttachmentMap.TOP
Johann-S's avatar
Johann-S committed
310
      if (this._menu.classList.contains(ClassName.MENURIGHT)) {
Johann-S's avatar
Johann-S committed
311
        placement = AttachmentMap.TOPEND
312
      }
Johann-S's avatar
Johann-S committed
313
    } else if (parentDropdown.classList.contains(ClassName.DROPRIGHT)) {
Johann-S's avatar
Johann-S committed
314
      placement = AttachmentMap.RIGHT
Johann-S's avatar
Johann-S committed
315
    } else if (parentDropdown.classList.contains(ClassName.DROPLEFT)) {
Johann-S's avatar
Johann-S committed
316
      placement = AttachmentMap.LEFT
Johann-S's avatar
Johann-S committed
317
    } else if (this._menu.classList.contains(ClassName.MENURIGHT)) {
Johann-S's avatar
Johann-S committed
318
      placement = AttachmentMap.BOTTOMEND
319
    }
XhmikosR's avatar
XhmikosR committed
320

Johann-S's avatar
Johann-S committed
321
322
    return placement
  }
323

Johann-S's avatar
Johann-S committed
324
  _detectNavbar() {
325
    return Boolean(SelectorEngine.closest(this._element, '.navbar'))
Johann-S's avatar
Johann-S committed
326
  }
327

328
329
330
  _getOffset() {
    const offset = {}

Johann-S's avatar
Johann-S committed
331
    if (typeof this._config.offset === 'function') {
XhmikosR's avatar
XhmikosR committed
332
      offset.fn = data => {
Johann-S's avatar
Johann-S committed
333
334
        data.offsets = {
          ...data.offsets,
335
          ...this._config.offset(data.offsets, this._element) || {}
336
        }
337

Johann-S's avatar
Johann-S committed
338
        return data
339
      }
Johann-S's avatar
Johann-S committed
340
    } else {
341
      offset.offset = this._config.offset
Johann-S's avatar
Johann-S committed
342
    }
343

344
345
346
347
    return offset
  }

  _getPopperConfig() {
Johann-S's avatar
Johann-S committed
348
349
350
    const popperConfig = {
      placement: this._getPlacement(),
      modifiers: {
351
        offset: this._getOffset(),
Johann-S's avatar
Johann-S committed
352
353
354
355
356
        flip: {
          enabled: this._config.flip
        },
        preventOverflow: {
          boundariesElement: this._config.boundary
357
358
        }
      }
Johann-S's avatar
Johann-S committed
359
    }
360

Johann-S's avatar
Johann-S committed
361
362
363
364
    // Disable Popper.js if we have a static display
    if (this._config.display === 'static') {
      popperConfig.modifiers.applyStyle = {
        enabled: false
365
      }
366
    }
367

Johann-S's avatar
Johann-S committed
368
369
    return popperConfig
  }
370

Johann-S's avatar
Johann-S committed
371
  // Static
fat's avatar
fat committed
372

Johann-S's avatar
Johann-S committed
373
374
375
  static _dropdownInterface(element, config) {
    let data = Data.getData(element, DATA_KEY)
    const _config = typeof config === 'object' ? config : null
fat's avatar
fat committed
376

Johann-S's avatar
Johann-S committed
377
378
379
    if (!data) {
      data = new Dropdown(element, _config)
    }
fat's avatar
fat committed
380

Johann-S's avatar
Johann-S committed
381
382
    if (typeof config === 'string') {
      if (typeof data[config] === 'undefined') {
XhmikosR's avatar
XhmikosR committed
383
        throw new TypeError(`No method named "${config}"`)
Johann-S's avatar
Johann-S committed
384
      }
XhmikosR's avatar
XhmikosR committed
385

Johann-S's avatar
Johann-S committed
386
387
388
389
390
391
392
      data[config]()
    }
  }

  static _jQueryInterface(config) {
    return this.each(function () {
      Dropdown._dropdownInterface(this, config)
Johann-S's avatar
Johann-S committed
393
394
395
396
397
398
399
    })
  }

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

402
    const toggles = makeArray(SelectorEngine.find(Selector.DATA_TOGGLE))
Johann-S's avatar
Johann-S committed
403
    for (let i = 0, len = toggles.length; i < len; i++) {
XhmikosR's avatar
XhmikosR committed
404
405
      const parent = Dropdown._getParentFromElement(toggles[i])
      const context = Data.getData(toggles[i], DATA_KEY)
Johann-S's avatar
Johann-S committed
406
407
      const relatedTarget = {
        relatedTarget: toggles[i]
fat's avatar
fat committed
408
409
      }

Johann-S's avatar
Johann-S committed
410
411
412
      if (event && event.type === 'click') {
        relatedTarget.clickEvent = event
      }
413

Johann-S's avatar
Johann-S committed
414
415
416
      if (!context) {
        continue
      }
Johann-S's avatar
Johann-S committed
417

Johann-S's avatar
Johann-S committed
418
      const dropdownMenu = context._menu
Johann-S's avatar
Johann-S committed
419
      if (!parent.classList.contains(ClassName.SHOW)) {
Johann-S's avatar
Johann-S committed
420
421
        continue
      }
fat's avatar
fat committed
422

Johann-S's avatar
Johann-S committed
423
      if (event && (event.type === 'click' &&
Johann-S's avatar
Johann-S committed
424
425
426
          /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
427
428
        continue
      }
fat's avatar
fat committed
429

Johann-S's avatar
Johann-S committed
430
431
      const hideEvent = EventHandler.trigger(parent, Event.HIDE, relatedTarget)
      if (hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
432
433
        continue
      }
fat's avatar
fat committed
434

Johann-S's avatar
Johann-S committed
435
436
437
      // If this is a touch-enabled device we remove the extra
      // empty mouseover listeners we added for iOS support
      if ('ontouchstart' in document.documentElement) {
438
        makeArray(document.body.children)
XhmikosR's avatar
XhmikosR committed
439
          .forEach(elem => EventHandler.off(elem, 'mouseover', null, noop()))
Johann-S's avatar
Johann-S committed
440
      }
441

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

Johann-S's avatar
Johann-S committed
444
445
446
      dropdownMenu.classList.remove(ClassName.SHOW)
      parent.classList.remove(ClassName.SHOW)
      EventHandler.trigger(parent, Event.HIDDEN, relatedTarget)
fat's avatar
fat committed
447
    }
Johann-S's avatar
Johann-S committed
448
  }
fat's avatar
fat committed
449

Johann-S's avatar
Johann-S committed
450
451
  static _getParentFromElement(element) {
    let parent
452
    const selector = getSelectorFromElement(element)
fat's avatar
fat committed
453

Johann-S's avatar
Johann-S committed
454
    if (selector) {
455
      parent = SelectorEngine.findOne(selector)
fat's avatar
fat committed
456
457
    }

Johann-S's avatar
Johann-S committed
458
459
    return parent || element.parentNode
  }
fat's avatar
fat committed
460

Johann-S's avatar
Johann-S committed
461
462
463
464
465
466
467
468
  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
XhmikosR's avatar
XhmikosR committed
469
470
    if (/input|textarea/i.test(event.target.tagName) ?
      event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE &&
Johann-S's avatar
Johann-S committed
471
      (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE ||
XhmikosR's avatar
XhmikosR committed
472
473
        SelectorEngine.closest(event.target, Selector.MENU)) :
      !REGEXP_KEYDOWN.test(event.which)) {
Johann-S's avatar
Johann-S committed
474
475
      return
    }
fat's avatar
fat committed
476

Johann-S's avatar
Johann-S committed
477
478
    event.preventDefault()
    event.stopPropagation()
fat's avatar
fat committed
479

Johann-S's avatar
Johann-S committed
480
    if (this.disabled || this.classList.contains(ClassName.DISABLED)) {
Johann-S's avatar
Johann-S committed
481
482
      return
    }
fat's avatar
fat committed
483

XhmikosR's avatar
XhmikosR committed
484
    const parent = Dropdown._getParentFromElement(this)
Johann-S's avatar
Johann-S committed
485
    const isActive = parent.classList.contains(ClassName.SHOW)
fat's avatar
fat committed
486

487
    if (!isActive || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
Johann-S's avatar
Johann-S committed
488
      if (event.which === ESCAPE_KEYCODE) {
489
        SelectorEngine.findOne(Selector.DATA_TOGGLE, parent).focus()
fat's avatar
fat committed
490
491
      }

492
      Dropdown._clearMenus()
Johann-S's avatar
Johann-S committed
493
494
      return
    }
fat's avatar
fat committed
495

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

Johann-S's avatar
Johann-S committed
498
    if (!items.length) {
Johann-S's avatar
Johann-S committed
499
500
      return
    }
fat's avatar
fat committed
501

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

Johann-S's avatar
Johann-S committed
504
505
506
    if (event.which === ARROW_UP_KEYCODE && index > 0) { // Up
      index--
    }
Jacob Thornton's avatar
Jacob Thornton committed
507

Johann-S's avatar
Johann-S committed
508
509
510
    if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { // Down
      index++
    }
fat's avatar
fat committed
511

Johann-S's avatar
Johann-S committed
512
513
    if (index < 0) {
      index = 0
fat's avatar
fat committed
514
    }
Johann-S's avatar
Johann-S committed
515
516

    items[index].focus()
fat's avatar
fat committed
517
  }
518
519
520
521

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

Johann-S's avatar
Johann-S committed
524
525
526
527
528
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
529

Johann-S's avatar
Johann-S committed
530
531
532
533
534
535
536
537
538
539
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
XhmikosR's avatar
XhmikosR committed
540
  .on(document, Event.CLICK_DATA_API, Selector.FORM_CHILD, e => e.stopPropagation())
Johann-S's avatar
Johann-S committed
541
542
543
544
545

/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
Johann-S's avatar
Johann-S committed
546
 * add .dropdown to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
547
548
 */

Johann-S's avatar
Johann-S committed
549
550
if (typeof $ !== 'undefined') {
  const JQUERY_NO_CONFLICT = $.fn[NAME]
XhmikosR's avatar
XhmikosR committed
551
552
553
  $.fn[NAME] = Dropdown._jQueryInterface
  $.fn[NAME].Constructor = Dropdown
  $.fn[NAME].noConflict = () => {
Johann-S's avatar
Johann-S committed
554
555
556
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Dropdown._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
557
}
fat's avatar
fat committed
558
559

export default Dropdown