dropdown.js 15 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
Johann-S's avatar
Johann-S committed
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'
Johann-S's avatar
Johann-S committed
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
    }

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

141
    Dropdown.clearMenus()
142

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

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

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

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

Johann-S's avatar
Johann-S committed
162
      let referenceElement = this._element
fat's avatar
fat committed
163

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

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

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

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

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

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

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

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

208
    const parent = Dropdown.getParentFromElement(this._element)
209
210
211
    const relatedTarget = {
      relatedTarget: this._element
    }
212

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

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

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

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

229
    const parent = Dropdown.getParentFromElement(this._element)
230
231
232
    const relatedTarget = {
      relatedTarget: this._element
    }
233

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

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

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

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

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

Johann-S's avatar
Johann-S committed
263
264
265
  // Private

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

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

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

    return config
  }

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

Johann-S's avatar
Johann-S committed
292
    return SelectorEngine.findOne(Selector.MENU, parent)
Johann-S's avatar
Johann-S committed
293
  }
fat's avatar
fat committed
294

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

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

Johann-S's avatar
Johann-S committed
313
314
    return placement
  }
315

Johann-S's avatar
Johann-S committed
316
  _detectNavbar() {
317
    return Boolean(SelectorEngine.closest(this._element, '.navbar'))
Johann-S's avatar
Johann-S committed
318
  }
319

320
321
322
  _getOffset() {
    const offset = {}

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

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

336
337
338
339
    return offset
  }

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

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

360
361
362
    return {
      ...popperConfig,
      ...this._config.popperConfig
363
    }
Johann-S's avatar
Johann-S committed
364
  }
365

Johann-S's avatar
Johann-S committed
366
  // Static
fat's avatar
fat committed
367

368
  static dropdownInterface(element, config) {
Johann-S's avatar
Johann-S committed
369
370
    let data = Data.getData(element, DATA_KEY)
    const _config = typeof config === 'object' ? config : null
fat's avatar
fat committed
371

Johann-S's avatar
Johann-S committed
372
373
374
    if (!data) {
      data = new Dropdown(element, _config)
    }
fat's avatar
fat committed
375

Johann-S's avatar
Johann-S committed
376
377
    if (typeof config === 'string') {
      if (typeof data[config] === 'undefined') {
XhmikosR's avatar
XhmikosR committed
378
        throw new TypeError(`No method named "${config}"`)
Johann-S's avatar
Johann-S committed
379
      }
XhmikosR's avatar
XhmikosR committed
380

Johann-S's avatar
Johann-S committed
381
382
383
384
      data[config]()
    }
  }

385
  static jQueryInterface(config) {
Johann-S's avatar
Johann-S committed
386
    return this.each(function () {
387
      Dropdown.dropdownInterface(this, config)
Johann-S's avatar
Johann-S committed
388
389
390
    })
  }

391
  static clearMenus(event) {
Johann-S's avatar
Johann-S committed
392
393
394
    if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH ||
      event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
      return
fat's avatar
fat committed
395
396
    }

397
    const toggles = makeArray(SelectorEngine.find(Selector.DATA_TOGGLE))
Johann-S's avatar
Johann-S committed
398
    for (let i = 0, len = toggles.length; i < len; i++) {
399
      const parent = Dropdown.getParentFromElement(toggles[i])
XhmikosR's avatar
XhmikosR committed
400
      const context = Data.getData(toggles[i], DATA_KEY)
Johann-S's avatar
Johann-S committed
401
402
      const relatedTarget = {
        relatedTarget: toggles[i]
fat's avatar
fat committed
403
404
      }

Johann-S's avatar
Johann-S committed
405
406
407
      if (event && event.type === 'click') {
        relatedTarget.clickEvent = event
      }
408

Johann-S's avatar
Johann-S committed
409
410
411
      if (!context) {
        continue
      }
Johann-S's avatar
Johann-S committed
412

Johann-S's avatar
Johann-S committed
413
      const dropdownMenu = context._menu
Johann-S's avatar
Johann-S committed
414
      if (!parent.classList.contains(ClassName.SHOW)) {
Johann-S's avatar
Johann-S committed
415
416
        continue
      }
fat's avatar
fat committed
417

Johann-S's avatar
Johann-S committed
418
      if (event && (event.type === 'click' &&
Johann-S's avatar
Johann-S committed
419
420
421
          /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
422
423
        continue
      }
fat's avatar
fat committed
424

Johann-S's avatar
Johann-S committed
425
426
      const hideEvent = EventHandler.trigger(parent, Event.HIDE, relatedTarget)
      if (hideEvent.defaultPrevented) {
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
432
      // If this is a touch-enabled device we remove the extra
      // empty mouseover listeners we added for iOS support
      if ('ontouchstart' in document.documentElement) {
433
        makeArray(document.body.children)
XhmikosR's avatar
XhmikosR committed
434
          .forEach(elem => EventHandler.off(elem, 'mouseover', null, noop()))
Johann-S's avatar
Johann-S committed
435
      }
436

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

439
440
441
442
      if (context._popper) {
        context._popper.destroy()
      }

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

449
  static getParentFromElement(element) {
450
    return getElementFromSelector(element) || element.parentNode
Johann-S's avatar
Johann-S committed
451
  }
fat's avatar
fat committed
452

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

Johann-S's avatar
Johann-S committed
469
470
    event.preventDefault()
    event.stopPropagation()
fat's avatar
fat committed
471

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

476
    const parent = Dropdown.getParentFromElement(this)
Johann-S's avatar
Johann-S committed
477
    const isActive = parent.classList.contains(ClassName.SHOW)
fat's avatar
fat committed
478

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

484
      Dropdown.clearMenus()
Johann-S's avatar
Johann-S committed
485
486
      return
    }
fat's avatar
fat committed
487

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

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

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

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

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

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

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

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

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

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

534
535
const $ = getjQuery()

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

export default Dropdown