Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * --------------------------------------------------------------------------
1441 ariadna 3
 * Bootstrap dropdown.js
1 efrain 4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
 * --------------------------------------------------------------------------
6
 */
7
 
1441 ariadna 8
import * as Popper from 'core/popper2'
9
import BaseComponent from './base-component'
10
import EventHandler from './dom/event-handler'
11
import Manipulator from './dom/manipulator'
12
import SelectorEngine from './dom/selector-engine'
13
import {
14
  defineJQueryPlugin,
15
  execute,
16
  getElement,
17
  getNextActiveElement,
18
  isDisabled,
19
  isElement,
20
  isRTL,
21
  isVisible,
22
  noop
23
} from './util/index'
1 efrain 24
 
25
/**
26
 * Constants
27
 */
28
 
29
const NAME = 'dropdown'
30
const DATA_KEY = 'bs.dropdown'
31
const EVENT_KEY = `.${DATA_KEY}`
32
const DATA_API_KEY = '.data-api'
33
 
1441 ariadna 34
const ESCAPE_KEY = 'Escape'
35
const TAB_KEY = 'Tab'
36
const ARROW_UP_KEY = 'ArrowUp'
37
const ARROW_DOWN_KEY = 'ArrowDown'
38
const RIGHT_MOUSE_BUTTON = 2 // MouseEvent.button value for the secondary button, usually the right button
1 efrain 39
 
