@cat-of-summer/st-script 1.0.2
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.
- package/LICENSE +21 -0
- package/README.md +130 -0
- package/dist/app/form.esm.min.js +1 -0
- package/dist/app/form.min.js +2 -0
- package/dist/app.esm.min.js +1 -0
- package/dist/app.min.js +2 -0
- package/dist/cookie.esm.min.js +1 -0
- package/dist/cookie.min.js +2 -0
- package/dist/core.esm.min.js +1 -0
- package/dist/core.min.js +2 -0
- package/dist/index.esm.min.js +1 -0
- package/dist/index.min.js +2 -0
- package/dist/loader.esm.min.js +1 -0
- package/dist/loader.min.js +2 -0
- package/dist/modal.esm.min.js +1 -0
- package/dist/modal.min.js +2 -0
- package/dist/route.esm.min.js +1 -0
- package/dist/route.min.js +2 -0
- package/dist/select.esm.min.js +1 -0
- package/dist/select.min.js +2 -0
- package/dist/st_button_widget.esm.min.js +1 -0
- package/dist/st_button_widget.min.js +2 -0
- package/dist/st_links_widget.esm.min.js +1 -0
- package/dist/st_links_widget.min.js +2 -0
- package/dist/st_mask.esm.min.js +1 -0
- package/dist/st_mask.min.js +2 -0
- package/dist/st_observer.esm.min.js +1 -0
- package/dist/st_observer.min.js +2 -0
- package/dist/st_typograf.esm.min.js +1 -0
- package/dist/st_typograf.min.js +2 -0
- package/dist/st_validator.esm.min.js +1 -0
- package/dist/st_validator.min.js +2 -0
- package/dist/toggle.esm.min.js +1 -0
- package/dist/toggle.min.js +2 -0
- package/dist/uploader.esm.min.js +1 -0
- package/dist/uploader.min.js +2 -0
- package/package.json +35 -0
- package/src/_traits/hasInstanceSymbol.js +29 -0
- package/src/app/form/index.js +357 -0
- package/src/app/index.js +1040 -0
- package/src/cookie/index.js +141 -0
- package/src/core/index.js +159 -0
- package/src/index.js +16 -0
- package/src/loader/index.js +169 -0
- package/src/modal/index.js +297 -0
- package/src/route/index.js +208 -0
- package/src/select/index.js +210 -0
- package/src/st_button_widget/README.txt +62 -0
- package/src/st_button_widget/index.js +322 -0
- package/src/st_links_widget/README.txt +54 -0
- package/src/st_links_widget/index.js +294 -0
- package/src/st_mask/index.js +183 -0
- package/src/st_observer/index.js +426 -0
- package/src/st_typograf/index.js +200 -0
- package/src/st_validator/index.js +57 -0
- package/src/toggle/index.js +181 -0
- package/src/uploader/index.js +297 -0
package/src/app/index.js
ADDED
|
@@ -0,0 +1,1040 @@
|
|
|
1
|
+
export default class App extends HTMLElement {
|
|
2
|
+
static #boolean_attributes = new Set(['disabled', 'checked', 'readonly', 'required', 'selected', 'hidden', 'open', 'autofocus']);
|
|
3
|
+
static #apps = new Map();
|
|
4
|
+
static #instances = new Set();
|
|
5
|
+
|
|
6
|
+
static create(options) {
|
|
7
|
+
let { app, setup = () => {}, template = ``, events = {} } = options;
|
|
8
|
+
if (App.#apps.has(app))
|
|
9
|
+
throw new Error(`Application "${app}" already exists!`);
|
|
10
|
+
App.#apps.set(app, { setup, template, events, config: options });
|
|
11
|
+
App.#instances.forEach(instance => {
|
|
12
|
+
if (instance.#booted !== true)
|
|
13
|
+
instance.#boot();
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static extend(app, options = {}) {
|
|
18
|
+
let base = App.#apps.get(app);
|
|
19
|
+
if (!base)
|
|
20
|
+
throw new Error(`Application "${app}" not found!`);
|
|
21
|
+
let baseConfig = base.config;
|
|
22
|
+
let merged = { ...baseConfig, ...options };
|
|
23
|
+
if (baseConfig.events || options.events)
|
|
24
|
+
merged.events = { ...baseConfig.events, ...options.events };
|
|
25
|
+
return App.create(merged);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
#booted = false;
|
|
29
|
+
#template = '';
|
|
30
|
+
#rendered = false;
|
|
31
|
+
#deps = new Map();
|
|
32
|
+
#activeEffect = null;
|
|
33
|
+
#updateQueue = new Set();
|
|
34
|
+
#flushing = false;
|
|
35
|
+
#bindings = [];
|
|
36
|
+
#loopContext = null;
|
|
37
|
+
#getters = new Map();
|
|
38
|
+
#attrObserver = null;
|
|
39
|
+
#watchers = [];
|
|
40
|
+
#triggeredKeys = new Set();
|
|
41
|
+
#pendingFlush = false;
|
|
42
|
+
#methodWrappers = new Map();
|
|
43
|
+
#renderToken = 0;
|
|
44
|
+
|
|
45
|
+
constructor() {
|
|
46
|
+
super();
|
|
47
|
+
App.#instances.add(this);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
#cleanupBindings(bindings) {
|
|
51
|
+
if (!bindings || !bindings.length) return;
|
|
52
|
+
bindings.forEach(binding => {
|
|
53
|
+
if (binding.type === 'event' && binding.element && binding.handler)
|
|
54
|
+
binding.element.removeEventListener(binding.eventName, binding.handler);
|
|
55
|
+
else if (binding.type === 'model' && binding.element && binding.handler) {
|
|
56
|
+
let eventName = binding.element.tagName === 'SELECT' ? 'change' : 'input';
|
|
57
|
+
binding.element.removeEventListener(eventName, binding.handler);
|
|
58
|
+
} else if (binding.type === 'attrSync' && binding.unwatch)
|
|
59
|
+
binding.unwatch();
|
|
60
|
+
|
|
61
|
+
if (binding.effect) {
|
|
62
|
+
this.#deps.forEach((effects, key) => {
|
|
63
|
+
effects.delete(binding.effect);
|
|
64
|
+
if (effects.size === 0)
|
|
65
|
+
this.#deps.delete(key);
|
|
66
|
+
});
|
|
67
|
+
this.#updateQueue.delete(binding.effect);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get template() {
|
|
73
|
+
return this.#template;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get attrs() {
|
|
77
|
+
return {
|
|
78
|
+
get: (name) => this.getAttribute(name),
|
|
79
|
+
set: (name, value) => this.setAttribute(name, value),
|
|
80
|
+
remove: (name) => this.removeAttribute(name),
|
|
81
|
+
has: (name) => this.hasAttribute(name),
|
|
82
|
+
toggle: (name, force) => this.toggleAttribute(name, force),
|
|
83
|
+
keys: () => Array.from(this.attributes).map(a => a.name),
|
|
84
|
+
entries: () => Array.from(this.attributes).map(a => [a.name, a.value])
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
addEventListener(type, listener, options) {
|
|
89
|
+
if ((type === 'setup' || type === 'rendered') && this.#rendered) {
|
|
90
|
+
queueMicrotask(() => listener(new CustomEvent(type)));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
super.addEventListener(type, listener, options);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
dispatchEvent(event, detail = null, options = {}) {
|
|
97
|
+
if (typeof event === 'string') {
|
|
98
|
+
let customEvent = new CustomEvent(event, {cancelable: true, ...options, detail});
|
|
99
|
+
return super.dispatchEvent(customEvent);
|
|
100
|
+
}
|
|
101
|
+
return super.dispatchEvent(event);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
applyTemplate(template = '') {
|
|
105
|
+
let tplContent = template || this.#template;
|
|
106
|
+
this.#cleanupBindings(this.#bindings);
|
|
107
|
+
this.#bindings = [];
|
|
108
|
+
this.innerHTML = '';
|
|
109
|
+
if (tplContent) {
|
|
110
|
+
let tpl = document.createElement('template');
|
|
111
|
+
tpl.innerHTML = tplContent.trim();
|
|
112
|
+
let fragment = tpl.content.cloneNode(true);
|
|
113
|
+
Array.from(fragment.childNodes).forEach(child => this.#processNode(child));
|
|
114
|
+
this.#mount(fragment);
|
|
115
|
+
}
|
|
116
|
+
let token = ++this.#renderToken;
|
|
117
|
+
new Promise(resolve => {
|
|
118
|
+
let timeout = null;
|
|
119
|
+
let observer;
|
|
120
|
+
let setMyTimeout = () => {
|
|
121
|
+
clearTimeout(timeout);
|
|
122
|
+
timeout = setTimeout(() => {
|
|
123
|
+
observer.disconnect();
|
|
124
|
+
this.#promiseCallback(Array.from(this.children)).then(resolve);
|
|
125
|
+
}, 0);
|
|
126
|
+
};
|
|
127
|
+
observer = new MutationObserver((r) => {
|
|
128
|
+
if (r.some(rec => rec.type === 'childList'))
|
|
129
|
+
setMyTimeout();
|
|
130
|
+
});
|
|
131
|
+
observer.observe(this, { childList: true });
|
|
132
|
+
setMyTimeout();
|
|
133
|
+
}).then(() => {
|
|
134
|
+
if (token !== this.#renderToken) return;
|
|
135
|
+
this.#rendered = true;
|
|
136
|
+
this.dispatchEvent('rendered');
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#mount(node) {
|
|
141
|
+
HTMLElement.prototype.appendChild.call(this, node);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
clone(options = {}) {
|
|
145
|
+
let app = this.attrs.get('app');
|
|
146
|
+
return App.extend(app, options);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
watch(source, callback, options = {}) {
|
|
150
|
+
if (typeof callback !== 'function') return () => {};
|
|
151
|
+
let { deep = false, immediate = false } = options;
|
|
152
|
+
if (typeof source === 'string' && source.endsWith('()')) {
|
|
153
|
+
let methodName = source.slice(0, -2);
|
|
154
|
+
let original = this[methodName];
|
|
155
|
+
if (typeof original !== 'function') return () => {};
|
|
156
|
+
let entry = this.#methodWrappers.get(methodName);
|
|
157
|
+
if (!entry) {
|
|
158
|
+
entry = { original, handlers: [] };
|
|
159
|
+
this.#methodWrappers.set(methodName, entry);
|
|
160
|
+
let wrapper = (...args) => {
|
|
161
|
+
let result = original.apply(this, args);
|
|
162
|
+
let runHandlers = (res) => {
|
|
163
|
+
for (let h of [...entry.handlers])
|
|
164
|
+
h.callback(args, res, h.unwatch);
|
|
165
|
+
};
|
|
166
|
+
if (result instanceof Promise)
|
|
167
|
+
return result.then(r => { runHandlers(r); return r; });
|
|
168
|
+
runHandlers(result);
|
|
169
|
+
return result;
|
|
170
|
+
};
|
|
171
|
+
this[methodName] = wrapper;
|
|
172
|
+
}
|
|
173
|
+
let unwatch = () => {
|
|
174
|
+
let idx = entry.handlers.findIndex(h => h.unwatch === unwatch);
|
|
175
|
+
if (idx !== -1) entry.handlers.splice(idx, 1);
|
|
176
|
+
if (entry.handlers.length === 0) {
|
|
177
|
+
this[methodName] = entry.original;
|
|
178
|
+
this.#methodWrappers.delete(methodName);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
entry.handlers.push({ callback, unwatch });
|
|
182
|
+
return unwatch;
|
|
183
|
+
}
|
|
184
|
+
if (typeof source === 'string') {
|
|
185
|
+
let key = source;
|
|
186
|
+
let unwatch = () => {
|
|
187
|
+
let idx = this.#watchers.findIndex(w => w.unwatch === unwatch);
|
|
188
|
+
if (idx !== -1) this.#watchers.splice(idx, 1);
|
|
189
|
+
};
|
|
190
|
+
let w = { type: 'key', key, callback, deep, immediate, lastValue: undefined, unwatch };
|
|
191
|
+
this.#watchers.push(w);
|
|
192
|
+
if (immediate) {
|
|
193
|
+
let val = this.#getValueByPath(key);
|
|
194
|
+
w.lastValue = deep && val !== null && typeof val === 'object'
|
|
195
|
+
? JSON.parse(JSON.stringify(val)) : val;
|
|
196
|
+
callback(val, undefined, unwatch);
|
|
197
|
+
}
|
|
198
|
+
return unwatch;
|
|
199
|
+
}
|
|
200
|
+
if (typeof source === 'function') {
|
|
201
|
+
let getter = source;
|
|
202
|
+
let lastValue = undefined;
|
|
203
|
+
let entry = { type: 'getter', getter, callback, deep, lastValue, effect: null, unwatch: null };
|
|
204
|
+
this.#watchers.push(entry);
|
|
205
|
+
let unwatch = () => {
|
|
206
|
+
let idx = this.#watchers.indexOf(entry);
|
|
207
|
+
if (idx !== -1) this.#watchers.splice(idx, 1);
|
|
208
|
+
if (entry.effect) {
|
|
209
|
+
this.#deps.forEach((effects, k) => {
|
|
210
|
+
effects.delete(entry.effect);
|
|
211
|
+
if (effects.size === 0) this.#deps.delete(k);
|
|
212
|
+
});
|
|
213
|
+
this.#updateQueue.delete(entry.effect);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
entry.unwatch = unwatch;
|
|
217
|
+
let effectFn = () => {
|
|
218
|
+
let newVal = getter.call(this);
|
|
219
|
+
let changed = deep ? !this.#deepEqual(newVal, lastValue) : newVal !== lastValue;
|
|
220
|
+
if (changed) {
|
|
221
|
+
let oldVal = lastValue;
|
|
222
|
+
lastValue = deep && newVal !== null && typeof newVal === 'object'
|
|
223
|
+
? JSON.parse(JSON.stringify(newVal)) : newVal;
|
|
224
|
+
callback(newVal, oldVal, entry.unwatch);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
let effect = this.#effect(effectFn);
|
|
228
|
+
entry.effect = effect;
|
|
229
|
+
return unwatch;
|
|
230
|
+
}
|
|
231
|
+
return () => {};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
unwatch() {
|
|
235
|
+
[...this.#watchers].forEach(w => w.unwatch());
|
|
236
|
+
this.#methodWrappers.forEach(entry => {
|
|
237
|
+
[...entry.handlers].forEach(h => h.unwatch());
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
hasWatchers() {
|
|
242
|
+
let methodCount = 0;
|
|
243
|
+
this.#methodWrappers.forEach(entry => { methodCount += entry.handlers.length; });
|
|
244
|
+
return this.#watchers.length + methodCount;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
watched(source, callback, options = {}) {
|
|
248
|
+
if (typeof source === 'string' && source.endsWith('()')) {
|
|
249
|
+
let methodName = source.slice(0, -2);
|
|
250
|
+
let entry = this.#methodWrappers.get(methodName);
|
|
251
|
+
if (entry) [...entry.handlers].forEach(h => h.unwatch());
|
|
252
|
+
} else if (typeof source === 'string') {
|
|
253
|
+
this.#watchers
|
|
254
|
+
.filter(w => w.type === 'key' && w.key === source)
|
|
255
|
+
.forEach(w => w.unwatch());
|
|
256
|
+
} else if (typeof source === 'function') {
|
|
257
|
+
this.#watchers
|
|
258
|
+
.filter(w => w.type === 'getter' && w.getter === source)
|
|
259
|
+
.forEach(w => w.unwatch());
|
|
260
|
+
}
|
|
261
|
+
return this.watch(source, callback, options);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#track(key) {
|
|
265
|
+
if (this.#activeEffect) {
|
|
266
|
+
if (!this.#deps.has(key))
|
|
267
|
+
this.#deps.set(key, new Set());
|
|
268
|
+
this.#deps.get(key).add(this.#activeEffect);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
#trigger(key) {
|
|
273
|
+
this.#triggeredKeys.add(key);
|
|
274
|
+
let effects = this.#deps.get(key);
|
|
275
|
+
if (effects)
|
|
276
|
+
effects.forEach(effect => this.#updateQueue.add(effect));
|
|
277
|
+
if (this.#flushing) {
|
|
278
|
+
this.#pendingFlush = true;
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
this.#flushing = true;
|
|
282
|
+
const runFlush = () => {
|
|
283
|
+
this.#updateQueue.forEach(effect => {
|
|
284
|
+
try {
|
|
285
|
+
effect();
|
|
286
|
+
} catch (e) {
|
|
287
|
+
console.error('Effect execution error:', e);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
this.#updateQueue.clear();
|
|
291
|
+
|
|
292
|
+
let keys = Array.from(this.#triggeredKeys);
|
|
293
|
+
let toRun = new Set();
|
|
294
|
+
for (let key of keys) {
|
|
295
|
+
for (let w of this.#watchers) {
|
|
296
|
+
if (w.type !== 'key') continue;
|
|
297
|
+
let match = w.key === key || (w.deep && (key === w.key || key.startsWith(w.key + '.')));
|
|
298
|
+
if (match) toRun.add(w);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
for (let w of toRun) {
|
|
302
|
+
if (w.type !== 'key') continue;
|
|
303
|
+
let newVal = this.#getValueByPath(w.key);
|
|
304
|
+
let changed = w.deep ? !this.#deepEqual(newVal, w.lastValue) : newVal !== w.lastValue;
|
|
305
|
+
if (!changed) continue;
|
|
306
|
+
let oldVal = w.lastValue;
|
|
307
|
+
w.lastValue = w.deep && newVal !== null && typeof newVal === 'object'
|
|
308
|
+
? JSON.parse(JSON.stringify(newVal)) : newVal;
|
|
309
|
+
w.callback(newVal, oldVal, w.unwatch);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
this.#triggeredKeys.clear();
|
|
313
|
+
this.#flushing = false;
|
|
314
|
+
if (this.#pendingFlush) {
|
|
315
|
+
this.#pendingFlush = false;
|
|
316
|
+
this.#flushing = true;
|
|
317
|
+
Promise.resolve().then(runFlush);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
Promise.resolve().then(runFlush);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
#getValueByPath(path) {
|
|
324
|
+
return path.split('.').reduce((o, k) => o?.[k], this);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
#deepEqual(a, b) {
|
|
328
|
+
if (Object.is(a, b)) return true;
|
|
329
|
+
if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') return false;
|
|
330
|
+
let keysA = Object.keys(a);
|
|
331
|
+
let keysB = Object.keys(b);
|
|
332
|
+
if (keysA.length !== keysB.length) return false;
|
|
333
|
+
for (let k of keysA) {
|
|
334
|
+
if (!keysB.includes(k) || !this.#deepEqual(a[k], b[k])) return false;
|
|
335
|
+
}
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
#createReactiveProxy(obj, path = []) {
|
|
340
|
+
if (obj === null || typeof obj !== 'object' || obj.__isReactive)
|
|
341
|
+
return obj;
|
|
342
|
+
let $this = this;
|
|
343
|
+
let proxy = new Proxy(obj, {
|
|
344
|
+
get(target, prop, receiver) {
|
|
345
|
+
if (prop === '__isReactive') return true;
|
|
346
|
+
const pathSegment = typeof prop === 'symbol' ? '<symbol>' : prop;
|
|
347
|
+
let fullPath = [...path, pathSegment].join('.');
|
|
348
|
+
$this.#track(fullPath);
|
|
349
|
+
let value = Reflect.get(target, prop, receiver);
|
|
350
|
+
if (value !== null && typeof value === 'object' && !value.__isReactive)
|
|
351
|
+
return $this.#createReactiveProxy(value, [...path, pathSegment]);
|
|
352
|
+
return value;
|
|
353
|
+
},
|
|
354
|
+
set(target, prop, value, receiver) {
|
|
355
|
+
let oldValue = target[prop];
|
|
356
|
+
if (oldValue === value) return true;
|
|
357
|
+
let result = Reflect.set(target, prop, value, receiver);
|
|
358
|
+
const setPathSegment = typeof prop === 'symbol' ? '<symbol>' : prop;
|
|
359
|
+
let fullPath = [...path, setPathSegment].join('.');
|
|
360
|
+
$this.#trigger(fullPath);
|
|
361
|
+
if (Array.isArray(target) && (prop === 'length' || !isNaN(prop))) {
|
|
362
|
+
let arrayPath = path.join('.');
|
|
363
|
+
if (arrayPath)
|
|
364
|
+
$this.#trigger(arrayPath);
|
|
365
|
+
}
|
|
366
|
+
return result;
|
|
367
|
+
},
|
|
368
|
+
deleteProperty(target, prop) {
|
|
369
|
+
let result = Reflect.deleteProperty(target, prop);
|
|
370
|
+
const delPathSegment = typeof prop === 'symbol' ? '<symbol>' : prop;
|
|
371
|
+
let fullPath = [...path, delPathSegment].join('.');
|
|
372
|
+
$this.#trigger(fullPath);
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
return proxy;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
#createReactiveFunction(fn, key) {
|
|
380
|
+
let $this = this;
|
|
381
|
+
return new Proxy(fn, {
|
|
382
|
+
apply(target, thisArg, args) {
|
|
383
|
+
let result = Reflect.apply(target, $this, args);
|
|
384
|
+
if (result instanceof Promise)
|
|
385
|
+
return result.then(r => {
|
|
386
|
+
$this.#trigger(key);
|
|
387
|
+
return r;
|
|
388
|
+
});
|
|
389
|
+
$this.#trigger(key);
|
|
390
|
+
return result;
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
#effect(fn) {
|
|
396
|
+
let capturedLoopCtx = this.#loopContext ? { ...this.#loopContext } : null;
|
|
397
|
+
let wrappedEffect = () => {
|
|
398
|
+
let savedValues = null;
|
|
399
|
+
if (capturedLoopCtx) {
|
|
400
|
+
savedValues = {};
|
|
401
|
+
for (let key of Object.keys(capturedLoopCtx)) {
|
|
402
|
+
savedValues[key] = this[key];
|
|
403
|
+
this[key] = capturedLoopCtx[key];
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
this.#activeEffect = wrappedEffect;
|
|
407
|
+
try {
|
|
408
|
+
fn();
|
|
409
|
+
} finally {
|
|
410
|
+
this.#activeEffect = null;
|
|
411
|
+
if (savedValues) {
|
|
412
|
+
for (let key of Object.keys(savedValues)) {
|
|
413
|
+
if (savedValues[key] !== undefined)
|
|
414
|
+
this[key] = savedValues[key];
|
|
415
|
+
else
|
|
416
|
+
delete this[key];
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
wrappedEffect();
|
|
422
|
+
return wrappedEffect;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
#evalExpression(expr) {
|
|
426
|
+
try {
|
|
427
|
+
return new Function('$event', `with(this) { return (${expr}); }`).call(this);
|
|
428
|
+
} catch (e) {
|
|
429
|
+
console.warn('Expression evaluation error:', expr, e);
|
|
430
|
+
return undefined;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
#execStatement(statement, event = null) {
|
|
435
|
+
try {
|
|
436
|
+
return new Function('$event', `with(this) { ${statement} }`).call(this, event);
|
|
437
|
+
} catch (e) {
|
|
438
|
+
console.error('Statement execution error:', statement, e);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
#processTextInterpolation(textNode) {
|
|
443
|
+
let originalText = textNode.nodeValue;
|
|
444
|
+
let parts = [];
|
|
445
|
+
let expressions = [];
|
|
446
|
+
let lastIndex = 0;
|
|
447
|
+
let regex = /\{\{(.+?)\}\}/g;
|
|
448
|
+
let match;
|
|
449
|
+
while ((match = regex.exec(originalText)) !== null) {
|
|
450
|
+
if (match.index > lastIndex)
|
|
451
|
+
parts.push(originalText.substring(lastIndex, match.index));
|
|
452
|
+
let expr = match[1].trim();
|
|
453
|
+
parts.push(null);
|
|
454
|
+
expressions.push({ index: parts.length - 1, expr });
|
|
455
|
+
lastIndex = regex.lastIndex;
|
|
456
|
+
}
|
|
457
|
+
if (lastIndex < originalText.length)
|
|
458
|
+
parts.push(originalText.substring(lastIndex));
|
|
459
|
+
let effect = this.#effect(() => {
|
|
460
|
+
expressions.forEach(({ index, expr }) => {
|
|
461
|
+
let value = this.#evalExpression(expr);
|
|
462
|
+
parts[index] = value == null ? '' : String(value);
|
|
463
|
+
});
|
|
464
|
+
textNode.nodeValue = parts.join('');
|
|
465
|
+
});
|
|
466
|
+
this.#bindings.push({ type: 'text', node: textNode, effect });
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
#bindEvent(element, attr) {
|
|
470
|
+
let eventName = attr.name.slice(1);
|
|
471
|
+
let statement = attr.value;
|
|
472
|
+
let capturedLoopCtx = this.#loopContext ? { ...this.#loopContext } : null;
|
|
473
|
+
let handler = (e) => {
|
|
474
|
+
if (capturedLoopCtx) {
|
|
475
|
+
let savedValues = {};
|
|
476
|
+
for (let key of Object.keys(capturedLoopCtx)) {
|
|
477
|
+
savedValues[key] = this[key];
|
|
478
|
+
this[key] = capturedLoopCtx[key];
|
|
479
|
+
}
|
|
480
|
+
this.#execStatement(statement, e);
|
|
481
|
+
for (let key of Object.keys(savedValues)) {
|
|
482
|
+
if (savedValues[key] !== undefined)
|
|
483
|
+
this[key] = savedValues[key];
|
|
484
|
+
else
|
|
485
|
+
delete this[key];
|
|
486
|
+
}
|
|
487
|
+
} else
|
|
488
|
+
this.#execStatement(statement, e);
|
|
489
|
+
};
|
|
490
|
+
element.addEventListener(eventName, handler);
|
|
491
|
+
this.#bindings.push({ type: 'event', element, eventName, handler });
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
#bindShow(element, attr) {
|
|
495
|
+
let expr = attr.value;
|
|
496
|
+
let effect = this.#effect(() => {
|
|
497
|
+
let visible = this.#evalExpression(expr);
|
|
498
|
+
if (visible)
|
|
499
|
+
element.style.display = '';
|
|
500
|
+
else
|
|
501
|
+
element.style.display = 'none';
|
|
502
|
+
});
|
|
503
|
+
this.#bindings.push({ type: 'show', element, effect });
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
#bindModel(element, attr) {
|
|
507
|
+
let prop = attr.value;
|
|
508
|
+
let effect = this.#effect(() => {
|
|
509
|
+
let value = this.#evalExpression(prop);
|
|
510
|
+
if (element.tagName === 'INPUT' && element.type === 'checkbox')
|
|
511
|
+
element.checked = !!value;
|
|
512
|
+
else if (element.tagName === 'INPUT' && element.type === 'radio')
|
|
513
|
+
element.checked = element.value === value;
|
|
514
|
+
else if (element.tagName === 'SELECT')
|
|
515
|
+
element.value = value;
|
|
516
|
+
else
|
|
517
|
+
element.value = value == null ? '' : value;
|
|
518
|
+
});
|
|
519
|
+
let capturedLoopCtx = this.#loopContext ? { ...this.#loopContext } : null;
|
|
520
|
+
let eventName = element.tagName === 'SELECT' ? 'change' : 'input';
|
|
521
|
+
let handler = (e) => {
|
|
522
|
+
let newValue;
|
|
523
|
+
if (element.type === 'checkbox')
|
|
524
|
+
newValue = element.checked;
|
|
525
|
+
else if (element.type === 'number' || element.type === 'range')
|
|
526
|
+
newValue = element.valueAsNumber;
|
|
527
|
+
else
|
|
528
|
+
newValue = element.value;
|
|
529
|
+
if (capturedLoopCtx) {
|
|
530
|
+
let savedValues = {};
|
|
531
|
+
for (let key of Object.keys(capturedLoopCtx)) {
|
|
532
|
+
savedValues[key] = this[key];
|
|
533
|
+
this[key] = capturedLoopCtx[key];
|
|
534
|
+
}
|
|
535
|
+
try {
|
|
536
|
+
new Function('value', `with(this) { ${prop} = value; }`).call(this, newValue);
|
|
537
|
+
} catch (e) {
|
|
538
|
+
console.error('Model binding error:', prop, e);
|
|
539
|
+
}
|
|
540
|
+
for (let key of Object.keys(savedValues)) {
|
|
541
|
+
if (savedValues[key] !== undefined)
|
|
542
|
+
this[key] = savedValues[key];
|
|
543
|
+
else
|
|
544
|
+
delete this[key];
|
|
545
|
+
}
|
|
546
|
+
} else {
|
|
547
|
+
try {
|
|
548
|
+
new Function('value', `with(this) { ${prop} = value; }`).call(this, newValue);
|
|
549
|
+
} catch (e) {
|
|
550
|
+
console.error('Model binding error:', prop, e);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
element.addEventListener(eventName, handler);
|
|
555
|
+
this.#bindings.push({ type: 'model', element, effect, handler });
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
#bindConditional(element, attr) {
|
|
559
|
+
let expr = attr.value;
|
|
560
|
+
let placeholder = document.createComment(`if: ${expr}`);
|
|
561
|
+
element.parentNode.insertBefore(placeholder, element);
|
|
562
|
+
let chain = [{ expr, element, bindings: [], processed: false }];
|
|
563
|
+
let nextSibling = element.nextElementSibling;
|
|
564
|
+
while (nextSibling) {
|
|
565
|
+
if (nextSibling.hasAttribute('#else-if')) {
|
|
566
|
+
let elseIfExpr = nextSibling.getAttribute('#else-if');
|
|
567
|
+
chain.push({ expr: elseIfExpr, element: nextSibling, bindings: [], processed: false });
|
|
568
|
+
nextSibling.removeAttribute('#else-if');
|
|
569
|
+
let next = nextSibling.nextElementSibling;
|
|
570
|
+
nextSibling.remove();
|
|
571
|
+
nextSibling = next;
|
|
572
|
+
} else if (nextSibling.hasAttribute('#else')) {
|
|
573
|
+
chain.push({ expr: 'true', element: nextSibling, bindings: [], processed: false });
|
|
574
|
+
nextSibling.removeAttribute('#else');
|
|
575
|
+
nextSibling.remove();
|
|
576
|
+
break;
|
|
577
|
+
} else
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
element.removeAttribute(attr.name);
|
|
581
|
+
element.remove();
|
|
582
|
+
let currentBranch = null;
|
|
583
|
+
let effect = this.#effect(() => {
|
|
584
|
+
let matchedBranch = null;
|
|
585
|
+
for (let branch of chain) {
|
|
586
|
+
let condition = this.#evalExpression(branch.expr);
|
|
587
|
+
if (condition) {
|
|
588
|
+
matchedBranch = branch;
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (currentBranch !== matchedBranch) {
|
|
593
|
+
if (currentBranch) {
|
|
594
|
+
if (currentBranch.element && currentBranch.element.parentNode)
|
|
595
|
+
currentBranch.element.remove();
|
|
596
|
+
this.#cleanupBindings(currentBranch.bindings);
|
|
597
|
+
currentBranch.bindings = [];
|
|
598
|
+
}
|
|
599
|
+
if (matchedBranch) {
|
|
600
|
+
placeholder.parentNode.insertBefore(matchedBranch.element, placeholder.nextSibling);
|
|
601
|
+
if (!matchedBranch.processed) {
|
|
602
|
+
let bindingsLengthBefore = this.#bindings.length;
|
|
603
|
+
this.#processNode(matchedBranch.element);
|
|
604
|
+
matchedBranch.bindings = this.#bindings.slice(bindingsLengthBefore);
|
|
605
|
+
matchedBranch.processed = true;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
currentBranch = matchedBranch;
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
this.#bindings.push({ type: 'conditional', placeholder, chain, effect });
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
#bindLoop(element, attr) {
|
|
615
|
+
let loopExpr = attr.value;
|
|
616
|
+
let match = loopExpr.match(/^\s*(\w+)\s+in\s+(.+)$/);
|
|
617
|
+
if (!match) {
|
|
618
|
+
console.error('Invalid for syntax:', loopExpr);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
let itemName = match[1];
|
|
622
|
+
let collectionExpr = match[2];
|
|
623
|
+
let parent = element.parentNode;
|
|
624
|
+
let nextSibling = element.nextSibling;
|
|
625
|
+
element.removeAttribute(attr.name);
|
|
626
|
+
let template = element.cloneNode(true);
|
|
627
|
+
element.remove();
|
|
628
|
+
let renderedItems = [];
|
|
629
|
+
let effect = this.#effect(() => {
|
|
630
|
+
let collection = this.#evalExpression(collectionExpr);
|
|
631
|
+
renderedItems.forEach(item => {
|
|
632
|
+
if (item.node && item.node.parentNode)
|
|
633
|
+
item.node.remove();
|
|
634
|
+
this.#cleanupBindings(item.bindings);
|
|
635
|
+
});
|
|
636
|
+
renderedItems = [];
|
|
637
|
+
let entries;
|
|
638
|
+
if (Array.isArray(collection)) {
|
|
639
|
+
entries = collection.map((value, index) => ({ value, key: undefined, index }));
|
|
640
|
+
} else if (collection !== null && typeof collection === 'object') {
|
|
641
|
+
entries = Object.entries(collection).map(([key, value], index) => ({ value, key, index }));
|
|
642
|
+
} else {
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
if (entries.length === 0) return;
|
|
646
|
+
let fragment = document.createDocumentFragment();
|
|
647
|
+
entries.forEach(({ value: item, key, index }) => {
|
|
648
|
+
let clone = template.cloneNode(true);
|
|
649
|
+
let originalItem = this[itemName];
|
|
650
|
+
let originalIndex = this.$index;
|
|
651
|
+
let originalKey = this.$key;
|
|
652
|
+
this[itemName] = item;
|
|
653
|
+
this.$index = index;
|
|
654
|
+
if (key !== undefined) this.$key = key;
|
|
655
|
+
let previousLoopCtx = this.#loopContext;
|
|
656
|
+
let loopCtx = { ...previousLoopCtx, [itemName]: item, $index: index };
|
|
657
|
+
if (key !== undefined) loopCtx.$key = key;
|
|
658
|
+
this.#loopContext = loopCtx;
|
|
659
|
+
let bindingsLengthBefore = this.#bindings.length;
|
|
660
|
+
this.#processNode(clone);
|
|
661
|
+
let itemBindings = this.#bindings.slice(bindingsLengthBefore);
|
|
662
|
+
this.#loopContext = previousLoopCtx;
|
|
663
|
+
if (originalItem !== undefined)
|
|
664
|
+
this[itemName] = originalItem;
|
|
665
|
+
else
|
|
666
|
+
delete this[itemName];
|
|
667
|
+
if (originalIndex !== undefined)
|
|
668
|
+
this.$index = originalIndex;
|
|
669
|
+
else
|
|
670
|
+
delete this.$index;
|
|
671
|
+
if (originalKey !== undefined)
|
|
672
|
+
this.$key = originalKey;
|
|
673
|
+
else
|
|
674
|
+
delete this.$key;
|
|
675
|
+
fragment.appendChild(clone);
|
|
676
|
+
renderedItems.push({ node: clone, bindings: itemBindings });
|
|
677
|
+
});
|
|
678
|
+
parent.insertBefore(fragment, nextSibling);
|
|
679
|
+
});
|
|
680
|
+
this.#bindings.push({ type: 'loop', parent, nextSibling, template, effect });
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
#bindAttribute(element, attr) {
|
|
684
|
+
let attrName = attr.name;
|
|
685
|
+
let attrValue = attr.value;
|
|
686
|
+
let parts = [];
|
|
687
|
+
let expressions = [];
|
|
688
|
+
let lastIndex = 0;
|
|
689
|
+
let regex = /\{\{(.+?)\}\}/g;
|
|
690
|
+
let match;
|
|
691
|
+
while ((match = regex.exec(attrValue)) !== null) {
|
|
692
|
+
if (match.index > lastIndex)
|
|
693
|
+
parts.push(attrValue.substring(lastIndex, match.index));
|
|
694
|
+
let expr = match[1].trim();
|
|
695
|
+
parts.push(null);
|
|
696
|
+
expressions.push({ index: parts.length - 1, expr });
|
|
697
|
+
lastIndex = regex.lastIndex;
|
|
698
|
+
}
|
|
699
|
+
if (lastIndex < attrValue.length)
|
|
700
|
+
parts.push(attrValue.substring(lastIndex));
|
|
701
|
+
let effect = this.#effect(() => {
|
|
702
|
+
expressions.forEach(({ index, expr }) => {
|
|
703
|
+
let value = this.#evalExpression(expr);
|
|
704
|
+
parts[index] = value == null ? '' : String(value);
|
|
705
|
+
});
|
|
706
|
+
let finalValue = parts.join('');
|
|
707
|
+
if (App.#boolean_attributes.has(attrName)) {
|
|
708
|
+
let falsy = finalValue === '' || finalValue === 'false' || finalValue === '0' || finalValue === 'null' || finalValue === 'undefined' || !finalValue;
|
|
709
|
+
if (falsy)
|
|
710
|
+
element.removeAttribute(attrName);
|
|
711
|
+
else
|
|
712
|
+
element.setAttribute(attrName, '');
|
|
713
|
+
} else
|
|
714
|
+
element.setAttribute(attrName, finalValue);
|
|
715
|
+
});
|
|
716
|
+
this.#bindings.push({ type: 'attribute', element, attrName, effect });
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
#processDirectives(element) {
|
|
720
|
+
let attrs = Array.from(element.attributes);
|
|
721
|
+
for (let attr of attrs) {
|
|
722
|
+
if (attr.name === '#if') {
|
|
723
|
+
this.#bindConditional(element, attr);
|
|
724
|
+
element.removeAttribute(attr.name);
|
|
725
|
+
return true;
|
|
726
|
+
}
|
|
727
|
+
else if (attr.name === '#for') {
|
|
728
|
+
this.#bindLoop(element, attr);
|
|
729
|
+
element.removeAttribute(attr.name);
|
|
730
|
+
return true;
|
|
731
|
+
}
|
|
732
|
+
else if (attr.name === '#show') {
|
|
733
|
+
this.#bindShow(element, attr);
|
|
734
|
+
element.removeAttribute(attr.name);
|
|
735
|
+
}
|
|
736
|
+
else if (attr.name === '#model') {
|
|
737
|
+
this.#bindModel(element, attr);
|
|
738
|
+
element.removeAttribute(attr.name);
|
|
739
|
+
}
|
|
740
|
+
else if (attr.name === '#once')
|
|
741
|
+
element.removeAttribute(attr.name);
|
|
742
|
+
else if (attr.name === '#pre') {
|
|
743
|
+
element.removeAttribute(attr.name);
|
|
744
|
+
return true;
|
|
745
|
+
}
|
|
746
|
+
else if (attr.name.startsWith('@')) {
|
|
747
|
+
this.#bindEvent(element, attr);
|
|
748
|
+
element.removeAttribute(attr.name);
|
|
749
|
+
}
|
|
750
|
+
else if (/\{\{.+?\}\}/.test(attr.value))
|
|
751
|
+
this.#bindAttribute(element, attr);
|
|
752
|
+
}
|
|
753
|
+
return false;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
#processNode(node) {
|
|
757
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
758
|
+
let elementRemoved = this.#processDirectives(node);
|
|
759
|
+
if (node.localName === 'st-app')
|
|
760
|
+
return;
|
|
761
|
+
if (!elementRemoved && (node.parentNode || node.childNodes.length > 0))
|
|
762
|
+
Array.from(node.childNodes).forEach(child => this.#processNode(child));
|
|
763
|
+
}
|
|
764
|
+
else if (node.nodeType === Node.TEXT_NODE && /\{\{.+?\}\}/.test(node.nodeValue))
|
|
765
|
+
this.#processTextInterpolation(node);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
#boot() {
|
|
769
|
+
let app = this.attrs.get('app');
|
|
770
|
+
let prototype = App.#apps.get(app);
|
|
771
|
+
|
|
772
|
+
if (prototype) {
|
|
773
|
+
this.addEventListener('setup', prototype.setup);
|
|
774
|
+
|
|
775
|
+
Object.entries(prototype.events).forEach(([event, handler]) =>
|
|
776
|
+
this.addEventListener(event, handler)
|
|
777
|
+
);
|
|
778
|
+
this.#template = prototype.template;
|
|
779
|
+
let config = prototype.config;
|
|
780
|
+
let descriptors = Object.getOwnPropertyDescriptors(config);
|
|
781
|
+
let skipKeys = new Set(['app', 'setup', 'template', 'events']);
|
|
782
|
+
Object.entries(descriptors).forEach(([key, descriptor]) => {
|
|
783
|
+
if (skipKeys.has(key)) return;
|
|
784
|
+
if (descriptor.get) {
|
|
785
|
+
let originalGetter = descriptor.get;
|
|
786
|
+
this.#getters.set(key, originalGetter);
|
|
787
|
+
Object.defineProperty(this, key, {
|
|
788
|
+
get() {
|
|
789
|
+
this.#track(key);
|
|
790
|
+
return originalGetter.call(this);
|
|
791
|
+
},
|
|
792
|
+
enumerable: descriptor.enumerable,
|
|
793
|
+
configurable: true
|
|
794
|
+
});
|
|
795
|
+
} else if (descriptor.value !== undefined) {
|
|
796
|
+
let value = descriptor.value;
|
|
797
|
+
let internalValue = value;
|
|
798
|
+
if (typeof value === 'object' && value !== null)
|
|
799
|
+
internalValue = this.#createReactiveProxy(value, [key]);
|
|
800
|
+
else if (typeof value === 'function')
|
|
801
|
+
internalValue = this.#createReactiveFunction(value, key);
|
|
802
|
+
Object.defineProperty(this, key, {
|
|
803
|
+
get() {
|
|
804
|
+
this.#track(key);
|
|
805
|
+
return internalValue;
|
|
806
|
+
},
|
|
807
|
+
set(newValue) {
|
|
808
|
+
if (internalValue !== newValue) {
|
|
809
|
+
if (typeof newValue === 'object' && newValue !== null)
|
|
810
|
+
internalValue = this.#createReactiveProxy(newValue, [key]);
|
|
811
|
+
else
|
|
812
|
+
internalValue = newValue;
|
|
813
|
+
this.#trigger(key);
|
|
814
|
+
}
|
|
815
|
+
},
|
|
816
|
+
enumerable: true,
|
|
817
|
+
configurable: true
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
if (typeof this.#booted === 'function')
|
|
823
|
+
this.#booted();
|
|
824
|
+
|
|
825
|
+
this.#booted = true;
|
|
826
|
+
} else {
|
|
827
|
+
if (!this.attrs.get('app')) {
|
|
828
|
+
if (typeof this.#booted === 'function')
|
|
829
|
+
this.#booted();
|
|
830
|
+
this.#booted = true;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
async #promiseCallback(node_array) {
|
|
836
|
+
await Promise.all(node_array.map(node => new Promise(resolve => {
|
|
837
|
+
let timeout = null;
|
|
838
|
+
let setMyTimeout = () => {
|
|
839
|
+
clearTimeout(timeout);
|
|
840
|
+
timeout = setTimeout(() => {
|
|
841
|
+
observer.disconnect();
|
|
842
|
+
this.#promiseCallback(Array.from(node.children)).then(resolve);
|
|
843
|
+
}, 0);
|
|
844
|
+
};
|
|
845
|
+
let observer = new MutationObserver((r) => {
|
|
846
|
+
if (r.some(rec => rec.type === 'childList'))
|
|
847
|
+
setMyTimeout();
|
|
848
|
+
});
|
|
849
|
+
observer.observe(node, { childList: true });
|
|
850
|
+
setMyTimeout();
|
|
851
|
+
})));
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
#serializeForAttr(value) {
|
|
855
|
+
if (value === null || value === undefined) return '';
|
|
856
|
+
if (value === true) return 'true';
|
|
857
|
+
if (value === false) return 'false';
|
|
858
|
+
if (typeof value === 'number') return String(value);
|
|
859
|
+
if (typeof value === 'object') return JSON.stringify(value);
|
|
860
|
+
return String(value);
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
#applySingleAttributeOption(name) {
|
|
864
|
+
if (!name.startsWith(':') || name === 'app') return;
|
|
865
|
+
let propName = name.replace(/^:+/,'');
|
|
866
|
+
let raw = this.getAttribute(name);
|
|
867
|
+
let descriptor = Object.getOwnPropertyDescriptor(this, propName);
|
|
868
|
+
let val = raw;
|
|
869
|
+
if (!raw) {
|
|
870
|
+
if (descriptor && (descriptor.get || descriptor.value !== undefined)) {
|
|
871
|
+
let existingVal = this[propName];
|
|
872
|
+
let serialized = this.#serializeForAttr(existingVal);
|
|
873
|
+
if (this.getAttribute(name) !== serialized)
|
|
874
|
+
this.setAttribute(name, serialized);
|
|
875
|
+
val = existingVal;
|
|
876
|
+
descriptor = null;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (descriptor && descriptor.set === undefined && descriptor.writable === false) return;
|
|
880
|
+
if (descriptor && descriptor.get !== undefined && descriptor.set === undefined) return;
|
|
881
|
+
if (raw) {
|
|
882
|
+
if (raw === 'true') val = true;
|
|
883
|
+
else if (raw === 'false') val = false;
|
|
884
|
+
else if (/^-?\d+(\.\d+)?$/.test(raw)) val = Number(raw);
|
|
885
|
+
else { try { val = JSON.parse(raw); } catch { val = raw; } }
|
|
886
|
+
}
|
|
887
|
+
if (!descriptor || !descriptor.set) {
|
|
888
|
+
let internalValue = val;
|
|
889
|
+
if (typeof val === 'object' && val !== null)
|
|
890
|
+
internalValue = this.#createReactiveProxy(val, [propName]);
|
|
891
|
+
else if (typeof val === 'function')
|
|
892
|
+
internalValue = this.#createReactiveFunction(val, propName);
|
|
893
|
+
let attrName = name;
|
|
894
|
+
Object.defineProperty(this, propName, {
|
|
895
|
+
get() {
|
|
896
|
+
this.#track(propName);
|
|
897
|
+
return internalValue;
|
|
898
|
+
},
|
|
899
|
+
set(newValue) {
|
|
900
|
+
if (internalValue !== newValue) {
|
|
901
|
+
if (typeof newValue === 'object' && newValue !== null)
|
|
902
|
+
internalValue = this.#createReactiveProxy(newValue, [propName]);
|
|
903
|
+
else
|
|
904
|
+
internalValue = newValue;
|
|
905
|
+
this.#trigger(propName);
|
|
906
|
+
let serialized = this.#serializeForAttr(newValue);
|
|
907
|
+
if (this.getAttribute(attrName) !== serialized)
|
|
908
|
+
this.setAttribute(attrName, serialized);
|
|
909
|
+
}
|
|
910
|
+
},
|
|
911
|
+
enumerable: true,
|
|
912
|
+
configurable: true
|
|
913
|
+
});
|
|
914
|
+
if (typeof val === 'object' && val !== null) {
|
|
915
|
+
let unwatch = this.watch(propName, () => {
|
|
916
|
+
let v = this[propName];
|
|
917
|
+
let s = this.#serializeForAttr(v);
|
|
918
|
+
if (this.getAttribute(name) !== s)
|
|
919
|
+
this.setAttribute(name, s);
|
|
920
|
+
}, { deep: true });
|
|
921
|
+
this.#bindings.push({ type: 'attrSync', unwatch });
|
|
922
|
+
}
|
|
923
|
+
} else
|
|
924
|
+
this[propName] = val;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
928
|
+
if (name.startsWith(':') && name !== 'app' && oldValue !== newValue && this.#booted === true)
|
|
929
|
+
this.#applySingleAttributeOption(name);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
connectedCallback() {
|
|
933
|
+
App.#instances.add(this);
|
|
934
|
+
if (this.#booted === true) {
|
|
935
|
+
if (!this.#attrObserver) {
|
|
936
|
+
this.#attrObserver = new MutationObserver(mutations => {
|
|
937
|
+
for (let m of mutations) {
|
|
938
|
+
if (m.type === 'attributes' && m.attributeName && m.attributeName.startsWith(':') && m.attributeName !== 'app')
|
|
939
|
+
this.#applySingleAttributeOption(m.attributeName);
|
|
940
|
+
}
|
|
941
|
+
});
|
|
942
|
+
this.#attrObserver.observe(this, { attributes: true });
|
|
943
|
+
}
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
this.#boot();
|
|
947
|
+
Promise.all([
|
|
948
|
+
new Promise(resolve => {
|
|
949
|
+
if (this.#booted === true) return resolve();
|
|
950
|
+
this.#booted = resolve;
|
|
951
|
+
}),
|
|
952
|
+
new Promise(resolve => {
|
|
953
|
+
let timeout = null;
|
|
954
|
+
let setMyTimeout = () => {
|
|
955
|
+
clearTimeout(timeout);
|
|
956
|
+
timeout = setTimeout(() => {
|
|
957
|
+
observer.disconnect();
|
|
958
|
+
this.#promiseCallback(Array.from(this.children)).then(resolve);
|
|
959
|
+
}, 0);
|
|
960
|
+
};
|
|
961
|
+
let observer = new MutationObserver((r) => {
|
|
962
|
+
if (r.some(rec => rec.type === 'childList'))
|
|
963
|
+
setMyTimeout();
|
|
964
|
+
});
|
|
965
|
+
observer.observe(this, { childList: true });
|
|
966
|
+
setMyTimeout();
|
|
967
|
+
})
|
|
968
|
+
]).then(() => {
|
|
969
|
+
this.dispatchEvent('setup');
|
|
970
|
+
for (let name of this.attrs.keys()) {
|
|
971
|
+
if (!name.startsWith(':') || name === 'app') continue;
|
|
972
|
+
this.#applySingleAttributeOption(name);
|
|
973
|
+
}
|
|
974
|
+
let appliedInline = false;
|
|
975
|
+
if (this.childNodes.length > 0) {
|
|
976
|
+
let hasNonEmptyContent = Array.from(this.childNodes).some(node =>
|
|
977
|
+
node.nodeType === Node.ELEMENT_NODE ||
|
|
978
|
+
(node.nodeType === Node.TEXT_NODE && node.textContent.trim() !== '')
|
|
979
|
+
);
|
|
980
|
+
if (hasNonEmptyContent) {
|
|
981
|
+
if (this.#template) {
|
|
982
|
+
this.applyTemplate(this.innerHTML);
|
|
983
|
+
appliedInline = true;
|
|
984
|
+
} else
|
|
985
|
+
this.#template = this.innerHTML;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
if (!appliedInline)
|
|
989
|
+
this.applyTemplate();
|
|
990
|
+
if (this.#booted === true && !this.#attrObserver) {
|
|
991
|
+
this.#attrObserver = new MutationObserver(mutations => {
|
|
992
|
+
for (let m of mutations) {
|
|
993
|
+
if (m.type === 'attributes' && m.attributeName && m.attributeName.startsWith(':') && m.attributeName !== 'app')
|
|
994
|
+
this.#applySingleAttributeOption(m.attributeName);
|
|
995
|
+
}
|
|
996
|
+
});
|
|
997
|
+
this.#attrObserver.observe(this, { attributes: true });
|
|
998
|
+
}
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
disconnectedCallback() {
|
|
1003
|
+
if (this.#attrObserver) {
|
|
1004
|
+
this.#attrObserver.disconnect();
|
|
1005
|
+
this.#attrObserver = null;
|
|
1006
|
+
}
|
|
1007
|
+
this.#cleanupBindings(this.#bindings);
|
|
1008
|
+
this.#bindings = [];
|
|
1009
|
+
for (let w of this.#watchers) {
|
|
1010
|
+
if (w.type === 'getter' && w.effect) {
|
|
1011
|
+
this.#deps.forEach((effects, k) => {
|
|
1012
|
+
effects.delete(w.effect);
|
|
1013
|
+
if (effects.size === 0) this.#deps.delete(k);
|
|
1014
|
+
});
|
|
1015
|
+
this.#updateQueue.delete(w.effect);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
this.#watchers = [];
|
|
1019
|
+
this.#methodWrappers.forEach((entry, methodName) => {
|
|
1020
|
+
this[methodName] = entry.original;
|
|
1021
|
+
});
|
|
1022
|
+
this.#methodWrappers.clear();
|
|
1023
|
+
this.#triggeredKeys.clear();
|
|
1024
|
+
this.#deps.clear();
|
|
1025
|
+
this.#updateQueue.clear();
|
|
1026
|
+
App.#instances.delete(this);
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
toggleAttribute(name, value_1 = '', value_2 = '') {
|
|
1030
|
+
let value = this.getAttribute(name);
|
|
1031
|
+
|
|
1032
|
+
this[
|
|
1033
|
+
value_1 == '' && value_2 == '' && (value || this.hasAttribute(name))
|
|
1034
|
+
? 'removeAttribute'
|
|
1035
|
+
: 'setAttribute'
|
|
1036
|
+
](name, value != value_1 ? value_1 : value_2);
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
static { customElements.define('st-app', this); }
|
|
1040
|
+
}
|