talos

Форк
0
523 строки · 13.7 Кб
1
/**
2
 * --------------------------------------------------------------------------
3
 * Bootstrap (v4.6.1): dropdown.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
 * --------------------------------------------------------------------------
6
 */
7

8
import $ from 'jquery'
9
import Popper from 'popper.js'
10
import Util from './util'
11

12
/**
13
 * Constants
14
 */
15

16
const NAME = 'dropdown'
17
const VERSION = '4.6.1'
18
const DATA_KEY = 'bs.dropdown'
19
const EVENT_KEY = `.${DATA_KEY}`
20
const DATA_API_KEY = '.data-api'
21
const JQUERY_NO_CONFLICT = $.fn[NAME]
22
const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
23
const SPACE_KEYCODE = 32 // KeyboardEvent.which value for space key
24
const TAB_KEYCODE = 9 // KeyboardEvent.which value for tab key
25
const ARROW_UP_KEYCODE = 38 // KeyboardEvent.which value for up arrow key
26
const ARROW_DOWN_KEYCODE = 40 // KeyboardEvent.which value for down arrow key
27
const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse)
28
const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`)
29

30
const CLASS_NAME_DISABLED = 'disabled'
31
const CLASS_NAME_SHOW = 'show'
32
const CLASS_NAME_DROPUP = 'dropup'
33
const CLASS_NAME_DROPRIGHT = 'dropright'
34
const CLASS_NAME_DROPLEFT = 'dropleft'
35
const CLASS_NAME_MENURIGHT = 'dropdown-menu-right'
36
const CLASS_NAME_POSITION_STATIC = 'position-static'
37

38
const EVENT_HIDE = `hide${EVENT_KEY}`
39
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
40
const EVENT_SHOW = `show${EVENT_KEY}`
41
const EVENT_SHOWN = `shown${EVENT_KEY}`
42
const EVENT_CLICK = `click${EVENT_KEY}`
43
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
44
const EVENT_KEYDOWN_DATA_API = `keydown${EVENT_KEY}${DATA_API_KEY}`
45
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`
46

47
const SELECTOR_DATA_TOGGLE = '[data-toggle="dropdown"]'
48
const SELECTOR_FORM_CHILD = '.dropdown form'
49
const SELECTOR_MENU = '.dropdown-menu'
50
const SELECTOR_NAVBAR_NAV = '.navbar-nav'
51
const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)'
52

53
const PLACEMENT_TOP = 'top-start'
54
const PLACEMENT_TOPEND = 'top-end'
55
const PLACEMENT_BOTTOM = 'bottom-start'
56
const PLACEMENT_BOTTOMEND = 'bottom-end'
57
const PLACEMENT_RIGHT = 'right-start'
58
const PLACEMENT_LEFT = 'left-start'
59

60
const Default = {
61
  offset: 0,
62
  flip: true,
63
  boundary: 'scrollParent',
64
  reference: 'toggle',
65
  display: 'dynamic',
66
  popperConfig: null
67
}
68

69
const DefaultType = {
70
  offset: '(number|string|function)',
71
  flip: 'boolean',
72
  boundary: '(string|element)',
73
  reference: '(string|element)',
74
  display: 'string',
75
  popperConfig: '(null|object)'
76
}
77

78
/**
79
 * Class definition
80
 */
81

82
class Dropdown {
83
  constructor(element, config) {
84
    this._element = element
85
    this._popper = null
86
    this._config = this._getConfig(config)
87
    this._menu = this._getMenuElement()
88
    this._inNavbar = this._detectNavbar()
89

90
    this._addEventListeners()
91
  }
92

93
  // Getters
94
  static get VERSION() {
95
    return VERSION
96
  }
97

98
  static get Default() {
99
    return Default
100
  }
101

102
  static get DefaultType() {
103
    return DefaultType
104
  }
105

106
  // Public
107
  toggle() {
108
    if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED)) {
109
      return
110
    }
111

112
    const isActive = $(this._menu).hasClass(CLASS_NAME_SHOW)
113

114
    Dropdown._clearMenus()
115

116
    if (isActive) {
117
      return
118
    }
119