40
const EVENT_HIDE = `hide${EVENT_KEY}`
41
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
42
const EVENT_SHOW = `show${EVENT_KEY}`
43
const EVENT_SHOWN = `shown${EVENT_KEY}`
44
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
45
const EVENT_KEYDOWN_DATA_API = `keydown${EVENT_KEY}${DATA_API_KEY}`
46
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`
47
 
1441 ariadna 48
const CLASS_NAME_SHOW = 'show'
49
const CLASS_NAME_DROPUP = 'dropup'
50
const CLASS_NAME_DROPEND = 'dropend'
51
const CLASS_NAME_DROPSTART = 'dropstart'
52
const CLASS_NAME_DROPUP_CENTER = 'dropup-center'
53
const CLASS_NAME_DROPDOWN_CENTER = 'dropdown-center'
54
 
55
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]:not(.disabled):not(:disabled)'
56
const SELECTOR_DATA_TOGGLE_SHOWN = `${SELECTOR_DATA_TOGGLE}.${CLASS_NAME_SHOW}`
1 efrain 57
const SELECTOR_MENU = '.dropdown-menu'
1441 ariadna 58
const SELECTOR_NAVBAR = '.navbar'
1 efrain 59
const SELECTOR_NAVBAR_NAV = '.navbar-nav'
60
const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)'
61
 
1441 ariadna 62
const PLACEMENT_TOP = isRTL() ? 'top-end' : 'top-start'
63
const PLACEMENT_TOPEND = isRTL() ? 'top-start' : 'top-end'
64
const PLACEMENT_BOTTOM = isRTL() ? 'bottom-end' : 'bottom-start'
65
const PLACEMENT_BOTTOMEND = isRTL() ? 'bottom-start' : 'bottom-end'
66
const PLACEMENT_RIGHT = isRTL() ? 'left-start' : 'right-start'
67
const PLACEMENT_LEFT = isRTL() ? 'right-start' : 'left-start'
68
const PLACEMENT_TOPCENTER = 'top'
69
const PLACEMENT_BOTTOMCENTER = 'bottom'
1 efrain 70
 
71
const Default = {
1441 ariadna 72
  autoClose: true,
73
  boundary: 'clippingParents',
1 efrain 74
  display: 'dynamic',
1441 ariadna 75
  offset: [0, 2],
76
  popperConfig: null,
77
  reference: 'toggle'
1 efrain 78
}
79
 
80
const DefaultType = {
1441 ariadna 81
  autoClose: '(boolean|string)',
1 efrain 82
  boundary: '(string|element)',
83
  display: 'string',
1441 ariadna 84
  offset: '(array|string|function)',
85
  popperConfig: '(null|object|function)',
86
  reference: '(string|element|object)'
1 efrain 87
}
88
 
89
/**
90
 * Class definition
91
 */
92
 
1441 ariadna 93
class Dropdown extends BaseComponent {
1 efrain 94
  constructor(element, config) {
1441 ariadna 95
    super(element, config)
96
 
1 efrain 97
    this._popper = null
1441 ariadna 98
    this._parent = this._element.parentNode // dropdown wrapper
99
    // TODO: v6 revert #37011 & change markup https://getbootstrap.com/docs/5.3/forms/input-group/
100
    this._menu = SelectorEngine.next(this._element, SELECTOR_MENU)[0] ||
101
      SelectorEngine.prev(this._element, SELECTOR_MENU)[0] ||
102
      SelectorEngine.findOne(SELECTOR_MENU, this._parent)
1 efrain 103
    this._inNavbar = this._detectNavbar()
104
  }
105
 
106
  // Getters
107
  static get Default() {
108
    return Default
109
  }
110
 
111
  static get DefaultType() {
112
    return DefaultType
113
  }
114
 
1441 ariadna 115
  static get NAME() {
116
    return NAME
117
  }
118
 
1 efrain 119
  // Public
120
  toggle() {
1441 ariadna 121
    return this._isShown() ? this.hide() : this.show()
1 efrain 122
  }
123
 
1441 ariadna 124
  show() {
125
    if (isDisabled(this._element) || this._isShown()) {
1 efrain 126
      return
127
    }
128
 
129
    const relatedTarget = {
130
      relatedTarget: this._element
131
    }
132
 
1441 ariadna 133
    const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, relatedTarget)
1 efrain 134
 
1441 ariadna 135
    if (showEvent.defaultPrevented) {
1 efrain 136
      return
137
    }
138
 
1441 ariadna 139
    this._createPopper()
1 efrain 140
 
141
    // If this is a touch-enabled device we add extra
142
    // empty mouseover listeners to the body's immediate children;
143
    // only needed because of broken event delegation on iOS
144
    // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
1441 ariadna 145
    if ('ontouchstart' in document.documentElement && !this._parent.closest(SELECTOR_NAVBAR_NAV)) {
146
      for (const element of [].concat(...document.body.children)) {
147
        EventHandler.on(element, 'mouseover', noop)
148
      }
1 efrain 149
    }
150
 
151
    this._element.focus()
152
    this._element.setAttribute('aria-expanded', true)
153
 
1441 ariadna 154
    this._menu.classList.add(CLASS_NAME_SHOW)
155
    this._element.classList.add(CLASS_NAME_SHOW)
156
    EventHandler.trigger(this._element, EVENT_SHOWN, relatedTarget)
1 efrain 157
  }
158
 
159
  hide() {
1441 ariadna 160
    if (isDisabled(this._element) || !this._isShown()) {
1 efrain 161
      return
162
    }
163
 
164
    const relatedTarget = {
165
      relatedTarget: this._element
166
    }
167
 
1441 ariadna 168
    this._completeHide(relatedTarget)
169
  }
1 efrain 170
 
1441 ariadna 171
  dispose() {
1 efrain 172
    if (this._popper) {
173
      this._popper.destroy()
174
    }
175
 
1441 ariadna 176
    super.dispose()
1 efrain 177
  }
178
 
179
  update() {
180
    this._inNavbar = this._detectNavbar()
1441 ariadna 181
    if (this._popper) {
182
      this._popper.update()
1 efrain 183
    }
184
  }
185
 
186
  // Private
1441 ariadna 187
  _completeHide(relatedTarget) {
188
    const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE, relatedTarget)
189
    if (hideEvent.defaultPrevented) {
190
      return
191
    }
192
 
193
    // If this is a touch-enabled device we remove the extra
194
    // empty mouseover listeners we added for iOS support
195
    if ('ontouchstart' in document.documentElement) {
196
      for (const element of [].concat(...document.body.children)) {
197
        EventHandler.off(element, 'mouseover', noop)
198
      }
199
    }
200
 
201
    if (this._popper) {
202
      this._popper.destroy()
203
    }
204
 
205
    this._menu.classList.remove(CLASS_NAME_SHOW)
206
    this._element.classList.remove(CLASS_NAME_SHOW)
207
    this._element.setAttribute('aria-expanded', 'false')
208
    Manipulator.removeDataAttribute(this._menu, 'popper')
209
    EventHandler.trigger(this._element, EVENT_HIDDEN, relatedTarget)
1 efrain 210
  }
211
 
212
  _getConfig(config) {
1441 ariadna 213
    config = super._getConfig(config)
214
 
215
    if (typeof config.reference === 'object' && !isElement(config.reference) &&
216
      typeof config.reference.getBoundingClientRect !== 'function'
217
    ) {
218
      // Popper virtual elements require a getBoundingClientRect method
219
      throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`)
1 efrain 220
    }
221
 
222
    return config
223
  }
224
 
1441 ariadna 225
  _createPopper() {
226
    if (typeof Popper === 'undefined') {
227
      throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)')
228
    }
