1 | /**
|
---|
2 | * --------------------------------------------------------------------------
|
---|
3 | * Bootstrap (v5.1.3): button.js
|
---|
4 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
5 | * --------------------------------------------------------------------------
|
---|
6 | */
|
---|
7 |
|
---|
8 | import { defineJQueryPlugin } from './util/index'
|
---|
9 | import EventHandler from './dom/event-handler'
|
---|
10 | import BaseComponent from './base-component'
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * ------------------------------------------------------------------------
|
---|
14 | * Constants
|
---|
15 | * ------------------------------------------------------------------------
|
---|
16 | */
|
---|
17 |
|
---|
18 | const NAME = 'button'
|
---|
19 | const DATA_KEY = 'bs.button'
|
---|
20 | const EVENT_KEY = `.${DATA_KEY}`
|
---|
21 | const DATA_API_KEY = '.data-api'
|
---|
22 |
|
---|
23 | const CLASS_NAME_ACTIVE = 'active'
|
---|
24 |
|
---|
25 | const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="button"]'
|
---|
26 |
|
---|
27 | const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * ------------------------------------------------------------------------
|
---|
31 | * Class Definition
|
---|
32 | * ------------------------------------------------------------------------
|
---|
33 | */
|
---|
34 |
|
---|
35 | class Button extends BaseComponent {
|
---|
36 | // Getters
|
---|
37 |
|
---|
38 | static get NAME() {
|
---|
39 | return NAME
|
---|
40 | }
|
---|
41 |
|
---|
42 | // Public
|
---|
43 |
|
---|
44 | toggle() {
|
---|
45 | // Toggle class and sync the `aria-pressed` attribute with the return value of the `.toggle()` method
|
---|
46 | this._element.setAttribute('aria-pressed', this._element.classList.toggle(CLASS_NAME_ACTIVE))
|
---|
47 | }
|
---|
48 |
|
---|
49 | // Static
|
---|
50 |
|
---|
51 | static jQueryInterface(config) {
|
---|
52 | return this.each(function () {
|
---|
53 | const data = Button.getOrCreateInstance(this)
|
---|
54 |
|
---|
55 | if (config === 'toggle') {
|
---|
56 | data[config]()
|
---|
57 | }
|
---|
58 | })
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * ------------------------------------------------------------------------
|
---|
64 | * Data Api implementation
|
---|
65 | * ------------------------------------------------------------------------
|
---|
66 | */
|
---|
67 |
|
---|
68 | EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
|
---|
69 | event.preventDefault()
|
---|
70 |
|
---|
71 | const button = event.target.closest(SELECTOR_DATA_TOGGLE)
|
---|
72 | const data = Button.getOrCreateInstance(button)
|
---|
73 |
|
---|
74 | data.toggle()
|
---|
75 | })
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * ------------------------------------------------------------------------
|
---|
79 | * jQuery
|
---|
80 | * ------------------------------------------------------------------------
|
---|
81 | * add .Button to jQuery only if jQuery is present
|
---|
82 | */
|
---|
83 |
|
---|
84 | defineJQueryPlugin(Button)
|
---|
85 |
|
---|
86 | export default Button
|
---|