-
-
Notifications
You must be signed in to change notification settings - Fork 78.7k
Expand file tree
/
Copy pathtoggler.js
More file actions
101 lines (77 loc) · 2.06 KB
/
Copy pathtoggler.js
File metadata and controls
101 lines (77 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* --------------------------------------------------------------------------
* Bootstrap toggler.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
import BaseComponent from './base-component.js'
import EventHandler from './dom/event-handler.js'
import { eventActionOnPlugin } from './util/component-functions.js'
/**
* Constants
*/
const NAME = 'toggler'
const DATA_KEY = 'bs.toggler'
const EVENT_KEY = `.${DATA_KEY}`
const EVENT_TOGGLE = `toggle${EVENT_KEY}`
const EVENT_TOGGLED = `toggled${EVENT_KEY}`
const EVENT_CLICK = 'click'
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="toggler"]'
const DefaultType = {
attribute: 'string',
value: '(string|number|boolean|null)'
}
const Default = {
attribute: 'class',
value: null
}
/**
* Class definition
*/
class Toggler extends BaseComponent {
// Getters
static get Default() {
return Default
}
static get DefaultType() {
return DefaultType
}
static get NAME() {
return NAME
}
// Public
toggle() {
const toggleEvent = EventHandler.trigger(this._element, EVENT_TOGGLE)
if (toggleEvent.defaultPrevented) {
return
}
this._execute()
EventHandler.trigger(this._element, EVENT_TOGGLED)
}
// Private
_execute() {
const { attribute, value } = this._config
if (attribute === 'id') {
return // You have to be kidding
}
// Nothing to toggle without a value (e.g. missing `data-bs-value`)
if (value === null || value === undefined) {
return
}
if (attribute === 'class') {
this._element.classList.toggle(value)
return
}
// Compare as strings since getAttribute() always returns a string
if (this._element.getAttribute(attribute) === String(value)) {
this._element.removeAttribute(attribute)
return
}
this._element.setAttribute(attribute, value)
}
}
/**
* Data API implementation
*/
eventActionOnPlugin(Toggler, EVENT_CLICK, SELECTOR_DATA_TOGGLE, 'toggle')
export default Toggler