1 efrain 229
 
1441 ariadna 230
    let referenceElement = this._element
231
 
232
    if (this._config.reference === 'parent') {
233
      referenceElement = this._parent
234
    } else if (isElement(this._config.reference)) {
235
      referenceElement = getElement(this._config.reference)
236
    } else if (typeof this._config.reference === 'object') {
237
      referenceElement = this._config.reference
1 efrain 238
    }
239
 
1441 ariadna 240
    const popperConfig = this._getPopperConfig()
241
    this._popper = Popper.createPopper(referenceElement, this._menu, popperConfig)
1 efrain 242
  }
243
 
1441 ariadna 244
  _isShown() {
245
    return this._menu.classList.contains(CLASS_NAME_SHOW)
246
  }
247
 
1 efrain 248
  _getPlacement() {
1441 ariadna 249
    const parentDropdown = this._parent
1 efrain 250
 
1441 ariadna 251
    if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
252
      return PLACEMENT_RIGHT
1 efrain 253
    }
254
 
1441 ariadna 255
    if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
256
      return PLACEMENT_LEFT
257
    }
258
 
259
    if (parentDropdown.classList.contains(CLASS_NAME_DROPUP_CENTER)) {
260
      return PLACEMENT_TOPCENTER
261
    }
262
 
263
    if (parentDropdown.classList.contains(CLASS_NAME_DROPDOWN_CENTER)) {
264
      return PLACEMENT_BOTTOMCENTER
265
    }
266
 
267
    // We need to trim the value because custom properties can also include spaces
268
    const isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end'
269
 
270
    if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
271
      return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP
272
    }
273
 
274
    return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM
1 efrain 275
  }
276
 
277
  _detectNavbar() {
1441 ariadna 278
    return this._element.closest(SELECTOR_NAVBAR) !== null
1 efrain 279
  }
280
 
281
  _getOffset() {
1441 ariadna 282
    const { offset } = this._config
1 efrain 283
 
1441 ariadna 284
    if (typeof offset === 'string') {
285
      return offset.split(',').map(value => Number.parseInt(value, 10))
286
    }
1 efrain 287
 
1441 ariadna 288
    if (typeof offset === 'function') {
289
      return popperData => offset(popperData, this._element)
1 efrain 290
    }
291
 
292
    return offset
293
  }
294
 
295
  _getPopperConfig() {
1441 ariadna 296
    const defaultBsPopperConfig = {
1 efrain 297
      placement: this._getPlacement(),
1441 ariadna 298
      modifiers: [{
299
        name: 'preventOverflow',
300
        options: {
301
          boundary: this._config.boundary
1 efrain 302
        }
1441 ariadna 303
      },
304
      {
305
        name: 'offset',
306
        options: {
307
          offset: this._getOffset()
308
        }
309
      }]
1 efrain 310
    }
311
 
1441 ariadna 312
    // Disable Popper if we have a static display or Dropdown is in Navbar
313
    if (this._inNavbar || this._config.display === 'static') {
314
      Manipulator.setDataAttribute(this._menu, 'popper', 'static') // TODO: v6 remove
315
      defaultBsPopperConfig.modifiers = [{
316
        name: 'applyStyles',
1 efrain 317
        enabled: false
1441 ariadna 318
      }]
1 efrain 319
    }
320
 
321
    return {
1441 ariadna 322
      ...defaultBsPopperConfig,
323
      ...execute(this._config.popperConfig, [defaultBsPopperConfig])
1 efrain 324
    }
325
  }
326
 
1441 ariadna 327
  _selectMenuItem({ key, target }) {
328
    const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(element => isVisible(element))
329
 
330
    if (!items.length) {
331
      return
332
    }
333
 
334
    // if target isn't included in items (e.g. when expanding the dropdown)
335
    // allow cycling to get the last item in case key equals ARROW_UP_KEY
336
    getNextActiveElement(items, target, key === ARROW_DOWN_KEY, !items.includes(target)).focus()
337
  }
338
 
1 efrain 339
  // Static
1441 ariadna 340
  static jQueryInterface(config) {
1 efrain 341
    return this.each(function () {
1441 ariadna 342
      const data = Dropdown.getOrCreateInstance(this, config)
1 efrain 343
 
1441 ariadna 344
      if (typeof config !== 'string') {
345
        return
1 efrain 346
      }
347
 
1441 ariadna 348
      if (typeof data[config] === 'undefined') {
349
        throw new TypeError(`No method named "${config}"`)
350
      }
1 efrain 351
 
1441 ariadna 352
      data[config]()
1 efrain 353
    })
354
  }
