@zcomponent/core 0.0.1 → 0.0.4

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.
@@ -1,20 +1,17 @@
1
- import { ConcreteBehavior } from "./behavior";
2
- import { InstanceOfComponent } from "./component";
1
+ import { Behavior, BehaviorConstructorProps } from "./behavior";
3
2
  import { ContextManager } from "./context";
4
- export declare type ActionConstructorType<T extends {}> = (props: T, ctx: ContextManager) => (() => void);
5
- export interface ActionBehaviorConstructorProps {
3
+ export interface ActionBehaviorConstructorProps extends BehaviorConstructorProps {
6
4
  /** @zprop
7
5
  * @zvalues events
8
6
  */
9
7
  event: string;
10
- instance: InstanceOfComponent<any>;
11
8
  }
12
- export interface ActionBehaviorProps {
13
- dispose: () => void;
14
- }
15
- export interface PreviewableActionBehaviorProps extends ActionBehaviorProps {
16
- /** @zprop */
17
- preview: () => void;
9
+ export declare abstract class ActionBehavior<ConstructorProps extends ActionBehaviorConstructorProps = ActionBehaviorConstructorProps> extends Behavior<any, ConstructorProps> {
10
+ /**
11
+ * @zprop
12
+ * @zdefault true
13
+ */
14
+ enabled: boolean;
15
+ constructor(props: ConstructorProps, contextManager: ContextManager);
16
+ abstract perform(): any;
18
17
  }
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>;
@@ -1,32 +1,22 @@
1
- import { defineBehavior } from "./behavior";
1
+ import { Behavior } from "./behavior";
2
2
  import { isEditTime } from "./contexts/environmentcontext";
3
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);
4
+ export class ActionBehavior extends Behavior {
5
+ constructor(props, contextManager) {
6
+ super(props, contextManager);
7
+ /**
8
+ * @zprop
9
+ * @zdefault true
10
+ */
11
+ this.enabled = true;
12
+ const evt = props.instance[props.event];
13
+ if (evt instanceof Event && !isEditTime(contextManager)) {
14
+ this.register(evt, e => {
15
+ this.perform();
16
+ if (e && typeof e === 'object' && typeof e['stopPropagation'] === 'function') {
17
+ e['stopPropagation'].call(e);
16
18
  }
17
- upstreamHandler?.();
18
- };
19
- evt.bindfn(handler);
19
+ });
20
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);
21
+ }
32
22
  }
