@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.mjs
CHANGED
|
@@ -517,92 +517,15 @@ function pointer(element, event) {
|
|
|
517
517
|
// utils
|
|
518
518
|
//----------------------------------------------------------------------------------------------------
|
|
519
519
|
const SYSTEM_EVENTS = ['start', 'update', 'render', 'stop', 'finalize'];
|
|
520
|
-
class UnitPromise {
|
|
521
|
-
constructor(promise, Component) {
|
|
522
|
-
this.promise = promise;
|
|
523
|
-
this.Component = Component;
|
|
524
|
-
}
|
|
525
|
-
then(callback) { return this.wrap('then', callback); }
|
|
526
|
-
catch(callback) { return this.wrap('catch', callback); }
|
|
527
|
-
finally(callback) { return this.wrap('finally', callback); }
|
|
528
|
-
wrap(key, callback) {
|
|
529
|
-
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
530
|
-
this.promise = this.promise[key]((...args) => Unit.scope(snapshot, callback, ...args));
|
|
531
|
-
return this;
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
class UnitTimer {
|
|
535
|
-
constructor() {
|
|
536
|
-
this.unit = null;
|
|
537
|
-
this.stack = [];
|
|
538
|
-
}
|
|
539
|
-
clear() {
|
|
540
|
-
var _a;
|
|
541
|
-
this.stack = [];
|
|
542
|
-
(_a = this.unit) === null || _a === void 0 ? void 0 : _a.finalize();
|
|
543
|
-
this.unit = null;
|
|
544
|
-
}
|
|
545
|
-
timeout(callback, duration = 0) {
|
|
546
|
-
return UnitTimer.execute(this, { callback, duration }, 1);
|
|
547
|
-
}
|
|
548
|
-
interval(callback, duration = 0, iterations = 0) {
|
|
549
|
-
return UnitTimer.execute(this, { callback, duration }, iterations);
|
|
550
|
-
}
|
|
551
|
-
transition(transition, duration = 0, easing) {
|
|
552
|
-
return UnitTimer.execute(this, { transition, duration, easing }, 1);
|
|
553
|
-
}
|
|
554
|
-
static execute(timer, options, iterations) {
|
|
555
|
-
const props = { options, iterations, snapshot: Unit.snapshot(Unit.currentUnit) };
|
|
556
|
-
if (timer.unit === null || timer.unit._.state === 'finalized') {
|
|
557
|
-
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, props);
|
|
558
|
-
}
|
|
559
|
-
else if (timer.stack.length === 0) {
|
|
560
|
-
timer.stack.push(props);
|
|
561
|
-
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
562
|
-
}
|
|
563
|
-
else {
|
|
564
|
-
timer.stack.push(props);
|
|
565
|
-
}
|
|
566
|
-
return timer;
|
|
567
|
-
}
|
|
568
|
-
static next(timer) {
|
|
569
|
-
if (timer.stack.length > 0) {
|
|
570
|
-
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.stack.shift());
|
|
571
|
-
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
static Component(unit, { options, iterations, snapshot }) {
|
|
575
|
-
let counter = 0;
|
|
576
|
-
let timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
|
|
577
|
-
function callback() {
|
|
578
|
-
if (options.callback)
|
|
579
|
-
Unit.scope(snapshot, options.callback);
|
|
580
|
-
if (iterations <= 0 || counter < iterations - 1) {
|
|
581
|
-
timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
|
|
582
|
-
}
|
|
583
|
-
else {
|
|
584
|
-
unit.finalize();
|
|
585
|
-
}
|
|
586
|
-
counter++;
|
|
587
|
-
}
|
|
588
|
-
function transition(value) {
|
|
589
|
-
if (options.transition)
|
|
590
|
-
Unit.scope(snapshot, options.transition, { value });
|
|
591
|
-
}
|
|
592
|
-
unit.on('finalize', () => timer.clear());
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
function DefaultComponent(unit, { text }) {
|
|
596
|
-
if (text !== undefined) {
|
|
597
|
-
unit.element.textContent = text;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
520
|
//----------------------------------------------------------------------------------------------------
|
|
601
521
|
// unit
|
|
602
522
|
//----------------------------------------------------------------------------------------------------
|
|
603
523
|
class Unit {
|
|
604
|
-
constructor(parent, target,
|
|
524
|
+
constructor(parent, target, Component, props) {
|
|
605
525
|
var _a;
|
|
526
|
+
const backup = Unit.currentUnit;
|
|
527
|
+
Unit.currentUnit = this;
|
|
528
|
+
parent === null || parent === void 0 ? void 0 : parent._.children.push(this);
|
|
606
529
|
let baseElement;
|
|
607
530
|
if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
608
531
|
baseElement = target;
|
|
@@ -614,20 +537,45 @@ class Unit {
|
|
|
614
537
|
baseElement = (document === null || document === void 0 ? void 0 : document.body) ? document.body : null;
|
|
615
538
|
}
|
|
616
539
|
let baseComponent;
|
|
617
|
-
if (typeof
|
|
618
|
-
baseComponent =
|
|
540
|
+
if (typeof Component === 'function') {
|
|
541
|
+
baseComponent = Component;
|
|
619
542
|
}
|
|
620
|
-
else if (typeof
|
|
621
|
-
baseComponent =
|
|
622
|
-
props = { text: component.toString() };
|
|
543
|
+
else if (typeof Component === 'string' || typeof Component === 'number') {
|
|
544
|
+
baseComponent = (unit) => { unit.element.textContent = Component.toString(); };
|
|
623
545
|
}
|
|
624
546
|
else {
|
|
625
|
-
baseComponent =
|
|
547
|
+
baseComponent = (unit) => { };
|
|
626
548
|
}
|
|
627
|
-
const baseContext = (_a = parent === null || parent === void 0 ? void 0 : parent._.currentContext) !== null && _a !== void 0 ? _a : {
|
|
628
|
-
this._ = {
|
|
629
|
-
|
|
630
|
-
|
|
549
|
+
const baseContext = (_a = parent === null || parent === void 0 ? void 0 : parent._.currentContext) !== null && _a !== void 0 ? _a : { previous: null };
|
|
550
|
+
this._ = {
|
|
551
|
+
parent,
|
|
552
|
+
state: 'invoked',
|
|
553
|
+
tostart: true,
|
|
554
|
+
protected: false,
|
|
555
|
+
currentElement: baseElement,
|
|
556
|
+
currentContext: baseContext,
|
|
557
|
+
currentComponent: null,
|
|
558
|
+
ancestors: parent ? [parent, ...parent._.ancestors] : [],
|
|
559
|
+
children: [],
|
|
560
|
+
nestElements: [],
|
|
561
|
+
promises: [],
|
|
562
|
+
results: {},
|
|
563
|
+
Components: [],
|
|
564
|
+
listeners: new MapMap(),
|
|
565
|
+
defines: {},
|
|
566
|
+
systems: { start: [], update: [], render: [], stop: [], finalize: [] },
|
|
567
|
+
eventor: new Eventor(),
|
|
568
|
+
};
|
|
569
|
+
// nest html element
|
|
570
|
+
if (typeof target === 'string') {
|
|
571
|
+
Unit.nest(this, target);
|
|
572
|
+
}
|
|
573
|
+
// setup Component
|
|
574
|
+
Unit.extend(this, baseComponent, props);
|
|
575
|
+
if (this._.state === 'invoked') {
|
|
576
|
+
this._.state = 'initialized';
|
|
577
|
+
}
|
|
578
|
+
Unit.currentUnit = backup;
|
|
631
579
|
}
|
|
632
580
|
get element() {
|
|
633
581
|
return this._.currentElement;
|
|
@@ -642,69 +590,40 @@ class Unit {
|
|
|
642
590
|
finalize() {
|
|
643
591
|
Unit.stop(this);
|
|
644
592
|
Unit.finalize(this);
|
|
645
|
-
if (this._.parent) {
|
|
646
|
-
this._.parent._.children = this._.parent._.children.filter((unit) => unit !== this);
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
reboot() {
|
|
650
|
-
let anchor = null;
|
|
651
|
-
if (this._.nestElements[0] && this._.nestElements[0].owned === true) {
|
|
652
|
-
anchor = this._.nestElements[0].element.nextElementSibling;
|
|
653
|
-
}
|
|
654
|
-
Unit.stop(this);
|
|
655
|
-
Unit.finalize(this);
|
|
656
|
-
Unit.initialize(this, anchor);
|
|
657
|
-
}
|
|
658
|
-
static initialize(unit, anchor) {
|
|
659
|
-
const backup = Unit.currentUnit;
|
|
660
|
-
Unit.currentUnit = unit;
|
|
661
|
-
unit._ = Object.assign(unit._, {
|
|
662
|
-
currentElement: unit._.baseElement,
|
|
663
|
-
currentContext: unit._.baseContext,
|
|
664
|
-
currentComponent: null,
|
|
665
|
-
anchor,
|
|
666
|
-
state: 'invoked',
|
|
667
|
-
tostart: true,
|
|
668
|
-
protected: false,
|
|
669
|
-
ancestors: unit._.parent ? [unit._.parent, ...unit._.parent._.ancestors] : [],
|
|
670
|
-
children: [],
|
|
671
|
-
nestElements: [],
|
|
672
|
-
promises: [],
|
|
673
|
-
components: [],
|
|
674
|
-
listeners: new MapMap(),
|
|
675
|
-
defines: {},
|
|
676
|
-
systems: { start: [], update: [], render: [], stop: [], finalize: [] },
|
|
677
|
-
eventor: new Eventor(),
|
|
678
|
-
});
|
|
679
|
-
// nest html element
|
|
680
|
-
if (typeof unit._.target === 'string') {
|
|
681
|
-
Unit.nest(unit, unit._.target);
|
|
682
|
-
}
|
|
683
|
-
// setup component
|
|
684
|
-
Unit.extend(unit, unit._.baseComponent, unit._.props);
|
|
685
|
-
// whether the unit promise was resolved
|
|
686
|
-
Promise.all(unit._.promises.map(p => p.promise)).then(() => unit._.state = 'initialized');
|
|
687
|
-
Unit.currentUnit = backup;
|
|
688
593
|
}
|
|
689
594
|
static finalize(unit) {
|
|
690
595
|
if (unit._.state !== 'finalized' && unit._.state !== 'finalizing') {
|
|
691
596
|
unit._.state = 'finalizing';
|
|
692
|
-
|
|
693
|
-
|
|
597
|
+
unit._.children.reverse().forEach((child) => child.finalize());
|
|
598
|
+
unit._.systems.finalize.reverse().forEach(({ execute }) => execute());
|
|
694
599
|
unit.off();
|
|
695
|
-
unit._.
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
600
|
+
unit._.nestElements.reverse().filter(item => item.owned).forEach(item => item.element.remove());
|
|
601
|
+
unit._.Components.forEach((Component) => Unit.component2units.delete(Component, unit));
|
|
602
|
+
// remove contexts
|
|
603
|
+
const contexts = Unit.unit2Contexts.get(unit);
|
|
604
|
+
contexts === null || contexts === void 0 ? void 0 : contexts.forEach((context) => {
|
|
605
|
+
let temp = context.previous;
|
|
606
|
+
while (temp !== null) {
|
|
607
|
+
if (contexts.has(temp) === false && temp.key !== undefined) {
|
|
608
|
+
context.previous = temp;
|
|
609
|
+
context.key = undefined;
|
|
610
|
+
context.value = undefined;
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
temp = temp.previous;
|
|
699
614
|
}
|
|
700
|
-
}
|
|
701
|
-
|
|
615
|
+
});
|
|
616
|
+
Unit.unit2Contexts.delete(unit);
|
|
617
|
+
unit._.currentContext = { previous: null };
|
|
702
618
|
// reset defines
|
|
703
619
|
Object.keys(unit._.defines).forEach((key) => {
|
|
704
620
|
delete unit[key];
|
|
705
621
|
});
|
|
706
622
|
unit._.defines = {};
|
|
707
623
|
unit._.state = 'finalized';
|
|
624
|
+
if (unit._.parent) {
|
|
625
|
+
unit._.parent._.children = unit._.parent._.children.filter((u) => u !== unit);
|
|
626
|
+
}
|
|
708
627
|
}
|
|
709
628
|
}
|
|
710
629
|
static nest(unit, target, textContent) {
|
|
@@ -716,16 +635,8 @@ class Unit {
|
|
|
716
635
|
else {
|
|
717
636
|
const match = target.match(/<((\w+)[^>]*?)\/?>/);
|
|
718
637
|
if (match !== null) {
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
unit._.anchor.insertAdjacentHTML('beforebegin', `<${match[1]}></${match[2]}>`);
|
|
722
|
-
element = unit._.anchor.previousElementSibling;
|
|
723
|
-
unit._.anchor = null;
|
|
724
|
-
}
|
|
725
|
-
else {
|
|
726
|
-
unit._.currentElement.insertAdjacentHTML('beforeend', `<${match[1]}></${match[2]}>`);
|
|
727
|
-
element = unit._.currentElement.children[unit._.currentElement.children.length - 1];
|
|
728
|
-
}
|
|
638
|
+
unit._.currentElement.insertAdjacentHTML('beforeend', `<${match[1]}></${match[2]}>`);
|
|
639
|
+
const element = unit._.currentElement.children[unit._.currentElement.children.length - 1];
|
|
729
640
|
unit._.currentElement = element;
|
|
730
641
|
if (textContent !== undefined) {
|
|
731
642
|
element.textContent = textContent.toString();
|
|
@@ -740,25 +651,16 @@ class Unit {
|
|
|
740
651
|
}
|
|
741
652
|
static extend(unit, Component, props) {
|
|
742
653
|
var _a;
|
|
743
|
-
if (unit._.
|
|
744
|
-
throw new Error(`The
|
|
654
|
+
if (unit._.Components.includes(Component) === true) {
|
|
655
|
+
throw new Error(`The Component is already extended.`);
|
|
745
656
|
}
|
|
746
657
|
else {
|
|
747
658
|
const backupComponent = unit._.currentComponent;
|
|
748
659
|
unit._.currentComponent = Component;
|
|
749
660
|
const defines = (_a = Component(unit, props !== null && props !== void 0 ? props : {})) !== null && _a !== void 0 ? _a : {};
|
|
750
|
-
if (unit._.parent && Component !== DefaultComponent) {
|
|
751
|
-
if (Component === unit._.baseComponent) {
|
|
752
|
-
Unit.context(unit._.parent, Component, unit);
|
|
753
|
-
}
|
|
754
|
-
else {
|
|
755
|
-
Unit.context(unit, Component, unit);
|
|
756
|
-
Unit.context(unit._.parent, Component, unit);
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
661
|
unit._.currentComponent = backupComponent;
|
|
760
662
|
Unit.component2units.add(Component, unit);
|
|
761
|
-
unit._.
|
|
663
|
+
unit._.Components.push(Component);
|
|
762
664
|
Object.keys(defines).forEach((key) => {
|
|
763
665
|
if (unit[key] !== undefined && unit._.defines[key] === undefined) {
|
|
764
666
|
throw new Error(`The property "${key}" already exists.`);
|
|
@@ -776,7 +678,7 @@ class Unit {
|
|
|
776
678
|
wrapper.value = (...args) => Unit.scope(snapshot, descriptor.value, ...args);
|
|
777
679
|
}
|
|
778
680
|
else {
|
|
779
|
-
throw new Error(`Only function properties can be defined as
|
|
681
|
+
throw new Error(`Only function properties can be defined as Component defines. [${key}]`);
|
|
780
682
|
}
|
|
781
683
|
Object.defineProperty(unit._.defines, key, wrapper);
|
|
782
684
|
Object.defineProperty(unit, key, wrapper);
|
|
@@ -836,7 +738,7 @@ class Unit {
|
|
|
836
738
|
Unit.currentUnit = snapshot.unit;
|
|
837
739
|
snapshot.unit._.currentContext = snapshot.context;
|
|
838
740
|
snapshot.unit._.currentElement = snapshot.element;
|
|
839
|
-
snapshot.unit._.currentComponent = snapshot.
|
|
741
|
+
snapshot.unit._.currentComponent = snapshot.Component;
|
|
840
742
|
return func(...args);
|
|
841
743
|
}
|
|
842
744
|
catch (error) {
|
|
@@ -846,21 +748,20 @@ class Unit {
|
|
|
846
748
|
Unit.currentUnit = currentUnit;
|
|
847
749
|
snapshot.unit._.currentContext = backup.context;
|
|
848
750
|
snapshot.unit._.currentElement = backup.element;
|
|
849
|
-
snapshot.unit._.currentComponent = backup.
|
|
751
|
+
snapshot.unit._.currentComponent = backup.Component;
|
|
850
752
|
}
|
|
851
753
|
}
|
|
852
754
|
static snapshot(unit) {
|
|
853
|
-
return { unit, context: unit._.currentContext, element: unit._.currentElement,
|
|
755
|
+
return { unit, context: unit._.currentContext, element: unit._.currentElement, Component: unit._.currentComponent };
|
|
854
756
|
}
|
|
855
|
-
static
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
}
|
|
757
|
+
static addContext(unit, orner, key, value) {
|
|
758
|
+
unit._.currentContext = { previous: unit._.currentContext, key, value };
|
|
759
|
+
Unit.unit2Contexts.add(orner, unit._.currentContext);
|
|
760
|
+
}
|
|
761
|
+
static getContext(unit, key) {
|
|
762
|
+
for (let context = unit._.currentContext; context.previous !== null; context = context.previous) {
|
|
763
|
+
if (context.key === key)
|
|
764
|
+
return context.value;
|
|
864
765
|
}
|
|
865
766
|
}
|
|
866
767
|
static find(Component) {
|
|
@@ -884,7 +785,7 @@ class Unit {
|
|
|
884
785
|
unit._.systems[type].push({ listener, execute });
|
|
885
786
|
}
|
|
886
787
|
if (unit._.listeners.has(type, listener) === false) {
|
|
887
|
-
unit._.listeners.set(type, listener, { element: unit.element,
|
|
788
|
+
unit._.listeners.set(type, listener, { element: unit.element, Component: unit._.currentComponent, execute });
|
|
888
789
|
Unit.type2units.add(type, unit);
|
|
889
790
|
if (/^[A-Za-z]/.test(type)) {
|
|
890
791
|
unit._.eventor.add(unit.element, type, execute, options);
|
|
@@ -926,11 +827,90 @@ class Unit {
|
|
|
926
827
|
}
|
|
927
828
|
}
|
|
928
829
|
Unit.currentComponent = () => { };
|
|
830
|
+
Unit.unit2Contexts = new MapSet();
|
|
929
831
|
Unit.component2units = new MapSet();
|
|
930
832
|
//----------------------------------------------------------------------------------------------------
|
|
931
833
|
// event
|
|
932
834
|
//----------------------------------------------------------------------------------------------------
|
|
933
835
|
Unit.type2units = new MapSet();
|
|
836
|
+
//----------------------------------------------------------------------------------------------------
|
|
837
|
+
// extensions
|
|
838
|
+
//----------------------------------------------------------------------------------------------------
|
|
839
|
+
class UnitPromise {
|
|
840
|
+
constructor(promise, Component) {
|
|
841
|
+
this.promise = promise;
|
|
842
|
+
this.Component = Component;
|
|
843
|
+
}
|
|
844
|
+
then(callback) { return this.wrap('then', callback); }
|
|
845
|
+
catch(callback) { return this.wrap('catch', callback); }
|
|
846
|
+
finally(callback) { return this.wrap('finally', callback); }
|
|
847
|
+
wrap(key, callback) {
|
|
848
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
849
|
+
this.promise = this.promise[key]((...args) => Unit.scope(snapshot, callback, ...args));
|
|
850
|
+
return this;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
class UnitTimer {
|
|
854
|
+
constructor() {
|
|
855
|
+
this.unit = null;
|
|
856
|
+
this.queue = [];
|
|
857
|
+
}
|
|
858
|
+
clear() {
|
|
859
|
+
var _a;
|
|
860
|
+
this.queue = [];
|
|
861
|
+
(_a = this.unit) === null || _a === void 0 ? void 0 : _a.finalize();
|
|
862
|
+
this.unit = null;
|
|
863
|
+
}
|
|
864
|
+
timeout(callback, duration = 0) {
|
|
865
|
+
return UnitTimer.execute(this, { callback, duration }, 1);
|
|
866
|
+
}
|
|
867
|
+
interval(callback, duration = 0, iterations = 0) {
|
|
868
|
+
return UnitTimer.execute(this, { callback, duration }, iterations);
|
|
869
|
+
}
|
|
870
|
+
transition(transition, duration = 0, easing) {
|
|
871
|
+
return UnitTimer.execute(this, { transition, duration, easing }, 1);
|
|
872
|
+
}
|
|
873
|
+
static execute(timer, options, iterations) {
|
|
874
|
+
const props = { options, iterations, snapshot: Unit.snapshot(Unit.currentUnit) };
|
|
875
|
+
if (timer.unit === null || timer.unit._.state === 'finalized') {
|
|
876
|
+
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, props);
|
|
877
|
+
}
|
|
878
|
+
else if (timer.queue.length === 0) {
|
|
879
|
+
timer.queue.push(props);
|
|
880
|
+
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
881
|
+
}
|
|
882
|
+
else {
|
|
883
|
+
timer.queue.push(props);
|
|
884
|
+
}
|
|
885
|
+
return timer;
|
|
886
|
+
}
|
|
887
|
+
static next(timer) {
|
|
888
|
+
if (timer.queue.length > 0) {
|
|
889
|
+
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.queue.shift());
|
|
890
|
+
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
static Component(unit, { options, iterations, snapshot }) {
|
|
894
|
+
let counter = 0;
|
|
895
|
+
let timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
|
|
896
|
+
function callback() {
|
|
897
|
+
if (options.callback)
|
|
898
|
+
Unit.scope(snapshot, options.callback);
|
|
899
|
+
if (iterations <= 0 || counter < iterations - 1) {
|
|
900
|
+
timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
|
|
901
|
+
}
|
|
902
|
+
else {
|
|
903
|
+
unit.finalize();
|
|
904
|
+
}
|
|
905
|
+
counter++;
|
|
906
|
+
}
|
|
907
|
+
function transition(value) {
|
|
908
|
+
if (options.transition)
|
|
909
|
+
Unit.scope(snapshot, options.transition, { value });
|
|
910
|
+
}
|
|
911
|
+
unit.on('finalize', () => timer.clear());
|
|
912
|
+
}
|
|
913
|
+
}
|
|
934
914
|
|
|
935
915
|
const xnew$1 = Object.assign(function (...args) {
|
|
936
916
|
if (Unit.rootUnit === undefined)
|
|
@@ -947,7 +927,9 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
947
927
|
}
|
|
948
928
|
const Component = args.shift();
|
|
949
929
|
const props = args.shift();
|
|
950
|
-
|
|
930
|
+
const unit = new Unit(Unit.currentUnit, target, Component, props);
|
|
931
|
+
Unit.addContext(Unit.currentUnit, unit, Component, unit);
|
|
932
|
+
return unit;
|
|
951
933
|
}, {
|
|
952
934
|
/**
|
|
953
935
|
* Creates a nested HTML/SVG element within the current component
|
|
@@ -984,7 +966,9 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
984
966
|
if (Unit.currentUnit._.state !== 'invoked') {
|
|
985
967
|
throw new Error('xnew.extend can not be called after initialized.');
|
|
986
968
|
}
|
|
987
|
-
|
|
969
|
+
const defines = Unit.extend(Unit.currentUnit, Component, props);
|
|
970
|
+
Unit.addContext(Unit.currentUnit, Unit.currentUnit, Component, Unit.currentUnit);
|
|
971
|
+
return defines;
|
|
988
972
|
}
|
|
989
973
|
catch (error) {
|
|
990
974
|
console.error('xnew.extend(component: Function, props?: Object): ', error);
|
|
@@ -993,7 +977,7 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
993
977
|
},
|
|
994
978
|
/**
|
|
995
979
|
* Gets a context value that can be accessed in follow context
|
|
996
|
-
* @param
|
|
980
|
+
* @param key - component function
|
|
997
981
|
* @returns The context value
|
|
998
982
|
* @example
|
|
999
983
|
* // Create unit
|
|
@@ -1003,12 +987,12 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
1003
987
|
* // Get context in child
|
|
1004
988
|
* const a = xnew.context(A)
|
|
1005
989
|
*/
|
|
1006
|
-
context(
|
|
990
|
+
context(key) {
|
|
1007
991
|
try {
|
|
1008
|
-
return Unit.
|
|
992
|
+
return Unit.getContext(Unit.currentUnit, key);
|
|
1009
993
|
}
|
|
1010
994
|
catch (error) {
|
|
1011
|
-
console.error('xnew.context(
|
|
995
|
+
console.error('xnew.context(key: any): ', error);
|
|
1012
996
|
throw error;
|
|
1013
997
|
}
|
|
1014
998
|
},
|
|
@@ -1021,13 +1005,18 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
1021
1005
|
*/
|
|
1022
1006
|
promise(promise) {
|
|
1023
1007
|
try {
|
|
1024
|
-
const
|
|
1008
|
+
const Component = Unit.currentUnit._.currentComponent;
|
|
1025
1009
|
let unitPromise;
|
|
1026
1010
|
if (promise instanceof Unit) {
|
|
1027
|
-
|
|
1011
|
+
const unit = promise;
|
|
1012
|
+
unitPromise = new UnitPromise(Promise.all(unit._.promises.map(p => p.promise)), Component)
|
|
1013
|
+
.then(() => unit._.results);
|
|
1014
|
+
}
|
|
1015
|
+
else if (promise instanceof Promise) {
|
|
1016
|
+
unitPromise = new UnitPromise(promise, Component);
|
|
1028
1017
|
}
|
|
1029
1018
|
else {
|
|
1030
|
-
unitPromise = new UnitPromise(promise,
|
|
1019
|
+
unitPromise = new UnitPromise(new Promise(xnew$1.scope(promise)), Component);
|
|
1031
1020
|
}
|
|
1032
1021
|
Unit.currentUnit._.promises.push(unitPromise);
|
|
1033
1022
|
return unitPromise;
|
|
@@ -1046,11 +1035,12 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
1046
1035
|
*/
|
|
1047
1036
|
then(callback) {
|
|
1048
1037
|
try {
|
|
1038
|
+
const currentUnit = Unit.currentUnit;
|
|
1049
1039
|
const Component = Unit.currentUnit._.currentComponent;
|
|
1050
1040
|
const promises = Unit.currentUnit._.promises;
|
|
1051
1041
|
return new UnitPromise(Promise.all(promises.map(p => p.promise)), null)
|
|
1052
|
-
.then((
|
|
1053
|
-
callback(
|
|
1042
|
+
.then(() => {
|
|
1043
|
+
callback(currentUnit._.results);
|
|
1054
1044
|
});
|
|
1055
1045
|
}
|
|
1056
1046
|
catch (error) {
|
|
@@ -1076,6 +1066,22 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
1076
1066
|
throw error;
|
|
1077
1067
|
}
|
|
1078
1068
|
},
|
|
1069
|
+
/**
|
|
1070
|
+
* Assigns a value to the current unit's promise
|
|
1071
|
+
* @param object - object to assign to the promise
|
|
1072
|
+
* @returns void
|
|
1073
|
+
* @example
|
|
1074
|
+
* xnew.assign({ data: 123});
|
|
1075
|
+
*/
|
|
1076
|
+
assign(object) {
|
|
1077
|
+
try {
|
|
1078
|
+
Object.assign(Unit.currentUnit._.results, object);
|
|
1079
|
+
}
|
|
1080
|
+
catch (error) {
|
|
1081
|
+
console.error('xnew.assign(object?: Record<string, any>): ', error);
|
|
1082
|
+
throw error;
|
|
1083
|
+
}
|
|
1084
|
+
},
|
|
1079
1085
|
/**
|
|
1080
1086
|
* Executes callback after all registered promises settle (resolve or reject)
|
|
1081
1087
|
* @param callback - Function to call after promises settle
|
|
@@ -1109,19 +1115,19 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
1109
1115
|
},
|
|
1110
1116
|
/**
|
|
1111
1117
|
* Finds all instances of a component in the component tree
|
|
1112
|
-
* @param
|
|
1118
|
+
* @param Component - Component function to search for
|
|
1113
1119
|
* @returns Array of Unit instances matching the component
|
|
1114
1120
|
* @throws Error if component parameter is invalid
|
|
1115
1121
|
* @example
|
|
1116
1122
|
* const buttons = xnew.find(ButtonComponent)
|
|
1117
1123
|
* buttons.forEach(btn => btn.finalize())
|
|
1118
1124
|
*/
|
|
1119
|
-
find(
|
|
1125
|
+
find(Component) {
|
|
1120
1126
|
try {
|
|
1121
|
-
return Unit.find(
|
|
1127
|
+
return Unit.find(Component);
|
|
1122
1128
|
}
|
|
1123
1129
|
catch (error) {
|
|
1124
|
-
console.error('xnew.find(
|
|
1130
|
+
console.error('xnew.find(Component: Function): ', error);
|
|
1125
1131
|
throw error;
|
|
1126
1132
|
}
|
|
1127
1133
|
},
|
|
@@ -1392,23 +1398,23 @@ function Panel(unit, { name, open = false, params }) {
|
|
|
1392
1398
|
const button = xnew$1(Button, { key });
|
|
1393
1399
|
return button;
|
|
1394
1400
|
},
|
|
1395
|
-
select(key, {
|
|
1401
|
+
select(key, { value, items = [] } = {}) {
|
|
1396
1402
|
var _a, _b;
|
|
1397
|
-
object[key] = (_b = (_a = object[key]) !== null && _a !== void 0 ? _a :
|
|
1398
|
-
const select = xnew$1(Select, { key, value: object[key],
|
|
1403
|
+
object[key] = (_b = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : items[0]) !== null && _b !== void 0 ? _b : '';
|
|
1404
|
+
const select = xnew$1(Select, { key, value: object[key], items });
|
|
1399
1405
|
select.on('input', ({ value }) => object[key] = value);
|
|
1400
1406
|
return select;
|
|
1401
1407
|
},
|
|
1402
|
-
range(key,
|
|
1403
|
-
var _a
|
|
1404
|
-
object[key] = (
|
|
1405
|
-
const number = xnew$1(Range,
|
|
1408
|
+
range(key, { value, min = 0, max = 100, step = 1 } = {}) {
|
|
1409
|
+
var _a;
|
|
1410
|
+
object[key] = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : min;
|
|
1411
|
+
const number = xnew$1(Range, { key, value: object[key], min, max, step });
|
|
1406
1412
|
number.on('input', ({ value }) => object[key] = value);
|
|
1407
1413
|
return number;
|
|
1408
1414
|
},
|
|
1409
|
-
checkbox(key) {
|
|
1415
|
+
checkbox(key, { value } = {}) {
|
|
1410
1416
|
var _a;
|
|
1411
|
-
object[key] = (_a = object[key]) !== null && _a !== void 0 ? _a : false;
|
|
1417
|
+
object[key] = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : false;
|
|
1412
1418
|
const checkbox = xnew$1(Checkbox, { key, value: object[key] });
|
|
1413
1419
|
checkbox.on('input', ({ value }) => object[key] = value);
|
|
1414
1420
|
return checkbox;
|
|
@@ -1492,14 +1498,14 @@ function Checkbox(unit, { key = '', value } = {}) {
|
|
|
1492
1498
|
update(value);
|
|
1493
1499
|
});
|
|
1494
1500
|
}
|
|
1495
|
-
function Select(_, { key = '', value,
|
|
1501
|
+
function Select(_, { key = '', value, items = [] } = {}) {
|
|
1496
1502
|
var _a;
|
|
1497
|
-
const initial = (_a = value !== null && value !== void 0 ? value :
|
|
1503
|
+
const initial = (_a = value !== null && value !== void 0 ? value : items[0]) !== null && _a !== void 0 ? _a : '';
|
|
1498
1504
|
xnew$1.nest(`<div style="position: relative; height: 2em; margin: 0.125em 0; padding: 0 0.5em; display: flex; align-items: center;">`);
|
|
1499
1505
|
xnew$1('<div style="flex: 1;">', key);
|
|
1500
1506
|
const native = xnew$1(`<select name="${key}" style="display: none;">`, () => {
|
|
1501
|
-
for (const
|
|
1502
|
-
xnew$1(`<option value="${
|
|
1507
|
+
for (const item of items) {
|
|
1508
|
+
xnew$1(`<option value="${item}" ${item === initial ? 'selected' : ''}>`, item);
|
|
1503
1509
|
}
|
|
1504
1510
|
});
|
|
1505
1511
|
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);
|
|
@@ -1519,13 +1525,13 @@ function Select(_, { key = '', value, options = [] } = {}) {
|
|
|
1519
1525
|
});
|
|
1520
1526
|
xnew$1.extend(Accordion);
|
|
1521
1527
|
xnew$1.nest(`<div style="position: relative; border: 1px solid ${currentColorA}; border-radius: 0.25em; overflow: hidden;">`);
|
|
1522
|
-
for (const
|
|
1523
|
-
const
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
button.element.textContent =
|
|
1528
|
-
native.element.value =
|
|
1528
|
+
for (const item of items) {
|
|
1529
|
+
const div = xnew$1(`<div style="height: 2em; padding: 0 0.5em; display: flex; align-items: center; cursor: pointer; user-select: none;">`, item);
|
|
1530
|
+
div.on('pointerover', () => div.element.style.background = currentColorB);
|
|
1531
|
+
div.on('pointerout', () => div.element.style.background = '');
|
|
1532
|
+
div.on('click', () => {
|
|
1533
|
+
button.element.textContent = item;
|
|
1534
|
+
native.element.value = item;
|
|
1529
1535
|
native.element.dispatchEvent(new Event('input', { bubbles: false }));
|
|
1530
1536
|
list.finalize();
|
|
1531
1537
|
});
|