@kaaiot/services
Version:
Type definitions for KaaIoT platform REST API service communication
162 lines (161 loc) • 6.72 kB
TypeScript
import { KaaFormSchema as FormSchema, KaaFormUISchema as FormUISchema } from "../../shared/types/forms";
import { ThemeConfiguration } from "../../shared/clients/theme/ThemeTypes";
export declare enum UpdateStrategy {
Replace = "replace",
Merge = "merge",
ReplaceValue = "ReplaceValue",
ReplaceDeep = "ReplaceDeep"
}
export interface WidgetMountElement {
rootNodeId: string;
rootNodeElement: HTMLDivElement;
}
export declare enum WidgetEvent {
ExportRegistered = "export.registered",
EditModeEnabled = "edit.mode.enabled",
ConfigViewEnabled = "config.view.enabled",
DashboardVariablesChange = "dashboard.variables.change"
}
export interface WidgetEventsSchema {
[WidgetEvent.ExportRegistered]: undefined;
[WidgetEvent.EditModeEnabled]: boolean;
[WidgetEvent.ConfigViewEnabled]: boolean;
[WidgetEvent.DashboardVariablesChange]: Record<string, any>;
}
export declare type ExportDataFunction = () => Promise<Blob>;
export declare type OnResizeCallback = () => void;
export declare type OnConfigChangeCallback<T = any> = (nextConfig: T, prevConfig: T) => void;
export declare type OnDestroyCallback = () => void;
export interface WidgetExportSummary {
rowsCount: number;
}
export interface WidgetServiceOpenLinkOptions {
newTab?: boolean;
}
/**
* External widget public contract
*/
export interface WidgetServiceProvider<Config = Record<string, any>> {
getTenantId(): string;
getWidgetId(): string;
getExportData(): Promise<Blob>;
/**
* Register a callback to be called when the export data is requested
* @param callback - The callback to be called when the export data is requested
*/
registerExportCallback(callback: ExportDataFunction): void;
/**
* Register a callback to be called when an event occurs
* @param eventName - The event name
* @param callback - The callback to be called when the event occurs
*/
onEvent<E extends WidgetEvent>(eventName: E, callback: (data?: WidgetEventsSchema[E]) => void): void;
canExport(): boolean;
/**
* Set the export summary
* @param summary - The export summary
*/
setExportSummary(summary: WidgetExportSummary): void;
/**
* Get the export summary
* @returns The export summary
*/
getExportSummary(): WidgetExportSummary;
/**
* Get the edit mode status
* @returns The edit mode status
*/
getEditModeStatus(): boolean;
/**
* Get the widget config. The configuration will be automatically populated with the dashboard variables.
* @returns The widget config
*/
getWidgetConfig(): Config | null;
/**
* Get the raw widget config. The configuration will not be automatically populated with the dashboard variables.
* @returns The raw widget config
*/
getRawWidgetConfig(): Config | null;
/**
* Get the theme colors currently used by the dashboard
* @returns The theme
*/
getTheme(): ThemeConfiguration;
/**
* Get the dashboard variables
* @returns The dashboard variables
*/
getDashboardVariables(): Record<string, any>;
/**
* Destroy the widget
*/
destroy(): void;
/**
* Register a callback to be called when the widget is resized
* @param callback - The callback to be called when the widget is resized
*/
onResize(callback: OnResizeCallback): void;
onDestroy(callback: OnDestroyCallback): void;
/**
* Register a callback to be called when the widget config changes. You will receive the previous and next config.
* Configuration is automatically populated with the dashboard variables.
* @param callback - The callback to be called when the widget config changes
*/
onConfigurationChange<T>(callback: OnConfigChangeCallback<T>): void;
/**
* Update variables in the widget
* @param variables - Variables to update
* @param scope - Scope of the variables can be 'dashboard' or 'widget'. Dashboard is the default scope.
* @param updateStrategy - Update strategy. Can be 'replace', 'merge', 'replaceValue', 'replaceDeep'. Merge is the default strategy.
*/
updateVariables(variables: Record<string, any>, scope?: 'dashboard' | 'widget', updateStrategy?: UpdateStrategy): void;
/**
* Open a link. You can use relative path to open a link without page reload with specific prefixes:
* - /dashboards - Open a link to a dashboard. In this case the current solution will be used to open the dashboard.
* - /solutions - Open a link to a solution. In this case the solution id will be extracted from the url and used to open the solution.
* @param url - URL to open
* @param options - Options
*/
openLink(url: string, options?: WidgetServiceOpenLinkOptions): void;
/**
* Get a translated string.
* @param key - Key of the string to translate
* @param variables - Variables to replace in the string
* @returns The translated string
*/
getStringTranslation(key: string, variables?: Record<string, string>): string;
/**
* Get the current timezone. Returns either the timezone from the user profile or the default timezone if the user timezone is not set.
* @returns The current timezone
*/
getCurrentTimezone(): string | null;
/**
* Format a date
* @param date - Date to format according to the current timezone
* @param forbidEmpty - If true, the date will be formatted even if it is empty
* @returns The formatted date
*/
formatDate(date: string | number, forbidEmpty?: boolean): string;
isIamcoreIntegrationEnabled(): boolean;
/**
* Update the widget config. Usually the configuration is updated automatically when the widget is configured in the Configuration Dialog.
* However, in some cases, you might need to update the configuration from the widget code to provide some default values that makes sense on creating the widget.
* @param configuration - Configuration to update
*/
updateWidgetConfig(configuration: Config): void;
}
export interface WidgetModuleContext<Config = Record<string, any>> {
widgetService: WidgetServiceProvider<Config>;
configuration: Config;
}
export interface WidgetModuleConfigurationSchema {
schema: FormSchema;
uiSchema: FormUISchema;
}
export declare type WidgetMount<Config = Record<string, any>> = (element: WidgetMountElement, context: WidgetModuleContext<Config>) => void;
export interface WidgetModule {
mount: WidgetMount;
unmount?: (widgetId: string) => void;
bootstrap?: () => Promise<void> | void;
configurationSchema?: () => WidgetModuleConfigurationSchema;
}