@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.js
CHANGED
|
@@ -143,11 +143,11 @@
|
|
|
143
143
|
else if (this.options.easing === 'ease-in') {
|
|
144
144
|
p = Math.pow((1.0 - Math.pow((1.0 - p), 0.5)), 2.0);
|
|
145
145
|
}
|
|
146
|
-
else if (this.options.easing === 'ease') {
|
|
147
|
-
p = (1.0 - Math.cos(p * Math.PI)) / 2.0;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
p = (
|
|
146
|
+
else if (this.options.easing === 'ease' || this.options.easing === 'ease-in-out') {
|
|
147
|
+
// p = (1.0 - Math.cos(p * Math.PI)) / 2.0;
|
|
148
|
+
const bias = (this.options.easing === 'ease') ? 0.7 : 1.0;
|
|
149
|
+
const s = Math.pow(p, bias);
|
|
150
|
+
p = s * s * (3 - 2 * s);
|
|
151
151
|
}
|
|
152
152
|
(_b = (_a = this.options).transition) === null || _b === void 0 ? void 0 : _b.call(_a, p);
|
|
153
153
|
});
|
|
@@ -205,7 +205,7 @@
|
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
-
class
|
|
208
|
+
class Eventor {
|
|
209
209
|
constructor() {
|
|
210
210
|
this.map = new MapMap();
|
|
211
211
|
}
|
|
@@ -525,12 +525,93 @@
|
|
|
525
525
|
// utils
|
|
526
526
|
//----------------------------------------------------------------------------------------------------
|
|
527
527
|
const SYSTEM_EVENTS = ['start', 'update', 'render', 'stop', 'finalize'];
|
|
528
|
+
class UnitPromise {
|
|
529
|
+
constructor(promise, component) {
|
|
530
|
+
this.promise = promise;
|
|
531
|
+
this.component = component;
|
|
532
|
+
}
|
|
533
|
+
then(callback) {
|
|
534
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
535
|
+
this.promise = this.promise.then((...args) => Unit.scope(snapshot, callback, ...args));
|
|
536
|
+
return this;
|
|
537
|
+
}
|
|
538
|
+
catch(callback) {
|
|
539
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
540
|
+
this.promise = this.promise.catch((...args) => Unit.scope(snapshot, callback, ...args));
|
|
541
|
+
return this;
|
|
542
|
+
}
|
|
543
|
+
finally(callback) {
|
|
544
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
545
|
+
this.promise = this.promise.finally(() => Unit.scope(snapshot, callback));
|
|
546
|
+
return this;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
class UnitTimer {
|
|
550
|
+
constructor(options) {
|
|
551
|
+
this.stack = [];
|
|
552
|
+
this.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, { options, snapshot: Unit.snapshot(Unit.currentUnit) });
|
|
553
|
+
}
|
|
554
|
+
clear() {
|
|
555
|
+
this.stack = [];
|
|
556
|
+
this.unit.finalize();
|
|
557
|
+
}
|
|
558
|
+
timeout(timeout, duration = 0) {
|
|
559
|
+
UnitTimer.execute(this, { timeout, duration, iterations: 1 });
|
|
560
|
+
return this;
|
|
561
|
+
}
|
|
562
|
+
iteration(timeout, duration = 0, iterations = -1) {
|
|
563
|
+
UnitTimer.execute(this, { timeout, duration, iterations });
|
|
564
|
+
return this;
|
|
565
|
+
}
|
|
566
|
+
transition(transition, duration = 0, easing) {
|
|
567
|
+
UnitTimer.execute(this, { transition, duration, iterations: 1, easing });
|
|
568
|
+
return this;
|
|
569
|
+
}
|
|
570
|
+
static execute(timer, options) {
|
|
571
|
+
if (timer.unit._.state === 'finalized') {
|
|
572
|
+
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, { options, snapshot: Unit.snapshot(Unit.currentUnit) });
|
|
573
|
+
}
|
|
574
|
+
else if (timer.stack.length === 0) {
|
|
575
|
+
timer.stack.push({ options, snapshot: Unit.snapshot(Unit.currentUnit) });
|
|
576
|
+
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
timer.stack.push({ options, snapshot: Unit.snapshot(Unit.currentUnit) });
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
static next(timer) {
|
|
583
|
+
if (timer.stack.length > 0) {
|
|
584
|
+
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.stack.shift());
|
|
585
|
+
timer.unit.on('finalize', () => UnitTimer.next(timer));
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
static Component(unit, { options, snapshot }) {
|
|
589
|
+
let counter = 0;
|
|
590
|
+
const timer = new Timer({
|
|
591
|
+
transition: (p) => {
|
|
592
|
+
if (options.transition)
|
|
593
|
+
Unit.scope(snapshot, options.transition, p);
|
|
594
|
+
},
|
|
595
|
+
timeout: () => {
|
|
596
|
+
if (options.transition)
|
|
597
|
+
Unit.scope(snapshot, options.transition, 1.0);
|
|
598
|
+
if (options.timeout)
|
|
599
|
+
Unit.scope(snapshot, options.timeout);
|
|
600
|
+
if (options.iterations && counter >= options.iterations - 1) {
|
|
601
|
+
unit.finalize();
|
|
602
|
+
}
|
|
603
|
+
counter++;
|
|
604
|
+
}, duration: options.duration, iterations: options.iterations, easing: options.easing
|
|
605
|
+
});
|
|
606
|
+
unit.on('finalize', () => timer.clear());
|
|
607
|
+
}
|
|
608
|
+
}
|
|
528
609
|
//----------------------------------------------------------------------------------------------------
|
|
529
610
|
// unit
|
|
530
611
|
//----------------------------------------------------------------------------------------------------
|
|
531
612
|
class Unit {
|
|
532
|
-
constructor(parent, target, component, props
|
|
533
|
-
var _a
|
|
613
|
+
constructor(parent, target, component, props) {
|
|
614
|
+
var _a;
|
|
534
615
|
let baseElement;
|
|
535
616
|
if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
536
617
|
baseElement = target;
|
|
@@ -545,15 +626,14 @@
|
|
|
545
626
|
if (typeof component === 'function') {
|
|
546
627
|
baseComponent = component;
|
|
547
628
|
}
|
|
548
|
-
else if (
|
|
549
|
-
baseComponent = (unit) => { unit.element.textContent = component; };
|
|
629
|
+
else if (component !== undefined) {
|
|
630
|
+
baseComponent = (unit) => { unit.element.textContent = component.toString(); };
|
|
550
631
|
}
|
|
551
632
|
else {
|
|
552
633
|
baseComponent = (unit) => { };
|
|
553
634
|
}
|
|
554
635
|
const baseContext = (_a = parent === null || parent === void 0 ? void 0 : parent._.currentContext) !== null && _a !== void 0 ? _a : { stack: null };
|
|
555
|
-
|
|
556
|
-
this._ = { parent, target, baseElement, baseContext, baseComponent, props, config: { protect } };
|
|
636
|
+
this._ = { parent, target, baseElement, baseContext, baseComponent, props };
|
|
557
637
|
parent === null || parent === void 0 ? void 0 : parent._.children.push(this);
|
|
558
638
|
Unit.initialize(this, null);
|
|
559
639
|
}
|
|
@@ -582,7 +662,6 @@
|
|
|
582
662
|
Unit.initialize(this, anchor);
|
|
583
663
|
}
|
|
584
664
|
static initialize(unit, anchor) {
|
|
585
|
-
var _a, _b;
|
|
586
665
|
const backup = Unit.currentUnit;
|
|
587
666
|
Unit.currentUnit = unit;
|
|
588
667
|
unit._ = Object.assign(unit._, {
|
|
@@ -592,7 +671,8 @@
|
|
|
592
671
|
anchor,
|
|
593
672
|
state: 'invoked',
|
|
594
673
|
tostart: true,
|
|
595
|
-
|
|
674
|
+
protected: false,
|
|
675
|
+
ancestors: unit._.parent ? [unit._.parent, ...unit._.parent._.ancestors] : [],
|
|
596
676
|
children: [],
|
|
597
677
|
elements: [],
|
|
598
678
|
promises: [],
|
|
@@ -600,7 +680,7 @@
|
|
|
600
680
|
listeners: new MapMap(),
|
|
601
681
|
defines: {},
|
|
602
682
|
systems: { start: [], update: [], render: [], stop: [], finalize: [] },
|
|
603
|
-
|
|
683
|
+
eventor: new Eventor(),
|
|
604
684
|
});
|
|
605
685
|
// nest html element
|
|
606
686
|
if (typeof unit._.target === 'string') {
|
|
@@ -633,11 +713,8 @@
|
|
|
633
713
|
unit._.state = 'finalized';
|
|
634
714
|
}
|
|
635
715
|
}
|
|
636
|
-
static nest(unit,
|
|
637
|
-
|
|
638
|
-
throw new Error('This function can not be called after initialized.');
|
|
639
|
-
}
|
|
640
|
-
const match = htmlString.match(/<((\w+)[^>]*?)\/?>/);
|
|
716
|
+
static nest(unit, tag, textContent) {
|
|
717
|
+
const match = tag.match(/<((\w+)[^>]*?)\/?>/);
|
|
641
718
|
if (match !== null) {
|
|
642
719
|
let element;
|
|
643
720
|
if (unit._.anchor !== null) {
|
|
@@ -651,20 +728,17 @@
|
|
|
651
728
|
}
|
|
652
729
|
unit._.currentElement = element;
|
|
653
730
|
if (textContent !== undefined) {
|
|
654
|
-
element.textContent = textContent;
|
|
731
|
+
element.textContent = textContent.toString();
|
|
655
732
|
}
|
|
656
733
|
unit._.elements.push(element);
|
|
657
734
|
return element;
|
|
658
735
|
}
|
|
659
736
|
else {
|
|
660
|
-
throw new Error(`
|
|
737
|
+
throw new Error(`xnew.nest: invalid html string [${tag}]`);
|
|
661
738
|
}
|
|
662
739
|
}
|
|
663
740
|
static extend(unit, component, props) {
|
|
664
741
|
var _a;
|
|
665
|
-
if (unit._.state !== 'invoked') {
|
|
666
|
-
throw new Error('This function can not be called after initialized.');
|
|
667
|
-
}
|
|
668
742
|
unit._.components.push(component);
|
|
669
743
|
Unit.component2units.add(component, unit);
|
|
670
744
|
const backupComponent = unit._.currentComponent;
|
|
@@ -677,12 +751,13 @@
|
|
|
677
751
|
}
|
|
678
752
|
const descriptor = Object.getOwnPropertyDescriptor(defines, key);
|
|
679
753
|
const wrapper = { configurable: true, enumerable: true };
|
|
754
|
+
const snapshot = Unit.snapshot(unit);
|
|
680
755
|
if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.get)
|
|
681
|
-
wrapper.get = Unit.
|
|
756
|
+
wrapper.get = (...args) => Unit.scope(snapshot, descriptor.get, ...args);
|
|
682
757
|
if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.set)
|
|
683
|
-
wrapper.set = Unit.
|
|
758
|
+
wrapper.set = (...args) => Unit.scope(snapshot, descriptor.set, ...args);
|
|
684
759
|
if (typeof (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) === 'function') {
|
|
685
|
-
wrapper.value = Unit.
|
|
760
|
+
wrapper.value = (...args) => Unit.scope(snapshot, descriptor.value, ...args);
|
|
686
761
|
}
|
|
687
762
|
else if ((descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) !== undefined) {
|
|
688
763
|
wrapper.writable = true;
|
|
@@ -735,10 +810,6 @@
|
|
|
735
810
|
});
|
|
736
811
|
Unit.rootUnit.on('finalize', () => ticker.clear());
|
|
737
812
|
}
|
|
738
|
-
static wrap(unit, listener) {
|
|
739
|
-
const snapshot = Unit.snapshot(unit);
|
|
740
|
-
return (...args) => Unit.scope(snapshot, listener, ...args);
|
|
741
|
-
}
|
|
742
813
|
static scope(snapshot, func, ...args) {
|
|
743
814
|
if (snapshot.unit._.state === 'finalized') {
|
|
744
815
|
return;
|
|
@@ -789,15 +860,16 @@
|
|
|
789
860
|
types.forEach((type) => Unit.off(this, type, listener));
|
|
790
861
|
}
|
|
791
862
|
static on(unit, type, listener, options) {
|
|
863
|
+
const snapshot = Unit.snapshot(Unit.currentUnit);
|
|
864
|
+
const execute = (...args) => Unit.scope(snapshot, listener, ...args);
|
|
792
865
|
if (SYSTEM_EVENTS.includes(type)) {
|
|
793
|
-
unit._.systems[type].push({ listener, execute
|
|
866
|
+
unit._.systems[type].push({ listener, execute });
|
|
794
867
|
}
|
|
795
868
|
if (unit._.listeners.has(type, listener) === false) {
|
|
796
|
-
const execute = Unit.wrap(Unit.currentUnit, listener);
|
|
797
869
|
unit._.listeners.set(type, listener, { element: unit.element, component: unit._.currentComponent, execute });
|
|
798
870
|
Unit.type2units.add(type, unit);
|
|
799
871
|
if (/^[A-Za-z]/.test(type)) {
|
|
800
|
-
unit._.
|
|
872
|
+
unit._.eventor.add(unit.element, type, execute, options);
|
|
801
873
|
}
|
|
802
874
|
}
|
|
803
875
|
}
|
|
@@ -811,7 +883,7 @@
|
|
|
811
883
|
return;
|
|
812
884
|
unit._.listeners.delete(type, listener);
|
|
813
885
|
if (/^[A-Za-z]/.test(type)) {
|
|
814
|
-
unit._.
|
|
886
|
+
unit._.eventor.remove(type, item.execute);
|
|
815
887
|
}
|
|
816
888
|
});
|
|
817
889
|
if (unit._.listeners.has(type) === false) {
|
|
@@ -824,7 +896,7 @@
|
|
|
824
896
|
if (type[0] === '+') {
|
|
825
897
|
(_a = Unit.type2units.get(type)) === null || _a === void 0 ? void 0 : _a.forEach((unit) => {
|
|
826
898
|
var _a;
|
|
827
|
-
const find = [unit, ...unit._.ancestors].find(u => u._.
|
|
899
|
+
const find = [unit, ...unit._.ancestors].find(u => u._.protected === true);
|
|
828
900
|
if (find === undefined || current._.ancestors.includes(find) === true || current === find) {
|
|
829
901
|
(_a = unit._.listeners.get(type)) === null || _a === void 0 ? void 0 : _a.forEach((item) => item.execute(...args));
|
|
830
902
|
}
|
|
@@ -841,92 +913,10 @@
|
|
|
841
913
|
// event
|
|
842
914
|
//----------------------------------------------------------------------------------------------------
|
|
843
915
|
Unit.type2units = new MapSet();
|
|
844
|
-
//----------------------------------------------------------------------------------------------------
|
|
845
|
-
// unit promise
|
|
846
|
-
//----------------------------------------------------------------------------------------------------
|
|
847
|
-
class UnitPromise {
|
|
848
|
-
constructor(promise, component) {
|
|
849
|
-
this.promise = promise;
|
|
850
|
-
this.component = component;
|
|
851
|
-
}
|
|
852
|
-
then(callback) {
|
|
853
|
-
this.promise = this.promise.then(Unit.wrap(Unit.currentUnit, callback));
|
|
854
|
-
return this;
|
|
855
|
-
}
|
|
856
|
-
catch(callback) {
|
|
857
|
-
this.promise = this.promise.catch(Unit.wrap(Unit.currentUnit, callback));
|
|
858
|
-
return this;
|
|
859
|
-
}
|
|
860
|
-
finally(callback) {
|
|
861
|
-
this.promise = this.promise.finally(Unit.wrap(Unit.currentUnit, callback));
|
|
862
|
-
return this;
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
//----------------------------------------------------------------------------------------------------
|
|
866
|
-
// unit timer
|
|
867
|
-
//----------------------------------------------------------------------------------------------------
|
|
868
|
-
class UnitTimer {
|
|
869
|
-
constructor(options) {
|
|
870
|
-
this.stack = [];
|
|
871
|
-
this.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, Object.assign({ snapshot: Unit.snapshot(Unit.currentUnit) }, options));
|
|
872
|
-
}
|
|
873
|
-
clear() {
|
|
874
|
-
this.stack = [];
|
|
875
|
-
this.unit.finalize();
|
|
876
|
-
}
|
|
877
|
-
timeout(timeout, duration = 0) {
|
|
878
|
-
UnitTimer.execute(this, { timeout, duration, iterations: 1 });
|
|
879
|
-
return this;
|
|
880
|
-
}
|
|
881
|
-
iteration(timeout, duration = 0, iterations = -1) {
|
|
882
|
-
UnitTimer.execute(this, { timeout, duration, iterations });
|
|
883
|
-
return this;
|
|
884
|
-
}
|
|
885
|
-
transition(transition, duration = 0, easing) {
|
|
886
|
-
UnitTimer.execute(this, { transition, duration, iterations: 1, easing });
|
|
887
|
-
return this;
|
|
888
|
-
}
|
|
889
|
-
static execute(timer, options) {
|
|
890
|
-
if (timer.unit._.state === 'finalized') {
|
|
891
|
-
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, Object.assign({ snapshot: Unit.snapshot(Unit.currentUnit) }, options));
|
|
892
|
-
}
|
|
893
|
-
else if (timer.stack.length === 0) {
|
|
894
|
-
timer.stack.push(Object.assign({ snapshot: Unit.snapshot(Unit.currentUnit) }, options));
|
|
895
|
-
timer.unit.on('finalize', () => { UnitTimer.next(timer); });
|
|
896
|
-
}
|
|
897
|
-
else {
|
|
898
|
-
timer.stack.push(Object.assign({ snapshot: Unit.snapshot(Unit.currentUnit) }, options));
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
static next(timer) {
|
|
902
|
-
if (timer.stack.length > 0) {
|
|
903
|
-
timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.stack.shift());
|
|
904
|
-
timer.unit.on('finalize', () => { UnitTimer.next(timer); });
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
static Component(unit, options) {
|
|
908
|
-
let counter = 0;
|
|
909
|
-
const timer = new Timer({
|
|
910
|
-
transition: (p) => {
|
|
911
|
-
if (options.transition)
|
|
912
|
-
Unit.scope(options.snapshot, options.transition, p);
|
|
913
|
-
},
|
|
914
|
-
timeout: () => {
|
|
915
|
-
if (options.transition)
|
|
916
|
-
Unit.scope(options.snapshot, options.transition, 1.0);
|
|
917
|
-
if (options.timeout)
|
|
918
|
-
Unit.scope(options.snapshot, options.timeout);
|
|
919
|
-
if (options.iterations && counter >= options.iterations - 1) {
|
|
920
|
-
unit.finalize();
|
|
921
|
-
}
|
|
922
|
-
counter++;
|
|
923
|
-
}, duration: options.duration, iterations: options.iterations, easing: options.easing
|
|
924
|
-
});
|
|
925
|
-
unit.on('finalize', () => timer.clear());
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
916
|
|
|
929
|
-
function
|
|
917
|
+
const xnew$1 = Object.assign(function (...args) {
|
|
918
|
+
if (Unit.rootUnit === undefined)
|
|
919
|
+
Unit.reset();
|
|
930
920
|
let target;
|
|
931
921
|
if (args[0] instanceof HTMLElement || args[0] instanceof SVGElement) {
|
|
932
922
|
target = args.shift(); // an existing html element
|
|
@@ -939,29 +929,26 @@
|
|
|
939
929
|
}
|
|
940
930
|
const component = args.shift();
|
|
941
931
|
const props = args.shift();
|
|
942
|
-
return
|
|
943
|
-
}
|
|
944
|
-
const xnew$1 = Object.assign(function (...args) {
|
|
945
|
-
if (Unit.rootUnit === undefined)
|
|
946
|
-
Unit.reset();
|
|
947
|
-
const { target, component, props } = parseArguments(...args);
|
|
948
|
-
return new Unit(Unit.currentUnit, target, component, props, { protect: false });
|
|
932
|
+
return new Unit(Unit.currentUnit, target, component, props);
|
|
949
933
|
}, {
|
|
950
934
|
/**
|
|
951
935
|
* Creates a nested HTML/SVG element within the current component
|
|
952
|
-
* @param
|
|
936
|
+
* @param tag - HTML or SVG tag name (e.g., '<div>', '<span>', '<svg>')
|
|
953
937
|
* @returns The created HTML/SVG element
|
|
954
938
|
* @throws Error if called after component initialization
|
|
955
939
|
* @example
|
|
956
940
|
* const div = xnew.nest('<div>')
|
|
957
941
|
* div.textContent = 'Hello'
|
|
958
942
|
*/
|
|
959
|
-
nest(
|
|
943
|
+
nest(tag, textContent) {
|
|
960
944
|
try {
|
|
961
|
-
|
|
945
|
+
if (Unit.currentUnit._.state !== 'invoked') {
|
|
946
|
+
throw new Error('xnew.nest can not be called after initialized.');
|
|
947
|
+
}
|
|
948
|
+
return Unit.nest(Unit.currentUnit, tag, textContent);
|
|
962
949
|
}
|
|
963
950
|
catch (error) {
|
|
964
|
-
console.error('xnew.nest(
|
|
951
|
+
console.error('xnew.nest(tag: string): ', error);
|
|
965
952
|
throw error;
|
|
966
953
|
}
|
|
967
954
|
},
|
|
@@ -976,6 +963,9 @@
|
|
|
976
963
|
*/
|
|
977
964
|
extend(component, props) {
|
|
978
965
|
try {
|
|
966
|
+
if (Unit.currentUnit._.state !== 'invoked') {
|
|
967
|
+
throw new Error('xnew.extend can not be called after initialized.');
|
|
968
|
+
}
|
|
979
969
|
return Unit.extend(Unit.currentUnit, component, props);
|
|
980
970
|
}
|
|
981
971
|
catch (error) {
|
|
@@ -1110,6 +1100,15 @@
|
|
|
1110
1100
|
throw error;
|
|
1111
1101
|
}
|
|
1112
1102
|
},
|
|
1103
|
+
/**
|
|
1104
|
+
* Emits a custom event to components
|
|
1105
|
+
* @param type - Event type to emit (prefix with '+' for global events, '-' for local events)
|
|
1106
|
+
* @param args - Additional arguments to pass to event listeners
|
|
1107
|
+
* @returns void
|
|
1108
|
+
* @example
|
|
1109
|
+
* xnew.emit('+globalevent', { data: 123 }); // Global event
|
|
1110
|
+
* xnew.emit('-localevent', { data: 123 }); // Local event
|
|
1111
|
+
*/
|
|
1113
1112
|
emit(type, ...args) {
|
|
1114
1113
|
try {
|
|
1115
1114
|
return Unit.emit(type, ...args);
|
|
@@ -1159,100 +1158,114 @@
|
|
|
1159
1158
|
transition(transition, duration = 0, easing = 'linear') {
|
|
1160
1159
|
return new UnitTimer({ transition, duration, easing, iterations: 1 });
|
|
1161
1160
|
},
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1161
|
+
/**
|
|
1162
|
+
* Call this method within a component function to enable protection.
|
|
1163
|
+
* Protected components will not respond to global events emitted via xnew.emit,
|
|
1164
|
+
* and will be excluded from xnew.find searches.
|
|
1165
|
+
* @example
|
|
1166
|
+
* function MyComponent(unit) {
|
|
1167
|
+
* xnew.protect();
|
|
1168
|
+
* // Component logic here
|
|
1169
|
+
* }
|
|
1170
|
+
*/
|
|
1171
|
+
protect() {
|
|
1172
|
+
Unit.currentUnit._.protected = true;
|
|
1167
1173
|
},
|
|
1168
1174
|
});
|
|
1169
1175
|
|
|
1170
1176
|
function Accordion(unit, { open = false, duration = 200, easing = 'ease' } = {}) {
|
|
1171
1177
|
xnew$1.context('xnew.accordion', unit);
|
|
1172
|
-
|
|
1173
|
-
|
|
1178
|
+
let state = open ? 1.0 : 0.0;
|
|
1179
|
+
let sign = open ? +1 : -1;
|
|
1180
|
+
let timer = xnew$1.timeout(() => xnew$1.emit('-transition', { state }));
|
|
1174
1181
|
return {
|
|
1175
|
-
state: open ? 1.0 : 0.0,
|
|
1176
1182
|
toggle() {
|
|
1177
|
-
|
|
1178
|
-
unit.close();
|
|
1179
|
-
}
|
|
1180
|
-
else if (unit.state === 0.0) {
|
|
1181
|
-
unit.open();
|
|
1182
|
-
}
|
|
1183
|
+
sign > 0 ? unit.close() : unit.open();
|
|
1183
1184
|
},
|
|
1184
1185
|
open() {
|
|
1185
|
-
if (
|
|
1186
|
-
|
|
1187
|
-
}
|
|
1186
|
+
if (sign < 0)
|
|
1187
|
+
transition();
|
|
1188
1188
|
},
|
|
1189
1189
|
close() {
|
|
1190
|
-
if (
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1190
|
+
if (sign > 0)
|
|
1191
|
+
transition();
|
|
1192
|
+
},
|
|
1193
|
+
};
|
|
1194
|
+
function transition() {
|
|
1195
|
+
sign *= -1;
|
|
1196
|
+
const d = sign > 0 ? 1 - state : state;
|
|
1197
|
+
timer.clear();
|
|
1198
|
+
timer = xnew$1.transition((x) => {
|
|
1199
|
+
const y = x < 1.0 ? (1 - x) * d : 0.0;
|
|
1200
|
+
state = sign > 0 ? 1.0 - y : y;
|
|
1201
|
+
xnew$1.emit('-transition', { state });
|
|
1202
|
+
}, duration * d, easing);
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
function Modal(unit, { duration = 200, easing = 'ease' } = {}) {
|
|
1206
|
+
xnew$1.context('xnew.modal', unit);
|
|
1207
|
+
let state = 0.0;
|
|
1208
|
+
let timer = xnew$1.transition((x) => {
|
|
1209
|
+
state = x;
|
|
1210
|
+
xnew$1.emit('-transition', { state });
|
|
1211
|
+
}, duration, easing);
|
|
1212
|
+
return {
|
|
1213
|
+
close() {
|
|
1214
|
+
const d = state;
|
|
1215
|
+
timer.clear();
|
|
1216
|
+
timer = xnew$1.transition((x) => {
|
|
1217
|
+
state = x < 1.0 ? (1 - x) * d : 0.0;
|
|
1218
|
+
xnew$1.emit('-transition', { state });
|
|
1219
|
+
}, duration * d, easing)
|
|
1220
|
+
.timeout(() => unit.finalize());
|
|
1221
|
+
},
|
|
1194
1222
|
};
|
|
1195
1223
|
}
|
|
1196
1224
|
|
|
1197
|
-
function Screen(unit, { width
|
|
1198
|
-
const size = { width, height };
|
|
1199
|
-
const
|
|
1200
|
-
|
|
1225
|
+
function Screen(unit, { width, height, fit = 'contain' } = {}) {
|
|
1226
|
+
const size = { width: width !== null && width !== void 0 ? width : 800, height: height !== null && height !== void 0 ? height : 600 };
|
|
1227
|
+
const outer = xnew$1.nest('<div style="position: relative; width: 100%; height: 100%; overflow: hidden;">');
|
|
1228
|
+
xnew$1().on('resize', resize);
|
|
1201
1229
|
const absolute = xnew$1.nest('<div style="position: absolute; margin: auto; container-type: size; overflow: hidden;">');
|
|
1202
|
-
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;">`);
|
|
1230
|
+
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;">`);
|
|
1203
1231
|
resize();
|
|
1204
1232
|
function resize() {
|
|
1205
|
-
const aspect = size.width / size.height;
|
|
1206
1233
|
const style = { width: '100%', height: '100%', top: 0, left: 0, bottom: 0, right: 0 };
|
|
1207
1234
|
if (fit === 'contain') {
|
|
1208
|
-
|
|
1209
|
-
|
|
1235
|
+
const aspect = size.width / size.height;
|
|
1236
|
+
if (outer.clientWidth < outer.clientHeight * aspect) {
|
|
1237
|
+
style.height = Math.floor(outer.clientWidth / aspect) + 'px';
|
|
1210
1238
|
}
|
|
1211
1239
|
else {
|
|
1212
|
-
style.width = Math.floor(
|
|
1240
|
+
style.width = Math.floor(outer.clientHeight * aspect) + 'px';
|
|
1213
1241
|
}
|
|
1214
1242
|
}
|
|
1215
1243
|
else if (fit === 'cover') {
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
style.
|
|
1244
|
+
const aspect = size.width / size.height;
|
|
1245
|
+
if (outer.clientWidth < outer.clientHeight * aspect) {
|
|
1246
|
+
style.width = Math.floor(outer.clientHeight * aspect) + 'px';
|
|
1247
|
+
style.left = Math.floor((outer.clientWidth - outer.clientHeight * aspect) / 2) + 'px';
|
|
1219
1248
|
style.right = 'auto';
|
|
1220
1249
|
}
|
|
1221
1250
|
else {
|
|
1222
|
-
style.height = Math.floor(
|
|
1223
|
-
style.top = Math.floor((
|
|
1251
|
+
style.height = Math.floor(outer.clientWidth / aspect) + 'px';
|
|
1252
|
+
style.top = Math.floor((outer.clientHeight - outer.clientWidth / aspect) / 2) + 'px';
|
|
1224
1253
|
style.bottom = 'auto';
|
|
1225
1254
|
}
|
|
1226
1255
|
}
|
|
1227
|
-
else
|
|
1256
|
+
else if (fit === 'resize') {
|
|
1257
|
+
size.width = outer.clientWidth > 0 ? outer.clientWidth : size.width;
|
|
1258
|
+
size.height = outer.clientHeight > 0 ? outer.clientHeight : size.height;
|
|
1259
|
+
console.log(size);
|
|
1260
|
+
canvas.element.setAttribute('width', size.width + 'px');
|
|
1261
|
+
canvas.element.setAttribute('height', size.height + 'px');
|
|
1262
|
+
}
|
|
1228
1263
|
Object.assign(absolute.style, style);
|
|
1229
1264
|
}
|
|
1230
1265
|
return {
|
|
1231
1266
|
get canvas() {
|
|
1232
1267
|
return canvas.element;
|
|
1233
1268
|
},
|
|
1234
|
-
resize(width, height) {
|
|
1235
|
-
size.width = width;
|
|
1236
|
-
size.height = height;
|
|
1237
|
-
canvas.element.setAttribute('width', width + 'px');
|
|
1238
|
-
canvas.element.setAttribute('height', height + 'px');
|
|
1239
|
-
resize();
|
|
1240
|
-
},
|
|
1241
|
-
};
|
|
1242
|
-
}
|
|
1243
|
-
|
|
1244
|
-
function Modal(unit, { duration = 200, easing = 'ease' } = {}) {
|
|
1245
|
-
xnew$1.context('xnew.modalframe', unit);
|
|
1246
|
-
xnew$1.nest('<div style="position: fixed; inset: 0; z-index: 1000;">');
|
|
1247
|
-
unit.on('click', ({ event }) => unit.close());
|
|
1248
|
-
unit.on('-transition', ({ state }) => unit.state = state);
|
|
1249
|
-
xnew$1.transition((x) => xnew$1.emit('-transition', { state: x }), duration, easing);
|
|
1250
|
-
return {
|
|
1251
|
-
state: 0.0,
|
|
1252
|
-
close() {
|
|
1253
|
-
xnew$1.transition((x) => xnew$1.emit('-transition', { state: 1.0 - x }), duration, easing)
|
|
1254
|
-
.timeout(() => unit.finalize());
|
|
1255
|
-
}
|
|
1256
1269
|
};
|
|
1257
1270
|
}
|
|
1258
1271
|
|
|
@@ -1307,7 +1320,7 @@
|
|
|
1307
1320
|
xnew$1.emit('-up', { type: '-up', vector });
|
|
1308
1321
|
});
|
|
1309
1322
|
}
|
|
1310
|
-
function
|
|
1323
|
+
function DPad(unit, { diagonal = true, stroke = 'currentColor', strokeOpacity = 0.8, strokeWidth = 2, strokeLinejoin = 'round', fill = '#FFF', fillOpacity = 0.8 } = {}) {
|
|
1311
1324
|
const outer = xnew$1.nest(`<div style="position: relative; width: 100%; height: 100%;">`);
|
|
1312
1325
|
let newsize = Math.min(outer.clientWidth, outer.clientHeight);
|
|
1313
1326
|
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;">`);
|
|
@@ -1603,7 +1616,7 @@
|
|
|
1603
1616
|
Modal,
|
|
1604
1617
|
Accordion,
|
|
1605
1618
|
AnalogStick,
|
|
1606
|
-
|
|
1619
|
+
DPad,
|
|
1607
1620
|
};
|
|
1608
1621
|
const audio = {
|
|
1609
1622
|
load(path) {
|