@stackoverflow/stacks 2.5.8 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,238 +1,238 @@
1
- import * as Stacks from "../../stacks";
2
-
3
- // Radio buttons only trigger a change event when they're *checked*, but not when
4
- // they're *unchecked*. Therefore, if we have an active `s-expandable-control` in
5
- // the document, we listen for change events on *all* radio buttons and find any
6
- // other radio buttons in the same `name` group, triggering a custom event on all
7
- // of them so the controller can re-evaluate.
8
- //
9
- // We're keeping a count of how many of these controllers are connected to the DOM,
10
- // so only have this global listener when we actually need it.
11
- const RADIO_OFF_EVENT = "s-expandable-control:radio-off";
12
-
13
- function globalChangeListener(e: UIEvent) {
14
- const target = e.target;
15
- if (
16
- !(target instanceof HTMLInputElement) ||
17
- target.nodeName !== "INPUT" ||
18
- target.type !== "radio"
19
- ) {
20
- return;
21
- }
22
- document
23
- .querySelectorAll('input[type="radio"][name="' + target.name + '"]')
24
- .forEach(function (other) {
25
- if (other === e.target) {
26
- return;
27
- }
28
- let customEvent;
29
- try {
30
- customEvent = new Event(RADIO_OFF_EVENT);
31
- } catch (ex) {
32
- // Internet Explorer
33
- customEvent = document.createEvent("Event");
34
- customEvent.initEvent(RADIO_OFF_EVENT, true, true);
35
- }
36
- other.dispatchEvent(customEvent);
37
- });
38
- }
39
-
40
- let refCount = 0;
41
- function globalChangeListenerRequired(required: boolean) {
42
- if (required) {
43
- refCount++;
44
- if (refCount === 1) {
45
- document.body.addEventListener(
46
- "change",
47
- globalChangeListener as EventListener
48
- );
49
- }
50
- } else {
51
- refCount--;
52
- if (refCount === 0) {
53
- document.body.removeEventListener(
54
- "change",
55
- globalChangeListener as EventListener
56
- );
57
- }
58
- }
59
- }
60
-
61
- export class ExpandableController extends Stacks.StacksController {
62
- private isCollapsed!: () => boolean;
63
- private events!: string[];
64
- private isCheckable!: boolean;
65
- private isRadio!: boolean;
66
- private lastKeydownClickTimestamp = 0;
67
-
68
- initialize() {
69
- if (
70
- this.element.nodeName === "INPUT" &&
71
- ["radio", "checkbox"].indexOf(
72
- (<HTMLInputElement>this.element).type
73
- ) >= 0
74
- ) {
75
- this.isCollapsed = this._isCollapsedForCheckable.bind(this);
76
- this.events = ["change", RADIO_OFF_EVENT];
77
- this.isCheckable = true;
78
- this.isRadio = (<HTMLInputElement>this.element).type === "radio";
79
- } else {
80
- this.isCollapsed = this._isCollapsedForClickable.bind(this);
81
- this.events = ["click", "keydown"];
82
- }
83
- this.listener = this.listener.bind(this);
84
- }
85
-
86
- // for non-checkable elements, the initial source of truth is the collapsed/expanded
87
- // state of the controlled element (unless the element doesn't exist)
88
- _isCollapsedForClickable() {
89
- const cc = this.controlledExpandables;
90
- // the element is considered collapsed if *any* target element is collapsed
91
- return cc.length > 0
92
- ? !cc.every((element) => element.classList.contains("is-expanded"))
93
- : this.element.getAttribute("aria-expanded") === "false";
94
- }
95
-
96
- // for checkable elements, the initial source of truth is the checked state
97
- _isCollapsedForCheckable() {
98
- return !(<HTMLInputElement>this.element).checked;
99
- }
100
-
101
- get controlledExpandables() {
102
- const attr = this.element.getAttribute("aria-controls");
103
- if (!attr) {
104
- throw `[aria-controls="targetId1 ... targetIdN"] attribute required`;
105
- }
106
- const result = attr
107
- .split(/\s+/g)
108
- .map((s) => document.getElementById(s))
109
- .filter((e): e is HTMLElement => !!e);
110
- if (!result.length) {
111
- throw "couldn't find controls";
112
- }
113
- return result;
114
- }
115
-
116
- _dispatchShowHideEvent(isShow: boolean) {
117
- this.triggerEvent(isShow ? "show" : "hide");
118
- }
119
-
120
- _toggleClass(doAdd: boolean) {
121
- if (!this.data.has("toggle-class")) {
122
- return;
123
- }
124
- const cl = this.element.classList;
125
- const toggleClass = this.data.get("toggle-class");
126
- if (!toggleClass) {
127
- throw "couldn't find toggle class";
128
- }
129
- toggleClass.split(/\s+/).forEach(function (cls) {
130
- cl.toggle(cls, !!doAdd);
131
- });
132
- }
133
-
134
- listener(e: Event) {
135
- let newCollapsed;
136
- if (this.isCheckable) {
137
- newCollapsed = !(<HTMLInputElement>this.element).checked;
138
- } else {
139
- if (
140
- e.type == "keydown" &&
141
- e instanceof KeyboardEvent &&
142
- e.keyCode != 13 &&
143
- e.keyCode != 32
144
- ) {
145
- return;
146
- }
147
- if (
148
- e.target !== e.currentTarget &&
149
- ["A", "BUTTON"].indexOf((<HTMLElement>e.target).nodeName) >= 0
150
- ) {
151
- return;
152
- }
153
-
154
- e.preventDefault();
155
-
156
- // Prevent "click" events from toggling the expandable within 300ms of "keydown".
157
- // e.preventDefault() should have done the same, but https://bugzilla.mozilla.org/show_bug.cgi?id=1487102
158
- // doesn't guarantee it.
159
- if (e.type == "keydown") {
160
- this.lastKeydownClickTimestamp = Date.now();
161
- } else if (
162
- e.type == "click" &&
163
- Date.now() - this.lastKeydownClickTimestamp < 300
164
- ) {
165
- return;
166
- }
167
- newCollapsed =
168
- this.element.getAttribute("aria-expanded") === "true";
169
- if (e.type === "click") {
170
- (<HTMLInputElement>this.element).blur();
171
- }
172
- }
173
- this.element.setAttribute(
174
- "aria-expanded",
175
- newCollapsed ? "false" : "true"
176
- );
177
- for (const controlledElement of this.controlledExpandables) {
178
- controlledElement.classList.toggle("is-expanded", !newCollapsed);
179
- }
180
- this._dispatchShowHideEvent(!newCollapsed);
181
- this._toggleClass(!newCollapsed);
182
- }
183
-
184
- connect() {
185
- this.events.forEach((e) => {
186
- this.element.addEventListener(e, this.listener.bind(this));
187
- }, this);
188
-
189
- if (this.isRadio) {
190
- globalChangeListenerRequired(true);
191
- }
192
-
193
- // synchronize state -- in all cases, this means setting the correct `aria-expanded`
194
- // attribute; for checkable controls this also means setting the `is-collapsed` class.
195
- // Note: aria-expanded is currently an invalid attribute on radio elements
196
- // Support for aria-expanded is being debated by the W3C https://github.com/w3c/aria/issues/1404 as recently as June 2022
197
- if (!this.isRadio) {
198
- this.element.setAttribute(
199
- "aria-expanded",
200
- this.isCollapsed() ? "false" : "true"
201
- );
202
- }
203
- if (this.isCheckable) {
204
- const cc = this.controlledExpandables;
205
- if (cc.length) {
206
- const expected = !this.isCollapsed();
207
- // if any element does not match the expected state, set them all to the expected state
208
- if (
209
- cc.some(
210
- (element) =>
211
- element.classList.contains("is-expanded") !==
212
- expected
213
- )
214
- ) {
215
- for (const controlledElement of this
216
- .controlledExpandables) {
217
- controlledElement.classList.toggle(
218
- "is-expanded",
219
- expected
220
- );
221
- }
222
- this._dispatchShowHideEvent(expected);
223
- this._toggleClass(expected);
224
- }
225
- }
226
- }
227
- }
228
-
229
- disconnect() {
230
- this.events.forEach((e) => {
231
- this.element.removeEventListener(e, this.listener.bind(this));
232
- }, this);
233
-
234
- if (this.isRadio) {
235
- globalChangeListenerRequired(false);
236
- }
237
- }
238
- }
1
+ import * as Stacks from "../../stacks";
2
+
3
+ // Radio buttons only trigger a change event when they're *checked*, but not when
4
+ // they're *unchecked*. Therefore, if we have an active `s-expandable-control` in
5
+ // the document, we listen for change events on *all* radio buttons and find any
6
+ // other radio buttons in the same `name` group, triggering a custom event on all
7
+ // of them so the controller can re-evaluate.
8
+ //
9
+ // We're keeping a count of how many of these controllers are connected to the DOM,
10
+ // so only have this global listener when we actually need it.
11
+ const RADIO_OFF_EVENT = "s-expandable-control:radio-off";
12
+
13
+ function globalChangeListener(e: UIEvent) {
14
+ const target = e.target;
15
+ if (
16
+ !(target instanceof HTMLInputElement) ||
17
+ target.nodeName !== "INPUT" ||
18
+ target.type !== "radio"
19
+ ) {
20
+ return;
21
+ }
22
+ document
23
+ .querySelectorAll('input[type="radio"][name="' + target.name + '"]')
24
+ .forEach(function (other) {
25
+ if (other === e.target) {
26
+ return;
27
+ }
28
+ let customEvent;
29
+ try {
30
+ customEvent = new Event(RADIO_OFF_EVENT);
31
+ } catch {
32
+ // Internet Explorer
33
+ customEvent = document.createEvent("Event");
34
+ customEvent.initEvent(RADIO_OFF_EVENT, true, true);
35
+ }
36
+ other.dispatchEvent(customEvent);
37
+ });
38
+ }
39
+
40
+ let refCount = 0;
41
+ function globalChangeListenerRequired(required: boolean) {
42
+ if (required) {
43
+ refCount++;
44
+ if (refCount === 1) {
45
+ document.body.addEventListener(
46
+ "change",
47
+ globalChangeListener as EventListener
48
+ );
49
+ }
50
+ } else {
51
+ refCount--;
52
+ if (refCount === 0) {
53
+ document.body.removeEventListener(
54
+ "change",
55
+ globalChangeListener as EventListener
56
+ );
57
+ }
58
+ }
59
+ }
60
+
61
+ export class ExpandableController extends Stacks.StacksController {
62
+ private isCollapsed!: () => boolean;
63
+ private events!: string[];
64
+ private isCheckable!: boolean;
65
+ private isRadio!: boolean;
66
+ private lastKeydownClickTimestamp = 0;
67
+
68
+ initialize() {
69
+ if (
70
+ this.element.nodeName === "INPUT" &&
71
+ ["radio", "checkbox"].indexOf(
72
+ (<HTMLInputElement>this.element).type
73
+ ) >= 0
74
+ ) {
75
+ this.isCollapsed = this._isCollapsedForCheckable.bind(this);
76
+ this.events = ["change", RADIO_OFF_EVENT];
77
+ this.isCheckable = true;
78
+ this.isRadio = (<HTMLInputElement>this.element).type === "radio";
79
+ } else {
80
+ this.isCollapsed = this._isCollapsedForClickable.bind(this);
81
+ this.events = ["click", "keydown"];
82
+ }
83
+ this.listener = this.listener.bind(this);
84
+ }
85
+
86
+ // for non-checkable elements, the initial source of truth is the collapsed/expanded
87
+ // state of the controlled element (unless the element doesn't exist)
88
+ _isCollapsedForClickable() {
89
+ const cc = this.controlledExpandables;
90
+ // the element is considered collapsed if *any* target element is collapsed
91
+ return cc.length > 0
92
+ ? !cc.every((element) => element.classList.contains("is-expanded"))
93
+ : this.element.getAttribute("aria-expanded") === "false";
94
+ }
95
+
96
+ // for checkable elements, the initial source of truth is the checked state
97
+ _isCollapsedForCheckable() {
98
+ return !(<HTMLInputElement>this.element).checked;
99
+ }
100
+
101
+ get controlledExpandables() {
102
+ const attr = this.element.getAttribute("aria-controls");
103
+ if (!attr) {
104
+ throw `[aria-controls="targetId1 ... targetIdN"] attribute required`;
105
+ }
106
+ const result = attr
107
+ .split(/\s+/g)
108
+ .map((s) => document.getElementById(s))
109
+ .filter((e): e is HTMLElement => !!e);
110
+ if (!result.length) {
111
+ throw "couldn't find controls";
112
+ }
113
+ return result;
114
+ }
115
+
116
+ _dispatchShowHideEvent(isShow: boolean) {
117
+ this.triggerEvent(isShow ? "show" : "hide");
118
+ }
119
+
120
+ _toggleClass(doAdd: boolean) {
121
+ if (!this.data.has("toggle-class")) {
122
+ return;
123
+ }
124
+ const cl = this.element.classList;
125
+ const toggleClass = this.data.get("toggle-class");
126
+ if (!toggleClass) {
127
+ throw "couldn't find toggle class";
128
+ }
129
+ toggleClass.split(/\s+/).forEach(function (cls) {
130
+ cl.toggle(cls, !!doAdd);
131
+ });
132
+ }
133
+
134
+ listener(e: Event) {
135
+ let newCollapsed;
136
+ if (this.isCheckable) {
137
+ newCollapsed = !(<HTMLInputElement>this.element).checked;
138
+ } else {
139
+ if (
140
+ e.type == "keydown" &&
141
+ e instanceof KeyboardEvent &&
142
+ e.keyCode != 13 &&
143
+ e.keyCode != 32
144
+ ) {
145
+ return;
146
+ }
147
+ if (
148
+ e.target !== e.currentTarget &&
149
+ ["A", "BUTTON"].indexOf((<HTMLElement>e.target).nodeName) >= 0
150
+ ) {
151
+ return;
152
+ }
153
+
154
+ e.preventDefault();
155
+
156
+ // Prevent "click" events from toggling the expandable within 300ms of "keydown".
157
+ // e.preventDefault() should have done the same, but https://bugzilla.mozilla.org/show_bug.cgi?id=1487102
158
+ // doesn't guarantee it.
159
+ if (e.type == "keydown") {
160
+ this.lastKeydownClickTimestamp = Date.now();
161
+ } else if (
162
+ e.type == "click" &&
163
+ Date.now() - this.lastKeydownClickTimestamp < 300
164
+ ) {
165
+ return;
166
+ }
167
+ newCollapsed =
168
+ this.element.getAttribute("aria-expanded") === "true";
169
+ if (e.type === "click") {
170
+ (<HTMLInputElement>this.element).blur();
171
+ }
172
+ }
173
+ this.element.setAttribute(
174
+ "aria-expanded",
175
+ newCollapsed ? "false" : "true"
176
+ );
177
+ for (const controlledElement of this.controlledExpandables) {
178
+ controlledElement.classList.toggle("is-expanded", !newCollapsed);
179
+ }
180
+ this._dispatchShowHideEvent(!newCollapsed);
181
+ this._toggleClass(!newCollapsed);
182
+ }
183
+
184
+ connect() {
185
+ this.events.forEach((e) => {
186
+ this.element.addEventListener(e, this.listener.bind(this));
187
+ }, this);
188
+
189
+ if (this.isRadio) {
190
+ globalChangeListenerRequired(true);
191
+ }
192
+
193
+ // synchronize state -- in all cases, this means setting the correct `aria-expanded`
194
+ // attribute; for checkable controls this also means setting the `is-collapsed` class.
195
+ // Note: aria-expanded is currently an invalid attribute on radio elements
196
+ // Support for aria-expanded is being debated by the W3C https://github.com/w3c/aria/issues/1404 as recently as June 2022
197
+ if (!this.isRadio) {
198
+ this.element.setAttribute(
199
+ "aria-expanded",
200
+ this.isCollapsed() ? "false" : "true"
201
+ );
202
+ }
203
+ if (this.isCheckable) {
204
+ const cc = this.controlledExpandables;
205
+ if (cc.length) {
206
+ const expected = !this.isCollapsed();
207
+ // if any element does not match the expected state, set them all to the expected state
208
+ if (
209
+ cc.some(
210
+ (element) =>
211
+ element.classList.contains("is-expanded") !==
212
+ expected
213
+ )
214
+ ) {
215
+ for (const controlledElement of this
216
+ .controlledExpandables) {
217
+ controlledElement.classList.toggle(
218
+ "is-expanded",
219
+ expected
220
+ );
221
+ }
222
+ this._dispatchShowHideEvent(expected);
223
+ this._toggleClass(expected);
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ disconnect() {
230
+ this.events.forEach((e) => {
231
+ this.element.removeEventListener(e, this.listener.bind(this));
232
+ }, this);
233
+
234
+ if (this.isRadio) {
235
+ globalChangeListenerRequired(false);
236
+ }
237
+ }
238
+ }