dropdown.js 12.4 KB
Newer Older
1
2
import $ from 'jquery'
import Popper from 'popper.js'
fat's avatar
fat committed
3
4
5
6
7
import Util from './util'


/**
 * --------------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
8
 * Bootstrap (v4.0.0-beta): dropdown.js
fat's avatar
fat committed
9
10
11
12
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

13
const Dropdown = (() => {
fat's avatar
fat committed
14

Johann-S's avatar
Johann-S committed
15
16
17
18
19
  /**
   * Check for Popper dependency
   * Popper - https://popper.js.org
   */
  if (typeof Popper === 'undefined') {
20
    throw new Error('Bootstrap dropdown require Popper.js (https://popper.js.org)')
Johann-S's avatar
Johann-S committed
21
  }
fat's avatar
fat committed
22
23
24
25
26
27
28

  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

29
  const NAME                     = 'dropdown'
Mark Otto's avatar
Mark Otto committed
30
  const VERSION                  = '4.0.0-beta'
31
32
33
34
35
  const DATA_KEY                 = 'bs.dropdown'
  const EVENT_KEY                = `.${DATA_KEY}`
  const DATA_API_KEY             = '.data-api'
  const JQUERY_NO_CONFLICT       = $.fn[NAME]
  const ESCAPE_KEYCODE           = 27 // KeyboardEvent.which value for Escape (Esc) key
36
  const SPACE_KEYCODE            = 32 // KeyboardEvent.which value for space key
Pierre-Denis Vanduynslager's avatar
Pierre-Denis Vanduynslager committed
37
  const TAB_KEYCODE              = 9 // KeyboardEvent.which value for tab key
38
39
40
  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)
