generated from mirisuzanne/web-component-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ground-control-raw.js
208 lines (175 loc) · 5.07 KB
/
ground-control-raw.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
class GroundControl extends HTMLElement {
static register(tagName) {
if ('customElements' in window) {
customElements.define(
tagName || 'ground-control',
GroundControl
);
}
};
static observedAttributes = [
'data-for', // <selector-list> of elements to update…
// by setting either…
'data-attr', // <attribute-name>
'data-prop', // <css-property-name>
// and optionally storing the value for later…
'data-local', // <localStorage key>
'data-session', // <sessionStorage key>
// override event listeners (mainly for inputs)
'data-event',
// provide an 'off' value (mainly for toggles)
'data-off',
];
static _appendShadowTemplate = (node) => {
const template = document.createElement('template');
const shadowRoot = node.attachShadow({ mode: 'open' });
template.innerHTML = `<slot></slot>`;
shadowRoot.appendChild(template.content.cloneNode(true));
};
static _adoptShadowStyles = (node) => {
const shadowStyle = new CSSStyleSheet();
shadowStyle.replaceSync(`:host { display: block }`);
node.shadowRoot.adoptedStyleSheets = [shadowStyle];
};
static blockDisplay = (node) => {
GroundControl._appendShadowTemplate(node);
GroundControl._adoptShadowStyles(node);
};
#related = {};
#inputId;
#currentValue;
hasInput;
initialValue;
constructor() {
super();
};
attributeChangedCallback(name, oldValue, newValue) {
if (newValue === oldValue) return;
switch (name) {
case 'data-for':
this.targets = newValue;
break;
case 'data-local':
localStorage.removeItem(oldValue);
break;
case 'data-session':
sessionStorage.removeItem(oldValue);
break;
case 'data-event':
if (this.onEventChange) this.onEventChange();
break;
}
this.broadCast();
};
connectedCallback() {
this.targets = this.dataset.for;
};
disconnectedCallback() {
this.#removeResetListener();
};
// getters and setters
set value(newValue) {
this.#currentValue = newValue;
if (this.onValueChange) this.onValueChange();
this.broadCast();
};
get value() { return this.#currentValue; }
get usedValue() { return this.value || this.dataset.off; }
set inputId(value) {
if (this.#inputId) { this.#removeResetListener(); }
this.#inputId = value;
this.#related.resets = this.#findAll(
`button:is([data-reset~=${value}], [data-reset="*"])`
);
this.#addResetListener();
this.#related.displays = this.#findAll(
`output[for=${value}]`
);
};
get inputId() { return this.#inputId; }
set targets(to) {
this.#related.targets = (to && typeof to === 'object')
? to
: this.#findAll(to || ':root');
};
get targets() { return this.#related.targets || []; }
get displays() { return this.#related.displays || []; }
get storedValue() {
return sessionStorage.getItem(this.dataset.session)
|| localStorage.getItem(this.dataset.local);
};
set storedValue(value) {
const clearWhen = [
this.initialValue,
undefined, 'undefined',
];
this.#updateStorage(clearWhen.includes(value));
};
// public methods
onValueChange;
onEventChange;
reSet = () => {
this.value = this.initialValue;
};
broadCast = () => {
if (!this.hasInput) { return; }
this.storedValue = this.value;
this.targets.forEach((el) => {
if (this.dataset.prop) {
if (this.usedValue) {
el.style.setProperty(this.dataset.prop, this.usedValue);
} else {
el.style.removeProperty(this.dataset.prop);
}
}
if (this.dataset.attr) {
if (this.usedValue) {
el.setAttribute(this.dataset.attr, this.usedValue);
} else {
el.removeAttribute(this.dataset.attr);
}
}
});
this.displays.forEach((el) => {
el.value = this.usedValue;
});
};
#updateStorage = (clear) => {
if (this.dataset.local) {
clear
? localStorage.removeItem(this.dataset.local)
: localStorage.setItem(this.dataset.local, this.value);
}
if (this.dataset.session) {
clear
? sessionStorage.removeItem(this.dataset.session)
: sessionStorage.setItem(this.dataset.session, this.value);
}
};
// private methods
#findAll = (selector) => [
...document.querySelectorAll(selector)
];
#addResetListener = () => {
this.#related.resets.forEach((resetBtn) => {
resetBtn.resetTargets = [
...(resetBtn.resetTargets || []),
this
];
if (!resetBtn.resetListener) {
resetBtn.resetListener = () => {
resetBtn.resetTargets.forEach((target) => { target.reSet(); });
}
resetBtn.addEventListener('click', resetBtn.resetListener);
}
});
};
#removeResetListener = () => {
this.#related.resets?.forEach((resetBtn) => {
resetBtn.resetTargets.filter((target) => target !== this);
if (resetBtn.resetTargets.length === 0) {
resetBtn.removeEventListener('click', resetBtn.resetListener);
}
});
};
}