@bbq-chat/widgets-angular 1.0.8 → 1.0.9
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/.angular/cache/21.0.5/ng-packagr/97cbacd0e5e4cb18d1fead4d7f3aee1c3863ba3ffbe7cb7dd7780f237a848a5c +1 -0
- package/.angular/cache/21.0.5/ng-packagr/tsbuildinfo/index.tsbuildinfo +1 -0
- package/.eslintrc.json +23 -0
- package/.prettierrc.json +8 -0
- package/EXAMPLES.md +484 -0
- package/README.md +119 -0
- package/angular.json +36 -0
- package/ng-package.json +9 -0
- package/package.json +20 -15
- package/src/angular-widget-renderer.spec.ts +157 -0
- package/src/components/button.component.ts +35 -0
- package/src/components/card.component.ts +52 -0
- package/src/components/datepicker.component.ts +63 -0
- package/src/components/dropdown.component.ts +65 -0
- package/src/components/fileupload.component.ts +71 -0
- package/src/components/form.component.ts +433 -0
- package/src/components/image.component.ts +33 -0
- package/src/components/imagecollection.component.ts +39 -0
- package/src/components/index.ts +20 -0
- package/src/components/input.component.ts +63 -0
- package/src/components/multiselect.component.ts +67 -0
- package/src/components/progressbar.component.ts +50 -0
- package/src/components/slider.component.ts +67 -0
- package/src/components/textarea.component.ts +63 -0
- package/src/components/themeswitcher.component.ts +46 -0
- package/src/components/toggle.component.ts +63 -0
- package/src/custom-widget-renderer.types.ts +120 -0
- package/src/examples/form-validation-listener.component.ts +41 -0
- package/src/public_api.ts +107 -0
- package/src/renderers/AngularWidgetRenderer.ts +100 -0
- package/src/renderers/built-in-components.ts +41 -0
- package/src/renderers/index.ts +7 -0
- package/src/services/form-validation.service.ts +21 -0
- package/src/widget-di.tokens.ts +95 -0
- package/src/widget-registry.service.ts +128 -0
- package/src/widget-renderer.component.ts +421 -0
- package/tsconfig.json +37 -0
- package/tsconfig.lib.json +18 -0
- package/tsconfig.lib.prod.json +11 -0
- package/tsconfig.spec.json +13 -0
- package/fesm2022/index.mjs +0 -1992
- package/fesm2022/index.mjs.map +0 -1
- package/types/index.d.ts +0 -618
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Type } from '@angular/core';
|
|
2
|
+
import { CustomWidgetComponent } from '../custom-widget-renderer.types';
|
|
3
|
+
import {
|
|
4
|
+
ButtonWidgetComponent,
|
|
5
|
+
CardWidgetComponent,
|
|
6
|
+
InputWidgetComponent,
|
|
7
|
+
TextAreaWidgetComponent,
|
|
8
|
+
DropdownWidgetComponent,
|
|
9
|
+
SliderWidgetComponent,
|
|
10
|
+
ToggleWidgetComponent,
|
|
11
|
+
FileUploadWidgetComponent,
|
|
12
|
+
ThemeSwitcherWidgetComponent,
|
|
13
|
+
DatePickerWidgetComponent,
|
|
14
|
+
MultiSelectWidgetComponent,
|
|
15
|
+
ProgressBarWidgetComponent,
|
|
16
|
+
FormWidgetComponent,
|
|
17
|
+
ImageWidgetComponent,
|
|
18
|
+
ImageCollectionWidgetComponent,
|
|
19
|
+
} from '../components';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Registry of all built-in widget components
|
|
23
|
+
* Maps widget type to Angular component class
|
|
24
|
+
*/
|
|
25
|
+
export const BUILT_IN_WIDGET_COMPONENTS: Record<string, Type<CustomWidgetComponent>> = {
|
|
26
|
+
button: ButtonWidgetComponent,
|
|
27
|
+
card: CardWidgetComponent,
|
|
28
|
+
input: InputWidgetComponent,
|
|
29
|
+
textarea: TextAreaWidgetComponent,
|
|
30
|
+
dropdown: DropdownWidgetComponent,
|
|
31
|
+
slider: SliderWidgetComponent,
|
|
32
|
+
toggle: ToggleWidgetComponent,
|
|
33
|
+
fileupload: FileUploadWidgetComponent,
|
|
34
|
+
themeswitcher: ThemeSwitcherWidgetComponent,
|
|
35
|
+
datepicker: DatePickerWidgetComponent,
|
|
36
|
+
multiselect: MultiSelectWidgetComponent,
|
|
37
|
+
progressbar: ProgressBarWidgetComponent,
|
|
38
|
+
form: FormWidgetComponent,
|
|
39
|
+
image: ImageWidgetComponent,
|
|
40
|
+
imagecollection: ImageCollectionWidgetComponent,
|
|
41
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { Observable, Subject } from 'rxjs';
|
|
3
|
+
|
|
4
|
+
export interface FormValidationEvent {
|
|
5
|
+
formId: string;
|
|
6
|
+
valid: boolean;
|
|
7
|
+
errors: Array<{ field: string; reason?: string }>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
@Injectable({ providedIn: 'root' })
|
|
11
|
+
export class FormValidationService {
|
|
12
|
+
private subject = new Subject<FormValidationEvent>();
|
|
13
|
+
|
|
14
|
+
get validation$(): Observable<FormValidationEvent> {
|
|
15
|
+
return this.subject.asObservable();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
emit(event: FormValidationEvent) {
|
|
19
|
+
try { this.subject.next(event); } catch { }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { InjectionToken } from '@angular/core';
|
|
2
|
+
import { SsrWidgetRenderer, WidgetEventManager, IWidgetActionHandler } from '@bbq-chat/widgets';
|
|
3
|
+
import { AngularWidgetRenderer } from './renderers/AngularWidgetRenderer';
|
|
4
|
+
import { BUILT_IN_WIDGET_COMPONENTS } from './renderers/built-in-components';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Injection token for WidgetEventManager factory
|
|
8
|
+
*
|
|
9
|
+
* Use this token to inject a factory function that creates WidgetEventManager instances.
|
|
10
|
+
* The factory accepts an optional action handler to configure the manager.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* constructor(@Inject(WIDGET_EVENT_MANAGER_FACTORY) private eventManagerFactory: WidgetEventManagerFactory) {
|
|
15
|
+
* const actionHandler = { handle: async (action, payload) => { ... } };
|
|
16
|
+
* this.eventManager = this.eventManagerFactory(actionHandler);
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type WidgetEventManagerFactory = (actionHandler?: IWidgetActionHandler) => WidgetEventManager;
|
|
21
|
+
|
|
22
|
+
export const WIDGET_EVENT_MANAGER_FACTORY = new InjectionToken<WidgetEventManagerFactory>(
|
|
23
|
+
'WIDGET_EVENT_MANAGER_FACTORY'
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Injection token for SsrWidgetRenderer
|
|
28
|
+
*
|
|
29
|
+
* Use this token to inject a SsrWidgetRenderer instance in your components.
|
|
30
|
+
* By default, WidgetRendererComponent provides this token with a factory that creates
|
|
31
|
+
* a new instance for each component.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* constructor(@Inject(SSR_WIDGET_RENDERER) private renderer: SsrWidgetRenderer) {}
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const SSR_WIDGET_RENDERER = new InjectionToken<SsrWidgetRenderer>(
|
|
39
|
+
'SSR_WIDGET_RENDERER'
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Injection token for AngularWidgetRenderer
|
|
44
|
+
*
|
|
45
|
+
* Use this token to inject an AngularWidgetRenderer instance in your components.
|
|
46
|
+
* This is the recommended renderer for Angular applications as it provides
|
|
47
|
+
* native Angular component rendering instead of HTML string rendering.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* constructor(@Inject(ANGULAR_WIDGET_RENDERER) private renderer: AngularWidgetRenderer) {}
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export const ANGULAR_WIDGET_RENDERER = new InjectionToken<AngularWidgetRenderer>(
|
|
55
|
+
'ANGULAR_WIDGET_RENDERER'
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Factory function for creating WidgetEventManager instances
|
|
60
|
+
*
|
|
61
|
+
* This factory is used by default in WidgetRendererComponent's providers array.
|
|
62
|
+
* You can override this in your own providers if you need custom initialization.
|
|
63
|
+
*
|
|
64
|
+
* @returns A factory function that creates WidgetEventManager instances
|
|
65
|
+
*/
|
|
66
|
+
export function widgetEventManagerFactoryProvider(): WidgetEventManagerFactory {
|
|
67
|
+
return (actionHandler?: IWidgetActionHandler) => new WidgetEventManager(actionHandler);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Factory function for creating SsrWidgetRenderer instances
|
|
72
|
+
*
|
|
73
|
+
* This factory is used by default in WidgetRendererComponent's providers array.
|
|
74
|
+
* You can override this in your own providers if you need custom initialization
|
|
75
|
+
* or custom rendering options.
|
|
76
|
+
*
|
|
77
|
+
* @returns A new SsrWidgetRenderer instance
|
|
78
|
+
*/
|
|
79
|
+
export function ssrWidgetRendererFactory(): SsrWidgetRenderer {
|
|
80
|
+
return new SsrWidgetRenderer();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Factory function for creating AngularWidgetRenderer instances
|
|
85
|
+
*
|
|
86
|
+
* This factory creates an AngularWidgetRenderer with all built-in widget components
|
|
87
|
+
* pre-registered. This is the recommended renderer for Angular applications.
|
|
88
|
+
*
|
|
89
|
+
* @returns A new AngularWidgetRenderer instance with built-in components registered
|
|
90
|
+
*/
|
|
91
|
+
export function angularWidgetRendererFactory(): AngularWidgetRenderer {
|
|
92
|
+
const renderer = new AngularWidgetRenderer();
|
|
93
|
+
renderer.registerBuiltInComponents(BUILT_IN_WIDGET_COMPONENTS);
|
|
94
|
+
return renderer;
|
|
95
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { customWidgetRegistry, ChatWidget } from '@bbq-chat/widgets';
|
|
3
|
+
import { CustomWidgetRenderer } from './custom-widget-renderer.types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Service for registering custom widget factories and renderers
|
|
7
|
+
*
|
|
8
|
+
* This service provides a centralized way to register custom widget types
|
|
9
|
+
* that extend the base widget functionality, including support for
|
|
10
|
+
* Angular components and templates as custom renderers.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* constructor(private widgetRegistry: WidgetRegistryService) {
|
|
15
|
+
* // Register a widget factory
|
|
16
|
+
* this.widgetRegistry.registerFactory('myWidget', (obj) => {
|
|
17
|
+
* if (obj.type === 'myWidget') {
|
|
18
|
+
* return new MyCustomWidget(obj.label, obj.action);
|
|
19
|
+
* }
|
|
20
|
+
* return null;
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Register a component-based renderer
|
|
24
|
+
* this.widgetRegistry.registerRenderer('myWidget', MyWidgetComponent);
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
@Injectable({
|
|
29
|
+
providedIn: 'root',
|
|
30
|
+
})
|
|
31
|
+
export class WidgetRegistryService {
|
|
32
|
+
private readonly customRenderers = new Map<string, CustomWidgetRenderer>();
|
|
33
|
+
/**
|
|
34
|
+
* Register a custom widget factory function
|
|
35
|
+
*
|
|
36
|
+
* @param type - The widget type identifier
|
|
37
|
+
* @param factory - Factory function that creates widget instances from plain objects
|
|
38
|
+
*/
|
|
39
|
+
registerFactory(
|
|
40
|
+
type: string,
|
|
41
|
+
factory: (obj: unknown) => ChatWidget | null
|
|
42
|
+
): void {
|
|
43
|
+
customWidgetRegistry.registerFactory(type, factory);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Register a widget class with automatic factory creation
|
|
48
|
+
*
|
|
49
|
+
* @param type - The widget type identifier
|
|
50
|
+
* @param ctor - Widget class constructor
|
|
51
|
+
*/
|
|
52
|
+
registerClass(type: string, ctor: any): void {
|
|
53
|
+
customWidgetRegistry.registerClass(type, ctor);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get a factory for a specific widget type
|
|
58
|
+
*
|
|
59
|
+
* @param type - The widget type identifier
|
|
60
|
+
* @returns The factory function if registered, undefined otherwise
|
|
61
|
+
*/
|
|
62
|
+
getFactory(type: string): ((obj: any) => ChatWidget | null) | undefined {
|
|
63
|
+
return customWidgetRegistry.getFactory(type);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Register a custom renderer for a specific widget type
|
|
68
|
+
*
|
|
69
|
+
* The renderer can be:
|
|
70
|
+
* - A function that returns HTML string
|
|
71
|
+
* - An Angular Component class
|
|
72
|
+
* - An Angular TemplateRef
|
|
73
|
+
*
|
|
74
|
+
* @param type - The widget type identifier
|
|
75
|
+
* @param renderer - The custom renderer (function, Component, or TemplateRef)
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* // HTML function renderer
|
|
80
|
+
* widgetRegistry.registerRenderer('weather', (widget) => `<div>${widget.label}</div>`);
|
|
81
|
+
*
|
|
82
|
+
* // Component renderer
|
|
83
|
+
* widgetRegistry.registerRenderer('weather', WeatherWidgetComponent);
|
|
84
|
+
*
|
|
85
|
+
* // Template renderer (from @ViewChild or elsewhere)
|
|
86
|
+
* widgetRegistry.registerRenderer('weather', this.weatherTemplate);
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
registerRenderer(type: string, renderer: CustomWidgetRenderer): void {
|
|
90
|
+
if (!type || typeof type !== 'string') {
|
|
91
|
+
throw new Error('type must be a non-empty string');
|
|
92
|
+
}
|
|
93
|
+
if (!renderer) {
|
|
94
|
+
throw new Error('renderer is required');
|
|
95
|
+
}
|
|
96
|
+
this.customRenderers.set(type, renderer);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get a custom renderer for a specific widget type
|
|
101
|
+
*
|
|
102
|
+
* @param type - The widget type identifier
|
|
103
|
+
* @returns The custom renderer if registered, undefined otherwise
|
|
104
|
+
*/
|
|
105
|
+
getRenderer(type: string): CustomWidgetRenderer | undefined {
|
|
106
|
+
return this.customRenderers.get(type);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Check if a custom renderer is registered for a widget type
|
|
111
|
+
*
|
|
112
|
+
* @param type - The widget type identifier
|
|
113
|
+
* @returns True if a custom renderer is registered, false otherwise
|
|
114
|
+
*/
|
|
115
|
+
hasRenderer(type: string): boolean {
|
|
116
|
+
return this.customRenderers.has(type);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Unregister a custom renderer for a widget type
|
|
121
|
+
*
|
|
122
|
+
* @param type - The widget type identifier
|
|
123
|
+
* @returns True if a renderer was removed, false if none was registered
|
|
124
|
+
*/
|
|
125
|
+
unregisterRenderer(type: string): boolean {
|
|
126
|
+
return this.customRenderers.delete(type);
|
|
127
|
+
}
|
|
128
|
+
}
|