355
 
1441 ariadna 356
  static clearMenus(event) {
357
    if (event.button === RIGHT_MOUSE_BUTTON || (event.type === 'keyup' && event.key !== TAB_KEY)) {
1 efrain 358
      return
359
    }
360
 
1441 ariadna 361
    const openToggles = SelectorEngine.find(SELECTOR_DATA_TOGGLE_SHOWN)
1 efrain 362
 
1441 ariadna 363
    for (const toggle of openToggles) {
364
      const context = Dropdown.getInstance(toggle)
365
      if (!context || context._config.autoClose === false) {
1 efrain 366
        continue
367
      }
368
 
1441 ariadna 369
      const composedPath = event.composedPath()
370
      const isMenuTarget = composedPath.includes(context._menu)
371
      if (
372
        composedPath.includes(context._element) ||
373
        (context._config.autoClose === 'inside' && !isMenuTarget) ||
374
        (context._config.autoClose === 'outside' && isMenuTarget)
375
      ) {
1 efrain 376
        continue
377
      }
378
 
1441 ariadna 379
      // Tab navigation through the dropdown menu or events from contained inputs shouldn't close the menu
380
      if (context._menu.contains(event.target) && ((event.type === 'keyup' && event.key === TAB_KEY) || /input|select|option|textarea|form/i.test(event.target.tagName))) {
1 efrain 381
        continue
382
      }
383
 
1441 ariadna 384
      const relatedTarget = { relatedTarget: context._element }
1 efrain 385
 
1441 ariadna 386
      if (event.type === 'click') {
387
        relatedTarget.clickEvent = event
1 efrain 388
      }
389
 
1441 ariadna 390
      context._completeHide(relatedTarget)
1 efrain 391
    }
392
  }
393
 
1441 ariadna 394
  static dataApiKeydownHandler(event) {
395
    // If not an UP | DOWN | ESCAPE key => not a dropdown command
396
    // If input/textarea && if key is other than ESCAPE => not a dropdown command
1 efrain 397
 
1441 ariadna 398
    const isInput = /input|textarea/i.test(event.target.tagName)
399
    const isEscapeEvent = event.key === ESCAPE_KEY
400
    const isUpOrDownEvent = [ARROW_UP_KEY, ARROW_DOWN_KEY].includes(event.key)
1 efrain 401
 
1441 ariadna 402
    if (!isUpOrDownEvent && !isEscapeEvent) {
1 efrain 403
      return
404
    }
405
 
1441 ariadna 406
    if (isInput && !isEscapeEvent) {
1 efrain 407
      return
408
    }
409
 
410
    event.preventDefault()
411
 
1441 ariadna 412
    // TODO: v6 revert #37011 & change markup https://getbootstrap.com/docs/5.3/forms/input-group/
413
    const getToggleButton = this.matches(SELECTOR_DATA_TOGGLE) ?
414
      this :
415
      (SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0] ||
416
        SelectorEngine.next(this, SELECTOR_DATA_TOGGLE)[0] ||
417
        SelectorEngine.findOne(SELECTOR_DATA_TOGGLE, event.delegateTarget.parentNode))
1 efrain 418
 
1441 ariadna 419
    const instance = Dropdown.getOrCreateInstance(getToggleButton)
1 efrain 420
 
1441 ariadna 421
    if (isUpOrDownEvent) {
422
      event.stopPropagation()
423
      instance.show()
424
      instance._selectMenuItem(event)
1 efrain 425
      return
426
    }
427
 
1441 ariadna 428
    if (instance._isShown()) { // else is escape and we check if it is shown
429
      event.stopPropagation()
430
      instance.hide()
431
      getToggleButton.focus()
1 efrain 432
    }
433
  }
434
}
435
 
436
/**
437
 * Data API implementation
438
 */
439
 
1441 ariadna 440
EventHandler.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown.dataApiKeydownHandler)
441
EventHandler.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown.dataApiKeydownHandler)
442
EventHandler.on(document, EVENT_CLICK_DATA_API, Dropdown.clearMenus)
443
EventHandler.on(document, EVENT_KEYUP_DATA_API, Dropdown.clearMenus)
444
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
445
  event.preventDefault()
446
  Dropdown.getOrCreateInstance(this).toggle()
447
})
1 efrain 448
 
449
/**
450
 * jQuery
451
 */
452
 
1441 ariadna 453
defineJQueryPlugin(Dropdown)
1 efrain 454
 
455
export default Dropdown