UNPKG

4.07 kBTypeScriptView Raw
1import Vizzu from './vizzu.js';
2import * as Anim from './types/anim.js';
3import { Events, EventType, EventHandler, EventMap } from './events.js';
4import { AnimCompleting } from './animcompleting.js';
5/** Available hooks for plugins in Vizzu. */
6export declare enum Hooks {
7 /** Called when the animate() parameters gets set in the library to prepare
8 the animation. */
9 prepareAnimation = "prepareAnimation",
10 /** Called when the animate() method called, and the lib shedules the call
11 to the animation queue. */
12 registerAnimation = "registerAnimation",
13 /** Called when all animate() parameter set and animation can be started. */
14 runAnimation = "runAnimation"
15}
16/** Plugin metainfo. */
17export interface PluginMeta {
18 /** Name of the plugin. If not set, the class name will be used
19 with a lowercase first char. */
20 name?: string;
21 /** Version of the plugin. Not used for now. */
22 version?: string;
23 /** List of plugins this plugin depends on.
24 Dependent plugins should be registered beforehand. */
25 depends?: string[];
26}
27export interface PrepareAnimationContext {
28 target: Anim.AnimTarget;
29 options?: Anim.ControlOptions;
30}
31export interface RegisterAnimationContext {
32 target: Anim.AnimTarget;
33 options?: Anim.ControlOptions;
34 promise: AnimCompleting;
35}
36export interface RunAnimationContext {
37 callback: (ok: boolean) => void;
38}
39export interface HookContexts {
40 [Hooks.prepareAnimation]: PrepareAnimationContext;
41 [Hooks.registerAnimation]: RegisterAnimationContext;
42 [Hooks.runAnimation]: RunAnimationContext;
43}
44type Next = () => void;
45/** Plugin hook implementation. Plugin hooks gets called by Vizzu in
46 priority order. Each hook should call the next hook in the chain,
47 or the default implementation won't run. */
48type PluginHook<T> = {
49 (ctx: T, next: Next): void;
50 priority?: number;
51};
52/** Set of plugin hook implementations. */
53export type PluginHooks<T extends Hooks = Hooks> = {
54 [key in T]?: PluginHook<HookContexts[key]>;
55};
56export type PluginListeners = {
57 [event in EventType]?: EventHandler<EventMap[event]>;
58};
59export interface PluginApi {
60 [apiMethod: string]: unknown;
61}
62/** Vizzu plugin interface. */
63export interface Plugin {
64 /** Metainfo about the plugin. */
65 meta?: PluginMeta;
66 /** Hooks the plugin implemenst. They work only if the plugin is enabled. */
67 hooks?: PluginHooks;
68 /** Event listeners the plugin implements. They work only if the plugin is enabled. */
69 listeners?: PluginListeners;
70 /** Any parameter or method the Plugin exposes for the user. */
71 api?: PluginApi;
72 /** Register called when the plugin added to vizzu, receiving the Vizzu instance. */
73 register?: (ctx: Vizzu) => void;
74 /** Unregister is called when detach() called on the Vizzu instance. */
75 unregister?: (ctx: Vizzu) => void;
76 /** Method called by Vizzu indicating for the plugin that it got switched on/off.
77 If switched off, its event listeners gets removed and its hooks won't be called. */
78 enable?: (enabled: boolean) => void;
79}
80interface HookExecutor<T extends Hooks> {
81 default: (last?: (ctx: HookContexts[T]) => void) => void;
82}
83export declare class PluginRegistry {
84 private _parent;
85 private _plugins;
86 private _events?;
87 constructor(parent: Vizzu, plugins?: Plugin[]);
88 init(events: Events): void;
89 enable(name: string, enabled: boolean): void;
90 getRegisteredName(instance: Plugin): string | undefined;
91 register(instance: Plugin, enabled?: boolean): string;
92 destruct(): void;
93 unregister(name: string): void;
94 api(name: string): PluginApi;
95 hook<T extends Hooks>(type: T, ctx: HookContexts[T]): HookExecutor<T>;
96 private _setEnabled;
97 private _enableEvents;
98 private _disableEvents;
99 private _validate;
100 private _validateName;
101 private _discoverName;
102 private _firstToLower;
103 private _validateDepends;
104 private _getByName;
105 private _exec;
106 private _getHooks;
107 private _executeHooks;
108}
109export {};