Pierre-Denis Vanduynslager's avatar
Indent    
Pierre-Denis Vanduynslager committed
41
  const REGEXP_KEYDOWN           = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`)
fat's avatar
fat committed
42
43

  const Event = {
XhmikosR's avatar
XhmikosR committed
44
45
46
47
48
    HIDE             : `hide${EVENT_KEY}`,
    HIDDEN           : `hidden${EVENT_KEY}`,
    SHOW             : `show${EVENT_KEY}`,
    SHOWN            : `shown${EVENT_KEY}`,
    CLICK            : `click${EVENT_KEY}`,
fat's avatar
fat committed
49
    CLICK_DATA_API   : `click${EVENT_KEY}${DATA_API_KEY}`,
Pierre-Denis Vanduynslager's avatar
Pierre-Denis Vanduynslager committed
50
51
    KEYDOWN_DATA_API : `keydown${EVENT_KEY}${DATA_API_KEY}`,
    KEYUP_DATA_API   : `keyup${EVENT_KEY}${DATA_API_KEY}`
fat's avatar
fat committed
52
53
54
  }

  const ClassName = {
55
56
57
58
59
    DISABLED  : 'disabled',
    SHOW      : 'show',
    DROPUP    : 'dropup',
    MENURIGHT : 'dropdown-menu-right',
    MENULEFT  : 'dropdown-menu-left'
fat's avatar
fat committed
60
61
62
63
64
  }

  const Selector = {
    DATA_TOGGLE   : '[data-toggle="dropdown"]',
    FORM_CHILD    : '.dropdown form',
65
    MENU          : '.dropdown-menu',
fat's avatar
fat committed
66
    NAVBAR_NAV    : '.navbar-nav',
67
    VISIBLE_ITEMS : '.dropdown-menu .dropdown-item:not(.disabled)'
fat's avatar
fat committed
68
69
  }

Johann-S's avatar
Johann-S committed
70
  const AttachmentMap = {
71
72
73
74
    TOP       : 'top-start',
    TOPEND    : 'top-end',
    BOTTOM    : 'bottom-start',
    BOTTOMEND : 'bottom-end'
Johann-S's avatar
Johann-S committed
75
76
  }

Johann-S's avatar
Johann-S committed
77
  const Default = {
Johann-S's avatar
Johann-S committed
78
    placement   : AttachmentMap.BOTTOM,
79
80
    offset      : 0,
    flip        : true
Johann-S's avatar
Johann-S committed
81
82
83
  }

  const DefaultType = {
84
    placement   : 'string',
85
86
    offset      : '(number|string)',
    flip        : 'boolean'
Johann-S's avatar
Johann-S committed
87
88
  }

fat's avatar
fat committed
89
90
91
92
93
94
95
96
97

  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */

  class Dropdown {

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

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

108
109
110
111
112
113
114

    // getters

    static get VERSION() {
      return VERSION
    }

Johann-S's avatar
Johann-S committed
115
116
117
118
119
120
121
122
    static get Default() {
      return Default
    }

    static get DefaultType() {
      return DefaultType
    }

fat's avatar
fat committed
123
124
125
    // public

    toggle() {
126
127
      if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED)) {
        return
fat's avatar
fat committed
128
129
      }

130
131
      const parent   = Dropdown._getParentFromElement(this._element)
      const isActive = $(this._menu).hasClass(ClassName.SHOW)
fat's avatar
fat committed
132
133
134
135

      Dropdown._clearMenus()

      if (isActive) {
136
        return
fat's avatar
fat committed
137
138
      }

139
      const relatedTarget = {
140
        relatedTarget : this._element
141
      }
Johann-S's avatar
Johann-S committed
142
      const showEvent = $.Event(Event.SHOW, relatedTarget)
fat's avatar
fat committed
143
144
145
146

      $(parent).trigger(showEvent)

      if (showEvent.isDefaultPrevented()) {
147
        return
fat's avatar
fat committed
148
149
      }

150
151
152
153
154
155
156
      let element = this._element
      // for dropup with alignment we use the parent as popper container
      if ($(parent).hasClass(ClassName.DROPUP)) {
        if ($(this._menu).hasClass(ClassName.MENULEFT) || $(this._menu).hasClass(ClassName.MENURIGHT)) {
          element = parent
        }
      }
157
      this._popper = new Popper(element, this._menu, this._getPopperConfig())
Johann-S's avatar
Johann-S committed
158

159
160
161
162
      // 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
163
164
      if ('ontouchstart' in document.documentElement &&
         !$(parent).closest(Selector.NAVBAR_NAV).length) {
165
        $('body').children().on('mouseover', null, $.noop)
166
167
      }

168
169
      this._element.focus()
      this._element.setAttribute('aria-expanded', true)
fat's avatar
fat committed
170

171
      $(this._menu).toggleClass(ClassName.SHOW)
Johann-S's avatar
Johann-S committed
172
173
174
      $(parent)
        .toggleClass(ClassName.SHOW)
        .trigger($.Event(Event.SHOWN, relatedTarget))
fat's avatar
fat committed
175
176
    }

fat's avatar
fat committed
177
178
179
180
    dispose() {
      $.removeData(this._element, DATA_KEY)
      $(this._element).off(EVENT_KEY)
      this._element = null
Johann-S's avatar
Johann-S committed
181
182
183
184
      this._menu = null
      if (this._popper !== null) {
        this._popper.destroy()
      }
185
      this._popper = null
fat's avatar
fat committed
186
187
    }

188
    update() {
189
      this._inNavbar = this._detectNavbar()
190
191
192
193
      if (this._popper !== null) {
        this._popper.scheduleUpdate()
      }
    }
fat's avatar
fat committed
194
195
196
197

    // private

    _addEventListeners() {
198
199
200
201
202
      $(this._element).on(Event.CLICK, (event) => {
        event.preventDefault()
        event.stopPropagation()
        this.toggle()
      })
fat's avatar
fat committed
203
204
    }

Johann-S's avatar
Johann-S committed
205
    _getConfig(config) {
206
      const elementData = $(this._element).data()
XhmikosR's avatar
XhmikosR committed
207
      if (typeof elementData.placement !== 'undefined') {
208
209
210
        elementData.placement = AttachmentMap[elementData.placement.toUpperCase()]
      }

Johann-S's avatar
Johann-S committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
      config = $.extend(
        {},
        this.constructor.Default,
        $(this._element).data(),
        config
      )

      Util.typeCheckConfig(
        NAME,
        config,
        this.constructor.DefaultType
      )

      return config
    }

    _getMenuElement() {
      if (!this._menu) {
229
        const parent = Dropdown._getParentFromElement(this._element)
Johann-S's avatar
Johann-S committed
230
231
232
233
        this._menu = $(parent).find(Selector.MENU)[0]
      }
      return this._menu
    }
fat's avatar
fat committed
234

235
236
237
238
239
240
241
242
243
    _getPlacement() {
      const $parentDropdown = $(this._element).parent()
      let placement = this._config.placement

      // Handle dropup
      if ($parentDropdown.hasClass(ClassName.DROPUP) || this._config.placement === AttachmentMap.TOP) {
        placement = AttachmentMap.TOP
        if ($(this._menu).hasClass(ClassName.MENURIGHT)) {
          placement = AttachmentMap.TOPEND
244
        }
Johann-S's avatar
Johann-S committed
245
246
      } else if ($(this._menu).hasClass(ClassName.MENURIGHT)) {
        placement = AttachmentMap.BOTTOMEND
247
      }
248
      return placement
249
250
    }

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    _detectNavbar() {
      return $(this._element).closest('.navbar').length > 0
    }

    _getPopperConfig() {
      const popperConfig = {
        placement : this._getPlacement(),
        modifiers : {
          offset : {
            offset : this._config.offset
          },
          flip : {
            enabled : this._config.flip
          }
        }
      }

268
      // Disable Popper.js for Dropdown in Navbar
269
      if (this._inNavbar) {
270
271
        popperConfig.modifiers.applyStyle = {
          enabled: !this._inNavbar
272
273
274
275
276
        }
      }
      return popperConfig
    }

fat's avatar
fat committed
277
278
279
280
    // static

    static _jQueryInterface(config) {
      return this.each(function () {
281
        let data = $(this).data(DATA_KEY)
282
        const _config = typeof config === 'object' ? config : null
fat's avatar
fat committed
283
284

        if (!data) {
Johann-S's avatar
Johann-S committed
285
          data = new Dropdown(this, _config)
286
          $(this).data(DATA_KEY, data)
fat's avatar
fat committed
287
288
289
        }

        if (typeof config === 'string') {
XhmikosR's avatar
XhmikosR committed
290
          if (typeof data[config] === 'undefined') {
291
292
            throw new Error(`No method named "${config}"`)
          }
293
          data[config]()
fat's avatar
fat committed
294
295
296
297
298
        }
      })
    }

    static _clearMenus(event) {
Pierre-Denis Vanduynslager's avatar
Pierre-Denis Vanduynslager committed
299
300
      if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH ||
        event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
fat's avatar
fat committed
301
302
303
        return
      }

304
      const toggles = $.makeArray($(Selector.DATA_TOGGLE))
fat's avatar
fat committed
305
      for (let i = 0; i < toggles.length; i++) {
306
        const parent        = Dropdown._getParentFromElement(toggles[i])
307
        const context       = $(toggles[i]).data(DATA_KEY)
308
309
310
        const relatedTarget = {
          relatedTarget : toggles[i]
        }
fat's avatar
fat committed
311

Johann-S's avatar
Johann-S committed
312
313
314
315
        if (!context) {
          continue
        }

316
        const dropdownMenu = context._menu
Starsam80's avatar
Starsam80 committed
317
        if (!$(parent).hasClass(ClassName.SHOW)) {
fat's avatar
fat committed
318
319
320
          continue
        }

321
        if (event && (event.type === 'click' &&
Pierre-Denis Vanduynslager's avatar
Pierre-Denis Vanduynslager committed
322
            /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE)
323
            && $.contains(parent, event.target)) {
fat's avatar
fat committed
324
325
326
          continue
        }

327
        const hideEvent = $.Event(Event.HIDE, relatedTarget)
fat's avatar
fat committed
328
329
330
331
332
        $(parent).trigger(hideEvent)
        if (hideEvent.isDefaultPrevented()) {
          continue
        }

333
334
335
        // if this is a touch-enabled device we remove the extra
        // empty mouseover listeners we added for iOS support
        if ('ontouchstart' in document.documentElement) {
336
          $('body').children().off('mouseover', null, $.noop)
337
338
        }

fat's avatar
fat committed
339
340
        toggles[i].setAttribute('aria-expanded', 'false')

Johann-S's avatar
Johann-S committed
341
        $(dropdownMenu).removeClass(ClassName.SHOW)
fat's avatar
fat committed
342
        $(parent)
Starsam80's avatar
Starsam80 committed
343
          .removeClass(ClassName.SHOW)
Jacob Thornton's avatar
Jacob Thornton committed
344
          .trigger($.Event(Event.HIDDEN, relatedTarget))
fat's avatar
fat committed
345
346
347
348
349
      }
    }

    static _getParentFromElement(element) {
      let parent
350
      const selector = Util.getSelectorFromElement(element)
fat's avatar
fat committed
351
352
353
354
355
356
357
358
359

      if (selector) {
        parent = $(selector)[0]
      }

      return parent || element.parentNode
    }

    static _dataApiKeydownHandler(event) {
Pierre-Denis Vanduynslager's avatar
Pierre-Denis Vanduynslager committed
360
      if (!REGEXP_KEYDOWN.test(event.which) || /button/i.test(event.target.tagName) && event.which === SPACE_KEYCODE ||
fat's avatar
fat committed
361
362
363
364
365
366
367
368
369
370
371
         /input|textarea/i.test(event.target.tagName)) {
        return
      }

      event.preventDefault()
      event.stopPropagation()

      if (this.disabled || $(this).hasClass(ClassName.DISABLED)) {
        return
      }

372
      const parent   = Dropdown._getParentFromElement(this)
Starsam80's avatar
Starsam80 committed
373
      const isActive = $(parent).hasClass(ClassName.SHOW)
fat's avatar
fat committed
374

375
376
      if (!isActive && (event.which !== ESCAPE_KEYCODE || event.which !== SPACE_KEYCODE) ||
           isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
fat's avatar
fat committed
377

378
        if (event.which === ESCAPE_KEYCODE) {
379
          const toggle = $(parent).find(Selector.DATA_TOGGLE)[0]
fat's avatar
fat committed
380
381
382
383
384
385
386
          $(toggle).trigger('focus')
        }

        $(this).trigger('click')
        return
      }

387
      const items = $(parent).find(Selector.VISIBLE_ITEMS).get()
fat's avatar
fat committed
388
389
390
391
392
393
394

      if (!items.length) {
        return
      }

      let index = items.indexOf(event.target)

395
      if (event.which === ARROW_UP_KEYCODE && index > 0) { // up
Jacob Thornton's avatar
Jacob Thornton committed
396
397
398
        index--
      }

399
      if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { // down
Jacob Thornton's avatar
Jacob Thornton committed
400
401
402
        index++
      }

403
      if (index < 0) {
Jacob Thornton's avatar
Jacob Thornton committed
404
405
        index = 0
      }
fat's avatar
fat committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419

      items[index].focus()
    }

  }


  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */

  $(document)
fat's avatar
fat committed
420
    .on(Event.KEYDOWN_DATA_API, Selector.DATA_TOGGLE,  Dropdown._dataApiKeydownHandler)
421
    .on(Event.KEYDOWN_DATA_API, Selector.MENU, Dropdown._dataApiKeydownHandler)
Pierre-Denis Vanduynslager's avatar
Pierre-Denis Vanduynslager committed
422
    .on(`${Event.CLICK_DATA_API} ${Event.KEYUP_DATA_API}`, Dropdown._clearMenus)
423
424
425
426
427
    .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
      event.preventDefault()
      event.stopPropagation()
      Dropdown._jQueryInterface.call($(this), 'toggle')
    })
Jacob Thornton's avatar
Jacob Thornton committed
428
429
430
    .on(Event.CLICK_DATA_API, Selector.FORM_CHILD, (e) => {
      e.stopPropagation()
    })
fat's avatar
fat committed
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447


  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */

  $.fn[NAME]             = Dropdown._jQueryInterface
  $.fn[NAME].Constructor = Dropdown
  $.fn[NAME].noConflict  = function () {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Dropdown._jQueryInterface
  }

  return Dropdown

448
})(jQuery, Popper)
fat's avatar
fat committed
449
450

export default Dropdown