package/lib/behavior.d.ts CHANGED
@@ -1,26 +1,23 @@
1
- import { Component, InstanceOfComponent } from './component';
1
+ import { Component } from './component';
2
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;
3
+ import { Event } from './event';
4
+ import { Observable } from './observable';
5
+ export declare type BehaviorConstructor<BehaviorType = Behavior> = BehaviorType extends Behavior<any, infer ConstructorPropsType> ? new (props: ConstructorPropsType, contextManager: ContextManager) => BehaviorType : never;
6
+ export declare type ConstructorPropsOfBehavior<BehaviorType> = BehaviorType extends Behavior<infer R> ? R : never;
7
+ export interface BehaviorConstructorProps<InstanceType extends Component = Component> {
8
+ instance: InstanceType;
9
+ }
10
+ export declare class Behavior<InstanceType extends Component = any, ConstructorPropsType extends BehaviorConstructorProps<InstanceType> = BehaviorConstructorProps<InstanceType>> {
11
+ constructorProps: ConstructorPropsType;
12
+ contextManager: ContextManager;
13
+ private static callSuperDispose;
14
+ onDispose: Event<[]>;
15
+ instance: InstanceType;
16
+ private _registered;
17
+ constructor(constructorProps: ConstructorPropsType, contextManager: ContextManager);
18
+ register<Args extends Array<any>>(evt: Event<Args>, fn: (...args: Args) => void): any;
19
+ register<Type>(observable: Observable<Type>, fn: (v: Type) => void): any;
20
+ dispose(): never;
21
+ }
22
+ export declare function shouldBehaviorRunAtDesignTime(b: BehaviorConstructor): boolean;
23
+ export declare function registerBehaviorRunAtDesignTime(b: BehaviorConstructor): Set<new (props: BehaviorConstructorProps<any>, contextManager: ContextManager) => Behavior<any, BehaviorConstructorProps<any>>>;
package/lib/behavior.js CHANGED
@@ -1,10 +1,38 @@
1
+ import { Event } from './event';
2
+ import { Observable } from './observable';
3
+ export class Behavior {
4
+ constructor(constructorProps, contextManager) {
5
+ this.constructorProps = constructorProps;
6
+ this.contextManager = contextManager;
7
+ this.onDispose = new Event();
8
+ this._registered = [];
9
+ this.instance = this.constructorProps.instance;
10
+ }
11
+ register(e, fn) {
12
+ if (e instanceof Event)
13
+ e.bindfn(fn);
14
+ else if (e instanceof Observable)
15
+ e.withValue(fn);
16
+ this._registered.push([e, fn]);
17
+ }
18
+ dispose() {
19
+ for (const entry of this._registered) {
20
+ if (entry[0] instanceof Event)
21
+ entry[0].unbindfn(entry[1]);
22
+ else if (entry[0] instanceof Observable)
23
+ entry[0].removeWithValue(entry[1]);
24
+ }
25
+ this._registered = [];
26
+ this.onDispose.emit();
27
+ this.onDispose.clear();
28
+ return undefined;
29
+ }
30
+ }
31
+ Behavior.callSuperDispose = Symbol('Calling super.dispose() is mandatory');
1
32
  const behaviorsToRunAtDesignTime = new Set();
2
33
  export function shouldBehaviorRunAtDesignTime(b) {
3
34
  return behaviorsToRunAtDesignTime.has(b);
4
35
  }
