Skip to content

Commit 16c025a

Browse files
committed
Fix reset controls, bump v0.3.0
1 parent 1a83dab commit 16c025a

13 files changed

+103
-68
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changes
22

3+
## v0.3.0 = 2024-09-20
4+
5+
- NEW: Allow `data-reset="*"` universal reset buttons
6+
- REFACTOR: Only add a single event listener per reset button
7+
- BREAKING: Replaced `GroundControl.onReset()` => `GroundControl.reSet()`
8+
- FIX: Changing the `switch-control.value` programmatically
9+
correctly invokes `onPress` or `onUnpress`
10+
311
## v0.2.1 - 2024-08-26
412

513
- FIX: `switch-control` raw file out-of-date 🙈

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ Make sure you include the `<script>` in your project
9292
<!-- 3rd party CDN, not recommended for production use -->
9393
<script
9494
type="module"
95-
src="https://www.unpkg.com/@terriblemia/ground-control@0.2.1/index.js"
95+
src="https://www.unpkg.com/@terriblemia/ground-control@0.3.0/index.js"
9696
></script>
9797
```
9898

9999
```html
100100
<!-- 3rd party CDN, not recommended for production use -->
101101
<script
102102
type="module"
103-
src="https://esm.sh/@terriblemia/ground-control@0.2.1"
103+
src="https://esm.sh/@terriblemia/ground-control@0.3.0"
104104
></script>
105105
```
106106

ground-control-raw.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ class GroundControl extends HTMLElement {
8989
get usedValue() { return this.value || this.dataset.off; }
9090

9191
set inputId(value) {
92-
this.#inputId = value;
92+
if (this.#inputId) { this.#removeResetListener(); }
9393

94-
this.#removeResetListener();
94+
this.#inputId = value;
9595
this.#related.resets = this.#findAll(
96-
`button[data-reset~=${value}]`
96+
`button:is([data-reset~=${value}], [data-reset="*"])`
9797
);
9898
this.#addResetListener();
9999

@@ -131,7 +131,7 @@ class GroundControl extends HTMLElement {
131131
onValueChange;
132132
onEventChange;
133133

134-
onReset = () => {
134+
reSet = () => {
135135
this.value = this.initialValue;
136136
};
137137

@@ -182,15 +182,27 @@ class GroundControl extends HTMLElement {
182182

183183
#addResetListener = () => {
184184
this.#related.resets.forEach((resetBtn) => {
185-
resetBtn.addEventListener('click', this.onReset);
185+
resetBtn.resetTargets = [
186+
...(resetBtn.resetTargets || []),
187+
this
188+
];
189+
190+
if (!resetBtn.resetListener) {
191+
resetBtn.resetListener = () => {
192+
resetBtn.resetTargets.forEach((target) => { target.reSet(); });
193+
}
194+
resetBtn.addEventListener('click', resetBtn.resetListener);
195+
}
186196
});
187197
};
188198

189199
#removeResetListener = () => {
190-
if (!this.#related.resets) { return; }
200+
this.#related.resets?.forEach((resetBtn) => {
201+
resetBtn.resetTargets.filter((target) => target !== this);
191202

192-
this.#related.resets.forEach((resetBtn) => {
193-
resetBtn.removeEventListener('click', this.onReset);
203+
if (resetBtn.resetTargets.length === 0) {
204+
resetBtn.removeEventListener('click', resetBtn.resetListener);
205+
}
194206
});
195207
};
196208
}

ground-control.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ export default class GroundControl extends HTMLElement {
8989
get usedValue() { return this.value || this.dataset.off; }
9090

9191
set inputId(value) {
92-
this.#inputId = value;
92+
if (this.#inputId) { this.#removeResetListener(); }
9393

94-
this.#removeResetListener();
94+
this.#inputId = value;
9595
this.#related.resets = this.#findAll(
96-
`button[data-reset~=${value}]`
96+
`button:is([data-reset~=${value}], [data-reset="*"])`
9797
);
9898
this.#addResetListener();
9999

@@ -131,7 +131,7 @@ export default class GroundControl extends HTMLElement {
131131
onValueChange;
132132
onEventChange;
133133

134-
onReset = () => {
134+
reSet = () => {
135135
this.value = this.initialValue;
136136
};
137137

@@ -182,15 +182,27 @@ export default class GroundControl extends HTMLElement {
182182

183183
#addResetListener = () => {
184184
this.#related.resets.forEach((resetBtn) => {
185-
resetBtn.addEventListener('click', this.onReset);
185+
resetBtn.resetTargets = [
186+
...(resetBtn.resetTargets || []),
187+
this
188+
];
189+
190+
if (!resetBtn.resetListener) {
191+
resetBtn.resetListener = () => {
192+
resetBtn.resetTargets.forEach((target) => { target.reSet(); });
193+
}
194+
resetBtn.addEventListener('click', resetBtn.resetListener);
195+
}
186196
});
187197
};
188198

189199
#removeResetListener = () => {
190-
if (!this.#related.resets) { return; }
200+
this.#related.resets?.forEach((resetBtn) => {
201+
resetBtn.resetTargets.filter((target) => target !== this);
191202

192-
this.#related.resets.forEach((resetBtn) => {
193-
resetBtn.removeEventListener('click', this.onReset);
203+
if (resetBtn.resetTargets.length === 0) {
204+
resetBtn.removeEventListener('click', resetBtn.resetListener);
205+
}
194206
});
195207
};
196208
}

index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ <h2>Outputs & Resets</h2>
265265
<dd><output for="color-scheme"></output></dd>
266266
</dl>
267267

268-
<button data-reset="blackout layout title-font color-scheme hue">reset all</button>
268+
<button data-reset="*">reset all</button>
269269

270270
<h2>ToDo</h2>
271271
<ul>
@@ -300,9 +300,10 @@ <h2>ToDo</h2>
300300
constructor() {
301301
super();
302302
GroundControl.blockDisplay(this);
303-
this.onPress = () => this.CSS = true;
304-
this.onUnPress = () => this.CSS = false;
305303
};
304+
305+
onPress = () => this.CSS = true;
306+
onUnPress = () => this.CSS = false;
306307
}
307308

308309
CSSToggle.register();

input-control-raw.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ class InputControl extends GroundControl {
1616

1717
#input;
1818

19+
get listenFor () {
20+
return this.dataset.event || 'change';
21+
}
22+
1923
constructor() {
2024
super();
2125
GroundControl.blockDisplay(this);
22-
23-
this.addEventListener('change', this.onInputChange);
24-
this.addEventListener('input', this.onInputChange);
2526
}
2627

2728
connectedCallback() {
@@ -38,16 +39,21 @@ class InputControl extends GroundControl {
3839

3940
this.initialValue = this.#input.value;
4041
this.value = this.storedValue || this.initialValue;
42+
43+
this.addEventListener(this.listenFor, this.onInputChange);
4144
}
4245

43-
onInputChange = (event) => {
44-
const onType = this.dataset.event || 'change';
45-
if (onType !== event.type) return;
46+
disconnectedCallback() {
47+
this.removeEventListener(this.listenFor, this.onInputChange);
48+
}
4649

50+
onInputChange = () => {
4751
this.value = this.#input.value;
4852
}
4953

5054
onValueChange = () => {
55+
if (this.#input.value === this.value) return;
56+
5157
this.#input.value = this.value;
5258
}
5359

input-control.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ export default class InputControl extends GroundControl {
1818

1919
#input;
2020

21+
get listenFor () {
22+
return this.dataset.event || 'change';
23+
}
24+
2125
constructor() {
2226
super();
2327
GroundControl.blockDisplay(this);
24-
25-
this.addEventListener('change', this.onInputChange);
26-
this.addEventListener('input', this.onInputChange);
2728
}
2829

2930
connectedCallback() {
@@ -40,16 +41,21 @@ export default class InputControl extends GroundControl {
4041

4142
this.initialValue = this.#input.value;
4243
this.value = this.storedValue || this.initialValue;
44+
45+
this.addEventListener(this.listenFor, this.onInputChange);
4346
}
4447

45-
onInputChange = (event) => {
46-
const onType = this.dataset.event || 'change';
47-
if (onType !== event.type) return;
48+
disconnectedCallback() {
49+
this.removeEventListener(this.listenFor, this.onInputChange);
50+
}
4851

52+
onInputChange = () => {
4953
this.value = this.#input.value;
5054
}
5155

5256
onValueChange = () => {
57+
if (this.#input.value === this.value) return;
58+
5359
this.#input.value = this.value;
5460
}
5561

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@terriblemia/ground-control",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "A Web Component for user control of HTML attributes and CSS properties",
55
"main": "index.js",
66
"scripts": {

switch-control-raw.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class SwitchControl extends GroundControl {
6969
break;
7070
}
7171

72-
this.doToggleActions();
72+
this.setValueToPressed();
7373
};
7474

7575
connectedCallback() {
@@ -80,7 +80,7 @@ class SwitchControl extends GroundControl {
8080
this.pressed = this.#isPressedValue(this.storedValue);
8181
}
8282

83-
this.doToggleActions();
83+
this.setValueToPressed();
8484
};
8585

8686
disconnectedCallback() {
@@ -89,26 +89,23 @@ class SwitchControl extends GroundControl {
8989

9090
onTogglePress = () => {
9191
this.pressed = !this.pressed;
92-
this.doToggleActions();
92+
this.setValueToPressed();
9393
};
9494

95-
doToggleActions = () => {
95+
setValueToPressed = () => {
9696
if (!this.toggle) return;
97-
9897
this.value = this.pressedValue;
98+
this.doToggleActions();
99+
}
99100

101+
doToggleActions = () => {
100102
if (this.pressed && this.onPress) this.onPress();
101103
if (!this.pressed && this.onUnPress) this.onUnPress();
102104
};
103105

104106
onValueChange = () => {
105-
if (this.usedValue !== this.pressedValue) {
106-
this.pressed = this.#isPressedValue(this.usedValue);
107-
}
108-
};
109-
110-
onReset = () => {
111-
this.pressed = this.#isPressedValue(this.initialValue);
107+
if (this.value === this.pressedValue) return;
108+
this.pressed = this.#isPressedValue(this.value);
112109
this.doToggleActions();
113110
};
114111

0 commit comments

Comments
 (0)