120
    this.show(true)
121
  }
122

123
  show(usePopper = false) {
124
    if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED) || $(this._menu).hasClass(CLASS_NAME_SHOW)) {
125
      return
126
    }
127

128
    const relatedTarget = {
129
      relatedTarget: this._element
130
    }
131
    const showEvent = $.Event(EVENT_SHOW, relatedTarget)
132
    const parent = Dropdown._getParentFromElement(this._element)
133

134
    $(parent).trigger(showEvent)
135

136
    if (showEvent.isDefaultPrevented()) {
137
      return
138
    }
139

140
    // Totally disable Popper for Dropdowns in Navbar
141
    if (!this._inNavbar && usePopper) {
142
      // Check for Popper dependency
143
      if (typeof Popper === 'undefined') {
144
        throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)')
145
      }
146

147
      let referenceElement = this._element
148

149
      if (this._config.reference === 'parent') {
150
        referenceElement = parent
151
      } else if (Util.isElement(this._config.reference)) {
152
        referenceElement = this._config.reference
153

154
        // Check if it's jQuery element
155
        if (typeof this._config.reference.jquery !== 'undefined') {
156
          referenceElement = this._config.reference[0]
157
        }
158
      }
159

160
      // If boundary is not `scrollParent`, then set position to `static`
161
      // to allow the menu to "escape" the scroll parent's boundaries
162
      // https://github.com/twbs/bootstrap/issues/24251
163
      if (this._config.boundary !== 'scrollParent') {
164
        $(parent).addClass(CLASS_NAME_POSITION_STATIC)
165
      }
166

167
      this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig())
168
    }
169

170
    // If this is a touch-enabled device we add extra
171
    // empty mouseover listeners to the body's immediate children;
172
    // only needed because of broken event delegation on iOS
173
    // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
174
    if ('ontouchstart' in document.documentElement &&
175
        $(parent).closest(SELECTOR_NAVBAR_NAV).length === 0) {
176
      $(document.body).children().on('mouseover', null, $.noop)
177
    }
178

179
    this._element.focus()
180
    this._element.setAttribute('aria-expanded', true)
181

182
    $(this._menu).toggleClass(CLASS_NAME_SHOW)
183
    $(parent)
184
      .toggleClass(CLASS_NAME_SHOW)
185
      .trigger($.Event(EVENT_SHOWN, relatedTarget))
186
  }
187

188
  hide() {
189
    if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED) || !$(this._menu).hasClass(CLASS_NAME_SHOW)) {
190
      return
191
    }
192

193
    const relatedTarget = {
194
      relatedTarget: this._element
195
    }
196
    const hideEvent = $.Event(EVENT_HIDE, relatedTarget)
197
    const parent = Dropdown._getParentFromElement(this._element)
198

199
    $(parent).trigger(hideEvent)
200

201
    if (hideEvent.isDefaultPrevented()) {
202
      return
203
    }
204

205
    if (this._popper) {
206
      this._popper.destroy()
207
    }
208

209
    $(this._menu).toggleClass(CLASS_NAME_SHOW)
210
    $(parent)
211
      .toggleClass(CLASS_NAME_SHOW)
212
      .trigger($.Event(EVENT_HIDDEN, relatedTarget))
213
  }
214

215
  dispose() {
216
    $.removeData(this._element, DATA_KEY)
217
    $(this._element).off(EVENT_KEY)
218
    this._element = null
219
    this._menu = null
220
    if (this._popper !== null) {
221
      this._popper.destroy()
222
      this._popper = null
223
    }
224
  }
225

226
  update() {
227
    this._inNavbar = this._detectNavbar()
228
    if (this._popper !== null) {
229
      this._popper.scheduleUpdate()
230
    }
231
  }
232

233
  // Private
234
  _addEventListeners() {
235
    $(this._element).on(EVENT_CLICK, event => {
236
      event.preventDefault()
237
      event.stopPropagation()
238
      this.toggle()
239
    })
240
  }
241

242
  _getConfig(config) {
243
    config = {
244
      ...this.constructor.Default,
245
      ...$(this._element).data(),
246
      ...config
247
    }
248

249
    Util.typeCheckConfig(
250
      NAME,
251
      config,
252
      this.constructor.DefaultType
253
    )
254

255
    return config
256
  }