5
- /** Register a new custom behavior */
6
- export function defineBehavior(c, runAtDesignTime = false) {
7
- if (runAtDesignTime)
8
- behaviorsToRunAtDesignTime.add(c);
9
- return c;
36
+ export function registerBehaviorRunAtDesignTime(b) {
37
+ return behaviorsToRunAtDesignTime.add(b);
10
38
  }
@@ -1,12 +1,23 @@
1
- export interface LaunchURLProps {
2
- /** @zprop */
1
+ import { ActionBehavior } from "../actionbehavior";
2
+ /**
3
+ * Navigates the user to the provided URL.
4
+ *
5
+ * **Important Note**: Safari on iOS will not open URLs in new tabs for pointer events except `onPointerUp`.
6
+ *
7
+ * @zbehavior
8
+ * @zgroup Actions
9
+ */
10
+ export declare class LaunchURL extends ActionBehavior {
11
+ /**
12
+ * The URL to open
13
+ * @zprop
14
+ */
3
15
  url: string;
4
- /** @zprop
16
+ /**
17
+ * If true, the URL will be opened in a new tab (using target '_blank')
18
+ * @zprop
5
19
  * @zdefault true
6
20
  */
7
21
  openInNew: boolean;
22
+ perform(): void;
8
23
  }
9
- /** @zbehavior
10
- * @zgroup Actions
11
- */
12
- export declare const LaunchURL: import("..").ConcreteBehavior<any, LaunchURLProps & import("../actionbehavior").ActionBehaviorConstructorProps, import("../actionbehavior").ActionBehaviorProps>;
@@ -1,12 +1,17 @@
1
- import { defineActionBehavior } from "../actionbehavior";
2
- /** @zbehavior
1
+ import { ActionBehavior } from "../actionbehavior";
2
+ /**
3
+ * Navigates the user to the provided URL.
4
+ *
5
+ * **Important Note**: Safari on iOS will not open URLs in new tabs for pointer events except `onPointerUp`.
6
+ *
7
+ * @zbehavior
3
8
  * @zgroup Actions
4
9
  */
5
- export const LaunchURL = defineActionBehavior((props) => {
6
- return () => {
7
- if (props.openInNew ?? true)
8
- window.open(props.url, '_blank', 'noopener noreferrer');
10
+ export class LaunchURL extends ActionBehavior {
11
+ perform() {
12
+ if (this.openInNew ?? true)
13
+ window.open(this.url, '_blank', 'noopener noreferrer');
9
14
  else
10
- window.location.href = props.url;
11
- };
12
- });
15
+ window.location.href = this.url;
16
+ }
17
+ }
@@ -1,10 +1,16 @@
1
- export interface LogAnalyticsEventProps {
1
+ import { ActionBehavior } from "../actionbehavior";
2
+ /**
3
+ * Logs an event through the currently configured analytics provider(s).
4
+ *
5
+ * Note: you must have added an analytics provider to the Hierarchy in one of your components for this behavior to have any effect.
6
+ *
7
+ * @zbehavior
8
+ * @zgroup Actions
9
+ */
10
+ export declare class LogAnalyticsEvent extends ActionBehavior {
2
11
  /** @zprop */
3
12
  name: string;
4
13
  /** @zprop */
5
14
  params: any;
15
+ perform(): void;
6
16
  }
7
- /** @zbehavior
8
- * @zgroup Actions
9
- */
10
- export declare const LogAnalyticsEvent: import("..").ConcreteBehavior<any, LogAnalyticsEventProps & import("../actionbehavior").ActionBehaviorConstructorProps, import("../actionbehavior").ActionBehaviorProps>;
@@ -1,10 +1,15 @@
1
- import { defineActionBehavior } from "../actionbehavior";
1
+ import { ActionBehavior } from "../actionbehavior";
2
2
  import { logEvent } from "../contexts/analyticscontext";
3
- /** @zbehavior
3
+ /**
4
+ * Logs an event through the currently configured analytics provider(s).
5
+ *
6
+ * Note: you must have added an analytics provider to the Hierarchy in one of your components for this behavior to have any effect.
7
+ *
8
+ * @zbehavior
4
9
  * @zgroup Actions
5
10
  */
6
- export const LogAnalyticsEvent = defineActionBehavior((props, mgr) => {
7
- return () => {
8
- logEvent(mgr, props.name, props.params);
9
- };
10
- });
11
+ export class LogAnalyticsEvent extends ActionBehavior {
12
+ perform() {
13
+ logEvent(this.contextManager, this.name, this.params);
14
+ }
15
+ }
@@ -1,4 +1,6 @@
1
- export interface PlaySoundProps {
1
+ import { ActionBehavior, ActionBehaviorConstructorProps } from "../actionbehavior";
2
+ import { ContextManager } from "../context";
3
+ export interface PlaySoundProps extends ActionBehaviorConstructorProps {
2
4
  /** The URL of the file to play
3
5
  * @zprop
4
6
  * @zvalues files *.wav
@@ -6,7 +8,17 @@ export interface PlaySoundProps {
6
8
  */
7
9
  source: string;
8
10
  }
9
- /** @zbehavior
11
+ /**
12
+ * Plays a sound
13
+ *
14
+ * @zbehavior
10
15
  * @zgroup Actions
11
16
  */
12
- export declare const PlaySound: import("..").ConcreteBehavior<any, PlaySoundProps & import("../actionbehavior").ActionBehaviorConstructorProps, import("../actionbehavior").PreviewableActionBehaviorProps>;
17
+ export declare class PlaySound extends ActionBehavior<PlaySoundProps> {
18
+ private _elm;
19
+ constructor(props: PlaySoundProps, contextManager: ContextManager);
20
+ perform(): void;
21
+ /** @zprop */
22
+ preview(): void;
23
+ dispose(): never;
24
+ }
@@ -1,18 +1,38 @@
1
- import { defineActionBehavior } from "../actionbehavior";
1
+ import { ActionBehavior, } from "../actionbehavior";
2
+ import { registerBehaviorRunAtDesignTime } from "../behavior";
2
3
  import { registerLoadable } from "../contexts/loadcontext";
3
- /** @zbehavior
4
+ /**
5
+ * Plays a sound
6
+ *
7
+ * @zbehavior
4
8
  * @zgroup Actions
5
9
  */
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);
10
+ export class PlaySound extends ActionBehavior {
11
+ constructor(props, contextManager) {
12
+ super(props, contextManager);
13
+ this._elm = document.createElement('audio');
14
+ this._elm.preload = 'auto';
15
+ registerLoadable(contextManager, new Promise((resolve, reject) => {
16
+ this._elm.addEventListener('canplaythrough', () => resolve());
17
+ this._elm.addEventListener('error', err => {
18
+ console.error('Unable to create PlaySound', err);
19
+ reject(err);
20
+ });
21
+ }));
22
+ this._elm.src = props.source;
23
+ }
24
+ perform() {
25
+ this._elm.currentTime = 0;
26
+ this._elm.play();
27
+ }
28
+ /** @zprop */
29
+ preview() {
30
+ this.perform();
31
+ }
32
+ dispose() {
33
+ this._elm.pause();
34
+ this._elm.src = '';
35
+ return super.dispose();
36
+ }
37
+ }
38
+ registerBehaviorRunAtDesignTime(PlaySound);
@@ -1,22 +1,30 @@
1
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;
2
+ import { Event } from './event';
3
+ import { Observable } from './observable';
4
+ export declare type ComponentConstructor<ComponentType = Component> = ComponentType extends Component<infer ConstructorPropsType> ? new (props: ConstructorPropsType, contextManager: ContextManager) => ComponentType : never;
5
+ export declare type ConstructorPropsOfComponent<ComponentType> = ComponentType extends Component<infer R> ? R : never;
6
+ export declare type ComponentChild<ComponentType extends Component> = [ComponentConstructor<ComponentType>, ConstructorPropsOfComponent<ComponentType>];
7
+ export declare type ComponentChildren = ComponentChild<any>[];
8
+ export interface ConstructorProps {
16
9
  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;
10
+ }
11
+ export interface ComponentOptions {
12
+ dontConstructChildren?: boolean;
13
+ }
14
+ export declare class Component<ConstructorPropsType extends ConstructorProps = ConstructorProps, RootType = any> {
15
+ constructorProps: ConstructorPropsType;
16
+ contextManager: ContextManager;
17
+ private _options?;
18
+ root?: RootType;
19
+ onDispose: Event<[]>;
20
+ children: Component[];
21
+ disposed: boolean;
22
+ private _registered;
23
+ constructor(constructorProps: ConstructorPropsType, contextManager: ContextManager, _options?: ComponentOptions | undefined);
24
+ forEachRoot(fn: (root: any) => void): void;
25
+ protected _constructChildren(children?: ComponentChildren): void;
26
+ protected register<Args extends Array<any>>(evt: Event<Args>, fn: (...args: Args) => void): any;
27
+ protected register<Type>(observable: Observable<Type>, fn: (v: Type) => void): any;
28
+ dispose(): never;
29
+ private static callSuperDispose;
30
+ }
package/lib/component.js CHANGED
@@ -1,4 +1,56 @@
1
- /** Register a new custom component */
2
- export function defineComponent(c) {
3
- return c;
1
+ import { Event } from './event';
2
+ import { Observable } from './observable';
3
+ export class Component {
4
+ constructor(constructorProps, contextManager, _options) {
5
+ this.constructorProps = constructorProps;
6
+ this.contextManager = contextManager;
7
+ this._options = _options;
8
+ this.onDispose = new Event();
9
+ this.children = [];
10
+ this.disposed = false;
11
+ this._registered = [];
12
+ if (this._options?.dontConstructChildren !== true)
13
+ this._constructChildren(this.constructorProps.children);
14
+ }
15
+ forEachRoot(fn) {
16
+ if (Array.isArray(this.root))
17
+ this.root.forEach(fn);
18
+ else if (this.root !== undefined)
19
+ fn(this.root);
20
+ }
21
+ _constructChildren(children) {
22
+ if (Array.isArray(children)) {
23
+ for (const child of children) {
24
+ this.children.push(new child[0](child[1], this.contextManager));
25
+ }
26
+ }
27
+ }
28
+ register(e, fn) {
29
+ if (e instanceof Event)
30
+ e.bindfn(fn);
31
+ else if (e instanceof Observable)
32
+ e.withValue(fn);
33
+ this._registered.push([e, fn]);
34
+ }
35
+ dispose() {
36
+ this.disposed = true;
37
+ for (const child of this.children) {
38
+ try {
39
+ child.dispose();
40
+ }
41
+ catch (err) { }
42
+ }
43
+ this.children = [];
44
+ for (const entry of this._registered) {
45
+ if (entry[0] instanceof Event)
46
+ entry[0].unbindfn(entry[1]);
47
+ else if (entry[0] instanceof Observable)
48
+ entry[0].removeWithValue(entry[1]);
49
+ }
50
+ this._registered = [];
51
+ this.onDispose.emit();
52
+ this.onDispose.clear();
53
+ return undefined;
54
+ }
4
55
  }
56
+ Component.callSuperDispose = Symbol('Calling super.dispose() is mandatory');
@@ -1,5 +1,5 @@
1
- import { Observable } from "../";
2
- export interface DefaultLoaderConstructorProps {
1
+ import { Component, Observable, ConstructorProps, ContextManager } from "..";
2
+ export interface DefaultLoaderConstructorProps extends ConstructorProps {
3
3
  /** @zprop
4
4
  * @zgroup Appearance
5
5
  * @zgrouppriority 10
@@ -16,23 +16,23 @@ export interface DefaultLoaderConstructorProps {
16
16
  * @zicon loader
17
17
  * @zgroup Advanced
18
18
  */
19
- export declare const DefaultLoader: (props: DefaultLoaderConstructorProps, mgr: import("../context").ContextManager) => {
20
- dispose: () => void;
19
+ export declare class DefaultLoader extends Component<DefaultLoaderConstructorProps> {
20
+ root: HTMLDivElement;
21
21
  /** @zprop
22
- * @zdefault false
23
- * @zgroup Design Time
24
- * @zgrouppriority 30
25
- */
26
- preview: Observable<boolean>;
22
+ * @zdefault false
23
+ * @zgroup Design Time
24
+ * @zgrouppriority 30
25
+ */
26
+ preview: Observable<boolean, never>;
27
27
  /** @zprop
28
28
  * @zgroup Text
29
29
  * @zgrouppriority 20
30
- */
30
+ */
31
31
  title: Observable<string>;
32
32
  /** @zprop
33
33
  * @zgroup Text
34
34
  * @zgrouppriority 20
35
- */
35
+ */
36
36
  subtitle: Observable<string>;
37
37
  /** @zprop
38
38
  * @zdefault black
@@ -47,9 +47,16 @@ export declare const DefaultLoader: (props: DefaultLoaderConstructorProps, mgr:
47
47
  */
48
48
  textColor: Observable<string>;
49
49
  /** @zprop
50
- * @zdefault 1000
51
- * @zgroup Appearance
52
- * @zgrouppriority 10
53
- */
50
+ * @zdefault 1000
51
+ * @zgroup Appearance
52
+ * @zgrouppriority 10
53
+ */
54
54
  zIndex: Observable<number>;
55
- };
55
+ private _percentageElement;
56
+ private _attached;
57
+ constructor(props: DefaultLoaderConstructorProps, contextManager: ContextManager);
58
+ private _updatePercentage;
59
+ private _updateVisibility;
60
+ private _update;
61
+ dispose(): never;
62
+ }