@mulsense/xnew 0.6.5 → 0.6.6
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/addons/xpixi.js +0 -1
- package/dist/addons/xpixi.mjs +0 -1
- package/dist/addons/xthree.js +0 -1
- package/dist/addons/xthree.mjs +0 -1
- package/dist/xnew.d.ts +41 -47
- package/dist/xnew.js +207 -246
- package/dist/xnew.mjs +207 -246
- package/package.json +1 -1
package/dist/xnew.js
CHANGED
|
@@ -523,92 +523,15 @@
|
|
|
523
523
|
// utils
|
|
524
524
|
//----------------------------------------------------------------------------------------------------
|
|
525
525
|
const SYSTEM_EVENTS = ['start', 'update', 'render', 'stop', 'finalize'];
|
|
526
|
-
class UnitPromise {
|
|
527
|
-
constructor(promise, Component) {
|
|
528
|
-
this.promise = promise;
|
|
529
|
-
this.Component = Component;
|
|
530
|
-
}
|
|
531
|
-
then(callback) { return this.wrap('then', callback); }
|
|
532
|
-
catch(callback) { return this.wrap('catch', callback); }
|
|
533
|
-
finally(callback) { return this.wrap('finally', callback); }
|
|
534
|
-
wrap(key, callback) {
|
|
535
|
-
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
536
|
-
this.promise = this.promise[key]((...args) => Unit.scope(snapshot, callback, ...args));
|
|
537
|
-
return this;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
class UnitTimer {
|
|
541
|
-
constructor() {
|
|
542
|
-
this.unit = null;
|
|
543
|
-
this.stack = [];
|
|
544
|
-
}
|
|
545
|
-
clear() {
|
|
546
|
-
var _a;
|
|
547
|
-
this.stack = [];
|
|
548
|
-
(_a = this.unit) === null || _a === void 0 ? void 0 : _a.finalize();
|
|
549
|
-
this.unit = null;
|
|
550
|
-
}
|
|
551
|
-
timeout(callback, duration = 0) {
|
|
552
|
-
return UnitTimer.execute(this, { callback, duration }, 1);
|
|
553
|
-
}
|
|
554
|
-
interval(callback, duration = 0, iterations = 0) {
|
|
555
|
-
return UnitTimer.execute(this, { callback, duration }, iterations);
|
|
556
|
-
}
|
|
557
|
-
transition(transition, duration = 0, easing) {
|
|
558
|
-
return UnitTimer.execute(this, { transition, duration, easing }, 1);
|
|
559
|
-
}
|
|
560
|
-
static execute(timer, options, iterations) {
|
|
561
|
-
const props = { options, iterations, snapshot: Unit.snapshot(Unit.currentUnit) };
|
|
562
|
-
if (timer.unit === null || timer.unit._.state === 'finalized') {
|
|
563
|
-
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, props);
|
|
564
|
-
}
|
|
565
|
-
else if (timer.stack.length === 0) {
|
|
566
|
-
timer.stack.push(props);
|
|
567
|
-
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
568
|
-
}
|
|
569
|
-
else {
|
|
570
|
-
timer.stack.push(props);
|
|
571
|
-
}
|
|
572
|
-
return timer;
|
|
573
|
-
}
|
|
574
|
-
static next(timer) {
|
|
575
|
-
if (timer.stack.length > 0) {
|
|
576
|
-
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.stack.shift());
|
|
577
|
-
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
static Component(unit, { options, iterations, snapshot }) {
|
|
581
|
-
let counter = 0;
|
|
582
|
-
let timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
|
|
583
|
-
function callback() {
|
|
584
|
-
if (options.callback)
|
|
585
|
-
Unit.scope(snapshot, options.callback);
|
|
586
|
-
if (iterations <= 0 || counter < iterations - 1) {
|
|
587
|
-
timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
|
|
588
|
-
}
|
|
589
|
-
else {
|
|
590
|
-
unit.finalize();
|
|
591
|
-
}
|
|
592
|
-
counter++;
|
|
593
|
-
}
|
|
594
|
-
function transition(value) {
|
|
595
|
-
if (options.transition)
|
|
596
|
-
Unit.scope(snapshot, options.transition, { value });
|
|
597
|
-
}
|
|
598
|
-
unit.on('finalize', () => timer.clear());
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
function DefaultComponent(unit, { text }) {
|
|
602
|
-
if (text !== undefined) {
|
|
603
|
-
unit.element.textContent = text;
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
526
|
//----------------------------------------------------------------------------------------------------
|
|
607
527
|
// unit
|
|
608
528
|
//----------------------------------------------------------------------------------------------------
|
|
609
529
|
class Unit {
|
|
610
|
-
constructor(parent, target,
|
|
530
|
+
constructor(parent, target, Component, props) {
|
|
611
531
|
var _a;
|
|
532
|
+
const backup = Unit.currentUnit;
|
|
533
|
+
Unit.currentUnit = this;
|
|
534
|
+
parent === null || parent === void 0 ? void 0 : parent._.children.push(this);
|
|
612
535
|
let baseElement;
|
|
613
536
|
if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
614
537
|
baseElement = target;
|
|
@@ -620,20 +543,45 @@
|
|
|
620
543
|
baseElement = (document === null || document === void 0 ? void 0 : document.body) ? document.body : null;
|
|
621
544
|
}
|
|
622
545
|
let baseComponent;
|
|
623
|
-
if (typeof
|
|
624
|
-
baseComponent =
|
|
546
|
+
if (typeof Component === 'function') {
|
|
547
|
+
baseComponent = Component;
|
|
625
548
|
}
|
|
626
|
-
else if (typeof
|
|
627
|
-
baseComponent =
|
|
628
|
-
props = { text: component.toString() };
|
|
549
|
+
else if (typeof Component === 'string' || typeof Component === 'number') {
|
|
550
|
+
baseComponent = (unit) => { unit.element.textContent = Component.toString(); };
|
|
629
551
|
}
|
|
630
552
|
else {
|
|
631
|
-
baseComponent =
|
|
553
|
+
baseComponent = (unit) => { };
|
|
632
554
|
}
|
|
633
|
-
const baseContext = (_a = parent === null || parent === void 0 ? void 0 : parent._.currentContext) !== null && _a !== void 0 ? _a : {
|
|
634
|
-
this._ = {
|
|
635
|
-
|
|
636
|
-
|
|
555
|
+
const baseContext = (_a = parent === null || parent === void 0 ? void 0 : parent._.currentContext) !== null && _a !== void 0 ? _a : { previous: null };
|
|
556
|
+
this._ = {
|
|
557
|
+
parent,
|
|
558
|
+
state: 'invoked',
|
|
559
|
+
tostart: true,
|
|
560
|
+
protected: false,
|
|
561
|
+
currentElement: baseElement,
|
|
562
|
+
currentContext: baseContext,
|
|
563
|
+
currentComponent: null,
|
|
564
|
+
ancestors: parent ? [parent, ...parent._.ancestors] : [],
|
|
565
|
+
children: [],
|
|
566
|
+
nestElements: [],
|
|
567
|
+
promises: [],
|
|
568
|
+
results: {},
|
|
569
|
+
Components: [],
|
|
570
|
+
listeners: new MapMap(),
|
|
571
|
+
defines: {},
|
|
572
|
+
systems: { start: [], update: [], render: [], stop: [], finalize: [] },
|
|
573
|
+
eventor: new Eventor(),
|
|
574
|
+
};
|
|
575
|
+
// nest html element
|
|
576
|
+
if (typeof target === 'string') {
|
|
577
|
+
Unit.nest(this, target);
|
|
578
|
+
}
|
|
579
|
+
// setup Component
|
|
580
|
+
Unit.extend(this, baseComponent, props);
|
|
581
|
+
if (this._.state === 'invoked') {
|
|
582
|
+
this._.state = 'initialized';
|
|
583
|
+
}
|
|
584
|
+
Unit.currentUnit = backup;
|
|
637
585
|
}
|
|
638
586
|
get element() {
|
|
639
587
|
return this._.currentElement;
|
|
@@ -648,77 +596,40 @@
|
|
|
648
596
|
finalize() {
|
|
649
597
|
Unit.stop(this);
|
|
650
598
|
Unit.finalize(this);
|
|
651
|
-
if (this._.parent) {
|
|
652
|
-
this._.parent._.children = this._.parent._.children.filter((unit) => unit !== this);
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
reboot() {
|
|
656
|
-
let anchor = null;
|
|
657
|
-
if (this._.nestElements[0] && this._.nestElements[0].owned === true) {
|
|
658
|
-
anchor = this._.nestElements[0].element.nextElementSibling;
|
|
659
|
-
}
|
|
660
|
-
Unit.stop(this);
|
|
661
|
-
Unit.finalize(this);
|
|
662
|
-
Unit.initialize(this, anchor);
|
|
663
|
-
}
|
|
664
|
-
static initialize(unit, anchor) {
|
|
665
|
-
const backup = Unit.currentUnit;
|
|
666
|
-
Unit.currentUnit = unit;
|
|
667
|
-
const done = {
|
|
668
|
-
promise: null,
|
|
669
|
-
resolve: null,
|
|
670
|
-
reject: null
|
|
671
|
-
};
|
|
672
|
-
done.promise = new Promise((resolve, reject) => { done.resolve = resolve; done.reject = reject; });
|
|
673
|
-
unit._ = Object.assign(unit._, {
|
|
674
|
-
currentElement: unit._.baseElement,
|
|
675
|
-
currentContext: unit._.baseContext,
|
|
676
|
-
currentComponent: null,
|
|
677
|
-
anchor,
|
|
678
|
-
state: 'invoked',
|
|
679
|
-
tostart: true,
|
|
680
|
-
protected: false,
|
|
681
|
-
ancestors: unit._.parent ? [unit._.parent, ...unit._.parent._.ancestors] : [],
|
|
682
|
-
children: [],
|
|
683
|
-
nestElements: [],
|
|
684
|
-
promises: [],
|
|
685
|
-
done,
|
|
686
|
-
components: [],
|
|
687
|
-
listeners: new MapMap(),
|
|
688
|
-
defines: {},
|
|
689
|
-
systems: { start: [], update: [], render: [], stop: [], finalize: [] },
|
|
690
|
-
eventor: new Eventor(),
|
|
691
|
-
});
|
|
692
|
-
// nest html element
|
|
693
|
-
if (typeof unit._.target === 'string') {
|
|
694
|
-
Unit.nest(unit, unit._.target);
|
|
695
|
-
}
|
|
696
|
-
// setup component
|
|
697
|
-
Unit.extend(unit, unit._.baseComponent, unit._.props);
|
|
698
|
-
// whether the unit promise was resolved
|
|
699
|
-
// Promise.all(unit._.promises.map(p => p.promise)).then(() => unit._.state = 'initialized');
|
|
700
|
-
unit._.state = 'initialized';
|
|
701
|
-
Unit.currentUnit = backup;
|
|
702
599
|
}
|
|
703
600
|
static finalize(unit) {
|
|
704
601
|
if (unit._.state !== 'finalized' && unit._.state !== 'finalizing') {
|
|
705
602
|
unit._.state = 'finalizing';
|
|
706
|
-
|
|
707
|
-
|
|
603
|
+
unit._.children.reverse().forEach((child) => child.finalize());
|
|
604
|
+
unit._.systems.finalize.reverse().forEach(({ execute }) => execute());
|
|
708
605
|
unit.off();
|
|
709
|
-
unit._.
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
606
|
+
unit._.nestElements.reverse().filter(item => item.owned).forEach(item => item.element.remove());
|
|
607
|
+
unit._.Components.forEach((Component) => Unit.component2units.delete(Component, unit));
|
|
608
|
+
// remove contexts
|
|
609
|
+
const contexts = Unit.unit2Contexts.get(unit);
|
|
610
|
+
contexts === null || contexts === void 0 ? void 0 : contexts.forEach((context) => {
|
|
611
|
+
let temp = context.previous;
|
|
612
|
+
while (temp !== null) {
|
|
613
|
+
if (contexts.has(temp) === false && temp.key !== undefined) {
|
|
614
|
+
context.previous = temp;
|
|
615
|
+
context.key = undefined;
|
|
616
|
+
context.value = undefined;
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
temp = temp.previous;
|
|
713
620
|
}
|
|
714
|
-
}
|
|
715
|
-
|
|
621
|
+
});
|
|
622
|
+
Unit.unit2Contexts.delete(unit);
|
|
623
|
+
unit._.currentContext = { previous: null };
|
|
716
624
|
// reset defines
|
|
717
625
|
Object.keys(unit._.defines).forEach((key) => {
|
|
718
626
|
delete unit[key];
|
|
719
627
|
});
|
|
720
628
|
unit._.defines = {};
|
|
721
629
|
unit._.state = 'finalized';
|
|
630
|
+
if (unit._.parent) {
|
|
631
|
+
unit._.parent._.children = unit._.parent._.children.filter((u) => u !== unit);
|
|
632
|
+
}
|
|
722
633
|
}
|
|
723
634
|
}
|
|
724
635
|
static nest(unit, target, textContent) {
|
|
@@ -730,16 +641,8 @@
|
|
|
730
641
|
else {
|
|
731
642
|
const match = target.match(/<((\w+)[^>]*?)\/?>/);
|
|
732
643
|
if (match !== null) {
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
unit._.anchor.insertAdjacentHTML('beforebegin', `<${match[1]}></${match[2]}>`);
|
|
736
|
-
element = unit._.anchor.previousElementSibling;
|
|
737
|
-
unit._.anchor = null;
|
|
738
|
-
}
|
|
739
|
-
else {
|
|
740
|
-
unit._.currentElement.insertAdjacentHTML('beforeend', `<${match[1]}></${match[2]}>`);
|
|
741
|
-
element = unit._.currentElement.children[unit._.currentElement.children.length - 1];
|
|
742
|
-
}
|
|
644
|
+
unit._.currentElement.insertAdjacentHTML('beforeend', `<${match[1]}></${match[2]}>`);
|
|
645
|
+
const element = unit._.currentElement.children[unit._.currentElement.children.length - 1];
|
|
743
646
|
unit._.currentElement = element;
|
|
744
647
|
if (textContent !== undefined) {
|
|
745
648
|
element.textContent = textContent.toString();
|
|
@@ -754,25 +657,16 @@
|
|
|
754
657
|
}
|
|
755
658
|
static extend(unit, Component, props) {
|
|
756
659
|
var _a;
|
|
757
|
-
if (unit._.
|
|
758
|
-
throw new Error(`The
|
|
660
|
+
if (unit._.Components.includes(Component) === true) {
|
|
661
|
+
throw new Error(`The Component is already extended.`);
|
|
759
662
|
}
|
|
760
663
|
else {
|
|
761
664
|
const backupComponent = unit._.currentComponent;
|
|
762
665
|
unit._.currentComponent = Component;
|
|
763
666
|
const defines = (_a = Component(unit, props !== null && props !== void 0 ? props : {})) !== null && _a !== void 0 ? _a : {};
|
|
764
|
-
if (unit._.parent && Component !== DefaultComponent) {
|
|
765
|
-
if (Component === unit._.baseComponent) {
|
|
766
|
-
Unit.context(unit._.parent, Component, unit);
|
|
767
|
-
}
|
|
768
|
-
else {
|
|
769
|
-
Unit.context(unit, Component, unit);
|
|
770
|
-
Unit.context(unit._.parent, Component, unit);
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
667
|
unit._.currentComponent = backupComponent;
|
|
774
668
|
Unit.component2units.add(Component, unit);
|
|
775
|
-
unit._.
|
|
669
|
+
unit._.Components.push(Component);
|
|
776
670
|
Object.keys(defines).forEach((key) => {
|
|
777
671
|
if (unit[key] !== undefined && unit._.defines[key] === undefined) {
|
|
778
672
|
throw new Error(`The property "${key}" already exists.`);
|
|
@@ -790,7 +684,7 @@
|
|
|
790
684
|
wrapper.value = (...args) => Unit.scope(snapshot, descriptor.value, ...args);
|
|
791
685
|
}
|
|
792
686
|
else {
|
|
793
|
-
throw new Error(`Only function properties can be defined as
|
|
687
|
+
throw new Error(`Only function properties can be defined as Component defines. [${key}]`);
|
|
794
688
|
}
|
|
795
689
|
Object.defineProperty(unit._.defines, key, wrapper);
|
|
796
690
|
Object.defineProperty(unit, key, wrapper);
|
|
@@ -850,7 +744,7 @@
|
|
|
850
744
|
Unit.currentUnit = snapshot.unit;
|
|
851
745
|
snapshot.unit._.currentContext = snapshot.context;
|
|
852
746
|
snapshot.unit._.currentElement = snapshot.element;
|
|
853
|
-
snapshot.unit._.currentComponent = snapshot.
|
|
747
|
+
snapshot.unit._.currentComponent = snapshot.Component;
|
|
854
748
|
return func(...args);
|
|
855
749
|
}
|
|
856
750
|
catch (error) {
|
|
@@ -860,21 +754,20 @@
|
|
|
860
754
|
Unit.currentUnit = currentUnit;
|
|
861
755
|
snapshot.unit._.currentContext = backup.context;
|
|
862
756
|
snapshot.unit._.currentElement = backup.element;
|
|
863
|
-
snapshot.unit._.currentComponent = backup.
|
|
757
|
+
snapshot.unit._.currentComponent = backup.Component;
|
|
864
758
|
}
|
|
865
759
|
}
|
|
866
760
|
static snapshot(unit) {
|
|
867
|
-
return { unit, context: unit._.currentContext, element: unit._.currentElement,
|
|
761
|
+
return { unit, context: unit._.currentContext, element: unit._.currentElement, Component: unit._.currentComponent };
|
|
868
762
|
}
|
|
869
|
-
static
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
}
|
|
763
|
+
static addContext(unit, orner, key, value) {
|
|
764
|
+
unit._.currentContext = { previous: unit._.currentContext, key, value };
|
|
765
|
+
Unit.unit2Contexts.add(orner, unit._.currentContext);
|
|
766
|
+
}
|
|
767
|
+
static getContext(unit, key) {
|
|
768
|
+
for (let context = unit._.currentContext; context.previous !== null; context = context.previous) {
|
|
769
|
+
if (context.key === key)
|
|
770
|
+
return context.value;
|
|
878
771
|
}
|
|
879
772
|
}
|
|
880
773
|
static find(Component) {
|
|
@@ -898,7 +791,7 @@
|
|
|
898
791
|
unit._.systems[type].push({ listener, execute });
|
|
899
792
|
}
|
|
900
793
|
if (unit._.listeners.has(type, listener) === false) {
|
|
901
|
-
unit._.listeners.set(type, listener, { element: unit.element,
|
|
794
|
+
unit._.listeners.set(type, listener, { element: unit.element, Component: unit._.currentComponent, execute });
|
|
902
795
|
Unit.type2units.add(type, unit);
|
|
903
796
|
if (/^[A-Za-z]/.test(type)) {
|
|
904
797
|
unit._.eventor.add(unit.element, type, execute, options);
|
|
@@ -940,11 +833,90 @@
|
|
|
940
833
|
}
|
|
941
834
|
}
|
|
942
835
|
Unit.currentComponent = () => { };
|
|
836
|
+
Unit.unit2Contexts = new MapSet();
|
|
943
837
|
Unit.component2units = new MapSet();
|
|
944
838
|
//----------------------------------------------------------------------------------------------------
|
|
945
839
|
// event
|
|
946
840
|
//----------------------------------------------------------------------------------------------------
|
|
947
841
|
Unit.type2units = new MapSet();
|
|
842
|
+
//----------------------------------------------------------------------------------------------------
|
|
843
|
+
// extensions
|
|
844
|
+
//----------------------------------------------------------------------------------------------------
|
|
845
|
+
class UnitPromise {
|
|
846
|
+
constructor(promise, Component) {
|
|
847
|
+
this.promise = promise;
|
|
848
|
+
this.Component = Component;
|
|
849
|
+
}
|
|
850
|
+
then(callback) { return this.wrap('then', callback); }
|
|
851
|
+
catch(callback) { return this.wrap('catch', callback); }
|
|
852
|
+
finally(callback) { return this.wrap('finally', callback); }
|
|
853
|
+
wrap(key, callback) {
|
|
854
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
855
|
+
this.promise = this.promise[key]((...args) => Unit.scope(snapshot, callback, ...args));
|
|
856
|
+
return this;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
class UnitTimer {
|
|
860
|
+
constructor() {
|
|
861
|
+
this.unit = null;
|
|
862
|
+
this.queue = [];
|
|
863
|
+
}
|
|
864
|
+
clear() {
|
|
865
|
+
var _a;
|
|
866
|
+
this.queue = [];
|
|
867
|
+
(_a = this.unit) === null || _a === void 0 ? void 0 : _a.finalize();
|
|
868
|
+
this.unit = null;
|
|
869
|
+
}
|
|
870
|
+
timeout(callback, duration = 0) {
|
|
871
|
+
return UnitTimer.execute(this, { callback, duration }, 1);
|
|
872
|
+
}
|
|
873
|
+
interval(callback, duration = 0, iterations = 0) {
|
|
874
|
+
return UnitTimer.execute(this, { callback, duration }, iterations);
|
|
875
|
+
}
|
|
876
|
+
transition(transition, duration = 0, easing) {
|
|
877
|
+
return UnitTimer.execute(this, { transition, duration, easing }, 1);
|
|
878
|
+
}
|
|
879
|
+
static execute(timer, options, iterations) {
|
|
880
|
+
const props = { options, iterations, snapshot: Unit.snapshot(Unit.currentUnit) };
|
|
881
|
+
if (timer.unit === null || timer.unit._.state === 'finalized') {
|
|
882
|
+
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, props);
|
|
883
|
+
}
|
|
884
|
+
else if (timer.queue.length === 0) {
|
|
885
|
+
timer.queue.push(props);
|
|
886
|
+
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
887
|
+
}
|
|
888
|
+
else {
|
|
889
|
+
timer.queue.push(props);
|
|
890
|
+
}
|
|
891
|
+
return timer;
|
|
892
|
+
}
|
|
893
|
+
static next(timer) {
|
|
894
|
+
if (timer.queue.length > 0) {
|
|
895
|
+
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.queue.shift());
|
|
896
|
+
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
static Component(unit, { options, iterations, snapshot }) {
|
|
900
|
+
let counter = 0;
|
|
901
|
+
let timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
|
|
902
|
+
function callback() {
|
|
903
|
+
if (options.callback)
|
|
904
|
+
Unit.scope(snapshot, options.callback);
|
|
905
|
+
if (iterations <= 0 || counter < iterations - 1) {
|
|
906
|
+
timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
|
|
907
|
+
}
|
|
908
|
+
else {
|
|
909
|
+
unit.finalize();
|
|
910
|
+
}
|
|
911
|
+
counter++;
|
|
912
|
+
}
|
|
913
|
+
function transition(value) {
|
|
914
|
+
if (options.transition)
|
|
915
|
+
Unit.scope(snapshot, options.transition, { value });
|
|
916
|
+
}
|
|
917
|
+
unit.on('finalize', () => timer.clear());
|
|
918
|
+
}
|
|
919
|
+
}
|
|
948
920
|
|
|
949
921
|
const xnew$1 = Object.assign(function (...args) {
|
|
950
922
|
if (Unit.rootUnit === undefined)
|
|
@@ -961,7 +933,9 @@
|
|
|
961
933
|
}
|
|
962
934
|
const Component = args.shift();
|
|
963
935
|
const props = args.shift();
|
|
964
|
-
|
|
936
|
+
const unit = new Unit(Unit.currentUnit, target, Component, props);
|
|
937
|
+
Unit.addContext(Unit.currentUnit, unit, Component, unit);
|
|
938
|
+
return unit;
|
|
965
939
|
}, {
|
|
966
940
|
/**
|
|
967
941
|
* Creates a nested HTML/SVG element within the current component
|
|
@@ -998,7 +972,9 @@
|
|
|
998
972
|
if (Unit.currentUnit._.state !== 'invoked') {
|
|
999
973
|
throw new Error('xnew.extend can not be called after initialized.');
|
|
1000
974
|
}
|
|
1001
|
-
|
|
975
|
+
const defines = Unit.extend(Unit.currentUnit, Component, props);
|
|
976
|
+
Unit.addContext(Unit.currentUnit, Unit.currentUnit, Component, Unit.currentUnit);
|
|
977
|
+
return defines;
|
|
1002
978
|
}
|
|
1003
979
|
catch (error) {
|
|
1004
980
|
console.error('xnew.extend(component: Function, props?: Object): ', error);
|
|
@@ -1007,7 +983,7 @@
|
|
|
1007
983
|
},
|
|
1008
984
|
/**
|
|
1009
985
|
* Gets a context value that can be accessed in follow context
|
|
1010
|
-
* @param
|
|
986
|
+
* @param key - component function
|
|
1011
987
|
* @returns The context value
|
|
1012
988
|
* @example
|
|
1013
989
|
* // Create unit
|
|
@@ -1017,12 +993,12 @@
|
|
|
1017
993
|
* // Get context in child
|
|
1018
994
|
* const a = xnew.context(A)
|
|
1019
995
|
*/
|
|
1020
|
-
context(
|
|
996
|
+
context(key) {
|
|
1021
997
|
try {
|
|
1022
|
-
return Unit.
|
|
998
|
+
return Unit.getContext(Unit.currentUnit, key);
|
|
1023
999
|
}
|
|
1024
1000
|
catch (error) {
|
|
1025
|
-
console.error('xnew.context(
|
|
1001
|
+
console.error('xnew.context(key: any): ', error);
|
|
1026
1002
|
throw error;
|
|
1027
1003
|
}
|
|
1028
1004
|
},
|
|
@@ -1035,16 +1011,18 @@
|
|
|
1035
1011
|
*/
|
|
1036
1012
|
promise(promise) {
|
|
1037
1013
|
try {
|
|
1038
|
-
const
|
|
1014
|
+
const Component = Unit.currentUnit._.currentComponent;
|
|
1039
1015
|
let unitPromise;
|
|
1040
1016
|
if (promise instanceof Unit) {
|
|
1041
|
-
|
|
1017
|
+
const unit = promise;
|
|
1018
|
+
unitPromise = new UnitPromise(Promise.all(unit._.promises.map(p => p.promise)), Component)
|
|
1019
|
+
.then(() => unit._.results);
|
|
1042
1020
|
}
|
|
1043
1021
|
else if (promise instanceof Promise) {
|
|
1044
|
-
unitPromise = new UnitPromise(promise,
|
|
1022
|
+
unitPromise = new UnitPromise(promise, Component);
|
|
1045
1023
|
}
|
|
1046
1024
|
else {
|
|
1047
|
-
unitPromise = new UnitPromise(new Promise(xnew$1.scope(promise)),
|
|
1025
|
+
unitPromise = new UnitPromise(new Promise(xnew$1.scope(promise)), Component);
|
|
1048
1026
|
}
|
|
1049
1027
|
Unit.currentUnit._.promises.push(unitPromise);
|
|
1050
1028
|
return unitPromise;
|
|
@@ -1063,11 +1041,12 @@
|
|
|
1063
1041
|
*/
|
|
1064
1042
|
then(callback) {
|
|
1065
1043
|
try {
|
|
1044
|
+
const currentUnit = Unit.currentUnit;
|
|
1066
1045
|
const Component = Unit.currentUnit._.currentComponent;
|
|
1067
1046
|
const promises = Unit.currentUnit._.promises;
|
|
1068
1047
|
return new UnitPromise(Promise.all(promises.map(p => p.promise)), null)
|
|
1069
|
-
.then((
|
|
1070
|
-
callback(
|
|
1048
|
+
.then(() => {
|
|
1049
|
+
callback(currentUnit._.results);
|
|
1071
1050
|
});
|
|
1072
1051
|
}
|
|
1073
1052
|
catch (error) {
|
|
@@ -1094,36 +1073,18 @@
|
|
|
1094
1073
|
}
|
|
1095
1074
|
},
|
|
1096
1075
|
/**
|
|
1097
|
-
*
|
|
1098
|
-
* @param
|
|
1076
|
+
* Assigns a value to the current unit's promise
|
|
1077
|
+
* @param object - object to assign to the promise
|
|
1099
1078
|
* @returns void
|
|
1100
1079
|
* @example
|
|
1101
|
-
* xnew.
|
|
1080
|
+
* xnew.assign({ data: 123});
|
|
1102
1081
|
*/
|
|
1103
|
-
|
|
1082
|
+
assign(object) {
|
|
1104
1083
|
try {
|
|
1105
|
-
|
|
1106
|
-
done.resolve(value);
|
|
1084
|
+
Object.assign(Unit.currentUnit._.results, object);
|
|
1107
1085
|
}
|
|
1108
1086
|
catch (error) {
|
|
1109
|
-
console.error('xnew.
|
|
1110
|
-
throw error;
|
|
1111
|
-
}
|
|
1112
|
-
},
|
|
1113
|
-
/**
|
|
1114
|
-
* Rejects the current unit's promise with the given reason
|
|
1115
|
-
* @param reason - Reason to reject the promise
|
|
1116
|
-
* @returns void
|
|
1117
|
-
* @example
|
|
1118
|
-
* xnew.reject(new Error('Something went wrong'));
|
|
1119
|
-
*/
|
|
1120
|
-
reject(reason) {
|
|
1121
|
-
try {
|
|
1122
|
-
const done = Unit.currentUnit._.done;
|
|
1123
|
-
done.reject(reason);
|
|
1124
|
-
}
|
|
1125
|
-
catch (error) {
|
|
1126
|
-
console.error('xnew.reject(reason?: any): ', error);
|
|
1087
|
+
console.error('xnew.assign(object?: Record<string, any>): ', error);
|
|
1127
1088
|
throw error;
|
|
1128
1089
|
}
|
|
1129
1090
|
},
|
|
@@ -1160,19 +1121,19 @@
|
|
|
1160
1121
|
},
|
|
1161
1122
|
/**
|
|
1162
1123
|
* Finds all instances of a component in the component tree
|
|
1163
|
-
* @param
|
|
1124
|
+
* @param Component - Component function to search for
|
|
1164
1125
|
* @returns Array of Unit instances matching the component
|
|
1165
1126
|
* @throws Error if component parameter is invalid
|
|
1166
1127
|
* @example
|
|
1167
1128
|
* const buttons = xnew.find(ButtonComponent)
|
|
1168
1129
|
* buttons.forEach(btn => btn.finalize())
|
|
1169
1130
|
*/
|
|
1170
|
-
find(
|
|
1131
|
+
find(Component) {
|
|
1171
1132
|
try {
|
|
1172
|
-
return Unit.find(
|
|
1133
|
+
return Unit.find(Component);
|
|
1173
1134
|
}
|
|
1174
1135
|
catch (error) {
|
|
1175
|
-
console.error('xnew.find(
|
|
1136
|
+
console.error('xnew.find(Component: Function): ', error);
|
|
1176
1137
|
throw error;
|
|
1177
1138
|
}
|
|
1178
1139
|
},
|
|
@@ -1443,23 +1404,23 @@
|
|
|
1443
1404
|
const button = xnew$1(Button, { key });
|
|
1444
1405
|
return button;
|
|
1445
1406
|
},
|
|
1446
|
-
select(key, {
|
|
1407
|
+
select(key, { value, items = [] } = {}) {
|
|
1447
1408
|
var _a, _b;
|
|
1448
|
-
object[key] = (_b = (_a = object[key]) !== null && _a !== void 0 ? _a :
|
|
1449
|
-
const select = xnew$1(Select, { key, value: object[key],
|
|
1409
|
+
object[key] = (_b = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : items[0]) !== null && _b !== void 0 ? _b : '';
|
|
1410
|
+
const select = xnew$1(Select, { key, value: object[key], items });
|
|
1450
1411
|
select.on('input', ({ value }) => object[key] = value);
|
|
1451
1412
|
return select;
|
|
1452
1413
|
},
|
|
1453
|
-
range(key,
|
|
1454
|
-
var _a
|
|
1455
|
-
object[key] = (
|
|
1456
|
-
const number = xnew$1(Range,
|
|
1414
|
+
range(key, { value, min = 0, max = 100, step = 1 } = {}) {
|
|
1415
|
+
var _a;
|
|
1416
|
+
object[key] = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : min;
|
|
1417
|
+
const number = xnew$1(Range, { key, value: object[key], min, max, step });
|
|
1457
1418
|
number.on('input', ({ value }) => object[key] = value);
|
|
1458
1419
|
return number;
|
|
1459
1420
|
},
|
|
1460
|
-
checkbox(key) {
|
|
1421
|
+
checkbox(key, { value } = {}) {
|
|
1461
1422
|
var _a;
|
|
1462
|
-
object[key] = (_a = object[key]) !== null && _a !== void 0 ? _a : false;
|
|
1423
|
+
object[key] = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : false;
|
|
1463
1424
|
const checkbox = xnew$1(Checkbox, { key, value: object[key] });
|
|
1464
1425
|
checkbox.on('input', ({ value }) => object[key] = value);
|
|
1465
1426
|
return checkbox;
|
|
@@ -1543,14 +1504,14 @@
|
|
|
1543
1504
|
update(value);
|
|
1544
1505
|
});
|
|
1545
1506
|
}
|
|
1546
|
-
function Select(_, { key = '', value,
|
|
1507
|
+
function Select(_, { key = '', value, items = [] } = {}) {
|
|
1547
1508
|
var _a;
|
|
1548
|
-
const initial = (_a = value !== null && value !== void 0 ? value :
|
|
1509
|
+
const initial = (_a = value !== null && value !== void 0 ? value : items[0]) !== null && _a !== void 0 ? _a : '';
|
|
1549
1510
|
xnew$1.nest(`<div style="position: relative; height: 2em; margin: 0.125em 0; padding: 0 0.5em; display: flex; align-items: center;">`);
|
|
1550
1511
|
xnew$1('<div style="flex: 1;">', key);
|
|
1551
1512
|
const native = xnew$1(`<select name="${key}" style="display: none;">`, () => {
|
|
1552
|
-
for (const
|
|
1553
|
-
xnew$1(`<option value="${
|
|
1513
|
+
for (const item of items) {
|
|
1514
|
+
xnew$1(`<option value="${item}" ${item === initial ? 'selected' : ''}>`, item);
|
|
1554
1515
|
}
|
|
1555
1516
|
});
|
|
1556
1517
|
const button = xnew$1(`<div style="height: 2em; padding: 0 1.5em 0 0.5em; display: flex; align-items: center; border: 1px solid ${currentColorA}; border-radius: 0.25em; cursor: pointer; user-select: none; min-width: 3em; white-space: nowrap;">`, initial);
|
|
@@ -1570,13 +1531,13 @@
|
|
|
1570
1531
|
});
|
|
1571
1532
|
xnew$1.extend(Accordion);
|
|
1572
1533
|
xnew$1.nest(`<div style="position: relative; border: 1px solid ${currentColorA}; border-radius: 0.25em; overflow: hidden;">`);
|
|
1573
|
-
for (const
|
|
1574
|
-
const
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
button.element.textContent =
|
|
1579
|
-
native.element.value =
|
|
1534
|
+
for (const item of items) {
|
|
1535
|
+
const div = xnew$1(`<div style="height: 2em; padding: 0 0.5em; display: flex; align-items: center; cursor: pointer; user-select: none;">`, item);
|
|
1536
|
+
div.on('pointerover', () => div.element.style.background = currentColorB);
|
|
1537
|
+
div.on('pointerout', () => div.element.style.background = '');
|
|
1538
|
+
div.on('click', () => {
|
|
1539
|
+
button.element.textContent = item;
|
|
1540
|
+
native.element.value = item;
|
|
1580
1541
|
native.element.dispatchEvent(new Event('input', { bubbles: false }));
|
|
1581
1542
|
list.finalize();
|
|
1582
1543
|
});
|