@mulsense/xnew 0.5.2 → 0.5.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/dist/xnew.d.ts +26 -32
- package/dist/xnew.js +206 -193
- package/dist/xnew.mjs +206 -193
- package/package.json +1 -1
package/dist/xnew.mjs
CHANGED
|
@@ -137,11 +137,11 @@ class Timer {
|
|
|
137
137
|
else if (this.options.easing === 'ease-in') {
|
|
138
138
|
p = Math.pow((1.0 - Math.pow((1.0 - p), 0.5)), 2.0);
|
|
139
139
|
}
|
|
140
|
-
else if (this.options.easing === 'ease') {
|
|
141
|
-
p = (1.0 - Math.cos(p * Math.PI)) / 2.0;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
p = (
|
|
140
|
+
else if (this.options.easing === 'ease' || this.options.easing === 'ease-in-out') {
|
|
141
|
+
// p = (1.0 - Math.cos(p * Math.PI)) / 2.0;
|
|
142
|
+
const bias = (this.options.easing === 'ease') ? 0.7 : 1.0;
|
|
143
|
+
const s = Math.pow(p, bias);
|
|
144
|
+
p = s * s * (3 - 2 * s);
|
|
145
145
|
}
|
|
146
146
|
(_b = (_a = this.options).transition) === null || _b === void 0 ? void 0 : _b.call(_a, p);
|
|
147
147
|
});
|
|
@@ -199,7 +199,7 @@ class Timer {
|
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
class
|
|
202
|
+
class Eventor {
|
|
203
203
|
constructor() {
|
|
204
204
|
this.map = new MapMap();
|
|
205
205
|
}
|
|
@@ -519,12 +519,93 @@ function pointer(element, event) {
|
|
|
519
519
|
// utils
|
|
520
520
|
//----------------------------------------------------------------------------------------------------
|
|
521
521
|
const SYSTEM_EVENTS = ['start', 'update', 'render', 'stop', 'finalize'];
|
|
522
|
+
class UnitPromise {
|
|
523
|
+
constructor(promise, component) {
|
|
524
|
+
this.promise = promise;
|
|
525
|
+
this.component = component;
|
|
526
|
+
}
|
|
527
|
+
then(callback) {
|
|
528
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
529
|
+
this.promise = this.promise.then((...args) => Unit.scope(snapshot, callback, ...args));
|
|
530
|
+
return this;
|
|
531
|
+
}
|
|
532
|
+
catch(callback) {
|
|
533
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
534
|
+
this.promise = this.promise.catch((...args) => Unit.scope(snapshot, callback, ...args));
|
|
535
|
+
return this;
|
|
536
|
+
}
|
|
537
|
+
finally(callback) {
|
|
538
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
539
|
+
this.promise = this.promise.finally(() => Unit.scope(snapshot, callback));
|
|
540
|
+
return this;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
class UnitTimer {
|
|
544
|
+
constructor(options) {
|
|
545
|
+
this.stack = [];
|
|
546
|
+
this.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, { options, snapshot: Unit.snapshot(Unit.currentUnit) });
|
|
547
|
+
}
|
|
548
|
+
clear() {
|
|
549
|
+
this.stack = [];
|
|
550
|
+
this.unit.finalize();
|
|
551
|
+
}
|
|
552
|
+
timeout(timeout, duration = 0) {
|
|
553
|
+
UnitTimer.execute(this, { timeout, duration, iterations: 1 });
|
|
554
|
+
return this;
|
|
555
|
+
}
|
|
556
|
+
iteration(timeout, duration = 0, iterations = -1) {
|
|
557
|
+
UnitTimer.execute(this, { timeout, duration, iterations });
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
transition(transition, duration = 0, easing) {
|
|
561
|
+
UnitTimer.execute(this, { transition, duration, iterations: 1, easing });
|
|
562
|
+
return this;
|
|
563
|
+
}
|
|
564
|
+
static execute(timer, options) {
|
|
565
|
+
if (timer.unit._.state === 'finalized') {
|
|
566
|
+
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, { options, snapshot: Unit.snapshot(Unit.currentUnit) });
|
|
567
|
+
}
|
|
568
|
+
else if (timer.stack.length === 0) {
|
|
569
|
+
timer.stack.push({ options, snapshot: Unit.snapshot(Unit.currentUnit) });
|
|
570
|
+
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
timer.stack.push({ options, snapshot: Unit.snapshot(Unit.currentUnit) });
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
static next(timer) {
|
|
577
|
+
if (timer.stack.length > 0) {
|
|
578
|
+
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.stack.shift());
|
|
579
|
+
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
static Component(unit, { options, snapshot }) {
|
|
583
|
+
let counter = 0;
|
|
584
|
+
const timer = new Timer({
|
|
585
|
+
transition: (p) => {
|
|
586
|
+
if (options.transition)
|
|
587
|
+
Unit.scope(snapshot, options.transition, p);
|
|
588
|
+
},
|
|
589
|
+
timeout: () => {
|
|
590
|
+
if (options.transition)
|
|
591
|
+
Unit.scope(snapshot, options.transition, 1.0);
|
|
592
|
+
if (options.timeout)
|
|
593
|
+
Unit.scope(snapshot, options.timeout);
|
|
594
|
+
if (options.iterations && counter >= options.iterations - 1) {
|
|
595
|
+
unit.finalize();
|
|
596
|
+
}
|
|
597
|
+
counter++;
|
|
598
|
+
}, duration: options.duration, iterations: options.iterations, easing: options.easing
|
|
599
|
+
});
|
|
600
|
+
unit.on('finalize', () => timer.clear());
|
|
601
|
+
}
|
|
602
|
+
}
|
|
522
603
|
//----------------------------------------------------------------------------------------------------
|
|
523
604
|
// unit
|
|
524
605
|
//----------------------------------------------------------------------------------------------------
|
|
525
606
|
class Unit {
|
|
526
|
-
constructor(parent, target, component, props
|
|
527
|
-
var _a
|
|
607
|
+
constructor(parent, target, component, props) {
|
|
608
|
+
var _a;
|
|
528
609
|
let baseElement;
|
|
529
610
|
if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
530
611
|
baseElement = target;
|
|
@@ -539,15 +620,14 @@ class Unit {
|
|
|
539
620
|
if (typeof component === 'function') {
|
|
540
621
|
baseComponent = component;
|
|
541
622
|
}
|
|
542
|
-
else if (
|
|
543
|
-
baseComponent = (unit) => { unit.element.textContent = component; };
|
|
623
|
+
else if (component !== undefined) {
|
|
624
|
+
baseComponent = (unit) => { unit.element.textContent = component.toString(); };
|
|
544
625
|
}
|
|
545
626
|
else {
|
|
546
627
|
baseComponent = (unit) => { };
|
|
547
628
|
}
|
|
548
629
|
const baseContext = (_a = parent === null || parent === void 0 ? void 0 : parent._.currentContext) !== null && _a !== void 0 ? _a : { stack: null };
|
|
549
|
-
|
|
550
|
-
this._ = { parent, target, baseElement, baseContext, baseComponent, props, config: { protect } };
|
|
630
|
+
this._ = { parent, target, baseElement, baseContext, baseComponent, props };
|
|
551
631
|
parent === null || parent === void 0 ? void 0 : parent._.children.push(this);
|
|
552
632
|
Unit.initialize(this, null);
|
|
553
633
|
}
|
|
@@ -576,7 +656,6 @@ class Unit {
|
|
|
576
656
|
Unit.initialize(this, anchor);
|
|
577
657
|
}
|
|
578
658
|
static initialize(unit, anchor) {
|
|
579
|
-
var _a, _b;
|
|
580
659
|
const backup = Unit.currentUnit;
|
|
581
660
|
Unit.currentUnit = unit;
|
|
582
661
|
unit._ = Object.assign(unit._, {
|
|
@@ -586,7 +665,8 @@ class Unit {
|
|
|
586
665
|
anchor,
|
|
587
666
|
state: 'invoked',
|
|
588
667
|
tostart: true,
|
|
589
|
-
|
|
668
|
+
protected: false,
|
|
669
|
+
ancestors: unit._.parent ? [unit._.parent, ...unit._.parent._.ancestors] : [],
|
|
590
670
|
children: [],
|
|
591
671
|
elements: [],
|
|
592
672
|
promises: [],
|
|
@@ -594,7 +674,7 @@ class Unit {
|
|
|
594
674
|
listeners: new MapMap(),
|
|
595
675
|
defines: {},
|
|
596
676
|
systems: { start: [], update: [], render: [], stop: [], finalize: [] },
|
|
597
|
-
|
|
677
|
+
eventor: new Eventor(),
|
|
598
678
|
});
|
|
599
679
|
// nest html element
|
|
600
680
|
if (typeof unit._.target === 'string') {
|
|
@@ -627,11 +707,8 @@ class Unit {
|
|
|
627
707
|
unit._.state = 'finalized';
|
|
628
708
|
}
|
|
629
709
|
}
|
|
630
|
-
static nest(unit,
|
|
631
|
-
|
|
632
|
-
throw new Error('This function can not be called after initialized.');
|
|
633
|
-
}
|
|
634
|
-
const match = htmlString.match(/<((\w+)[^>]*?)\/?>/);
|
|
710
|
+
static nest(unit, tag, textContent) {
|
|
711
|
+
const match = tag.match(/<((\w+)[^>]*?)\/?>/);
|
|
635
712
|
if (match !== null) {
|
|
636
713
|
let element;
|
|
637
714
|
if (unit._.anchor !== null) {
|
|
@@ -645,20 +722,17 @@ class Unit {
|
|
|
645
722
|
}
|
|
646
723
|
unit._.currentElement = element;
|
|
647
724
|
if (textContent !== undefined) {
|
|
648
|
-
element.textContent = textContent;
|
|
725
|
+
element.textContent = textContent.toString();
|
|
649
726
|
}
|
|
650
727
|
unit._.elements.push(element);
|
|
651
728
|
return element;
|
|
652
729
|
}
|
|
653
730
|
else {
|
|
654
|
-
throw new Error(`
|
|
731
|
+
throw new Error(`xnew.nest: invalid html string [${tag}]`);
|
|
655
732
|
}
|
|
656
733
|
}
|
|
657
734
|
static extend(unit, component, props) {
|
|
658
735
|
var _a;
|
|
659
|
-
if (unit._.state !== 'invoked') {
|
|
660
|
-
throw new Error('This function can not be called after initialized.');
|
|
661
|
-
}
|
|
662
736
|
unit._.components.push(component);
|
|
663
737
|
Unit.component2units.add(component, unit);
|
|
664
738
|
const backupComponent = unit._.currentComponent;
|
|
@@ -671,12 +745,13 @@ class Unit {
|
|
|
671
745
|
}
|
|
672
746
|
const descriptor = Object.getOwnPropertyDescriptor(defines, key);
|
|
673
747
|
const wrapper = { configurable: true, enumerable: true };
|
|
748
|
+
const snapshot = Unit.snapshot(unit);
|
|
674
749
|
if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.get)
|
|
675
|
-
wrapper.get = Unit.
|
|
750
|
+
wrapper.get = (...args) => Unit.scope(snapshot, descriptor.get, ...args);
|
|
676
751
|
if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.set)
|
|
677
|
-
wrapper.set = Unit.
|
|
752
|
+
wrapper.set = (...args) => Unit.scope(snapshot, descriptor.set, ...args);
|
|
678
753
|
if (typeof (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) === 'function') {
|
|
679
|
-
wrapper.value = Unit.
|
|
754
|
+
wrapper.value = (...args) => Unit.scope(snapshot, descriptor.value, ...args);
|
|
680
755
|
}
|
|
681
756
|
else if ((descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) !== undefined) {
|
|
682
757
|
wrapper.writable = true;
|
|
@@ -729,10 +804,6 @@ class Unit {
|
|
|
729
804
|
});
|
|
730
805
|
Unit.rootUnit.on('finalize', () => ticker.clear());
|
|
731
806
|
}
|
|
732
|
-
static wrap(unit, listener) {
|
|
733
|
-
const snapshot = Unit.snapshot(unit);
|
|
734
|
-
return (...args) => Unit.scope(snapshot, listener, ...args);
|
|
735
|
-
}
|
|
736
807
|
static scope(snapshot, func, ...args) {
|
|
737
808
|
if (snapshot.unit._.state === 'finalized') {
|
|
738
809
|
return;
|
|
@@ -783,15 +854,16 @@ class Unit {
|
|
|
783
854
|
types.forEach((type) => Unit.off(this, type, listener));
|
|
784
855
|
}
|
|
785
856
|
static on(unit, type, listener, options) {
|
|
857
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
858
|
+
const execute = (...args) => Unit.scope(snapshot, listener, ...args);
|
|
786
859
|
if (SYSTEM_EVENTS.includes(type)) {
|
|
787
|
-
unit._.systems[type].push({ listener, execute
|
|
860
|
+
unit._.systems[type].push({ listener, execute });
|
|
788
861
|
}
|
|
789
862
|
if (unit._.listeners.has(type, listener) === false) {
|
|
790
|
-
const execute = Unit.wrap(Unit.currentUnit, listener);
|
|
791
863
|
unit._.listeners.set(type, listener, { element: unit.element, component: unit._.currentComponent, execute });
|
|
792
864
|
Unit.type2units.add(type, unit);
|
|
793
865
|
if (/^[A-Za-z]/.test(type)) {
|
|
794
|
-
unit._.
|
|
866
|
+
unit._.eventor.add(unit.element, type, execute, options);
|
|
795
867
|
}
|
|
796
868
|
}
|
|
797
869
|
}
|
|
@@ -805,7 +877,7 @@ class Unit {
|
|
|
805
877
|
return;
|
|
806
878
|
unit._.listeners.delete(type, listener);
|
|
807
879
|
if (/^[A-Za-z]/.test(type)) {
|
|
808
|
-
unit._.
|
|
880
|
+
unit._.eventor.remove(type, item.execute);
|
|
809
881
|
}
|
|
810
882
|
});
|
|
811
883
|
if (unit._.listeners.has(type) === false) {
|
|
@@ -818,7 +890,7 @@ class Unit {
|
|
|
818
890
|
if (type[0] === '+') {
|
|
819
891
|
(_a = Unit.type2units.get(type)) === null || _a === void 0 ? void 0 : _a.forEach((unit) => {
|
|
820
892
|
var _a;
|
|
821
|
-
const find = [unit, ...unit._.ancestors].find(u => u._.
|
|
893
|
+
const find = [unit, ...unit._.ancestors].find(u => u._.protected === true);
|
|
822
894
|
if (find === undefined || current._.ancestors.includes(find) === true || current === find) {
|
|
823
895
|
(_a = unit._.listeners.get(type)) === null || _a === void 0 ? void 0 : _a.forEach((item) => item.execute(...args));
|
|
824
896
|
}
|
|
@@ -835,92 +907,10 @@ Unit.component2units = new MapSet();
|
|
|
835
907
|
// event
|
|
836
908
|
//----------------------------------------------------------------------------------------------------
|
|
837
909
|
Unit.type2units = new MapSet();
|
|
838
|
-
//----------------------------------------------------------------------------------------------------
|
|
839
|
-
// unit promise
|
|
840
|
-
//----------------------------------------------------------------------------------------------------
|
|
841
|
-
class UnitPromise {
|
|
842
|
-
constructor(promise, component) {
|
|
843
|
-
this.promise = promise;
|
|
844
|
-
this.component = component;
|
|
845
|
-
}
|
|
846
|
-
then(callback) {
|
|
847
|
-
this.promise = this.promise.then(Unit.wrap(Unit.currentUnit, callback));
|
|
848
|
-
return this;
|
|
849
|
-
}
|
|
850
|
-
catch(callback) {
|
|
851
|
-
this.promise = this.promise.catch(Unit.wrap(Unit.currentUnit, callback));
|
|
852
|
-
return this;
|
|
853
|
-
}
|
|
854
|
-
finally(callback) {
|
|
855
|
-
this.promise = this.promise.finally(Unit.wrap(Unit.currentUnit, callback));
|
|
856
|
-
return this;
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
//----------------------------------------------------------------------------------------------------
|
|
860
|
-
// unit timer
|
|
861
|
-
//----------------------------------------------------------------------------------------------------
|
|
862
|
-
class UnitTimer {
|
|
863
|
-
constructor(options) {
|
|
864
|
-
this.stack = [];
|
|
865
|
-
this.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, Object.assign({ snapshot: Unit.snapshot(Unit.currentUnit) }, options));
|
|
866
|
-
}
|
|
867
|
-
clear() {
|
|
868
|
-
this.stack = [];
|
|
869
|
-
this.unit.finalize();
|
|
870
|
-
}
|
|
871
|
-
timeout(timeout, duration = 0) {
|
|
872
|
-
UnitTimer.execute(this, { timeout, duration, iterations: 1 });
|
|
873
|
-
return this;
|
|
874
|
-
}
|
|
875
|
-
iteration(timeout, duration = 0, iterations = -1) {
|
|
876
|
-
UnitTimer.execute(this, { timeout, duration, iterations });
|
|
877
|
-
return this;
|
|
878
|
-
}
|
|
879
|
-
transition(transition, duration = 0, easing) {
|
|
880
|
-
UnitTimer.execute(this, { transition, duration, iterations: 1, easing });
|
|
881
|
-
return this;
|
|
882
|
-
}
|
|
883
|
-
static execute(timer, options) {
|
|
884
|
-
if (timer.unit._.state === 'finalized') {
|
|
885
|
-
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, Object.assign({ snapshot: Unit.snapshot(Unit.currentUnit) }, options));
|
|
886
|
-
}
|
|
887
|
-
else if (timer.stack.length === 0) {
|
|
888
|
-
timer.stack.push(Object.assign({ snapshot: Unit.snapshot(Unit.currentUnit) }, options));
|
|
889
|
-
timer.unit.on('finalize', () => { UnitTimer.next(timer); });
|
|
890
|
-
}
|
|
891
|
-
else {
|
|
892
|
-
timer.stack.push(Object.assign({ snapshot: Unit.snapshot(Unit.currentUnit) }, options));
|
|
893
|
-
}
|
|
894
|
-
}
|
|
895
|
-
static next(timer) {
|
|
896
|
-
if (timer.stack.length > 0) {
|
|
897
|
-
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.stack.shift());
|
|
898
|
-
timer.unit.on('finalize', () => { UnitTimer.next(timer); });
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
static Component(unit, options) {
|
|
902
|
-
let counter = 0;
|
|
903
|
-
const timer = new Timer({
|
|
904
|
-
transition: (p) => {
|
|
905
|
-
if (options.transition)
|
|
906
|
-
Unit.scope(options.snapshot, options.transition, p);
|
|
907
|
-
},
|
|
908
|
-
timeout: () => {
|
|
909
|
-
if (options.transition)
|
|
910
|
-
Unit.scope(options.snapshot, options.transition, 1.0);
|
|
911
|
-
if (options.timeout)
|
|
912
|
-
Unit.scope(options.snapshot, options.timeout);
|
|
913
|
-
if (options.iterations && counter >= options.iterations - 1) {
|
|
914
|
-
unit.finalize();
|
|
915
|
-
}
|
|
916
|
-
counter++;
|
|
917
|
-
}, duration: options.duration, iterations: options.iterations, easing: options.easing
|
|
918
|
-
});
|
|
919
|
-
unit.on('finalize', () => timer.clear());
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
910
|
|
|
923
|
-
function
|
|
911
|
+
const xnew$1 = Object.assign(function (...args) {
|
|
912
|
+
if (Unit.rootUnit === undefined)
|
|
913
|
+
Unit.reset();
|
|
924
914
|
let target;
|
|
925
915
|
if (args[0] instanceof HTMLElement || args[0] instanceof SVGElement) {
|
|
926
916
|
target = args.shift(); // an existing html element
|
|
@@ -933,29 +923,26 @@ function parseArguments(...args) {
|
|
|
933
923
|
}
|
|
934
924
|
const component = args.shift();
|
|
935
925
|
const props = args.shift();
|
|
936
|
-
return
|
|
937
|
-
}
|
|
938
|
-
const xnew$1 = Object.assign(function (...args) {
|
|
939
|
-
if (Unit.rootUnit === undefined)
|
|
940
|
-
Unit.reset();
|
|
941
|
-
const { target, component, props } = parseArguments(...args);
|
|
942
|
-
return new Unit(Unit.currentUnit, target, component, props, { protect: false });
|
|
926
|
+
return new Unit(Unit.currentUnit, target, component, props);
|
|
943
927
|
}, {
|
|
944
928
|
/**
|
|
945
929
|
* Creates a nested HTML/SVG element within the current component
|
|
946
|
-
* @param
|
|
930
|
+
* @param tag - HTML or SVG tag name (e.g., '<div>', '<span>', '<svg>')
|
|
947
931
|
* @returns The created HTML/SVG element
|
|
948
932
|
* @throws Error if called after component initialization
|
|
949
933
|
* @example
|
|
950
934
|
* const div = xnew.nest('<div>')
|
|
951
935
|
* div.textContent = 'Hello'
|
|
952
936
|
*/
|
|
953
|
-
nest(
|
|
937
|
+
nest(tag, textContent) {
|
|
954
938
|
try {
|
|
955
|
-
|
|
939
|
+
if (Unit.currentUnit._.state !== 'invoked') {
|
|
940
|
+
throw new Error('xnew.nest can not be called after initialized.');
|
|
941
|
+
}
|
|
942
|
+
return Unit.nest(Unit.currentUnit, tag, textContent);
|
|
956
943
|
}
|
|
957
944
|
catch (error) {
|
|
958
|
-
console.error('xnew.nest(
|
|
945
|
+
console.error('xnew.nest(tag: string): ', error);
|
|
959
946
|
throw error;
|
|
960
947
|
}
|
|
961
948
|
},
|
|
@@ -970,6 +957,9 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
970
957
|
*/
|
|
971
958
|
extend(component, props) {
|
|
972
959
|
try {
|
|
960
|
+
if (Unit.currentUnit._.state !== 'invoked') {
|
|
961
|
+
throw new Error('xnew.extend can not be called after initialized.');
|
|
962
|
+
}
|
|
973
963
|
return Unit.extend(Unit.currentUnit, component, props);
|
|
974
964
|
}
|
|
975
965
|
catch (error) {
|
|
@@ -1104,6 +1094,15 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
1104
1094
|
throw error;
|
|
1105
1095
|
}
|
|
1106
1096
|
},
|
|
1097
|
+
/**
|
|
1098
|
+
* Emits a custom event to components
|
|
1099
|
+
* @param type - Event type to emit (prefix with '+' for global events, '-' for local events)
|
|
1100
|
+
* @param args - Additional arguments to pass to event listeners
|
|
1101
|
+
* @returns void
|
|
1102
|
+
* @example
|
|
1103
|
+
* xnew.emit('+globalevent', { data: 123 }); // Global event
|
|
1104
|
+
* xnew.emit('-localevent', { data: 123 }); // Local event
|
|
1105
|
+
*/
|
|
1107
1106
|
emit(type, ...args) {
|
|
1108
1107
|
try {
|
|
1109
1108
|
return Unit.emit(type, ...args);
|
|
@@ -1153,100 +1152,114 @@ const xnew$1 = Object.assign(function (...args) {
|
|
|
1153
1152
|
transition(transition, duration = 0, easing = 'linear') {
|
|
1154
1153
|
return new UnitTimer({ transition, duration, easing, iterations: 1 });
|
|
1155
1154
|
},
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1155
|
+
/**
|
|
1156
|
+
* Call this method within a component function to enable protection.
|
|
1157
|
+
* Protected components will not respond to global events emitted via xnew.emit,
|
|
1158
|
+
* and will be excluded from xnew.find searches.
|
|
1159
|
+
* @example
|
|
1160
|
+
* function MyComponent(unit) {
|
|
1161
|
+
* xnew.protect();
|
|
1162
|
+
* // Component logic here
|
|
1163
|
+
* }
|
|
1164
|
+
*/
|
|
1165
|
+
protect() {
|
|
1166
|
+
Unit.currentUnit._.protected = true;
|
|
1161
1167
|
},
|
|
1162
1168
|
});
|
|
1163
1169
|
|
|
1164
1170
|
function Accordion(unit, { open = false, duration = 200, easing = 'ease' } = {}) {
|
|
1165
1171
|
xnew$1.context('xnew.accordion', unit);
|
|
1166
|
-
|
|
1167
|
-
|
|
1172
|
+
let state = open ? 1.0 : 0.0;
|
|
1173
|
+
let sign = open ? +1 : -1;
|
|
1174
|
+
let timer = xnew$1.timeout(() => xnew$1.emit('-transition', { state }));
|
|
1168
1175
|
return {
|
|
1169
|
-
state: open ? 1.0 : 0.0,
|
|
1170
1176
|
toggle() {
|
|
1171
|
-
|
|
1172
|
-
unit.close();
|
|
1173
|
-
}
|
|
1174
|
-
else if (unit.state === 0.0) {
|
|
1175
|
-
unit.open();
|
|
1176
|
-
}
|
|
1177
|
+
sign > 0 ? unit.close() : unit.open();
|
|
1177
1178
|
},
|
|
1178
1179
|
open() {
|
|
1179
|
-
if (
|
|
1180
|
-
|
|
1181
|
-
}
|
|
1180
|
+
if (sign < 0)
|
|
1181
|
+
transition();
|
|
1182
1182
|
},
|
|
1183
1183
|
close() {
|
|
1184
|
-
if (
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1184
|
+
if (sign > 0)
|
|
1185
|
+
transition();
|
|
1186
|
+
},
|
|
1187
|
+
};
|
|
1188
|
+
function transition() {
|
|
1189
|
+
sign *= -1;
|
|
1190
|
+
const d = sign > 0 ? 1 - state : state;
|
|
1191
|
+
timer.clear();
|
|
1192
|
+
timer = xnew$1.transition((x) => {
|
|
1193
|
+
const y = x < 1.0 ? (1 - x) * d : 0.0;
|
|
1194
|
+
state = sign > 0 ? 1.0 - y : y;
|
|
1195
|
+
xnew$1.emit('-transition', { state });
|
|
1196
|
+
}, duration * d, easing);
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
function Modal(unit, { duration = 200, easing = 'ease' } = {}) {
|
|
1200
|
+
xnew$1.context('xnew.modal', unit);
|
|
1201
|
+
let state = 0.0;
|
|
1202
|
+
let timer = xnew$1.transition((x) => {
|
|
1203
|
+
state = x;
|
|
1204
|
+
xnew$1.emit('-transition', { state });
|
|
1205
|
+
}, duration, easing);
|
|
1206
|
+
return {
|
|
1207
|
+
close() {
|
|
1208
|
+
const d = state;
|
|
1209
|
+
timer.clear();
|
|
1210
|
+
timer = xnew$1.transition((x) => {
|
|
1211
|
+
state = x < 1.0 ? (1 - x) * d : 0.0;
|
|
1212
|
+
xnew$1.emit('-transition', { state });
|
|
1213
|
+
}, duration * d, easing)
|
|
1214
|
+
.timeout(() => unit.finalize());
|
|
1215
|
+
},
|
|
1188
1216
|
};
|
|
1189
1217
|
}
|
|
1190
1218
|
|
|
1191
|
-
function Screen(unit, { width
|
|
1192
|
-
const size = { width, height };
|
|
1193
|
-
const
|
|
1194
|
-
|
|
1219
|
+
function Screen(unit, { width, height, fit = 'contain' } = {}) {
|
|
1220
|
+
const size = { width: width !== null && width !== void 0 ? width : 800, height: height !== null && height !== void 0 ? height : 600 };
|
|
1221
|
+
const outer = xnew$1.nest('<div style="position: relative; width: 100%; height: 100%; overflow: hidden;">');
|
|
1222
|
+
xnew$1().on('resize', resize);
|
|
1195
1223
|
const absolute = xnew$1.nest('<div style="position: absolute; margin: auto; container-type: size; overflow: hidden;">');
|
|
1196
|
-
const canvas = xnew$1(`<canvas width="${width}" height="${height}" style="width: 100%; height: 100%; vertical-align: bottom; user-select: none; user-drag: none; pointer-events: auto;">`);
|
|
1224
|
+
const canvas = xnew$1(`<canvas width="${size.width}" height="${size.height}" style="width: 100%; height: 100%; vertical-align: bottom; user-select: none; user-drag: none; pointer-events: auto;">`);
|
|
1197
1225
|
resize();
|
|
1198
1226
|
function resize() {
|
|
1199
|
-
const aspect = size.width / size.height;
|
|
1200
1227
|
const style = { width: '100%', height: '100%', top: 0, left: 0, bottom: 0, right: 0 };
|
|
1201
1228
|
if (fit === 'contain') {
|
|
1202
|
-
|
|
1203
|
-
|
|
1229
|
+
const aspect = size.width / size.height;
|
|
1230
|
+
if (outer.clientWidth < outer.clientHeight * aspect) {
|
|
1231
|
+
style.height = Math.floor(outer.clientWidth / aspect) + 'px';
|
|
1204
1232
|
}
|
|
1205
1233
|
else {
|
|
1206
|
-
style.width = Math.floor(
|
|
1234
|
+
style.width = Math.floor(outer.clientHeight * aspect) + 'px';
|
|
1207
1235
|
}
|
|
1208
1236
|
}
|
|
1209
1237
|
else if (fit === 'cover') {
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
style.
|
|
1238
|
+
const aspect = size.width / size.height;
|
|
1239
|
+
if (outer.clientWidth < outer.clientHeight * aspect) {
|
|
1240
|
+
style.width = Math.floor(outer.clientHeight * aspect) + 'px';
|
|
1241
|
+
style.left = Math.floor((outer.clientWidth - outer.clientHeight * aspect) / 2) + 'px';
|
|
1213
1242
|
style.right = 'auto';
|
|
1214
1243
|
}
|
|
1215
1244
|
else {
|
|
1216
|
-
style.height = Math.floor(
|
|
1217
|
-
style.top = Math.floor((
|
|
1245
|
+
style.height = Math.floor(outer.clientWidth / aspect) + 'px';
|
|
1246
|
+
style.top = Math.floor((outer.clientHeight - outer.clientWidth / aspect) / 2) + 'px';
|
|
1218
1247
|
style.bottom = 'auto';
|
|
1219
1248
|
}
|
|
1220
1249
|
}
|
|
1221
|
-
else
|
|
1250
|
+
else if (fit === 'resize') {
|
|
1251
|
+
size.width = outer.clientWidth > 0 ? outer.clientWidth : size.width;
|
|
1252
|
+
size.height = outer.clientHeight > 0 ? outer.clientHeight : size.height;
|
|
1253
|
+
console.log(size);
|
|
1254
|
+
canvas.element.setAttribute('width', size.width + 'px');
|
|
1255
|
+
canvas.element.setAttribute('height', size.height + 'px');
|
|
1256
|
+
}
|
|
1222
1257
|
Object.assign(absolute.style, style);
|
|
1223
1258
|
}
|
|
1224
1259
|
return {
|
|
1225
1260
|
get canvas() {
|
|
1226
1261
|
return canvas.element;
|
|
1227
1262
|
},
|
|
1228
|
-
resize(width, height) {
|
|
1229
|
-
size.width = width;
|
|
1230
|
-
size.height = height;
|
|
1231
|
-
canvas.element.setAttribute('width', width + 'px');
|
|
1232
|
-
canvas.element.setAttribute('height', height + 'px');
|
|
1233
|
-
resize();
|
|
1234
|
-
},
|
|
1235
|
-
};
|
|
1236
|
-
}
|
|
1237
|
-
|
|
1238
|
-
function Modal(unit, { duration = 200, easing = 'ease' } = {}) {
|
|
1239
|
-
xnew$1.context('xnew.modalframe', unit);
|
|
1240
|
-
xnew$1.nest('<div style="position: fixed; inset: 0; z-index: 1000;">');
|
|
1241
|
-
unit.on('click', ({ event }) => unit.close());
|
|
1242
|
-
unit.on('-transition', ({ state }) => unit.state = state);
|
|
1243
|
-
xnew$1.transition((x) => xnew$1.emit('-transition', { state: x }), duration, easing);
|
|
1244
|
-
return {
|
|
1245
|
-
state: 0.0,
|
|
1246
|
-
close() {
|
|
1247
|
-
xnew$1.transition((x) => xnew$1.emit('-transition', { state: 1.0 - x }), duration, easing)
|
|
1248
|
-
.timeout(() => unit.finalize());
|
|
1249
|
-
}
|
|
1250
1263
|
};
|
|
1251
1264
|
}
|
|
1252
1265
|
|
|
@@ -1301,7 +1314,7 @@ function AnalogStick(unit, { stroke = 'currentColor', strokeOpacity = 0.8, strok
|
|
|
1301
1314
|
xnew$1.emit('-up', { type: '-up', vector });
|
|
1302
1315
|
});
|
|
1303
1316
|
}
|
|
1304
|
-
function
|
|
1317
|
+
function DPad(unit, { diagonal = true, stroke = 'currentColor', strokeOpacity = 0.8, strokeWidth = 2, strokeLinejoin = 'round', fill = '#FFF', fillOpacity = 0.8 } = {}) {
|
|
1305
1318
|
const outer = xnew$1.nest(`<div style="position: relative; width: 100%; height: 100%;">`);
|
|
1306
1319
|
let newsize = Math.min(outer.clientWidth, outer.clientHeight);
|
|
1307
1320
|
const inner = xnew$1.nest(`<div style="position: absolute; width: ${newsize}px; height: ${newsize}px; margin: auto; inset: 0; cursor: pointer; pointer-select: none; pointer-events: auto; overflow: hidden;">`);
|
|
@@ -1597,7 +1610,7 @@ const basics = {
|
|
|
1597
1610
|
Modal,
|
|
1598
1611
|
Accordion,
|
|
1599
1612
|
AnalogStick,
|
|
1600
|
-
|
|
1613
|
+
DPad,
|
|
1601
1614
|
};
|
|
1602
1615
|
const audio = {
|
|
1603
1616
|
load(path) {
|