@zcomponent/core 0.0.1
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/README.md +7 -0
- package/css/zcomponent.css +35 -0
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/lib/actionbehavior.d.ts +20 -0
- package/lib/actionbehavior.js +32 -0
- package/lib/animation.d.ts +129 -0
- package/lib/animation.js +1 -0
- package/lib/behavior.d.ts +26 -0
- package/lib/behavior.js +10 -0
- package/lib/behaviors/LaunchURL.d.ts +12 -0
- package/lib/behaviors/LaunchURL.js +12 -0
- package/lib/behaviors/LogAnalyticsEvent.d.ts +10 -0
- package/lib/behaviors/LogAnalyticsEvent.js +10 -0
- package/lib/behaviors/PlaySound.d.ts +12 -0
- package/lib/behaviors/PlaySound.js +18 -0
- package/lib/component.d.ts +22 -0
- package/lib/component.js +4 -0
- package/lib/components/DefaultLoader.d.ts +55 -0
- package/lib/components/DefaultLoader.js +112 -0
- package/lib/context.d.ts +28 -0
- package/lib/context.js +65 -0
- package/lib/contexts/analyticscontext.d.ts +9 -0
- package/lib/contexts/analyticscontext.js +17 -0
- package/lib/contexts/canvascontext.d.ts +15 -0
- package/lib/contexts/canvascontext.js +45 -0
- package/lib/contexts/cookieconsentcontext.d.ts +38 -0
- package/lib/contexts/cookieconsentcontext.js +63 -0
- package/lib/contexts/environmentcontext.d.ts +9 -0
- package/lib/contexts/environmentcontext.js +14 -0
- package/lib/contexts/loadcontext.d.ts +24 -0
- package/lib/contexts/loadcontext.js +94 -0
- package/lib/contexts/usereventcontext.d.ts +12 -0
- package/lib/contexts/usereventcontext.js +32 -0
- package/lib/data.d.ts +38 -0
- package/lib/data.js +240 -0
- package/lib/event.d.ts +24 -0
- package/lib/event.js +61 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +20 -0
- package/lib/interfaces.d.ts +67 -0
- package/lib/interfaces.js +1 -0
- package/lib/observable.d.ts +16 -0
- package/lib/observable.js +116 -0
- package/lib/props.d.ts +0 -0
- package/lib/props.js +4 -0
- package/lib/selectors.d.ts +13 -0
- package/lib/selectors.js +173 -0
- package/lib/types.d.ts +105 -0
- package/lib/types.js +295 -0
- package/lib/validators.d.ts +8 -0
- package/lib/validators.js +32 -0
- package/lib/zcomponent.d.ts +55 -0
- package/lib/zcomponent.js +226 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
.zcomponent-defaultloader {
|
|
2
|
+
position: fixed;
|
|
3
|
+
font-family: sans-serif;
|
|
4
|
+
color: white;
|
|
5
|
+
background-color: black;
|
|
6
|
+
display: flex;
|
|
7
|
+
align-items: center;
|
|
8
|
+
justify-content: center;
|
|
9
|
+
background-size: cover;
|
|
10
|
+
background-position: center center;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
#zcomponent-defaultloader-container {
|
|
14
|
+
text-align: center;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#zcomponent-defaultloader-subtitle {
|
|
18
|
+
margin-bottom: 32px;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#zcomponent-defaultloader-progress {
|
|
22
|
+
position: relative;
|
|
23
|
+
width: 300px;
|
|
24
|
+
background-color: #333;
|
|
25
|
+
height: 12px;
|
|
26
|
+
border-radius: 6px;
|
|
27
|
+
margin: 0 auto;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#zcomponent-defaultloader-progress-percentage {
|
|
31
|
+
position: relative;
|
|
32
|
+
background-color: white;
|
|
33
|
+
height: 12px;
|
|
34
|
+
border-radius: 6px;
|
|
35
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib';
|
package/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ConcreteBehavior } from "./behavior";
|
|
2
|
+
import { InstanceOfComponent } from "./component";
|
|
3
|
+
import { ContextManager } from "./context";
|
|
4
|
+
export declare type ActionConstructorType<T extends {}> = (props: T, ctx: ContextManager) => (() => void);
|
|
5
|
+
export interface ActionBehaviorConstructorProps {
|
|
6
|
+
/** @zprop
|
|
7
|
+
* @zvalues events
|
|
8
|
+
*/
|
|
9
|
+
event: string;
|
|
10
|
+
instance: InstanceOfComponent<any>;
|
|
11
|
+
}
|
|
12
|
+
export interface ActionBehaviorProps {
|
|
13
|
+
dispose: () => void;
|
|
14
|
+
}
|
|
15
|
+
export interface PreviewableActionBehaviorProps extends ActionBehaviorProps {
|
|
16
|
+
/** @zprop */
|
|
17
|
+
preview: () => void;
|
|
18
|
+
}
|
|
19
|
+
export declare function defineActionBehavior<ActionConstructorProps extends {}>(a: ActionConstructorType<ActionConstructorProps>): ConcreteBehavior<any, ActionConstructorProps & ActionBehaviorConstructorProps, ActionBehaviorProps>;
|
|
20
|
+
export declare function defineActionBehavior<ActionConstructorProps extends {}>(a: ActionConstructorType<ActionConstructorProps>, preview: true): ConcreteBehavior<any, ActionConstructorProps & ActionBehaviorConstructorProps, PreviewableActionBehaviorProps>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { defineBehavior } from "./behavior";
|
|
2
|
+
import { isEditTime } from "./contexts/environmentcontext";
|
|
3
|
+
import { Event } from "./event";
|
|
4
|
+
export function defineActionBehavior(a, preview) {
|
|
5
|
+
return defineBehavior((props, ctx) => {
|
|
6
|
+
let handler;
|
|
7
|
+
let evt;
|
|
8
|
+
const upstreamHandler = (!isEditTime(ctx) || preview) ? a(props, ctx) : undefined;
|
|
9
|
+
if (props.event && !isEditTime(ctx)) {
|
|
10
|
+
const evt = props.instance[props.event];
|
|
11
|
+
if (!evt || !(evt instanceof Event))
|
|
12
|
+
throw new Error(`Event '${props.event}' does not exist on instance`);
|
|
13
|
+
const handler = (evt) => {
|
|
14
|
+
if (evt && typeof evt === 'object' && typeof evt['stopPropagation'] === 'function') {
|
|
15
|
+
evt['stopPropagation'].call(evt);
|
|
16
|
+
}
|
|
17
|
+
upstreamHandler?.();
|
|
18
|
+
};
|
|
19
|
+
evt.bindfn(handler);
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
/** @zprop */
|
|
23
|
+
preview: preview ? () => {
|
|
24
|
+
upstreamHandler?.();
|
|
25
|
+
} : undefined,
|
|
26
|
+
dispose: () => {
|
|
27
|
+
if (handler)
|
|
28
|
+
evt?.unbindfn(handler);
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}, true);
|
|
32
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
export declare type ByID<T> = {
|
|
2
|
+
[id: string]: T | undefined;
|
|
3
|
+
};
|
|
4
|
+
export declare type ByNodeProperty<T> = {
|
|
5
|
+
[id: string]: {
|
|
6
|
+
[id: string]: T | undefined;
|
|
7
|
+
} | undefined;
|
|
8
|
+
};
|
|
9
|
+
export interface Animation {
|
|
10
|
+
clips: ByID<Clip>;
|
|
11
|
+
layers: ByID<Layer>;
|
|
12
|
+
}
|
|
13
|
+
export declare type BlendType = 'overlay' | 'add';
|
|
14
|
+
export interface FadeParameters {
|
|
15
|
+
time: number;
|
|
16
|
+
pin?: 'start' | 'middle' | 'end';
|
|
17
|
+
}
|
|
18
|
+
export interface Layer {
|
|
19
|
+
defaultFadeParameters?: FadeParameters;
|
|
20
|
+
clips: LayerClip[];
|
|
21
|
+
}
|
|
22
|
+
export interface LayerClip {
|
|
23
|
+
clipID: string;
|
|
24
|
+
loop?: string;
|
|
25
|
+
reverse?: boolean;
|
|
26
|
+
playbackSpeed?: number;
|
|
27
|
+
}
|
|
28
|
+
export declare type Clip = StateClip | TimelineClip;
|
|
29
|
+
export interface BaseClip {
|
|
30
|
+
type: 'timeline' | 'state';
|
|
31
|
+
}
|
|
32
|
+
export interface StateClip extends BaseClip {
|
|
33
|
+
type: 'state';
|
|
34
|
+
values: ByNodeProperty<any>;
|
|
35
|
+
defaultFadeParameters?: FadeParameters;
|
|
36
|
+
fadeParameters: ByNodeProperty<FadeParameters>;
|
|
37
|
+
blend: ByNodeProperty<StateClipBlend>;
|
|
38
|
+
}
|
|
39
|
+
export interface StateClipBlend {
|
|
40
|
+
weight?: number;
|
|
41
|
+
type?: BlendType;
|
|
42
|
+
}
|
|
43
|
+
export interface TimelineClip extends BaseClip {
|
|
44
|
+
type: 'timeline';
|
|
45
|
+
length: number;
|
|
46
|
+
tracks: ByID<Track>;
|
|
47
|
+
trackOrder: string[];
|
|
48
|
+
defaultFadeParameters?: FadeParameters;
|
|
49
|
+
timeSourceTrack?: string;
|
|
50
|
+
}
|
|
51
|
+
export declare type Track = PropertyTrack | ClipTrack | StreamTrack | FunctionTrack | LabelTrack;
|
|
52
|
+
export interface BaseTrack {
|
|
53
|
+
type: 'property' | 'clip' | 'stream' | 'function' | 'label';
|
|
54
|
+
}
|
|
55
|
+
export interface Keyframe<T> {
|
|
56
|
+
v: T;
|
|
57
|
+
t: number;
|
|
58
|
+
label?: string;
|
|
59
|
+
easing?: Easing;
|
|
60
|
+
}
|
|
61
|
+
export declare type Easing = 'linear' | 'step' | 'bounce' | Bezier[];
|
|
62
|
+
export declare type BezierCP = [number, number];
|
|
63
|
+
export declare type Bezier = [
|
|
64
|
+
BezierCP,
|
|
65
|
+
BezierCP,
|
|
66
|
+
[number, number]
|
|
67
|
+
] | [
|
|
68
|
+
BezierCP,
|
|
69
|
+
BezierCP
|
|
70
|
+
];
|
|
71
|
+
export interface PropertyTrack extends BaseTrack {
|
|
72
|
+
type: 'property';
|
|
73
|
+
entityID: string;
|
|
74
|
+
property: string;
|
|
75
|
+
values: Keyframe<any>[];
|
|
76
|
+
blend: BlendType;
|
|
77
|
+
weight: Keyframe<number>[];
|
|
78
|
+
fadeParameters?: FadeParameters;
|
|
79
|
+
}
|
|
80
|
+
export interface ClipTrack extends BaseTrack {
|
|
81
|
+
type: 'clip';
|
|
82
|
+
clipID: string;
|
|
83
|
+
data: ClipTrackEntry[];
|
|
84
|
+
weight: Keyframe<number>[];
|
|
85
|
+
}
|
|
86
|
+
export interface ClipTrackEntry {
|
|
87
|
+
t0: number;
|
|
88
|
+
t1: number;
|
|
89
|
+
s0: number;
|
|
90
|
+
rate: number;
|
|
91
|
+
}
|
|
92
|
+
export interface StreamTrack extends BaseTrack {
|
|
93
|
+
type: 'stream';
|
|
94
|
+
entityID: string;
|
|
95
|
+
data: StreamTrackEntry[];
|
|
96
|
+
weight: Keyframe<number>[];
|
|
97
|
+
}
|
|
98
|
+
export interface StreamTrackEntry {
|
|
99
|
+
t0: number;
|
|
100
|
+
t1: number;
|
|
101
|
+
s0: number;
|
|
102
|
+
rate: number;
|
|
103
|
+
}
|
|
104
|
+
export interface FunctionTrack extends BaseTrack {
|
|
105
|
+
type: 'function';
|
|
106
|
+
data: FunctionTrackEntry[];
|
|
107
|
+
}
|
|
108
|
+
export declare type FunctionTrackEntry = EntityFunctionTrackEntry | ImportFunctionTrackEntry;
|
|
109
|
+
export interface BaseFunctionTrackEntry {
|
|
110
|
+
type: 'entity' | 'import';
|
|
111
|
+
args: any[];
|
|
112
|
+
}
|
|
113
|
+
export interface EntityFunctionTrackEntry extends BaseFunctionTrackEntry {
|
|
114
|
+
type: 'entity';
|
|
115
|
+
entityID: string;
|
|
116
|
+
functionName: string;
|
|
117
|
+
}
|
|
118
|
+
export interface ImportFunctionTrackEntry extends BaseFunctionTrackEntry {
|
|
119
|
+
type: 'import';
|
|
120
|
+
import: string;
|
|
121
|
+
}
|
|
122
|
+
export interface LabelTrack extends BaseTrack {
|
|
123
|
+
type: 'label';
|
|
124
|
+
data: LabelTrackEntry[];
|
|
125
|
+
}
|
|
126
|
+
export interface LabelTrackEntry {
|
|
127
|
+
t: number;
|
|
128
|
+
label: string;
|
|
129
|
+
}
|
package/lib/animation.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Component, InstanceOfComponent } from './component';
|
|
2
|
+
import { ContextManager } from './context';
|
|
3
|
+
export declare type BehaviorInstance<PropsType extends {} = {
|
|
4
|
+
[id: string]: any;
|
|
5
|
+
}> = PropsType & {
|
|
6
|
+
dispose?: () => void;
|
|
7
|
+
[id: string]: any;
|
|
8
|
+
};
|
|
9
|
+
export declare type Behavior<ComponentType extends Component = () => {}, ConstructorPropsType extends {
|
|
10
|
+
instance: InstanceOfComponent<ComponentType>;
|
|
11
|
+
} = {
|
|
12
|
+
[id: string]: any;
|
|
13
|
+
instance: InstanceOfComponent<ComponentType>;
|
|
14
|
+
}, PropsType extends {} = {}> = (constructorProps: ConstructorPropsType, context: ContextManager) => BehaviorInstance<PropsType> | undefined;
|
|
15
|
+
export declare type ConcreteBehavior<ComponentType extends Component = () => {}, ConstructorPropsType extends {
|
|
16
|
+
instance: InstanceOfComponent<ComponentType>;
|
|
17
|
+
} = {
|
|
18
|
+
[id: string]: any;
|
|
19
|
+
instance: InstanceOfComponent<ComponentType>;
|
|
20
|
+
}, PropsType extends {} = {}> = (constructorProps: ConstructorPropsType, context: ContextManager) => BehaviorInstance<PropsType>;
|
|
21
|
+
export declare type ConstructorPropsOfBehavior<BehaviorType extends Behavior> = Parameters<BehaviorType>[0];
|
|
22
|
+
export declare type PropsOfBehavior<BehaviorType extends Behavior> = ReturnType<BehaviorType>;
|
|
23
|
+
export declare type InstanceOfBehavior<BehaviorType extends Behavior> = ReturnType<BehaviorType>;
|
|
24
|
+
export declare function shouldBehaviorRunAtDesignTime(b: Behavior): boolean;
|
|
25
|
+
/** Register a new custom behavior */
|
|
26
|
+
export declare function defineBehavior<BehaviorType extends Behavior>(c: BehaviorType, runAtDesignTime?: boolean): BehaviorType;
|
package/lib/behavior.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const behaviorsToRunAtDesignTime = new Set();
|
|
2
|
+
export function shouldBehaviorRunAtDesignTime(b) {
|
|
3
|
+
return behaviorsToRunAtDesignTime.has(b);
|
|
4
|
+
}
|
|
5
|
+
/** Register a new custom behavior */
|
|
6
|
+
export function defineBehavior(c, runAtDesignTime = false) {
|
|
7
|
+
if (runAtDesignTime)
|
|
8
|
+
behaviorsToRunAtDesignTime.add(c);
|
|
9
|
+
return c;
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface LaunchURLProps {
|
|
2
|
+
/** @zprop */
|
|
3
|
+
url: string;
|
|
4
|
+
/** @zprop
|
|
5
|
+
* @zdefault true
|
|
6
|
+
*/
|
|
7
|
+
openInNew: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** @zbehavior
|
|
10
|
+
* @zgroup Actions
|
|
11
|
+
*/
|
|
12
|
+
export declare const LaunchURL: import("..").ConcreteBehavior<any, LaunchURLProps & import("../actionbehavior").ActionBehaviorConstructorProps, import("../actionbehavior").ActionBehaviorProps>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineActionBehavior } from "../actionbehavior";
|
|
2
|
+
/** @zbehavior
|
|
3
|
+
* @zgroup Actions
|
|
4
|
+
*/
|
|
5
|
+
export const LaunchURL = defineActionBehavior((props) => {
|
|
6
|
+
return () => {
|
|
7
|
+
if (props.openInNew ?? true)
|
|
8
|
+
window.open(props.url, '_blank', 'noopener noreferrer');
|
|
9
|
+
else
|
|
10
|
+
window.location.href = props.url;
|
|
11
|
+
};
|
|
12
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface LogAnalyticsEventProps {
|
|
2
|
+
/** @zprop */
|
|
3
|
+
name: string;
|
|
4
|
+
/** @zprop */
|
|
5
|
+
params: any;
|
|
6
|
+
}
|
|
7
|
+
/** @zbehavior
|
|
8
|
+
* @zgroup Actions
|
|
9
|
+
*/
|
|
10
|
+
export declare const LogAnalyticsEvent: import("..").ConcreteBehavior<any, LogAnalyticsEventProps & import("../actionbehavior").ActionBehaviorConstructorProps, import("../actionbehavior").ActionBehaviorProps>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { defineActionBehavior } from "../actionbehavior";
|
|
2
|
+
import { logEvent } from "../contexts/analyticscontext";
|
|
3
|
+
/** @zbehavior
|
|
4
|
+
* @zgroup Actions
|
|
5
|
+
*/
|
|
6
|
+
export const LogAnalyticsEvent = defineActionBehavior((props, mgr) => {
|
|
7
|
+
return () => {
|
|
8
|
+
logEvent(mgr, props.name, props.params);
|
|
9
|
+
};
|
|
10
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface PlaySoundProps {
|
|
2
|
+
/** The URL of the file to play
|
|
3
|
+
* @zprop
|
|
4
|
+
* @zvalues files *.wav
|
|
5
|
+
* @zvalues files *.mp3
|
|
6
|
+
*/
|
|
7
|
+
source: string;
|
|
8
|
+
}
|
|
9
|
+
/** @zbehavior
|
|
10
|
+
* @zgroup Actions
|
|
11
|
+
*/
|
|
12
|
+
export declare const PlaySound: import("..").ConcreteBehavior<any, PlaySoundProps & import("../actionbehavior").ActionBehaviorConstructorProps, import("../actionbehavior").PreviewableActionBehaviorProps>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineActionBehavior } from "../actionbehavior";
|
|
2
|
+
import { registerLoadable } from "../contexts/loadcontext";
|
|
3
|
+
/** @zbehavior
|
|
4
|
+
* @zgroup Actions
|
|
5
|
+
*/
|
|
6
|
+
export const PlaySound = defineActionBehavior((props, mgr) => {
|
|
7
|
+
const elm = document.createElement('audio');
|
|
8
|
+
elm.preload = 'auto';
|
|
9
|
+
registerLoadable(mgr, new Promise((resolve, reject) => {
|
|
10
|
+
elm.addEventListener('canplaythrough', () => resolve());
|
|
11
|
+
elm.addEventListener('error', reject);
|
|
12
|
+
}));
|
|
13
|
+
elm.src = props.source;
|
|
14
|
+
return () => {
|
|
15
|
+
elm.currentTime = 0;
|
|
16
|
+
elm.play();
|
|
17
|
+
};
|
|
18
|
+
}, true);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ContextManager } from './context';
|
|
2
|
+
export declare type ComponentInstance<PropsType extends {} = {
|
|
3
|
+
[id: string]: any;
|
|
4
|
+
}, RootType = any> = PropsType & {
|
|
5
|
+
dispose?: () => void;
|
|
6
|
+
root?: RootType;
|
|
7
|
+
[id: string]: any;
|
|
8
|
+
};
|
|
9
|
+
export declare type ComponentChildren = [Component, {
|
|
10
|
+
[id: string]: any;
|
|
11
|
+
}][];
|
|
12
|
+
export declare type Component<ConstructorPropsType extends {
|
|
13
|
+
children?: ComponentChildren;
|
|
14
|
+
} = {
|
|
15
|
+
[id: string]: any;
|
|
16
|
+
children?: ComponentChildren;
|
|
17
|
+
}, PropsType extends {} = {}, RootType = any> = (constructorProps: ConstructorPropsType, context: ContextManager) => ComponentInstance<PropsType, RootType> | undefined;
|
|
18
|
+
export declare type ConstructorPropsOfComponent<ComponentType extends Component> = Parameters<ComponentType>[0];
|
|
19
|
+
export declare type PropsOfComponent<ComponentType extends Component> = ReturnType<ComponentType>;
|
|
20
|
+
export declare type InstanceOfComponent<ComponentType extends Component> = ReturnType<ComponentType>;
|
|
21
|
+
/** Register a new custom component */
|
|
22
|
+
export declare function defineComponent<ComponentType extends Component>(c: ComponentType): ComponentType;
|
package/lib/component.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Observable } from "../";
|
|
2
|
+
export interface DefaultLoaderConstructorProps {
|
|
3
|
+
/** @zprop
|
|
4
|
+
* @zgroup Appearance
|
|
5
|
+
* @zgrouppriority 10
|
|
6
|
+
* @zvalues files *.jpeg
|
|
7
|
+
* @zvalues files *.png
|
|
8
|
+
* @zvalues files *.jpg
|
|
9
|
+
*/
|
|
10
|
+
backgroundImage?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The DefaultLoader shows a customizable loading bar while the assets of your experience are being downloaded and parsed by the browser.
|
|
14
|
+
*
|
|
15
|
+
* @zcomponent
|
|
16
|
+
* @zicon loader
|
|
17
|
+
* @zgroup Advanced
|
|
18
|
+
*/
|
|
19
|
+
export declare const DefaultLoader: (props: DefaultLoaderConstructorProps, mgr: import("../context").ContextManager) => {
|
|
20
|
+
dispose: () => void;
|
|
21
|
+
/** @zprop
|
|
22
|
+
* @zdefault false
|
|
23
|
+
* @zgroup Design Time
|
|
24
|
+
* @zgrouppriority 30
|
|
25
|
+
*/
|
|
26
|
+
preview: Observable<boolean>;
|
|
27
|
+
/** @zprop
|
|
28
|
+
* @zgroup Text
|
|
29
|
+
* @zgrouppriority 20
|
|
30
|
+
*/
|
|
31
|
+
title: Observable<string>;
|
|
32
|
+
/** @zprop
|
|
33
|
+
* @zgroup Text
|
|
34
|
+
* @zgrouppriority 20
|
|
35
|
+
*/
|
|
36
|
+
subtitle: Observable<string>;
|
|
37
|
+
/** @zprop
|
|
38
|
+
* @zdefault black
|
|
39
|
+
* @zgroup Appearance
|
|
40
|
+
* @zgrouppriority 10
|
|
41
|
+
*/
|
|
42
|
+
backgroundColor: Observable<string>;
|
|
43
|
+
/** @zprop
|
|
44
|
+
* @zdefault white
|
|
45
|
+
* @zgroup Appearance
|
|
46
|
+
* @zgrouppriority 10
|
|
47
|
+
*/
|
|
48
|
+
textColor: Observable<string>;
|
|
49
|
+
/** @zprop
|
|
50
|
+
* @zdefault 1000
|
|
51
|
+
* @zgroup Appearance
|
|
52
|
+
* @zgrouppriority 10
|
|
53
|
+
*/
|
|
54
|
+
zIndex: Observable<number>;
|
|
55
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { defineComponent, useCanvas, Observable, isDesignTime, useIsLoaded, useLoadPercent } from "../";
|
|
2
|
+
/**
|
|
3
|
+
* The DefaultLoader shows a customizable loading bar while the assets of your experience are being downloaded and parsed by the browser.
|
|
4
|
+
*
|
|
5
|
+
* @zcomponent
|
|
6
|
+
* @zicon loader
|
|
7
|
+
* @zgroup Advanced
|
|
8
|
+
*/
|
|
9
|
+
export const DefaultLoader = defineComponent((props, mgr) => {
|
|
10
|
+
const preview = new Observable(false);
|
|
11
|
+
let disposed = false;
|
|
12
|
+
let attached = false;
|
|
13
|
+
const canvas = useCanvas(mgr);
|
|
14
|
+
const div = document.createElement('div');
|
|
15
|
+
div.innerHTML = loaderHTML;
|
|
16
|
+
div.className = "zcomponent-defaultloader";
|
|
17
|
+
if (props.backgroundImage) {
|
|
18
|
+
div.style.backgroundImage = `url(${props.backgroundImage})`;
|
|
19
|
+
}
|
|
20
|
+
const titleElement = div.querySelector("#zcomponent-defaultloader-title") || document.createElement("h1");
|
|
21
|
+
const subtitleElement = div.querySelector("#zcomponent-defaultloader-subtitle") || document.createElement("h1");
|
|
22
|
+
const percentageElement = div.querySelector("#zcomponent-defaultloader-progress-percentage") || document.createElement("div");
|
|
23
|
+
const title = new Observable('', t => titleElement.innerText = t);
|
|
24
|
+
const subtitle = new Observable('', t => subtitleElement.innerText = t);
|
|
25
|
+
const textColor = new Observable('white', t => div.style.color = t ?? 'white');
|
|
26
|
+
const backgroundColor = new Observable('black', t => div.style.backgroundColor = t ?? 'black');
|
|
27
|
+
const zIndex = new Observable(1000, t => div.style.zIndex = t.toString());
|
|
28
|
+
const update = () => {
|
|
29
|
+
if (disposed)
|
|
30
|
+
return;
|
|
31
|
+
const rect = canvas.getBoundingClientRect();
|
|
32
|
+
div.style.left = rect.left.toString() + "px";
|
|
33
|
+
div.style.top = rect.top.toString() + "px";
|
|
34
|
+
div.style.width = rect.width.toString() + "px";
|
|
35
|
+
div.style.height = rect.height.toString() + "px";
|
|
36
|
+
requestAnimationFrame(update);
|
|
37
|
+
};
|
|
38
|
+
const updatePercentage = () => {
|
|
39
|
+
const p = (isDesignTime(mgr) && preview.value) ? 30 : useLoadPercent(mgr).value;
|
|
40
|
+
percentageElement.style.width = p.toString() + "%";
|
|
41
|
+
};
|
|
42
|
+
const updateVisibility = () => {
|
|
43
|
+
let shouldBeAttached = false;
|
|
44
|
+
if (!isDesignTime(mgr) && !useIsLoaded(mgr).value)
|
|
45
|
+
shouldBeAttached = true;
|
|
46
|
+
if (isDesignTime(mgr) && preview.value)
|
|
47
|
+
shouldBeAttached = true;
|
|
48
|
+
if (!attached && shouldBeAttached) {
|
|
49
|
+
document.body.appendChild(div);
|
|
50
|
+
attached = true;
|
|
51
|
+
}
|
|
52
|
+
else if (attached && !shouldBeAttached) {
|
|
53
|
+
div.remove();
|
|
54
|
+
attached = false;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
preview.withValue(updateVisibility);
|
|
58
|
+
preview.withValue(updatePercentage);
|
|
59
|
+
useIsLoaded(mgr).withValue(updateVisibility);
|
|
60
|
+
useLoadPercent(mgr).withValue(updatePercentage);
|
|
61
|
+
update();
|
|
62
|
+
return {
|
|
63
|
+
dispose: () => {
|
|
64
|
+
disposed = true;
|
|
65
|
+
useIsLoaded(mgr).removeWithValue(updateVisibility);
|
|
66
|
+
useLoadPercent(mgr).removeWithValue(updatePercentage);
|
|
67
|
+
},
|
|
68
|
+
/** @zprop
|
|
69
|
+
* @zdefault false
|
|
70
|
+
* @zgroup Design Time
|
|
71
|
+
* @zgrouppriority 30
|
|
72
|
+
*/
|
|
73
|
+
preview,
|
|
74
|
+
/** @zprop
|
|
75
|
+
* @zgroup Text
|
|
76
|
+
* @zgrouppriority 20
|
|
77
|
+
*/
|
|
78
|
+
title,
|
|
79
|
+
/** @zprop
|
|
80
|
+
* @zgroup Text
|
|
81
|
+
* @zgrouppriority 20
|
|
82
|
+
*/
|
|
83
|
+
subtitle,
|
|
84
|
+
/** @zprop
|
|
85
|
+
* @zdefault black
|
|
86
|
+
* @zgroup Appearance
|
|
87
|
+
* @zgrouppriority 10
|
|
88
|
+
*/
|
|
89
|
+
backgroundColor,
|
|
90
|
+
/** @zprop
|
|
91
|
+
* @zdefault white
|
|
92
|
+
* @zgroup Appearance
|
|
93
|
+
* @zgrouppriority 10
|
|
94
|
+
*/
|
|
95
|
+
textColor,
|
|
96
|
+
/** @zprop
|
|
97
|
+
* @zdefault 1000
|
|
98
|
+
* @zgroup Appearance
|
|
99
|
+
* @zgrouppriority 10
|
|
100
|
+
*/
|
|
101
|
+
zIndex,
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
const loaderHTML = `
|
|
105
|
+
<div id="zcomponent-defaultloader-container">
|
|
106
|
+
<h1 id="zcomponent-defaultloader-title"></h1>
|
|
107
|
+
<div id="zcomponent-defaultloader-subtitle"></div>
|
|
108
|
+
<div id="zcomponent-defaultloader-progress">
|
|
109
|
+
<div id="zcomponent-defaultloader-progress-percentage"></div>
|
|
110
|
+
</div>
|
|
111
|
+
</div>
|
|
112
|
+
`;
|
package/lib/context.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface ContextType<T extends {}> {
|
|
2
|
+
key: string;
|
|
3
|
+
hasDefault: false;
|
|
4
|
+
}
|
|
5
|
+
export interface ContextTypeWithDefault<T extends {}, Args extends Array<any> = []> {
|
|
6
|
+
key: string;
|
|
7
|
+
hasDefault: true;
|
|
8
|
+
def: (ctx: ContextManager, ...args: Args) => T;
|
|
9
|
+
}
|
|
10
|
+
export declare function defineContextType<T extends {}>(): ContextType<T>;
|
|
11
|
+
export declare function defineContextType<T extends {}, Args extends Array<any> = []>(def: (ctx: ContextManager, ...args: Args) => T): ContextTypeWithDefault<T, Args>;
|
|
12
|
+
export declare class ContextManager {
|
|
13
|
+
private _byKey;
|
|
14
|
+
private _root;
|
|
15
|
+
constructor();
|
|
16
|
+
static create(): ContextManager;
|
|
17
|
+
static create<T extends {}>(type: ContextType<T>, props: T): ContextManager;
|
|
18
|
+
static create<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): ContextManager;
|
|
19
|
+
add<T extends {}>(type: ContextType<T>, obj: T): T;
|
|
20
|
+
add<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): T;
|
|
21
|
+
fork<T extends {}>(type: ContextType<T>, obj: T): [ContextManager, T];
|
|
22
|
+
fork<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): [ContextManager, T];
|
|
23
|
+
get<T extends {}>(type: ContextType<T>): T | undefined;
|
|
24
|
+
get<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): T;
|
|
25
|
+
getExisting<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>): T | undefined;
|
|
26
|
+
getOrThrow<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>): T;
|
|
27
|
+
delete<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>): void;
|
|
28
|
+
}
|
package/lib/context.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
let _nextContextKey = 1;
|
|
2
|
+
export function defineContextType(def) {
|
|
3
|
+
_nextContextKey++;
|
|
4
|
+
if (def) {
|
|
5
|
+
return {
|
|
6
|
+
key: _nextContextKey.toString(),
|
|
7
|
+
hasDefault: def !== undefined,
|
|
8
|
+
def,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
else
|
|
12
|
+
return {
|
|
13
|
+
key: _nextContextKey.toString(),
|
|
14
|
+
hasDefault: false
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export class ContextManager {
|
|
18
|
+
constructor() {
|
|
19
|
+
this._byKey = {};
|
|
20
|
+
this._root = this;
|
|
21
|
+
}
|
|
22
|
+
static create(type, ...args) {
|
|
23
|
+
const ctx = new ContextManager();
|
|
24
|
+
if (type) {
|
|
25
|
+
if (type.hasDefault)
|
|
26
|
+
ctx.add(type, ...args);
|
|
27
|
+
else
|
|
28
|
+
ctx.add(type, args[0]);
|
|
29
|
+
}
|
|
30
|
+
return ctx;
|
|
31
|
+
}
|
|
32
|
+
add(type, ...args) {
|
|
33
|
+
const root = this._root;
|
|
34
|
+
const t = type.hasDefault ? type.def(root, ...args) : args[0];
|
|
35
|
+
root._byKey[type.key] = t;
|
|
36
|
+
return t;
|
|
37
|
+
}
|
|
38
|
+
fork(type, ...args) {
|
|
39
|
+
const ret = new ContextManager();
|
|
40
|
+
ret._root = this._root;
|
|
41
|
+
ret._byKey = Object.create(this._byKey);
|
|
42
|
+
const t = type.hasDefault ? type.def(ret, ...args) : args[0];
|
|
43
|
+
ret._byKey[type.key] = t;
|
|
44
|
+
return [ret, t];
|
|
45
|
+
}
|
|
46
|
+
get(type, ...args) {
|
|
47
|
+
let entry = this._byKey[type.key];
|
|
48
|
+
if (!entry && type.hasDefault) {
|
|
49
|
+
entry = this.add(type, ...args);
|
|
50
|
+
}
|
|
51
|
+
return entry;
|
|
52
|
+
}
|
|
53
|
+
getExisting(type) {
|
|
54
|
+
return this._byKey[type.key];
|
|
55
|
+
}
|
|
56
|
+
getOrThrow(type) {
|
|
57
|
+
const res = this._byKey[type.key];
|
|
58
|
+
if (!res)
|
|
59
|
+
throw new Error("Required context not present");
|
|
60
|
+
return res;
|
|
61
|
+
}
|
|
62
|
+
delete(type) {
|
|
63
|
+
delete this._byKey[type.key];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ContextManager } from '../context';
|
|
2
|
+
import { Event } from '../event';
|
|
3
|
+
export interface AnalyticsContext {
|
|
4
|
+
onEvent: Event<[string, any]>;
|
|
5
|
+
logEvent: (name: string, params?: any) => void;
|
|
6
|
+
}
|
|
7
|
+
export declare const AnalyticsContext: import("../context").ContextTypeWithDefault<AnalyticsContext, []>;
|
|
8
|
+
export declare function logEvent(mgr: ContextManager, name: string, params?: any): void;
|
|
9
|
+
export declare function useAnalyticsOnEvent(mgr: ContextManager): Event<[string, any]>;
|