257

258
  _getMenuElement() {
259
    if (!this._menu) {
260
      const parent = Dropdown._getParentFromElement(this._element)
261

262
      if (parent) {
263
        this._menu = parent.querySelector(SELECTOR_MENU)
264
      }
265
    }
266

267
    return this._menu
268
  }
269

270
  _getPlacement() {
271
    const $parentDropdown = $(this._element.parentNode)
272
    let placement = PLACEMENT_BOTTOM
273

274
    // Handle dropup
275
    if ($parentDropdown.hasClass(CLASS_NAME_DROPUP)) {
276
      placement = $(this._menu).hasClass(CLASS_NAME_MENURIGHT) ?
277
        PLACEMENT_TOPEND :
278
        PLACEMENT_TOP
279
    } else if ($parentDropdown.hasClass(CLASS_NAME_DROPRIGHT)) {
280
      placement = PLACEMENT_RIGHT
281
    } else if ($parentDropdown.hasClass(CLASS_NAME_DROPLEFT)) {
282
      placement = PLACEMENT_LEFT
283
    } else if ($(this._menu).hasClass(CLASS_NAME_MENURIGHT)) {
284
      placement = PLACEMENT_BOTTOMEND
285
    }
286

287
    return placement
288
  }
289

290
  _detectNavbar() {
291
    return $(this._element).closest('.navbar').length > 0
292
  }
293

294
  _getOffset() {
295
    const offset = {}
296

297
    if (typeof this._config.offset === 'function') {
298
      offset.fn = data => {
299
        data.offsets = {
300
          ...data.offsets,
301
          ...this._config.offset(data.offsets, this._element)
302
        }
303

304
        return data
305
      }
306
    } else {
307
      offset.offset = this._config.offset
308
    }
309

310
    return offset
311
  }
312

313
  _getPopperConfig() {
314
    const popperConfig = {
315
      placement: this._getPlacement(),
316
      modifiers: {
317
        offset: this._getOffset(),
318
        flip: {
319
          enabled: this._config.flip
320
        },
321
        preventOverflow: {
322
          boundariesElement: this._config.boundary
323
        }
324
      }
325
    }
326

327
    // Disable Popper if we have a static display
328
    if (this._config.display === 'static') {
329
      popperConfig.modifiers.applyStyle = {
330
        enabled: false
331
      }
332
    }
333

334
    return {
335
      ...popperConfig,
336
      ...this._config.popperConfig
337
    }
338
  }
339

340
  // Static
341
  static _jQueryInterface(config) {
342
    return this.each(function () {
343
      let data = $(this).data(DATA_KEY)
344
      const _config = typeof config === 'object' ? config : null
345

346
      if (!data) {
347
        data = new Dropdown(this, _config)
348
        $(this).data(DATA_KEY, data)
349
      }
350

351
      if (typeof config === 'string') {
352
        if (typeof data[config] === 'undefined') {
353
          throw new TypeError(`No method named "${config}"`)
354
        }
355

356
        data[config]()
357
      }
358
    })
359
  }
360

361
  static _clearMenus(event) {
362
    if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH ||
363
      event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
364
      return
365
    }
366

367
    const toggles = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE))
368

