simplyview 2.1.0 → 3.0.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.
- package/dist/simply.app.js +1120 -0
- package/dist/simply.app.js.map +7 -0
- package/dist/simply.app.min.js +2 -0
- package/dist/simply.app.min.js.map +7 -0
- package/dist/simply.everything.js +1583 -2025
- package/dist/simply.everything.js.map +7 -0
- package/dist/simply.everything.min.js +2 -0
- package/dist/simply.everything.min.js.map +7 -0
- package/package.json +8 -3
- package/src/action.mjs +12 -0
- package/src/activate.mjs +63 -0
- package/src/app.mjs +40 -0
- package/src/bind.mjs +572 -0
- package/src/command.mjs +125 -0
- package/src/everything.mjs +27 -0
- package/src/include.mjs +191 -0
- package/src/key.mjs +55 -0
- package/src/model.mjs +151 -0
- package/src/route.mjs +222 -0
- package/src/state.mjs +536 -0
- package/js/.eslintrc.json +0 -29
- package/js/simply.action.js +0 -115
- package/js/simply.activate.js +0 -79
- package/js/simply.api.js +0 -228
- package/js/simply.app.js +0 -63
- package/js/simply.collect.js +0 -72
- package/js/simply.command.js +0 -196
- package/js/simply.include.js +0 -226
- package/js/simply.keyboard.js +0 -62
- package/js/simply.modules.js +0 -22
- package/js/simply.observe.js +0 -349
- package/js/simply.path.js +0 -48
- package/js/simply.render.js +0 -125
- package/js/simply.resize.js +0 -80
- package/js/simply.route.js +0 -234
- package/js/simply.view.js +0 -35
- package/js/simply.viewmodel.js +0 -189
- /package/{js/simply.include.next.js → src/include.next.js} +0 -0
|
@@ -0,0 +1,1120 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
// src/route.mjs
|
|
3
|
+
function routes(options) {
|
|
4
|
+
return new SimplyRoute(options);
|
|
5
|
+
}
|
|
6
|
+
var SimplyRoute = class {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.root = options.root || "/";
|
|
9
|
+
this.app = options.app;
|
|
10
|
+
this.clear();
|
|
11
|
+
if (options.routes) {
|
|
12
|
+
this.load(options.routes);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
load(routes2) {
|
|
16
|
+
parseRoutes(routes2, this.routeInfo);
|
|
17
|
+
}
|
|
18
|
+
clear() {
|
|
19
|
+
this.routeInfo = [];
|
|
20
|
+
this.listeners = {
|
|
21
|
+
match: {},
|
|
22
|
+
call: {},
|
|
23
|
+
finish: {}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
match(path, options) {
|
|
27
|
+
let args = {
|
|
28
|
+
path,
|
|
29
|
+
options
|
|
30
|
+
};
|
|
31
|
+
args = this.runListeners("match", args);
|
|
32
|
+
path = args.path ? args.path : path;
|
|
33
|
+
let matches;
|
|
34
|
+
if (!path) {
|
|
35
|
+
if (this.match(document.location.pathname + document.location.hash)) {
|
|
36
|
+
return true;
|
|
37
|
+
} else {
|
|
38
|
+
return this.match(document.location.pathname);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
path = getPath(path);
|
|
42
|
+
for (let route of this.routeInfo) {
|
|
43
|
+
matches = route.match.exec(path);
|
|
44
|
+
if (matches && matches.length) {
|
|
45
|
+
var params = {};
|
|
46
|
+
route.params.forEach((key, i) => {
|
|
47
|
+
if (key == "*") {
|
|
48
|
+
key = "remainder";
|
|
49
|
+
}
|
|
50
|
+
params[key] = matches[i + 1];
|
|
51
|
+
});
|
|
52
|
+
Object.assign(params, options);
|
|
53
|
+
args.route = route;
|
|
54
|
+
args.params = params;
|
|
55
|
+
args = this.runListeners("call", args);
|
|
56
|
+
params = args.params ? args.params : params;
|
|
57
|
+
args.result = route.action.call(route, params);
|
|
58
|
+
this.runListeners("finish", args);
|
|
59
|
+
return args.result;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (path && path[path.length - 1] != "/") {
|
|
63
|
+
return this.match(path + "/", options);
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
runListeners(action, params) {
|
|
68
|
+
if (!Object.keys(this.listeners[action])) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
Object.keys(this.listeners[action]).forEach((route) => {
|
|
72
|
+
var routeRe = getRegexpFromRoute(route);
|
|
73
|
+
if (routeRe.exec(params.path)) {
|
|
74
|
+
var result;
|
|
75
|
+
for (let callback of this.listeners[action][route]) {
|
|
76
|
+
result = callback.call(this.app, params);
|
|
77
|
+
if (result) {
|
|
78
|
+
params = result;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return params;
|
|
84
|
+
}
|
|
85
|
+
handleEvents() {
|
|
86
|
+
globalThis.addEventListener("popstate", () => {
|
|
87
|
+
if (this.match(getPath(document.location.pathname + document.location.hash, this.root)) === false) {
|
|
88
|
+
this.match(getPath(document.location.pathname, this.root));
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
globalThis.document.addEventListener("click", (evt) => {
|
|
92
|
+
if (evt.ctrlKey) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (evt.which != 1) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
var link = evt.target;
|
|
99
|
+
while (link && link.tagName != "A") {
|
|
100
|
+
link = link.parentElement;
|
|
101
|
+
}
|
|
102
|
+
if (link && link.pathname && link.hostname == globalThis.location.hostname && !link.link && !link.dataset.simplyCommand) {
|
|
103
|
+
let path = getPath(link.pathname + link.hash, this.root);
|
|
104
|
+
if (!this.has(path)) {
|
|
105
|
+
path = getPath(link.pathname, this.root);
|
|
106
|
+
}
|
|
107
|
+
if (this.has(path)) {
|
|
108
|
+
let params = this.runListeners("goto", { path });
|
|
109
|
+
if (params.path) {
|
|
110
|
+
this.goto(params.path);
|
|
111
|
+
}
|
|
112
|
+
evt.preventDefault();
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
goto(path) {
|
|
119
|
+
history.pushState({}, "", getURL(path));
|
|
120
|
+
return this.match(path);
|
|
121
|
+
}
|
|
122
|
+
has(path) {
|
|
123
|
+
path = getPath(path, this.root);
|
|
124
|
+
for (let route of this.routeInfo) {
|
|
125
|
+
var matches = route.match.exec(path);
|
|
126
|
+
if (matches && matches.length) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
addListener(action, route, callback) {
|
|
133
|
+
if (["goto", "match", "call", "finish"].indexOf(action) == -1) {
|
|
134
|
+
throw new Error("Unknown action " + action);
|
|
135
|
+
}
|
|
136
|
+
if (!this.listeners[action][route]) {
|
|
137
|
+
this.listeners[action][route] = [];
|
|
138
|
+
}
|
|
139
|
+
this.listeners[action][route].push(callback);
|
|
140
|
+
}
|
|
141
|
+
removeListener(action, route, callback) {
|
|
142
|
+
if (["match", "call", "finish"].indexOf(action) == -1) {
|
|
143
|
+
throw new Error("Unknown action " + action);
|
|
144
|
+
}
|
|
145
|
+
if (!this.listeners[action][route]) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this.listeners[action][route] = this.listeners[action][route].filter((listener) => {
|
|
149
|
+
return listener != callback;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
init(options) {
|
|
153
|
+
if (options.root) {
|
|
154
|
+
this.root = options.root;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
function getPath(path, root = "/") {
|
|
159
|
+
if (path.substring(0, root.length) == root || root[root.length - 1] == "/" && path.length == root.length - 1 && path == root.substring(0, path.length)) {
|
|
160
|
+
path = path.substring(root.length);
|
|
161
|
+
}
|
|
162
|
+
if (path[0] != "/" && path[0] != "#") {
|
|
163
|
+
path = "/" + path;
|
|
164
|
+
}
|
|
165
|
+
return path;
|
|
166
|
+
}
|
|
167
|
+
function getURL(path, root) {
|
|
168
|
+
path = getPath(path, root);
|
|
169
|
+
if (root[root.length - 1] === "/" && path[0] === "/") {
|
|
170
|
+
path = path.substring(1);
|
|
171
|
+
}
|
|
172
|
+
return root + path;
|
|
173
|
+
}
|
|
174
|
+
function getRegexpFromRoute(route) {
|
|
175
|
+
return new RegExp("^" + route.replace(/:\w+/g, "([^/]+)").replace(/:\*/, "(.*)"));
|
|
176
|
+
}
|
|
177
|
+
function parseRoutes(routes2) {
|
|
178
|
+
let routeInfo = [];
|
|
179
|
+
const paths = Object.keys(routes2);
|
|
180
|
+
const matchParams = /:(\w+|\*)/g;
|
|
181
|
+
for (let path of paths) {
|
|
182
|
+
let matches = [];
|
|
183
|
+
let params = [];
|
|
184
|
+
do {
|
|
185
|
+
matches = matchParams.exec(path);
|
|
186
|
+
if (matches) {
|
|
187
|
+
params.push(matches[1]);
|
|
188
|
+
}
|
|
189
|
+
} while (matches);
|
|
190
|
+
routeInfo.push({
|
|
191
|
+
match: getRegexpFromRoute(path),
|
|
192
|
+
params,
|
|
193
|
+
action: routes2[path]
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
return routeInfo;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// src/command.mjs
|
|
200
|
+
var SimplyCommands = class {
|
|
201
|
+
constructor(options = {}) {
|
|
202
|
+
if (!options.app) {
|
|
203
|
+
options.app = {};
|
|
204
|
+
}
|
|
205
|
+
if (!options.app.container) {
|
|
206
|
+
options.app.container = document.body;
|
|
207
|
+
}
|
|
208
|
+
this.$handlers = options.handlers || defaultHandlers;
|
|
209
|
+
if (options.commands) {
|
|
210
|
+
Object.assign(this, options.commands);
|
|
211
|
+
}
|
|
212
|
+
const commandHandler = (evt) => {
|
|
213
|
+
const command = getCommand(evt, this.$handlers);
|
|
214
|
+
if (!command) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (!this[command.name]) {
|
|
218
|
+
console.error("simply.command: undefined command " + command.name, command.source);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const shouldContinue = this[command.name].call(options.app, command.source, command.value);
|
|
222
|
+
if (shouldContinue === false) {
|
|
223
|
+
evt.preventDefault();
|
|
224
|
+
evt.stopPropagation();
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
options.app.container.addEventListener("click", commandHandler);
|
|
229
|
+
options.app.container.addEventListener("submit", commandHandler);
|
|
230
|
+
options.app.container.addEventListener("change", commandHandler);
|
|
231
|
+
options.app.container.addEventListener("input", commandHandler);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
function commands(options = {}) {
|
|
235
|
+
return new SimplyCommands(options);
|
|
236
|
+
}
|
|
237
|
+
function getCommand(evt, handlers) {
|
|
238
|
+
var el = evt.target.closest("[data-simply-command]");
|
|
239
|
+
if (el) {
|
|
240
|
+
for (let handler of handlers) {
|
|
241
|
+
if (el.matches(handler.match)) {
|
|
242
|
+
if (handler.check(el, evt)) {
|
|
243
|
+
return {
|
|
244
|
+
name: el.dataset.simplyCommand,
|
|
245
|
+
source: el,
|
|
246
|
+
value: handler.get(el)
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
var defaultHandlers = [
|
|
256
|
+
{
|
|
257
|
+
match: "input,select,textarea",
|
|
258
|
+
get: function(el) {
|
|
259
|
+
if (el.tagName === "SELECT" && el.multiple) {
|
|
260
|
+
let values = [];
|
|
261
|
+
for (let option of el.options) {
|
|
262
|
+
if (option.selected) {
|
|
263
|
+
values.push(option.value);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return values;
|
|
267
|
+
}
|
|
268
|
+
return el.dataset.simplyValue || el.value;
|
|
269
|
+
},
|
|
270
|
+
check: function(el, evt) {
|
|
271
|
+
return evt.type == "change" || el.dataset.simplyImmediate && evt.type == "input";
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
match: "a,button",
|
|
276
|
+
get: function(el) {
|
|
277
|
+
return el.dataset.simplyValue || el.href || el.value;
|
|
278
|
+
},
|
|
279
|
+
check: function(el, evt) {
|
|
280
|
+
return evt.type == "click" && evt.ctrlKey == false && evt.button == 0;
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
match: "form",
|
|
285
|
+
get: function(el) {
|
|
286
|
+
let data = {};
|
|
287
|
+
for (let input of Array.from(el.elements)) {
|
|
288
|
+
if (input.tagName == "INPUT" && (input.type == "checkbox" || input.type == "radio")) {
|
|
289
|
+
if (!input.checked) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (data[input.name] && !Array.isArray(data[input.name])) {
|
|
294
|
+
data[input.name] = [data[input.name]];
|
|
295
|
+
}
|
|
296
|
+
if (Array.isArray(data[input.name])) {
|
|
297
|
+
data[input.name].push(input.value);
|
|
298
|
+
} else {
|
|
299
|
+
data[input.name] = input.value;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return data;
|
|
303
|
+
},
|
|
304
|
+
check: function(el, evt) {
|
|
305
|
+
return evt.type == "submit";
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
match: "*",
|
|
310
|
+
get: function(el) {
|
|
311
|
+
return el.dataset.simplyValue;
|
|
312
|
+
},
|
|
313
|
+
check: function(el, evt) {
|
|
314
|
+
return evt.type == "click" && evt.ctrlKey == false && evt.button == 0;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
];
|
|
318
|
+
|
|
319
|
+
// src/action.mjs
|
|
320
|
+
function actions(options) {
|
|
321
|
+
if (options.app) {
|
|
322
|
+
const actionHandler = {
|
|
323
|
+
get: (target, property) => {
|
|
324
|
+
return target[property].bind(options.app);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
return new Proxy(options.actions, actionHandler);
|
|
328
|
+
} else {
|
|
329
|
+
return options;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// src/key.mjs
|
|
334
|
+
var SimplyKeys = class {
|
|
335
|
+
constructor(options = {}) {
|
|
336
|
+
if (!options.app) {
|
|
337
|
+
options.app = {};
|
|
338
|
+
}
|
|
339
|
+
if (!options.app.container) {
|
|
340
|
+
options.app.container = document.body;
|
|
341
|
+
}
|
|
342
|
+
Object.assign(this, options.keys);
|
|
343
|
+
const keyHandler = (e) => {
|
|
344
|
+
if (e.isComposing || e.keyCode === 229) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (e.defaultPrevented) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (!e.target) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
let selectedKeyboard = "default";
|
|
354
|
+
if (e.target.closest("[data-simply-keyboard]")) {
|
|
355
|
+
selectedKeyboard = e.target.closest("[data-simply-keyboard]").dataset.simplyKeyboard;
|
|
356
|
+
}
|
|
357
|
+
let key = "";
|
|
358
|
+
if (e.ctrlKey && e.keyCode != 17) {
|
|
359
|
+
key += "Control+";
|
|
360
|
+
}
|
|
361
|
+
if (e.metaKey && e.keyCode != 224) {
|
|
362
|
+
key += "Meta+";
|
|
363
|
+
}
|
|
364
|
+
if (e.altKey && e.keyCode != 18) {
|
|
365
|
+
key += "Alt+";
|
|
366
|
+
}
|
|
367
|
+
if (e.shiftKey && e.keyCode != 16) {
|
|
368
|
+
key += "Shift+";
|
|
369
|
+
}
|
|
370
|
+
key += e.key;
|
|
371
|
+
if (this[selectedKeyboard] && this[selectedKeyboard][key]) {
|
|
372
|
+
let keyboard = this[selectedKeyboard];
|
|
373
|
+
keyboard[key].call(options.app, e);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
options.app.container.addEventListener("keydown", keyHandler);
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
function keys(options = {}) {
|
|
380
|
+
return new SimplyKeys(options);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// src/state.mjs
|
|
384
|
+
var iterate = Symbol("iterate");
|
|
385
|
+
if (!Symbol.xRay) {
|
|
386
|
+
Symbol.xRay = Symbol("xRay");
|
|
387
|
+
}
|
|
388
|
+
var signalHandler = {
|
|
389
|
+
get: (target, property, receiver) => {
|
|
390
|
+
if (property === Symbol.xRay) {
|
|
391
|
+
return target;
|
|
392
|
+
}
|
|
393
|
+
const value = target?.[property];
|
|
394
|
+
notifyGet(receiver, property);
|
|
395
|
+
if (typeof value === "function") {
|
|
396
|
+
if (Array.isArray(target)) {
|
|
397
|
+
return (...args) => {
|
|
398
|
+
let l = target.length;
|
|
399
|
+
let result = value.apply(receiver, args);
|
|
400
|
+
if (l != target.length) {
|
|
401
|
+
notifySet(receiver, makeContext("length", { was: l, now: target.length }));
|
|
402
|
+
}
|
|
403
|
+
return result;
|
|
404
|
+
};
|
|
405
|
+
} else if (target instanceof Set || target instanceof Map) {
|
|
406
|
+
return (...args) => {
|
|
407
|
+
let s = target.size;
|
|
408
|
+
let result = value.apply(target, args);
|
|
409
|
+
if (s != target.size) {
|
|
410
|
+
notifySet(receiver, makeContext("size", { was: s, now: target.size }));
|
|
411
|
+
}
|
|
412
|
+
if (["set", "add", "clear", "delete"].includes(property)) {
|
|
413
|
+
notifySet(receiver, makeContext({ entries: {}, forEach: {}, has: {}, keys: {}, values: {}, [Symbol.iterator]: {} }));
|
|
414
|
+
}
|
|
415
|
+
return result;
|
|
416
|
+
};
|
|
417
|
+
} else if (target instanceof HTMLElement || target instanceof Number || target instanceof String || target instanceof Boolean) {
|
|
418
|
+
return value.bind(target);
|
|
419
|
+
} else {
|
|
420
|
+
return value.bind(receiver);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (value && typeof value == "object") {
|
|
424
|
+
return signal(value);
|
|
425
|
+
}
|
|
426
|
+
return value;
|
|
427
|
+
},
|
|
428
|
+
set: (target, property, value, receiver) => {
|
|
429
|
+
value = value?.[Symbol.xRay] || value;
|
|
430
|
+
let current = target[property];
|
|
431
|
+
if (current !== value) {
|
|
432
|
+
target[property] = value;
|
|
433
|
+
notifySet(receiver, makeContext(property, { was: current, now: value }));
|
|
434
|
+
}
|
|
435
|
+
if (typeof current === "undefined") {
|
|
436
|
+
notifySet(receiver, makeContext(iterate, {}));
|
|
437
|
+
}
|
|
438
|
+
return true;
|
|
439
|
+
},
|
|
440
|
+
has: (target, property) => {
|
|
441
|
+
let receiver = signals.get(target);
|
|
442
|
+
if (receiver) {
|
|
443
|
+
notifyGet(receiver, property);
|
|
444
|
+
}
|
|
445
|
+
return Object.hasOwn(target, property);
|
|
446
|
+
},
|
|
447
|
+
deleteProperty: (target, property) => {
|
|
448
|
+
if (typeof target[property] !== "undefined") {
|
|
449
|
+
let current = target[property];
|
|
450
|
+
delete target[property];
|
|
451
|
+
let receiver = signals.get(target);
|
|
452
|
+
notifySet(receiver, makeContext(property, { delete: true, was: current }));
|
|
453
|
+
}
|
|
454
|
+
return true;
|
|
455
|
+
},
|
|
456
|
+
defineProperty: (target, property, descriptor) => {
|
|
457
|
+
if (typeof target[property] === "undefined") {
|
|
458
|
+
let receiver = signals.get(target);
|
|
459
|
+
notifySet(receiver, makeContext(iterate, {}));
|
|
460
|
+
}
|
|
461
|
+
return Object.defineProperty(target, property, descriptor);
|
|
462
|
+
},
|
|
463
|
+
ownKeys: (target) => {
|
|
464
|
+
let receiver = signals.get(target);
|
|
465
|
+
notifyGet(receiver, iterate);
|
|
466
|
+
return Reflect.ownKeys(target);
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
var signals = /* @__PURE__ */ new WeakMap();
|
|
470
|
+
function signal(v) {
|
|
471
|
+
if (!signals.has(v)) {
|
|
472
|
+
signals.set(v, new Proxy(v, signalHandler));
|
|
473
|
+
}
|
|
474
|
+
return signals.get(v);
|
|
475
|
+
}
|
|
476
|
+
var batchedListeners = /* @__PURE__ */ new Set();
|
|
477
|
+
var batchMode = 0;
|
|
478
|
+
function notifySet(self, context = {}) {
|
|
479
|
+
let listeners = [];
|
|
480
|
+
context.forEach((change, property) => {
|
|
481
|
+
let propListeners = getListeners(self, property);
|
|
482
|
+
if (propListeners?.length) {
|
|
483
|
+
for (let listener of propListeners) {
|
|
484
|
+
addContext(listener, makeContext(property, change));
|
|
485
|
+
}
|
|
486
|
+
listeners = listeners.concat(propListeners);
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
listeners = new Set(listeners.filter(Boolean));
|
|
490
|
+
if (listeners) {
|
|
491
|
+
if (batchMode) {
|
|
492
|
+
batchedListeners = batchedListeners.union(listeners);
|
|
493
|
+
} else {
|
|
494
|
+
const currentEffect = computeStack[computeStack.length - 1];
|
|
495
|
+
for (let listener of Array.from(listeners)) {
|
|
496
|
+
if (listener != currentEffect && listener?.needsUpdate) {
|
|
497
|
+
listener();
|
|
498
|
+
}
|
|
499
|
+
clearContext(listener);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function makeContext(property, change) {
|
|
505
|
+
let context = /* @__PURE__ */ new Map();
|
|
506
|
+
if (typeof property === "object") {
|
|
507
|
+
for (let prop in property) {
|
|
508
|
+
context.set(prop, property[prop]);
|
|
509
|
+
}
|
|
510
|
+
} else {
|
|
511
|
+
context.set(property, change);
|
|
512
|
+
}
|
|
513
|
+
return context;
|
|
514
|
+
}
|
|
515
|
+
function addContext(listener, context) {
|
|
516
|
+
if (!listener.context) {
|
|
517
|
+
listener.context = context;
|
|
518
|
+
} else {
|
|
519
|
+
context.forEach((change, property) => {
|
|
520
|
+
listener.context.set(property, change);
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
listener.needsUpdate = true;
|
|
524
|
+
}
|
|
525
|
+
function clearContext(listener) {
|
|
526
|
+
delete listener.context;
|
|
527
|
+
delete listener.needsUpdate;
|
|
528
|
+
}
|
|
529
|
+
function notifyGet(self, property) {
|
|
530
|
+
let currentCompute = computeStack[computeStack.length - 1];
|
|
531
|
+
if (currentCompute) {
|
|
532
|
+
setListeners(self, property, currentCompute);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
var listenersMap = /* @__PURE__ */ new WeakMap();
|
|
536
|
+
var computeMap = /* @__PURE__ */ new WeakMap();
|
|
537
|
+
function getListeners(self, property) {
|
|
538
|
+
let listeners = listenersMap.get(self);
|
|
539
|
+
return listeners ? Array.from(listeners.get(property) || []) : [];
|
|
540
|
+
}
|
|
541
|
+
function setListeners(self, property, compute) {
|
|
542
|
+
if (!listenersMap.has(self)) {
|
|
543
|
+
listenersMap.set(self, /* @__PURE__ */ new Map());
|
|
544
|
+
}
|
|
545
|
+
let listeners = listenersMap.get(self);
|
|
546
|
+
if (!listeners.has(property)) {
|
|
547
|
+
listeners.set(property, /* @__PURE__ */ new Set());
|
|
548
|
+
}
|
|
549
|
+
listeners.get(property).add(compute);
|
|
550
|
+
if (!computeMap.has(compute)) {
|
|
551
|
+
computeMap.set(compute, /* @__PURE__ */ new Map());
|
|
552
|
+
}
|
|
553
|
+
let connectedSignals = computeMap.get(compute);
|
|
554
|
+
if (!connectedSignals.has(property)) {
|
|
555
|
+
connectedSignals.set(property, /* @__PURE__ */ new Set());
|
|
556
|
+
}
|
|
557
|
+
connectedSignals.get(property).add(self);
|
|
558
|
+
}
|
|
559
|
+
function clearListeners(compute) {
|
|
560
|
+
let connectedSignals = computeMap.get(compute);
|
|
561
|
+
if (connectedSignals) {
|
|
562
|
+
connectedSignals.forEach((property) => {
|
|
563
|
+
property.forEach((s) => {
|
|
564
|
+
let listeners = listenersMap.get(s);
|
|
565
|
+
if (listeners.has(property)) {
|
|
566
|
+
listeners.get(property).delete(compute);
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
var computeStack = [];
|
|
573
|
+
var effectStack = [];
|
|
574
|
+
var effectMap = /* @__PURE__ */ new WeakMap();
|
|
575
|
+
var signalStack = [];
|
|
576
|
+
function destroy(connectedSignal) {
|
|
577
|
+
const computeEffect = effectMap.get(connectedSignal)?.deref();
|
|
578
|
+
if (!computeEffect) {
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
clearListeners(computeEffect);
|
|
582
|
+
let fn = computeEffect.fn;
|
|
583
|
+
signals.remove(fn);
|
|
584
|
+
effectMap.delete(connectedSignal);
|
|
585
|
+
}
|
|
586
|
+
function throttledEffect(fn, throttleTime) {
|
|
587
|
+
if (effectStack.findIndex((f) => fn == f) !== -1) {
|
|
588
|
+
throw new Error("Recursive update() call", { cause: fn });
|
|
589
|
+
}
|
|
590
|
+
effectStack.push(fn);
|
|
591
|
+
let connectedSignal = signals.get(fn);
|
|
592
|
+
if (!connectedSignal) {
|
|
593
|
+
connectedSignal = signal({
|
|
594
|
+
current: null
|
|
595
|
+
});
|
|
596
|
+
signals.set(fn, connectedSignal);
|
|
597
|
+
}
|
|
598
|
+
let throttled = false;
|
|
599
|
+
let hasChange = true;
|
|
600
|
+
const computeEffect = function computeEffect2() {
|
|
601
|
+
if (signalStack.findIndex((s) => s == connectedSignal) !== -1) {
|
|
602
|
+
throw new Error("Cyclical dependency in update() call", { cause: fn });
|
|
603
|
+
}
|
|
604
|
+
if (throttled && throttled > Date.now()) {
|
|
605
|
+
hasChange = true;
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
clearListeners(computeEffect2);
|
|
609
|
+
computeStack.push(computeEffect2);
|
|
610
|
+
signalStack.push(connectedSignal);
|
|
611
|
+
let result;
|
|
612
|
+
try {
|
|
613
|
+
result = fn(computeEffect2, computeStack, signalStack);
|
|
614
|
+
} finally {
|
|
615
|
+
hasChange = false;
|
|
616
|
+
computeStack.pop();
|
|
617
|
+
signalStack.pop();
|
|
618
|
+
if (result instanceof Promise) {
|
|
619
|
+
result.then((result2) => {
|
|
620
|
+
connectedSignal.current = result2;
|
|
621
|
+
});
|
|
622
|
+
} else {
|
|
623
|
+
connectedSignal.current = result;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
throttled = Date.now() + throttleTime;
|
|
627
|
+
globalThis.setTimeout(() => {
|
|
628
|
+
if (hasChange) {
|
|
629
|
+
computeEffect2();
|
|
630
|
+
}
|
|
631
|
+
}, throttleTime);
|
|
632
|
+
};
|
|
633
|
+
computeEffect();
|
|
634
|
+
return connectedSignal;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// src/bind.mjs
|
|
638
|
+
var SimplyBind = class {
|
|
639
|
+
constructor(options) {
|
|
640
|
+
this.bindings = /* @__PURE__ */ new Map();
|
|
641
|
+
const defaultOptions = {
|
|
642
|
+
container: document.body,
|
|
643
|
+
attribute: "data-bind",
|
|
644
|
+
transformers: [],
|
|
645
|
+
defaultTransformers: [defaultTransformer]
|
|
646
|
+
};
|
|
647
|
+
if (!options?.root) {
|
|
648
|
+
throw new Error("bind needs at least options.root set");
|
|
649
|
+
}
|
|
650
|
+
this.options = Object.assign({}, defaultOptions, options);
|
|
651
|
+
const attribute = this.options.attribute;
|
|
652
|
+
const render = (el) => {
|
|
653
|
+
this.bindings.set(el, throttledEffect(() => {
|
|
654
|
+
const context = {
|
|
655
|
+
templates: el.querySelectorAll(":scope > template"),
|
|
656
|
+
path: this.getBindingPath(el)
|
|
657
|
+
};
|
|
658
|
+
context.value = getValueByPath(this.options.root, context.path);
|
|
659
|
+
context.element = el;
|
|
660
|
+
runTransformers(context);
|
|
661
|
+
}, 100));
|
|
662
|
+
};
|
|
663
|
+
const runTransformers = (context) => {
|
|
664
|
+
let transformers = this.options.defaultTransformers || [];
|
|
665
|
+
if (context.element.dataset.transform) {
|
|
666
|
+
context.element.dataset.transform.split(" ").filter(Boolean).forEach((t) => {
|
|
667
|
+
if (this.options.transformers[t]) {
|
|
668
|
+
transformers.push(this.options.transformers[t]);
|
|
669
|
+
} else {
|
|
670
|
+
console.warn("No transformer with name " + t + " configured", { cause: context.element });
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
let next;
|
|
675
|
+
for (let transformer of transformers) {
|
|
676
|
+
next = /* @__PURE__ */ ((next2, transformer2) => {
|
|
677
|
+
return (context2) => {
|
|
678
|
+
return transformer2.call(this, context2, next2);
|
|
679
|
+
};
|
|
680
|
+
})(next, transformer);
|
|
681
|
+
}
|
|
682
|
+
next(context);
|
|
683
|
+
};
|
|
684
|
+
const applyBindings = (bindings2) => {
|
|
685
|
+
for (let bindingEl of bindings2) {
|
|
686
|
+
render(bindingEl);
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
const updateBindings = (changes) => {
|
|
690
|
+
for (const change of changes) {
|
|
691
|
+
if (change.type == "childList" && change.addedNodes) {
|
|
692
|
+
for (let node of change.addedNodes) {
|
|
693
|
+
if (node instanceof HTMLElement) {
|
|
694
|
+
let bindings2 = Array.from(node.querySelectorAll(`[${attribute}]`));
|
|
695
|
+
if (node.matches(`[${attribute}]`)) {
|
|
696
|
+
bindings2.unshift(node);
|
|
697
|
+
}
|
|
698
|
+
if (bindings2.length) {
|
|
699
|
+
applyBindings(bindings2);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
this.observer = new MutationObserver((changes) => {
|
|
707
|
+
updateBindings(changes);
|
|
708
|
+
});
|
|
709
|
+
this.observer.observe(options.container, {
|
|
710
|
+
subtree: true,
|
|
711
|
+
childList: true
|
|
712
|
+
});
|
|
713
|
+
const bindings = this.options.container.querySelectorAll("[" + this.options.attribute + "]:not(template)");
|
|
714
|
+
if (bindings.length) {
|
|
715
|
+
applyBindings(bindings);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Finds the first matching template and creates a new DocumentFragment
|
|
720
|
+
* with the correct data bind attributes in it (prepends the current path)
|
|
721
|
+
*/
|
|
722
|
+
applyTemplate(context) {
|
|
723
|
+
const path = context.path;
|
|
724
|
+
const templates = context.templates;
|
|
725
|
+
const list = context.list;
|
|
726
|
+
const index = context.index;
|
|
727
|
+
const parent = context.parent;
|
|
728
|
+
const value = list ? list[index] : context.value;
|
|
729
|
+
let template = this.findTemplate(templates, value);
|
|
730
|
+
if (!template) {
|
|
731
|
+
let result = new DocumentFragment();
|
|
732
|
+
result.innerHTML = "<!-- no matching template -->";
|
|
733
|
+
return result;
|
|
734
|
+
}
|
|
735
|
+
let clone = template.content.cloneNode(true);
|
|
736
|
+
if (!clone.children?.length) {
|
|
737
|
+
throw new Error("template must contain a single html element", { cause: template });
|
|
738
|
+
}
|
|
739
|
+
if (clone.children.length > 1) {
|
|
740
|
+
throw new Error("template must contain a single root node", { cause: template });
|
|
741
|
+
}
|
|
742
|
+
const bindings = clone.querySelectorAll("[" + this.options.attribute + "]");
|
|
743
|
+
const attribute = this.options.attribute;
|
|
744
|
+
for (let binding of bindings) {
|
|
745
|
+
const bind2 = binding.getAttribute(attribute);
|
|
746
|
+
if (bind2.substring(0, "#root.".length) == "#root.") {
|
|
747
|
+
binding.setAttribute(attribute, bind2.substring("#root.".length));
|
|
748
|
+
} else if (bind2 == "#value" && index != null) {
|
|
749
|
+
binding.setAttribute(attribute, path + "." + index);
|
|
750
|
+
} else if (index != null) {
|
|
751
|
+
binding.setAttribute(attribute, path + "." + index + "." + bind2);
|
|
752
|
+
} else {
|
|
753
|
+
binding.setAttribute(attribute, parent + "." + bind2);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
if (typeof index !== "undefined") {
|
|
757
|
+
clone.children[0].setAttribute(attribute + "-key", index);
|
|
758
|
+
}
|
|
759
|
+
clone.children[0].$bindTemplate = template;
|
|
760
|
+
return clone;
|
|
761
|
+
}
|
|
762
|
+
getBindingPath(el) {
|
|
763
|
+
return el.getAttribute(this.options.attribute);
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Finds the first template from an array of templates that
|
|
767
|
+
* matches the given value.
|
|
768
|
+
*/
|
|
769
|
+
findTemplate(templates, value) {
|
|
770
|
+
const templateMatches = (t) => {
|
|
771
|
+
let path = this.getBindingPath(t);
|
|
772
|
+
let currentItem;
|
|
773
|
+
if (path) {
|
|
774
|
+
if (path.substr(0, 6) == "#root.") {
|
|
775
|
+
currentItem = getValueByPath(this.options.root, path);
|
|
776
|
+
} else {
|
|
777
|
+
currentItem = getValueByPath(value, path);
|
|
778
|
+
}
|
|
779
|
+
} else {
|
|
780
|
+
currentItem = value;
|
|
781
|
+
}
|
|
782
|
+
const strItem = "" + currentItem;
|
|
783
|
+
let matches = t.getAttribute(this.options.attribute + "-match");
|
|
784
|
+
if (matches) {
|
|
785
|
+
if (matches === "#empty" && !currentItem) {
|
|
786
|
+
return t;
|
|
787
|
+
} else if (matches === "#notempty" && currentItem) {
|
|
788
|
+
return t;
|
|
789
|
+
}
|
|
790
|
+
if (strItem.match(matches)) {
|
|
791
|
+
return t;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
if (!matches) {
|
|
795
|
+
if (currentItem) {
|
|
796
|
+
return t;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
let template = Array.from(templates).find(templateMatches);
|
|
801
|
+
let rel = template?.getAttribute("rel");
|
|
802
|
+
if (rel) {
|
|
803
|
+
let replacement = document.querySelector("template#" + rel);
|
|
804
|
+
if (!replacement) {
|
|
805
|
+
throw new Error("Could not find template with id " + rel);
|
|
806
|
+
}
|
|
807
|
+
template = replacement;
|
|
808
|
+
}
|
|
809
|
+
return template;
|
|
810
|
+
}
|
|
811
|
+
destroy() {
|
|
812
|
+
this.bindings.forEach((binding) => {
|
|
813
|
+
destroy(binding);
|
|
814
|
+
});
|
|
815
|
+
this.bindings = /* @__PURE__ */ new Map();
|
|
816
|
+
this.observer.disconnect();
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
function bind(options) {
|
|
820
|
+
return new SimplyBind(options);
|
|
821
|
+
}
|
|
822
|
+
function matchValue(a, b) {
|
|
823
|
+
if (a == "#empty" && !b) {
|
|
824
|
+
return true;
|
|
825
|
+
}
|
|
826
|
+
if (b == "#empty" && !a) {
|
|
827
|
+
return true;
|
|
828
|
+
}
|
|
829
|
+
if ("" + a == "" + b) {
|
|
830
|
+
return true;
|
|
831
|
+
}
|
|
832
|
+
return false;
|
|
833
|
+
}
|
|
834
|
+
function getValueByPath(root, path) {
|
|
835
|
+
let parts = path.split(".");
|
|
836
|
+
let curr = root;
|
|
837
|
+
let part, prevPart;
|
|
838
|
+
while (parts.length && curr) {
|
|
839
|
+
part = parts.shift();
|
|
840
|
+
if (part == "#key") {
|
|
841
|
+
return prevPart;
|
|
842
|
+
} else if (part == "#value") {
|
|
843
|
+
return curr;
|
|
844
|
+
} else if (part == "#root") {
|
|
845
|
+
curr = root;
|
|
846
|
+
} else {
|
|
847
|
+
part = decodeURIComponent(part);
|
|
848
|
+
curr = curr[part];
|
|
849
|
+
prevPart = part;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
return curr;
|
|
853
|
+
}
|
|
854
|
+
function defaultTransformer(context) {
|
|
855
|
+
const el = context.element;
|
|
856
|
+
const templates = context.templates;
|
|
857
|
+
const templatesCount = templates.length;
|
|
858
|
+
const path = context.path;
|
|
859
|
+
const value = context.value;
|
|
860
|
+
const attribute = this.options.attribute;
|
|
861
|
+
if (Array.isArray(value) && templates?.length) {
|
|
862
|
+
transformArrayByTemplates.call(this, context);
|
|
863
|
+
} else if (typeof value == "object" && templates?.length) {
|
|
864
|
+
transformObjectByTemplates.call(this, context);
|
|
865
|
+
} else if (templates?.length) {
|
|
866
|
+
transformLiteralByTemplates.call(this, context);
|
|
867
|
+
} else if (el.tagName == "INPUT") {
|
|
868
|
+
transformInput.call(this, context);
|
|
869
|
+
} else if (el.tagName == "BUTTON") {
|
|
870
|
+
transformButton.call(this, context);
|
|
871
|
+
} else if (el.tagName == "SELECT") {
|
|
872
|
+
transformSelect.call(this, context);
|
|
873
|
+
} else if (el.tagName == "A") {
|
|
874
|
+
transformAnchor.call(this, context);
|
|
875
|
+
} else {
|
|
876
|
+
transformElement.call(this, context);
|
|
877
|
+
}
|
|
878
|
+
return context;
|
|
879
|
+
}
|
|
880
|
+
function transformArrayByTemplates(context) {
|
|
881
|
+
const el = context.element;
|
|
882
|
+
const templates = context.templates;
|
|
883
|
+
const templatesCount = templates.length;
|
|
884
|
+
const path = context.path;
|
|
885
|
+
const value = context.value;
|
|
886
|
+
const attribute = this.options.attribute;
|
|
887
|
+
let items = el.querySelectorAll(":scope > [" + attribute + "-key]");
|
|
888
|
+
let lastKey = 0;
|
|
889
|
+
let skipped = 0;
|
|
890
|
+
context.list = value;
|
|
891
|
+
for (let item of items) {
|
|
892
|
+
let currentKey = parseInt(item.getAttribute(attribute + "-key"));
|
|
893
|
+
if (currentKey > lastKey) {
|
|
894
|
+
context.index = lastKey;
|
|
895
|
+
el.insertBefore(this.applyTemplate(context), item);
|
|
896
|
+
} else if (currentKey < lastKey) {
|
|
897
|
+
item.remove();
|
|
898
|
+
} else {
|
|
899
|
+
let bindings = Array.from(item.querySelectorAll(`[${attribute}]`));
|
|
900
|
+
if (item.matches(`[${attribute}]`)) {
|
|
901
|
+
bindings.unshift(item);
|
|
902
|
+
}
|
|
903
|
+
let needsReplacement = bindings.find((b) => {
|
|
904
|
+
let databind = b.getAttribute(attribute);
|
|
905
|
+
return databind.substr(0, 5) !== "#root" && databind.substr(0, path.length) !== path;
|
|
906
|
+
});
|
|
907
|
+
if (!needsReplacement) {
|
|
908
|
+
if (item.$bindTemplate) {
|
|
909
|
+
let newTemplate = this.findTemplate(templates, value[lastKey]);
|
|
910
|
+
if (newTemplate != item.$bindTemplate) {
|
|
911
|
+
needsReplacement = true;
|
|
912
|
+
if (!newTemplate) {
|
|
913
|
+
skipped++;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
if (needsReplacement) {
|
|
919
|
+
context.index = lastKey;
|
|
920
|
+
el.replaceChild(this.applyTemplate(context), item);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
lastKey++;
|
|
924
|
+
if (lastKey >= value.length) {
|
|
925
|
+
break;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
items = el.querySelectorAll(":scope > [" + attribute + "-key]");
|
|
929
|
+
let length = items.length + skipped;
|
|
930
|
+
if (length > value.length) {
|
|
931
|
+
while (length > value.length) {
|
|
932
|
+
let child = el.querySelectorAll(":scope > :not(template)")?.[length - 1];
|
|
933
|
+
child?.remove();
|
|
934
|
+
length--;
|
|
935
|
+
}
|
|
936
|
+
} else if (length < value.length) {
|
|
937
|
+
while (length < value.length) {
|
|
938
|
+
context.index = length;
|
|
939
|
+
el.appendChild(this.applyTemplate(context));
|
|
940
|
+
length++;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
function transformObjectByTemplates(context) {
|
|
945
|
+
const el = context.element;
|
|
946
|
+
const templates = context.templates;
|
|
947
|
+
const templatesCount = templates.length;
|
|
948
|
+
const path = context.path;
|
|
949
|
+
const value = context.value;
|
|
950
|
+
const attribute = this.options.attribute;
|
|
951
|
+
context.list = value;
|
|
952
|
+
let list = Object.entries(value);
|
|
953
|
+
let items = el.querySelectorAll(":scope > [" + attribute + "-key]");
|
|
954
|
+
let current = 0;
|
|
955
|
+
let skipped = 0;
|
|
956
|
+
for (let item of items) {
|
|
957
|
+
if (current >= list.length) {
|
|
958
|
+
break;
|
|
959
|
+
}
|
|
960
|
+
let key = list[current][0];
|
|
961
|
+
current++;
|
|
962
|
+
let keypath = path + "." + key;
|
|
963
|
+
let needsReplacement;
|
|
964
|
+
const databind = item.getAttribute(attribute);
|
|
965
|
+
if (databind && databind.substr(0, keypath.length) != keypath) {
|
|
966
|
+
needsReplacement = true;
|
|
967
|
+
} else {
|
|
968
|
+
let bindings = Array.from(item.querySelectorAll(`[${attribute}]`));
|
|
969
|
+
needsReplacement = bindings.find((b) => {
|
|
970
|
+
const db = b.getAttribute(attribute);
|
|
971
|
+
return db.substr(0, 5) !== "#root" && db.substr(0, keypath.length) !== keypath;
|
|
972
|
+
});
|
|
973
|
+
if (!needsReplacement) {
|
|
974
|
+
if (item.$bindTemplate) {
|
|
975
|
+
let newTemplate = this.findTemplate(templates, value[key]);
|
|
976
|
+
if (newTemplate != item.$bindTemplate) {
|
|
977
|
+
needsReplacement = true;
|
|
978
|
+
if (!newTemplate) {
|
|
979
|
+
skipped++;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
if (needsReplacement) {
|
|
986
|
+
context.index = key;
|
|
987
|
+
let clone = this.applyTemplate(context);
|
|
988
|
+
el.replaceChild(clone, item);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
items = el.querySelectorAll(":scope > [" + attribute + "-key]");
|
|
992
|
+
let length = items.length + skipped;
|
|
993
|
+
if (length > list.length) {
|
|
994
|
+
while (length > list.length) {
|
|
995
|
+
let child = el.querySelectorAll(":scope > :not(template)")?.[length - 1];
|
|
996
|
+
child?.remove();
|
|
997
|
+
length--;
|
|
998
|
+
}
|
|
999
|
+
} else if (length < list.length) {
|
|
1000
|
+
while (length < list.length) {
|
|
1001
|
+
context.index = list[length][0];
|
|
1002
|
+
el.appendChild(this.applyTemplate(context));
|
|
1003
|
+
length++;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
function transformLiteralByTemplates(context) {
|
|
1008
|
+
const el = context.element;
|
|
1009
|
+
const templates = context.templates;
|
|
1010
|
+
const value = context.value;
|
|
1011
|
+
const attribute = this.options.attribute;
|
|
1012
|
+
const rendered = el.querySelector(":scope > :not(template)");
|
|
1013
|
+
const template = this.findTemplate(templates, value);
|
|
1014
|
+
context.parent = el.parentElement?.closest(`[${attribute}]`)?.getAttribute(attribute) || "#root";
|
|
1015
|
+
if (rendered) {
|
|
1016
|
+
if (template) {
|
|
1017
|
+
if (rendered?.$bindTemplate != template) {
|
|
1018
|
+
const clone = this.applyTemplate(context);
|
|
1019
|
+
el.replaceChild(clone, rendered);
|
|
1020
|
+
}
|
|
1021
|
+
} else {
|
|
1022
|
+
el.removeChild(rendered);
|
|
1023
|
+
}
|
|
1024
|
+
} else if (template) {
|
|
1025
|
+
const clone = this.applyTemplate(context);
|
|
1026
|
+
el.appendChild(clone);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
function transformInput(context) {
|
|
1030
|
+
const el = context.element;
|
|
1031
|
+
const value = context.value;
|
|
1032
|
+
if (el.type == "checkbox" || el.type == "radio") {
|
|
1033
|
+
if (matchValue(el.value, value)) {
|
|
1034
|
+
el.checked = true;
|
|
1035
|
+
} else {
|
|
1036
|
+
el.checked = false;
|
|
1037
|
+
}
|
|
1038
|
+
} else if (!matchValue(el.value, value)) {
|
|
1039
|
+
el.value = "" + value;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
function transformButton(context) {
|
|
1043
|
+
const el = context.element;
|
|
1044
|
+
const value = context.value;
|
|
1045
|
+
if (!matchValue(el.value, value)) {
|
|
1046
|
+
el.value = "" + value;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
function transformSelect(context) {
|
|
1050
|
+
const el = context.element;
|
|
1051
|
+
const value = context.value;
|
|
1052
|
+
if (el.multiple) {
|
|
1053
|
+
if (Array.isArray(value)) {
|
|
1054
|
+
for (let option of el.options) {
|
|
1055
|
+
if (value.indexOf(option.value) === false) {
|
|
1056
|
+
option.selected = false;
|
|
1057
|
+
} else {
|
|
1058
|
+
option.selected = true;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
} else {
|
|
1063
|
+
let option = el.options.find((o) => matchValue(o.value, value));
|
|
1064
|
+
if (option) {
|
|
1065
|
+
option.selected = true;
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
function transformAnchor(context) {
|
|
1070
|
+
const el = context.element;
|
|
1071
|
+
const value = context.value;
|
|
1072
|
+
if (value?.innerHTML && !matchValue(el.innerHTML, value.innerHTML)) {
|
|
1073
|
+
el.innerHTML = "" + value.innerHTML;
|
|
1074
|
+
}
|
|
1075
|
+
if (value?.href && !matchValue(el.href, value.href)) {
|
|
1076
|
+
el.href = "" + value.href;
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
function transformElement(context) {
|
|
1080
|
+
const el = context.element;
|
|
1081
|
+
const value = context.value;
|
|
1082
|
+
if (!matchValue(el.innerHTML, value)) {
|
|
1083
|
+
el.innerHTML = "" + value;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
// src/app.mjs
|
|
1088
|
+
var SimplyApp = class {
|
|
1089
|
+
constructor(options = {}) {
|
|
1090
|
+
this.container = options.container || document.body;
|
|
1091
|
+
if (!options.state) {
|
|
1092
|
+
options.state = {};
|
|
1093
|
+
}
|
|
1094
|
+
this.state = signal(options.state);
|
|
1095
|
+
if (options.commands) {
|
|
1096
|
+
this.commands = commands({ app: this, container: this.container, commands: options.commands });
|
|
1097
|
+
}
|
|
1098
|
+
if (options.keys) {
|
|
1099
|
+
this.keys = keys({ app: this, keys: options.keys });
|
|
1100
|
+
}
|
|
1101
|
+
if (options.routes) {
|
|
1102
|
+
this.routes = routes({ app: this, routes: options.routes });
|
|
1103
|
+
}
|
|
1104
|
+
if (options.actions) {
|
|
1105
|
+
this.actions = actions({ app: this, actions: options.actions });
|
|
1106
|
+
}
|
|
1107
|
+
let bindOptions = { container: this.container, root: this.state };
|
|
1108
|
+
if (options.defaultTransformers) {
|
|
1109
|
+
bindOptions.defaultTransformers = options.defaultTransformers;
|
|
1110
|
+
}
|
|
1111
|
+
if (options.transformers) {
|
|
1112
|
+
bindOptions.transformers = options.transformers;
|
|
1113
|
+
}
|
|
1114
|
+
this.bind = bind(bindOptions);
|
|
1115
|
+
}
|
|
1116
|
+
};
|
|
1117
|
+
function app(options = {}) {
|
|
1118
|
+
return new SimplyApp(options);
|
|
1119
|
+
}
|
|
1120
|
+
})();
|