simplyview 3.0.2 → 3.0.3
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/README.md +2 -5
- package/dist/simply.app.js +86 -713
- package/dist/simply.app.min.js +1 -1
- package/dist/simply.app.min.js.map +4 -4
- package/dist/simply.everything.js +101 -1020
- package/dist/simply.everything.min.js +1 -1
- package/dist/simply.everything.min.js.map +4 -4
- package/package.json +4 -4
- package/src/app.mjs +3 -13
- package/src/bind.mjs +132 -37
- package/src/command.mjs +1 -1
- package/src/everything.mjs +6 -10
- package/src/include.mjs +13 -10
- package/src/key.mjs +74 -21
- package/src/route.mjs +3 -2
- package/src/view.mjs +20 -0
- package/src/changes.md +0 -18
package/dist/simply.app.js
CHANGED
|
@@ -43,11 +43,11 @@
|
|
|
43
43
|
matches = route.match.exec(path);
|
|
44
44
|
if (matches && matches.length) {
|
|
45
45
|
var params = {};
|
|
46
|
-
route.params.forEach((key,
|
|
46
|
+
route.params.forEach((key, i2) => {
|
|
47
47
|
if (key == "*") {
|
|
48
48
|
key = "remainder";
|
|
49
49
|
}
|
|
50
|
-
params[key] = matches[
|
|
50
|
+
params[key] = matches[i2 + 1];
|
|
51
51
|
});
|
|
52
52
|
Object.assign(params, options);
|
|
53
53
|
args.route = route;
|
|
@@ -62,6 +62,8 @@
|
|
|
62
62
|
if (path && path[path.length - 1] != "/") {
|
|
63
63
|
return this.match(path + "/", options);
|
|
64
64
|
}
|
|
65
|
+
console.log(path, this.routeInfo);
|
|
66
|
+
process.exit();
|
|
65
67
|
return false;
|
|
66
68
|
}
|
|
67
69
|
runListeners(action, params) {
|
|
@@ -174,8 +176,7 @@
|
|
|
174
176
|
function getRegexpFromRoute(route) {
|
|
175
177
|
return new RegExp("^" + route.replace(/:\w+/g, "([^/]+)").replace(/:\*/, "(.*)"));
|
|
176
178
|
}
|
|
177
|
-
function parseRoutes(routes2) {
|
|
178
|
-
let routeInfo = [];
|
|
179
|
+
function parseRoutes(routes2, routeInfo) {
|
|
179
180
|
const paths = Object.keys(routes2);
|
|
180
181
|
const matchParams = /:(\w+|\*)/g;
|
|
181
182
|
for (let path of paths) {
|
|
@@ -219,7 +220,7 @@
|
|
|
219
220
|
return;
|
|
220
221
|
}
|
|
221
222
|
const shouldContinue = this[command.name].call(options.app, command.source, command.value);
|
|
222
|
-
if (shouldContinue
|
|
223
|
+
if (shouldContinue !== true) {
|
|
223
224
|
evt.preventDefault();
|
|
224
225
|
evt.stopPropagation();
|
|
225
226
|
return false;
|
|
@@ -331,7 +332,14 @@
|
|
|
331
332
|
}
|
|
332
333
|
|
|
333
334
|
// src/key.mjs
|
|
334
|
-
var
|
|
335
|
+
var KEY = Object.freeze({
|
|
336
|
+
Compose: 229,
|
|
337
|
+
Control: 17,
|
|
338
|
+
Meta: 224,
|
|
339
|
+
Alt: 18,
|
|
340
|
+
Shift: 16
|
|
341
|
+
});
|
|
342
|
+
var SimplyKey = class {
|
|
335
343
|
constructor(options = {}) {
|
|
336
344
|
if (!options.app) {
|
|
337
345
|
options.app = {};
|
|
@@ -341,7 +349,7 @@
|
|
|
341
349
|
}
|
|
342
350
|
Object.assign(this, options.keys);
|
|
343
351
|
const keyHandler = (e) => {
|
|
344
|
-
if (e.isComposing || e.keyCode ===
|
|
352
|
+
if (e.isComposing || e.keyCode === KEY.Compose) {
|
|
345
353
|
return;
|
|
346
354
|
}
|
|
347
355
|
if (e.defaultPrevented) {
|
|
@@ -354,714 +362,88 @@
|
|
|
354
362
|
if (e.target.closest("[data-simply-keyboard]")) {
|
|
355
363
|
selectedKeyboard = e.target.closest("[data-simply-keyboard]").dataset.simplyKeyboard;
|
|
356
364
|
}
|
|
357
|
-
let
|
|
358
|
-
if (e.ctrlKey && e.keyCode !=
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
if (e.metaKey && e.keyCode !=
|
|
362
|
-
|
|
363
|
-
}
|
|
364
|
-
if (e.altKey && e.keyCode !=
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
if (e.shiftKey && e.keyCode !=
|
|
368
|
-
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
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);
|
|
365
|
+
let keyCombination = [];
|
|
366
|
+
if (e.ctrlKey && e.keyCode != KEY.Control) {
|
|
367
|
+
keyCombination.push("Control");
|
|
368
|
+
}
|
|
369
|
+
if (e.metaKey && e.keyCode != KEY.Meta) {
|
|
370
|
+
keyCombination.push("Meta");
|
|
371
|
+
}
|
|
372
|
+
if (e.altKey && e.keyCode != KEY.Alt) {
|
|
373
|
+
keyCombination.push("Alt");
|
|
374
|
+
}
|
|
375
|
+
if (e.shiftKey && e.keyCode != KEY.Shift) {
|
|
376
|
+
keyCombination.push("Shift");
|
|
377
|
+
}
|
|
378
|
+
keyCombination.push(e.key.toLowerCase());
|
|
379
|
+
let keyboards = [];
|
|
380
|
+
let keyboardElement = event.target.closest("[data-simply-keyboard]");
|
|
381
|
+
while (keyboardElement) {
|
|
382
|
+
keyboards.push(keyboardElement.dataset.simplyKeyboard);
|
|
383
|
+
keyboardElement = keyboardElement.parentNode.closest("[data-simply-keyboard]");
|
|
384
|
+
}
|
|
385
|
+
keyboards.push("");
|
|
386
|
+
let keyboard, subkeyboard;
|
|
387
|
+
let separators = ["+", "-"];
|
|
388
|
+
for (i in keyboards) {
|
|
389
|
+
keyboard = keyboards[i];
|
|
390
|
+
if (keyboard == "") {
|
|
391
|
+
subkeyboard = "default";
|
|
392
|
+
} else {
|
|
393
|
+
subkeyboard = keyboard;
|
|
394
|
+
keyboard += ".";
|
|
567
395
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
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 });
|
|
396
|
+
for (let separator of separators) {
|
|
397
|
+
let keyString = keyCombination.join(separator);
|
|
398
|
+
if (this[subkeyboard] && typeof this[subkeyboard][keyString] == "function") {
|
|
399
|
+
let _continue = this[subkeyboard][keyString].call(this[subkeyboard], e);
|
|
400
|
+
if (!_continue) {
|
|
401
|
+
e.preventDefault();
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
671
404
|
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
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
|
-
}
|
|
405
|
+
if (typeof this[subkeyboard + keyString] == "function") {
|
|
406
|
+
let _continue = this[subkeyboard + keyString].call(this, e);
|
|
407
|
+
if (!_continue) {
|
|
408
|
+
e.preventDefault();
|
|
409
|
+
return;
|
|
701
410
|
}
|
|
702
411
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
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
|
-
return t;
|
|
796
|
-
}
|
|
797
|
-
};
|
|
798
|
-
let template = Array.from(templates).find(templateMatches);
|
|
799
|
-
let rel = template?.getAttribute("rel");
|
|
800
|
-
if (rel) {
|
|
801
|
-
let replacement = document.querySelector("template#" + rel);
|
|
802
|
-
if (!replacement) {
|
|
803
|
-
throw new Error("Could not find template with id " + rel);
|
|
804
|
-
}
|
|
805
|
-
template = replacement;
|
|
806
|
-
}
|
|
807
|
-
return template;
|
|
808
|
-
}
|
|
809
|
-
destroy() {
|
|
810
|
-
this.bindings.forEach((binding) => {
|
|
811
|
-
destroy(binding);
|
|
812
|
-
});
|
|
813
|
-
this.bindings = /* @__PURE__ */ new Map();
|
|
814
|
-
this.observer.disconnect();
|
|
815
|
-
}
|
|
816
|
-
};
|
|
817
|
-
function bind(options) {
|
|
818
|
-
return new SimplyBind(options);
|
|
819
|
-
}
|
|
820
|
-
function matchValue(a, b) {
|
|
821
|
-
if (a == "#empty" && !b) {
|
|
822
|
-
return true;
|
|
823
|
-
}
|
|
824
|
-
if (b == "#empty" && !a) {
|
|
825
|
-
return true;
|
|
826
|
-
}
|
|
827
|
-
if ("" + a == "" + b) {
|
|
828
|
-
return true;
|
|
829
|
-
}
|
|
830
|
-
return false;
|
|
831
|
-
}
|
|
832
|
-
function getValueByPath(root, path) {
|
|
833
|
-
let parts = path.split(".");
|
|
834
|
-
let curr = root;
|
|
835
|
-
let part, prevPart;
|
|
836
|
-
while (parts.length && curr) {
|
|
837
|
-
part = parts.shift();
|
|
838
|
-
if (part == "#key") {
|
|
839
|
-
return prevPart;
|
|
840
|
-
} else if (part == "#value") {
|
|
841
|
-
return curr;
|
|
842
|
-
} else if (part == "#root") {
|
|
843
|
-
curr = root;
|
|
844
|
-
} else {
|
|
845
|
-
part = decodeURIComponent(part);
|
|
846
|
-
curr = curr[part];
|
|
847
|
-
prevPart = part;
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
return curr;
|
|
851
|
-
}
|
|
852
|
-
function defaultTransformer(context) {
|
|
853
|
-
const el = context.element;
|
|
854
|
-
const templates = context.templates;
|
|
855
|
-
const templatesCount = templates.length;
|
|
856
|
-
const path = context.path;
|
|
857
|
-
const value = context.value;
|
|
858
|
-
const attribute = this.options.attribute;
|
|
859
|
-
if (Array.isArray(value) && templates?.length) {
|
|
860
|
-
transformArrayByTemplates.call(this, context);
|
|
861
|
-
} else if (typeof value == "object" && templates?.length) {
|
|
862
|
-
transformObjectByTemplates.call(this, context);
|
|
863
|
-
} else if (templates?.length) {
|
|
864
|
-
transformLiteralByTemplates.call(this, context);
|
|
865
|
-
} else if (el.tagName == "INPUT") {
|
|
866
|
-
transformInput.call(this, context);
|
|
867
|
-
} else if (el.tagName == "BUTTON") {
|
|
868
|
-
transformButton.call(this, context);
|
|
869
|
-
} else if (el.tagName == "SELECT") {
|
|
870
|
-
transformSelect.call(this, context);
|
|
871
|
-
} else if (el.tagName == "A") {
|
|
872
|
-
transformAnchor.call(this, context);
|
|
873
|
-
} else {
|
|
874
|
-
transformElement.call(this, context);
|
|
875
|
-
}
|
|
876
|
-
return context;
|
|
877
|
-
}
|
|
878
|
-
function transformArrayByTemplates(context) {
|
|
879
|
-
const el = context.element;
|
|
880
|
-
const templates = context.templates;
|
|
881
|
-
const templatesCount = templates.length;
|
|
882
|
-
const path = context.path;
|
|
883
|
-
const value = context.value;
|
|
884
|
-
const attribute = this.options.attribute;
|
|
885
|
-
let items = el.querySelectorAll(":scope > [" + attribute + "-key]");
|
|
886
|
-
let lastKey = 0;
|
|
887
|
-
let skipped = 0;
|
|
888
|
-
context.list = value;
|
|
889
|
-
for (let item2 of items) {
|
|
890
|
-
let currentKey = parseInt(item2.getAttribute(attribute + "-key"));
|
|
891
|
-
if (currentKey > lastKey) {
|
|
892
|
-
context.index = lastKey;
|
|
893
|
-
el.insertBefore(this.applyTemplate(context), item2);
|
|
894
|
-
} else if (currentKey < lastKey) {
|
|
895
|
-
item2.remove();
|
|
896
|
-
} else {
|
|
897
|
-
let bindings = Array.from(item2.querySelectorAll(`[${attribute}]`));
|
|
898
|
-
if (item2.matches(`[${attribute}]`)) {
|
|
899
|
-
bindings.unshift(item2);
|
|
900
|
-
}
|
|
901
|
-
let needsReplacement = bindings.find((b) => {
|
|
902
|
-
let databind = b.getAttribute(attribute);
|
|
903
|
-
return databind.substr(0, 5) !== "#root" && databind.substr(0, path.length) !== path;
|
|
904
|
-
});
|
|
905
|
-
if (!needsReplacement) {
|
|
906
|
-
if (item2.$bindTemplate) {
|
|
907
|
-
let newTemplate = this.findTemplate(templates, value[lastKey]);
|
|
908
|
-
if (newTemplate != item2.$bindTemplate) {
|
|
909
|
-
needsReplacement = true;
|
|
910
|
-
if (!newTemplate) {
|
|
911
|
-
skipped++;
|
|
412
|
+
if (this[selectedKeyboard] && this[selectedKeyboard][keyString]) {
|
|
413
|
+
let targets = options.app.container.querySelectorAll('[data-simply-accesskey="' + keyboard + keyString + '"]');
|
|
414
|
+
if (targets.length) {
|
|
415
|
+
targets.forEach((t) => t.click());
|
|
416
|
+
e.preventDefault();
|
|
912
417
|
}
|
|
913
418
|
}
|
|
914
419
|
}
|
|
915
420
|
}
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
el.replaceChild(this.applyTemplate(context), item2);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
lastKey++;
|
|
922
|
-
if (lastKey >= value.length) {
|
|
923
|
-
break;
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
items = el.querySelectorAll(":scope > [" + attribute + "-key]");
|
|
927
|
-
let length = items.length + skipped;
|
|
928
|
-
if (length > value.length) {
|
|
929
|
-
while (length > value.length) {
|
|
930
|
-
let child = el.querySelectorAll(":scope > :not(template)")?.[length - 1];
|
|
931
|
-
child?.remove();
|
|
932
|
-
length--;
|
|
933
|
-
}
|
|
934
|
-
} else if (length < value.length) {
|
|
935
|
-
while (length < value.length) {
|
|
936
|
-
context.index = length;
|
|
937
|
-
el.appendChild(this.applyTemplate(context));
|
|
938
|
-
length++;
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
function transformObjectByTemplates(context) {
|
|
943
|
-
const el = context.element;
|
|
944
|
-
const templates = context.templates;
|
|
945
|
-
const templatesCount = templates.length;
|
|
946
|
-
const path = context.path;
|
|
947
|
-
const value = context.value;
|
|
948
|
-
const attribute = this.options.attribute;
|
|
949
|
-
context.list = value;
|
|
950
|
-
let items = Array.from(el.querySelectorAll(":scope > [" + attribute + "-key]"));
|
|
951
|
-
for (let key in context.list) {
|
|
952
|
-
context.index = key;
|
|
953
|
-
let item2 = items.shift();
|
|
954
|
-
if (!item2) {
|
|
955
|
-
el.appendChild(this.applyTemplate(context));
|
|
956
|
-
continue;
|
|
957
|
-
}
|
|
958
|
-
if (item2.getAttribute[attribute + "-key"] != key) {
|
|
959
|
-
items.unshift(item2);
|
|
960
|
-
let outOfOrderItem = el.querySelector(":scope > [" + attribute + '-key="' + key + '"]');
|
|
961
|
-
if (!outOfOrderItem) {
|
|
962
|
-
let clone = this.applyTemplate(context);
|
|
963
|
-
if (clone.firstElementChild) {
|
|
964
|
-
el.insertBefore(clone, item2);
|
|
965
|
-
}
|
|
966
|
-
continue;
|
|
967
|
-
} else {
|
|
968
|
-
el.insertBefore(outOfOrderItem, item2);
|
|
969
|
-
item2 = outOfOrderItem;
|
|
970
|
-
items = items.filter((i) => i != outOfOrderItem);
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
let newTemplate = this.findTemplate(templates, value[key]);
|
|
974
|
-
if (newTemplate != item2.$bindTemplate) {
|
|
975
|
-
let clone = this.applyTemplate(context);
|
|
976
|
-
el.replaceChild(clone, item2);
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
while (items.length) {
|
|
980
|
-
item = items.shift();
|
|
981
|
-
item.remove();
|
|
982
|
-
}
|
|
983
|
-
}
|
|
984
|
-
function transformLiteralByTemplates(context) {
|
|
985
|
-
const el = context.element;
|
|
986
|
-
const templates = context.templates;
|
|
987
|
-
const value = context.value;
|
|
988
|
-
const attribute = this.options.attribute;
|
|
989
|
-
const rendered = el.querySelector(":scope > :not(template)");
|
|
990
|
-
const template = this.findTemplate(templates, value);
|
|
991
|
-
context.parent = el.parentElement?.closest(`[${attribute}]`)?.getAttribute(attribute) || "#root";
|
|
992
|
-
if (rendered) {
|
|
993
|
-
if (template) {
|
|
994
|
-
if (rendered?.$bindTemplate != template) {
|
|
995
|
-
const clone = this.applyTemplate(context);
|
|
996
|
-
el.replaceChild(clone, rendered);
|
|
997
|
-
}
|
|
998
|
-
} else {
|
|
999
|
-
el.removeChild(rendered);
|
|
1000
|
-
}
|
|
1001
|
-
} else if (template) {
|
|
1002
|
-
const clone = this.applyTemplate(context);
|
|
1003
|
-
el.appendChild(clone);
|
|
421
|
+
};
|
|
422
|
+
options.app.container.addEventListener("keydown", keyHandler);
|
|
1004
423
|
}
|
|
424
|
+
};
|
|
425
|
+
function keys(options = {}) {
|
|
426
|
+
return new SimplyKey(options);
|
|
1005
427
|
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
if (
|
|
1010
|
-
|
|
1011
|
-
|
|
428
|
+
|
|
429
|
+
// src/view.mjs
|
|
430
|
+
function view(options) {
|
|
431
|
+
if (options.app) {
|
|
432
|
+
options.app.view = options.view || {};
|
|
433
|
+
const load = () => {
|
|
434
|
+
const data = options.app.view;
|
|
435
|
+
const path = globalThis.editor.data.getDataPath(options.app.container || document.body);
|
|
436
|
+
options.app.view = globalThis.editor.currentData[path];
|
|
437
|
+
Object.assign(options.app.view, data);
|
|
438
|
+
};
|
|
439
|
+
if (globalThis.editor && globalThis.editor.currentData) {
|
|
440
|
+
load();
|
|
1012
441
|
} else {
|
|
1013
|
-
|
|
1014
|
-
}
|
|
1015
|
-
} else if (!matchValue(el.value, value)) {
|
|
1016
|
-
el.value = "" + value;
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
function transformButton(context) {
|
|
1020
|
-
const el = context.element;
|
|
1021
|
-
const value = context.value;
|
|
1022
|
-
if (!matchValue(el.value, value)) {
|
|
1023
|
-
el.value = "" + value;
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
function transformSelect(context) {
|
|
1027
|
-
const el = context.element;
|
|
1028
|
-
const value = context.value;
|
|
1029
|
-
if (el.multiple) {
|
|
1030
|
-
if (Array.isArray(value)) {
|
|
1031
|
-
for (let option of el.options) {
|
|
1032
|
-
if (value.indexOf(option.value) === false) {
|
|
1033
|
-
option.selected = false;
|
|
1034
|
-
} else {
|
|
1035
|
-
option.selected = true;
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
442
|
+
document.addEventListener("simply-content-loaded", load);
|
|
1038
443
|
}
|
|
444
|
+
return options.app.view;
|
|
1039
445
|
} else {
|
|
1040
|
-
|
|
1041
|
-
if (option) {
|
|
1042
|
-
option.selected = true;
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
function transformAnchor(context) {
|
|
1047
|
-
const el = context.element;
|
|
1048
|
-
const value = context.value;
|
|
1049
|
-
if (value?.innerHTML && !matchValue(el.innerHTML, value.innerHTML)) {
|
|
1050
|
-
el.innerHTML = "" + value.innerHTML;
|
|
1051
|
-
}
|
|
1052
|
-
if (value?.href && !matchValue(el.href, value.href)) {
|
|
1053
|
-
el.href = "" + value.href;
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
function transformElement(context) {
|
|
1057
|
-
const el = context.element;
|
|
1058
|
-
const value = context.value;
|
|
1059
|
-
if (!matchValue(el.innerHTML, value)) {
|
|
1060
|
-
if (typeof value == "undefined" || value == null) {
|
|
1061
|
-
el.innerHTML = "";
|
|
1062
|
-
} else {
|
|
1063
|
-
el.innerHTML = "" + value;
|
|
1064
|
-
}
|
|
446
|
+
return options.view;
|
|
1065
447
|
}
|
|
1066
448
|
}
|
|
1067
449
|
|
|
@@ -1069,10 +451,6 @@
|
|
|
1069
451
|
var SimplyApp = class {
|
|
1070
452
|
constructor(options = {}) {
|
|
1071
453
|
this.container = options.container || document.body;
|
|
1072
|
-
if (!options.state) {
|
|
1073
|
-
options.state = {};
|
|
1074
|
-
}
|
|
1075
|
-
this.state = signal(options.state);
|
|
1076
454
|
if (options.commands) {
|
|
1077
455
|
this.commands = commands({ app: this, container: this.container, commands: options.commands });
|
|
1078
456
|
}
|
|
@@ -1085,14 +463,9 @@
|
|
|
1085
463
|
if (options.actions) {
|
|
1086
464
|
this.actions = actions({ app: this, actions: options.actions });
|
|
1087
465
|
}
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
bindOptions.defaultTransformers = options.defaultTransformers;
|
|
1091
|
-
}
|
|
1092
|
-
if (options.transformers) {
|
|
1093
|
-
bindOptions.transformers = options.transformers;
|
|
466
|
+
if (options.view) {
|
|
467
|
+
this.view = view({ app: this, view: options.view });
|
|
1094
468
|
}
|
|
1095
|
-
this.bind = bind(bindOptions);
|
|
1096
469
|
}
|
|
1097
470
|
};
|
|
1098
471
|
function app(options = {}) {
|