@mulsense/xnew 0.6.4 → 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/xmatter.js +1 -1
- package/dist/addons/xmatter.mjs +1 -1
- package/dist/addons/xpixi.js +4 -2
- package/dist/addons/xpixi.mjs +4 -2
- package/dist/addons/xrapier2d.js +1 -1
- package/dist/addons/xrapier2d.mjs +1 -1
- package/dist/addons/xrapier3d.js +1 -1
- package/dist/addons/xrapier3d.mjs +1 -1
- package/dist/addons/xthree.js +1 -1
- package/dist/addons/xthree.mjs +1 -1
- package/dist/xnew.d.ts +45 -44
- package/dist/xnew.js +219 -213
- package/dist/xnew.mjs +219 -213
- 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,69 +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
|
-
unit._ = Object.assign(unit._, {
|
|
668
|
-
currentElement: unit._.baseElement,
|
|
669
|
-
currentContext: unit._.baseContext,
|
|
670
|
-
currentComponent: null,
|
|
671
|
-
anchor,
|
|
672
|
-
state: 'invoked',
|
|
673
|
-
tostart: true,
|
|
674
|
-
protected: false,
|
|
675
|
-
ancestors: unit._.parent ? [unit._.parent, ...unit._.parent._.ancestors] : [],
|
|
676
|
-
children: [],
|
|
677
|
-
nestElements: [],
|
|
678
|
-
promises: [],
|
|
679
|
-
components: [],
|
|
680
|
-
listeners: new MapMap(),
|
|
681
|
-
defines: {},
|
|
682
|
-
systems: { start: [], update: [], render: [], stop: [], finalize: [] },
|
|
683
|
-
eventor: new Eventor(),
|
|
684
|
-
});
|
|
685
|
-
// nest html element
|
|
686
|
-
if (typeof unit._.target === 'string') {
|
|
687
|
-
Unit.nest(unit, unit._.target);
|
|
688
|
-
}
|
|
689
|
-
// setup component
|
|
690
|
-
Unit.extend(unit, unit._.baseComponent, unit._.props);
|
|
691
|
-
// whether the unit promise was resolved
|
|
692
|
-
Promise.all(unit._.promises.map(p => p.promise)).then(() => unit._.state = 'initialized');
|
|
693
|
-
Unit.currentUnit = backup;
|
|
694
599
|
}
|
|
695
600
|
static finalize(unit) {
|
|
696
601
|
if (unit._.state !== 'finalized' && unit._.state !== 'finalizing') {
|
|
697
602
|
unit._.state = 'finalizing';
|
|
698
|
-
|
|
699
|
-
|
|
603
|
+
unit._.children.reverse().forEach((child) => child.finalize());
|
|
604
|
+
unit._.systems.finalize.reverse().forEach(({ execute }) => execute());
|
|
700
605
|
unit.off();
|
|
701
|
-
unit._.
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
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;
|
|
705
620
|
}
|
|
706
|
-
}
|
|
707
|
-
|
|
621
|
+
});
|
|
622
|
+
Unit.unit2Contexts.delete(unit);
|
|
623
|
+
unit._.currentContext = { previous: null };
|
|
708
624
|
// reset defines
|
|
709
625
|
Object.keys(unit._.defines).forEach((key) => {
|
|
710
626
|
delete unit[key];
|
|
711
627
|
});
|
|
712
628
|
unit._.defines = {};
|
|
713
629
|
unit._.state = 'finalized';
|
|
630
|
+
if (unit._.parent) {
|
|
631
|
+
unit._.parent._.children = unit._.parent._.children.filter((u) => u !== unit);
|
|
632
|
+
}
|
|
714
633
|
}
|
|
715
634
|
}
|
|
716
635
|
static nest(unit, target, textContent) {
|
|
@@ -722,16 +641,8 @@
|
|
|
722
641
|
else {
|
|
723
642
|
const match = target.match(/<((\w+)[^>]*?)\/?>/);
|
|
724
643
|
if (match !== null) {
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
unit._.anchor.insertAdjacentHTML('beforebegin', `<${match[1]}></${match[2]}>`);
|
|
728
|
-
element = unit._.anchor.previousElementSibling;
|
|
729
|
-
unit._.anchor = null;
|
|
730
|
-
}
|
|
731
|
-
else {
|
|
732
|
-
unit._.currentElement.insertAdjacentHTML('beforeend', `<${match[1]}></${match[2]}>`);
|
|
733
|
-
element = unit._.currentElement.children[unit._.currentElement.children.length - 1];
|
|
734
|
-
}
|
|
644
|
+
unit._.currentElement.insertAdjacentHTML('beforeend', `<${match[1]}></${match[2]}>`);
|
|
645
|
+
const element = unit._.currentElement.children[unit._.currentElement.children.length - 1];
|
|
735
646
|
unit._.currentElement = element;
|
|
736
647
|
if (textContent !== undefined) {
|
|
737
648
|
element.textContent = textContent.toString();
|
|
@@ -746,25 +657,16 @@
|
|
|
746
657
|
}
|
|
747
658
|
static extend(unit, Component, props) {
|
|
748
659
|
var _a;
|
|
749
|
-
if (unit._.
|
|
750
|
-
throw new Error(`The
|
|
660
|
+
if (unit._.Components.includes(Component) === true) {
|
|
661
|
+
throw new Error(`The Component is already extended.`);
|
|
751
662
|
}
|
|
752
663
|
else {
|
|
753
664
|
const backupComponent = unit._.currentComponent;
|
|
754
665
|
unit._.currentComponent = Component;
|
|
755
666
|
const defines = (_a = Component(unit, props !== null && props !== void 0 ? props : {})) !== null && _a !== void 0 ? _a : {};
|
|
756
|
-
if (unit._.parent && Component !== DefaultComponent) {
|
|
757
|
-
if (Component === unit._.baseComponent) {
|
|
758
|
-
Unit.context(unit._.parent, Component, unit);
|
|
759
|
-
}
|
|
760
|
-
else {
|
|
761
|
-
Unit.context(unit, Component, unit);
|
|
762
|
-
Unit.context(unit._.parent, Component, unit);
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
667
|
unit._.currentComponent = backupComponent;
|
|
766
668
|
Unit.component2units.add(Component, unit);
|
|
767
|
-
unit._.
|
|
669
|
+
unit._.Components.push(Component);
|
|
768
670
|
Object.keys(defines).forEach((key) => {
|
|
769
671
|
if (unit[key] !== undefined && unit._.defines[key] === undefined) {
|
|
770
672
|
throw new Error(`The property "${key}" already exists.`);
|
|
@@ -782,7 +684,7 @@
|
|
|
782
684
|
wrapper.value = (...args) => Unit.scope(snapshot, descriptor.value, ...args);
|
|
783
685
|
}
|
|
784
686
|
else {
|
|
785
|
-
throw new Error(`Only function properties can be defined as
|
|
687
|
+
throw new Error(`Only function properties can be defined as Component defines. [${key}]`);
|
|
786
688
|
}
|
|
787
689
|
Object.defineProperty(unit._.defines, key, wrapper);
|
|
788
690
|
Object.defineProperty(unit, key, wrapper);
|
|
@@ -842,7 +744,7 @@
|
|
|
842
744
|
Unit.currentUnit = snapshot.unit;
|
|
843
745
|
snapshot.unit._.currentContext = snapshot.context;
|
|
844
746
|
snapshot.unit._.currentElement = snapshot.element;
|
|
845
|
-
snapshot.unit._.currentComponent = snapshot.
|
|
747
|
+
snapshot.unit._.currentComponent = snapshot.Component;
|
|
846
748
|
return func(...args);
|
|
847
749
|
}
|
|
848
750
|
catch (error) {
|
|
@@ -852,21 +754,20 @@
|
|
|
852
754
|
Unit.currentUnit = currentUnit;
|
|
853
755
|
snapshot.unit._.currentContext = backup.context;
|
|
854
756
|
snapshot.unit._.currentElement = backup.element;
|
|
855
|
-
snapshot.unit._.currentComponent = backup.
|
|
757
|
+
snapshot.unit._.currentComponent = backup.Component;
|
|
856
758
|
}
|
|
857
759
|
}
|
|
858
760
|
static snapshot(unit) {
|
|
859
|
-
return { unit, context: unit._.currentContext, element: unit._.currentElement,
|
|
761
|
+
return { unit, context: unit._.currentContext, element: unit._.currentElement, Component: unit._.currentComponent };
|
|
860
762
|
}
|
|
861
|
-
static
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
}
|
|
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;
|
|
870
771
|
}
|
|
871
772
|
}
|
|
872
773
|
static find(Component) {
|
|
@@ -890,7 +791,7 @@
|
|
|
890
791
|
unit._.systems[type].push({ listener, execute });
|
|
891
792
|
}
|
|
892
793
|
if (unit._.listeners.has(type, listener) === false) {
|
|
893
|
-
unit._.listeners.set(type, listener, { element: unit.element,
|
|
794
|
+
unit._.listeners.set(type, listener, { element: unit.element, Component: unit._.currentComponent, execute });
|
|
894
795
|
Unit.type2units.add(type, unit);
|
|
895
796
|
if (/^[A-Za-z]/.test(type)) {
|
|
896
797
|
unit._.eventor.add(unit.element, type, execute, options);
|
|
@@ -932,11 +833,90 @@
|
|
|
932
833
|
}
|
|
933
834
|
}
|
|
934
835
|
Unit.currentComponent = () => { };
|
|
836
|
+
Unit.unit2Contexts = new MapSet();
|
|
935
837
|
Unit.component2units = new MapSet();
|
|
936
838
|
//----------------------------------------------------------------------------------------------------
|
|
937
839
|
// event
|
|
938
840
|
//----------------------------------------------------------------------------------------------------
|
|
939
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
|
+
}
|
|
940
920
|
|
|
941
921
|
const xnew$1 = Object.assign(function (...args) {
|
|
942
922
|
if (Unit.rootUnit === undefined)
|
|
@@ -953,7 +933,9 @@
|
|
|
953
933
|
}
|
|
954
934
|
const Component = args.shift();
|
|
955
935
|
const props = args.shift();
|
|
956
|
-
|
|
936
|
+
const unit = new Unit(Unit.currentUnit, target, Component, props);
|
|
937
|
+
Unit.addContext(Unit.currentUnit, unit, Component, unit);
|
|
938
|
+
return unit;
|
|
957
939
|
}, {
|
|
958
940
|
/**
|
|
959
941
|
* Creates a nested HTML/SVG element within the current component
|
|
@@ -990,7 +972,9 @@
|
|
|
990
972
|
if (Unit.currentUnit._.state !== 'invoked') {
|
|
991
973
|
throw new Error('xnew.extend can not be called after initialized.');
|
|
992
974
|
}
|
|
993
|
-
|
|
975
|
+
const defines = Unit.extend(Unit.currentUnit, Component, props);
|
|
976
|
+
Unit.addContext(Unit.currentUnit, Unit.currentUnit, Component, Unit.currentUnit);
|
|
977
|
+
return defines;
|
|
994
978
|
}
|
|
995
979
|
catch (error) {
|
|
996
980
|
console.error('xnew.extend(component: Function, props?: Object): ', error);
|
|
@@ -999,7 +983,7 @@
|
|
|
999
983
|
},
|
|
1000
984
|
/**
|
|
1001
985
|
* Gets a context value that can be accessed in follow context
|
|
1002
|
-
* @param
|
|
986
|
+
* @param key - component function
|
|
1003
987
|
* @returns The context value
|
|
1004
988
|
* @example
|
|
1005
989
|
* // Create unit
|
|
@@ -1009,12 +993,12 @@
|
|
|
1009
993
|
* // Get context in child
|
|
1010
994
|
* const a = xnew.context(A)
|
|
1011
995
|
*/
|
|
1012
|
-
context(
|
|
996
|
+
context(key) {
|
|
1013
997
|
try {
|
|
1014
|
-
return Unit.
|
|
998
|
+
return Unit.getContext(Unit.currentUnit, key);
|
|
1015
999
|
}
|
|
1016
1000
|
catch (error) {
|
|
1017
|
-
console.error('xnew.context(
|
|
1001
|
+
console.error('xnew.context(key: any): ', error);
|
|
1018
1002
|
throw error;
|
|
1019
1003
|
}
|
|
1020
1004
|
},
|
|
@@ -1027,13 +1011,18 @@
|
|
|
1027
1011
|
*/
|
|
1028
1012
|
promise(promise) {
|
|
1029
1013
|
try {
|
|
1030
|
-
const
|
|
1014
|
+
const Component = Unit.currentUnit._.currentComponent;
|
|
1031
1015
|
let unitPromise;
|
|
1032
1016
|
if (promise instanceof Unit) {
|
|
1033
|
-
|
|
1017
|
+
const unit = promise;
|
|
1018
|
+
unitPromise = new UnitPromise(Promise.all(unit._.promises.map(p => p.promise)), Component)
|
|
1019
|
+
.then(() => unit._.results);
|
|
1020
|
+
}
|
|
1021
|
+
else if (promise instanceof Promise) {
|
|
1022
|
+
unitPromise = new UnitPromise(promise, Component);
|
|
1034
1023
|
}
|
|
1035
1024
|
else {
|
|
1036
|
-
unitPromise = new UnitPromise(promise,
|
|
1025
|
+
unitPromise = new UnitPromise(new Promise(xnew$1.scope(promise)), Component);
|
|
1037
1026
|
}
|
|
1038
1027
|
Unit.currentUnit._.promises.push(unitPromise);
|
|
1039
1028
|
return unitPromise;
|
|
@@ -1052,11 +1041,12 @@
|
|
|
1052
1041
|
*/
|
|
1053
1042
|
then(callback) {
|
|
1054
1043
|
try {
|
|
1044
|
+
const currentUnit = Unit.currentUnit;
|
|
1055
1045
|
const Component = Unit.currentUnit._.currentComponent;
|
|
1056
1046
|
const promises = Unit.currentUnit._.promises;
|
|
1057
1047
|
return new UnitPromise(Promise.all(promises.map(p => p.promise)), null)
|
|
1058
|
-
.then((
|
|
1059
|
-
callback(
|
|
1048
|
+
.then(() => {
|
|
1049
|
+
callback(currentUnit._.results);
|
|
1060
1050
|
});
|
|
1061
1051
|
}
|
|
1062
1052
|
catch (error) {
|
|
@@ -1082,6 +1072,22 @@
|
|
|
1082
1072
|
throw error;
|
|
1083
1073
|
}
|
|
1084
1074
|
},
|
|
1075
|
+
/**
|
|
1076
|
+
* Assigns a value to the current unit's promise
|
|
1077
|
+
* @param object - object to assign to the promise
|
|
1078
|
+
* @returns void
|
|
1079
|
+
* @example
|
|
1080
|
+
* xnew.assign({ data: 123});
|
|
1081
|
+
*/
|
|
1082
|
+
assign(object) {
|
|
1083
|
+
try {
|
|
1084
|
+
Object.assign(Unit.currentUnit._.results, object);
|
|
1085
|
+
}
|
|
1086
|
+
catch (error) {
|
|
1087
|
+
console.error('xnew.assign(object?: Record<string, any>): ', error);
|
|
1088
|
+
throw error;
|
|
1089
|
+
}
|
|
1090
|
+
},
|
|
1085
1091
|
/**
|
|
1086
1092
|
* Executes callback after all registered promises settle (resolve or reject)
|
|
1087
1093
|
* @param callback - Function to call after promises settle
|
|
@@ -1115,19 +1121,19 @@
|
|
|
1115
1121
|
},
|
|
1116
1122
|
/**
|
|
1117
1123
|
* Finds all instances of a component in the component tree
|
|
1118
|
-
* @param
|
|
1124
|
+
* @param Component - Component function to search for
|
|
1119
1125
|
* @returns Array of Unit instances matching the component
|
|
1120
1126
|
* @throws Error if component parameter is invalid
|
|
1121
1127
|
* @example
|
|
1122
1128
|
* const buttons = xnew.find(ButtonComponent)
|
|
1123
1129
|
* buttons.forEach(btn => btn.finalize())
|
|
1124
1130
|
*/
|
|
1125
|
-
find(
|
|
1131
|
+
find(Component) {
|
|
1126
1132
|
try {
|
|
1127
|
-
return Unit.find(
|
|
1133
|
+
return Unit.find(Component);
|
|
1128
1134
|
}
|
|
1129
1135
|
catch (error) {
|
|
1130
|
-
console.error('xnew.find(
|
|
1136
|
+
console.error('xnew.find(Component: Function): ', error);
|
|
1131
1137
|
throw error;
|
|
1132
1138
|
}
|
|
1133
1139
|
},
|
|
@@ -1398,23 +1404,23 @@
|
|
|
1398
1404
|
const button = xnew$1(Button, { key });
|
|
1399
1405
|
return button;
|
|
1400
1406
|
},
|
|
1401
|
-
select(key, {
|
|
1407
|
+
select(key, { value, items = [] } = {}) {
|
|
1402
1408
|
var _a, _b;
|
|
1403
|
-
object[key] = (_b = (_a = object[key]) !== null && _a !== void 0 ? _a :
|
|
1404
|
-
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 });
|
|
1405
1411
|
select.on('input', ({ value }) => object[key] = value);
|
|
1406
1412
|
return select;
|
|
1407
1413
|
},
|
|
1408
|
-
range(key,
|
|
1409
|
-
var _a
|
|
1410
|
-
object[key] = (
|
|
1411
|
-
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 });
|
|
1412
1418
|
number.on('input', ({ value }) => object[key] = value);
|
|
1413
1419
|
return number;
|
|
1414
1420
|
},
|
|
1415
|
-
checkbox(key) {
|
|
1421
|
+
checkbox(key, { value } = {}) {
|
|
1416
1422
|
var _a;
|
|
1417
|
-
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;
|
|
1418
1424
|
const checkbox = xnew$1(Checkbox, { key, value: object[key] });
|
|
1419
1425
|
checkbox.on('input', ({ value }) => object[key] = value);
|
|
1420
1426
|
return checkbox;
|
|
@@ -1498,14 +1504,14 @@
|
|
|
1498
1504
|
update(value);
|
|
1499
1505
|
});
|
|
1500
1506
|
}
|
|
1501
|
-
function Select(_, { key = '', value,
|
|
1507
|
+
function Select(_, { key = '', value, items = [] } = {}) {
|
|
1502
1508
|
var _a;
|
|
1503
|
-
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 : '';
|
|
1504
1510
|
xnew$1.nest(`<div style="position: relative; height: 2em; margin: 0.125em 0; padding: 0 0.5em; display: flex; align-items: center;">`);
|
|
1505
1511
|
xnew$1('<div style="flex: 1;">', key);
|
|
1506
1512
|
const native = xnew$1(`<select name="${key}" style="display: none;">`, () => {
|
|
1507
|
-
for (const
|
|
1508
|
-
xnew$1(`<option value="${
|
|
1513
|
+
for (const item of items) {
|
|
1514
|
+
xnew$1(`<option value="${item}" ${item === initial ? 'selected' : ''}>`, item);
|
|
1509
1515
|
}
|
|
1510
1516
|
});
|
|
1511
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);
|
|
@@ -1525,13 +1531,13 @@
|
|
|
1525
1531
|
});
|
|
1526
1532
|
xnew$1.extend(Accordion);
|
|
1527
1533
|
xnew$1.nest(`<div style="position: relative; border: 1px solid ${currentColorA}; border-radius: 0.25em; overflow: hidden;">`);
|
|
1528
|
-
for (const
|
|
1529
|
-
const
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
button.element.textContent =
|
|
1534
|
-
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;
|
|
1535
1541
|
native.element.dispatchEvent(new Event('input', { bubbles: false }));
|
|
1536
1542
|
list.finalize();
|
|
1537
1543
|
});
|