1 | import BaseComponent from 'bootstrap/js/src/base-component.js'
|
2 |
|
3 | import { reflow, getElementFromSelector } from 'bootstrap/js/src/util'
|
4 | import EventHandler from 'bootstrap/js/src/dom/event-handler'
|
5 | import SelectorEngine from 'bootstrap/js/src/dom/selector-engine'
|
6 |
|
7 | const NAME = 'dimmer'
|
8 | const DATA_KEY = 'bs.dimmer'
|
9 | const EVENT_KEY = `.${DATA_KEY}`
|
10 |
|
11 |
|
12 | const EVENT_CLICK = `click${EVENT_KEY}`
|
13 |
|
14 | const CLASS_NAME_FADE = 'fade'
|
15 | const CLASS_NAME_SHOW = 'show'
|
16 |
|
17 | const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dimmer"]'
|
18 |
|
19 | class Dimmer extends BaseComponent {
|
20 | constructor(element) {
|
21 | super(element)
|
22 |
|
23 | this._isShown = false
|
24 | this._isTransitioning = false
|
25 |
|
26 | this._init()
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 | static get NAME() {
|
32 | return NAME
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 | show() {
|
38 | if (this._isShown || this._isTransitioning) {
|
39 | return
|
40 | }
|
41 |
|
42 | this._isShown = true
|
43 |
|
44 | if (this._isAnimated()) {
|
45 | this._isTransitioning = true
|
46 | }
|
47 |
|
48 | this._showElement()
|
49 | }
|
50 |
|
51 | hide() {
|
52 | if (!this._isShown || this._isTransitioning) {
|
53 | return
|
54 | }
|
55 |
|
56 | this._isShown = false
|
57 | const isAnimated = this._isAnimated()
|
58 |
|
59 | if (isAnimated) {
|
60 | this._isTransitioning = true
|
61 | }
|
62 |
|
63 | this._element.classList.remove(CLASS_NAME_SHOW)
|
64 |
|
65 | this._queueCallback(() => this._hideElement(), this._element, isAnimated)
|
66 | }
|
67 |
|
68 |
|
69 | _init() {
|
70 | if (this._element.classList.contains(CLASS_NAME_SHOW)) {
|
71 | this.show()
|
72 | }
|
73 | }
|
74 |
|
75 | _isAnimated() {
|
76 | return this._element.classList.contains(CLASS_NAME_FADE)
|
77 | }
|
78 |
|
79 | _showElement() {
|
80 | const isAnimated = this._isAnimated()
|
81 |
|
82 | this._element.style.display = 'flex'
|
83 | this._element.removeAttribute('aria-hidden')
|
84 |
|
85 |
|
86 |
|
87 | if (isAnimated) {
|
88 | reflow(this._element)
|
89 | }
|
90 |
|
91 | this._element.classList.add(CLASS_NAME_SHOW)
|
92 |
|
93 | const transitionComplete = () => {
|
94 | this._isTransitioning = false
|
95 | }
|
96 |
|
97 | this._queueCallback(transitionComplete, this._element, isAnimated)
|
98 | }
|
99 |
|
100 | _hideElement() {
|
101 | this._element.style.display = 'none'
|
102 | this._element.setAttribute('aria-hidden', true)
|
103 |
|
104 |
|
105 | this._isTransitioning = false
|
106 | }
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | SelectorEngine.find(SELECTOR_DATA_TOGGLE).forEach((toggle) => {
|
116 | const dimmerElement = getElementFromSelector(toggle)
|
117 | const dimmer = Dimmer.getOrCreateInstance(dimmerElement)
|
118 |
|
119 | EventHandler.on(toggle, EVENT_CLICK, () => {
|
120 | toggle.checked ? dimmer.show() : dimmer.hide()
|
121 | })
|
122 |
|
123 | if (toggle.checked) {
|
124 | dimmer.show()
|
125 | }
|
126 | })
|
127 |
|
128 | export default Dimmer
|