369
    for (let i = 0, len = toggles.length; i < len; i++) {
370
      const parent = Dropdown._getParentFromElement(toggles[i])
371
      const context = $(toggles[i]).data(DATA_KEY)
372
      const relatedTarget = {
373
        relatedTarget: toggles[i]
374
      }
375

376
      if (event && event.type === 'click') {
377
        relatedTarget.clickEvent = event
378
      }
379

380
      if (!context) {
381
        continue
382
      }
383

384
      const dropdownMenu = context._menu
385
      if (!$(parent).hasClass(CLASS_NAME_SHOW)) {
386
        continue
387
      }
388

389
      if (event && (event.type === 'click' &&
390
          /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) &&
391
          $.contains(parent, event.target)) {
392
        continue
393
      }
394

395
      const hideEvent = $.Event(EVENT_HIDE, relatedTarget)
396
      $(parent).trigger(hideEvent)
397
      if (hideEvent.isDefaultPrevented()) {
398
        continue
399
      }
400

401
      // If this is a touch-enabled device we remove the extra
402
      // empty mouseover listeners we added for iOS support
403
      if ('ontouchstart' in document.documentElement) {
404
        $(document.body).children().off('mouseover', null, $.noop)
405
      }
406

407
      toggles[i].setAttribute('aria-expanded', 'false')
408

409
      if (context._popper) {
410
        context._popper.destroy()
411
      }
412

413
      $(dropdownMenu).removeClass(CLASS_NAME_SHOW)
414
      $(parent)
415
        .removeClass(CLASS_NAME_SHOW)
416
        .trigger($.Event(EVENT_HIDDEN, relatedTarget))
417
    }
418
  }
419

420
  static _getParentFromElement(element) {
421
    let parent
422
    const selector = Util.getSelectorFromElement(element)
423

424
    if (selector) {
425
      parent = document.querySelector(selector)
426
    }
427

428
    return parent || element.parentNode
429
  }
430

431
  // eslint-disable-next-line complexity
432
  static _dataApiKeydownHandler(event) {
433
    // If not input/textarea:
434
    //  - And not a key in REGEXP_KEYDOWN => not a dropdown command
435
    // If input/textarea:
436
    //  - If space key => not a dropdown command
437
    //  - If key is other than escape
438
    //    - If key is not up or down => not a dropdown command
439
    //    - If trigger inside the menu => not a dropdown command
440
    if (/input|textarea/i.test(event.target.tagName) ?
441
      event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE &&
442
      (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE ||
443
        $(event.target).closest(SELECTOR_MENU).length) : !REGEXP_KEYDOWN.test(event.which)) {
444
      return
445
    }
446

447
    if (this.disabled || $(this).hasClass(CLASS_NAME_DISABLED)) {
448
      return
449
    }
450

451
    const parent = Dropdown._getParentFromElement(this)
452
    const isActive = $(parent).hasClass(CLASS_NAME_SHOW)
453

454
    if (!isActive && event.which === ESCAPE_KEYCODE) {
455
      return
456
    }
457

458
    event.preventDefault()
459
    event.stopPropagation()
460

461
    if (!isActive || (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
462
      if (event.which === ESCAPE_KEYCODE) {
463
        $(parent.querySelector(SELECTOR_DATA_TOGGLE)).trigger('focus')
464
      }
465

466
      $(this).trigger('click')
467
      return
468
    }
469

470
    const items = [].slice.call(parent.querySelectorAll(SELECTOR_VISIBLE_ITEMS))
471
      .filter(item => $(item).is(':visible'))
472

473
    if (items.length === 0) {
474
      return
475
    }
476

477
    let index = items.indexOf(event.target)
478

479
    if (event.which === ARROW_UP_KEYCODE && index > 0) { // Up
480
      index--
481
    }
482

483
    if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { // Down
484
      index++
485
    }
486

487
    if (index < 0) {
488
      index = 0
489
    }
490

491
    items[index].focus()
492
  }
493
}
494

495
/**
496
 * Data API implementation
497
 */
498

499
$(document)
500
  .on(EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown._dataApiKeydownHandler)
501
  .on(EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown._dataApiKeydownHandler)
502
  .on(`${EVENT_CLICK_DATA_API} ${EVENT_KEYUP_DATA_API}`, Dropdown._clearMenus)
503
  .on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
504
    event.preventDefault()
505
    event.stopPropagation()
506
    Dropdown._jQueryInterface.call($(this), 'toggle')
507
  })
508
  .on(EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, e => {
509
    e.stopPropagation()
510
  })
511

512
/**
513
 * jQuery
514
 */
515

516
$.fn[NAME] = Dropdown._jQueryInterface
517
$.fn[NAME].Constructor = Dropdown
518
$.fn[NAME].noConflict = () => {
519
  $.fn[NAME] = JQUERY_NO_CONFLICT
520
  return Dropdown._jQueryInterface
521
}
522

523
export default Dropdown
524

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.