@agnos-ui/angular-headless 0.8.1 → 0.9.0-next.0

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.
Files changed (50) hide show
  1. package/fesm2022/agnos-ui-angular-headless.mjs +169 -164
  2. package/fesm2022/agnos-ui-angular-headless.mjs.map +1 -1
  3. package/index.d.ts +439 -1
  4. package/package.json +4 -4
  5. package/components/toast/index.d.ts +0 -2
  6. package/components/toast/toaster.service.d.ts +0 -30
  7. package/config.d.ts +0 -47
  8. package/generated/components/accordion/index.d.ts +0 -1
  9. package/generated/components/alert/index.d.ts +0 -1
  10. package/generated/components/carousel/index.d.ts +0 -1
  11. package/generated/components/modal/index.d.ts +0 -1
  12. package/generated/components/pagination/index.d.ts +0 -1
  13. package/generated/components/progressbar/index.d.ts +0 -1
  14. package/generated/components/rating/index.d.ts +0 -1
  15. package/generated/components/select/index.d.ts +0 -1
  16. package/generated/components/slider/index.d.ts +0 -1
  17. package/generated/components/toast/index.d.ts +0 -1
  18. package/generated/components/tree/index.d.ts +0 -1
  19. package/generated/config.d.ts +0 -1
  20. package/generated/index.d.ts +0 -35
  21. package/generated/services/extendWidget.d.ts +0 -1
  22. package/generated/services/floatingUI.d.ts +0 -1
  23. package/generated/services/focustrack.d.ts +0 -1
  24. package/generated/services/hash.d.ts +0 -1
  25. package/generated/services/intersection.d.ts +0 -1
  26. package/generated/services/matchMedia.d.ts +0 -1
  27. package/generated/services/navManager.d.ts +0 -1
  28. package/generated/services/portal.d.ts +0 -1
  29. package/generated/services/resizeObserver.d.ts +0 -1
  30. package/generated/services/siblingsInert.d.ts +0 -1
  31. package/generated/services/transitions/baseTransitions.d.ts +0 -1
  32. package/generated/services/transitions/collapse.d.ts +0 -1
  33. package/generated/services/transitions/cssTransitions.d.ts +0 -1
  34. package/generated/services/transitions/simpleClassTransition.d.ts +0 -1
  35. package/generated/slot.directive.d.ts +0 -1
  36. package/generated/types.d.ts +0 -1
  37. package/generated/utils/coercion.d.ts +0 -1
  38. package/generated/utils/directive.d.ts +0 -1
  39. package/generated/utils/func.d.ts +0 -1
  40. package/generated/utils/stores.d.ts +0 -1
  41. package/generated/utils/widget.d.ts +0 -1
  42. package/generated/utils/writables.d.ts +0 -1
  43. package/generated/utils/zone.d.ts +0 -1
  44. package/slot.directive.d.ts +0 -34
  45. package/types.d.ts +0 -116
  46. package/utils/coercion.d.ts +0 -20
  47. package/utils/directive.d.ts +0 -54
  48. package/utils/stores.d.ts +0 -31
  49. package/utils/widget.d.ts +0 -62
  50. package/utils/zone.d.ts +0 -26
@@ -13,18 +13,19 @@ export * from '@agnos-ui/core/services/transitions/cssTransitions';
13
13
  export * from '@agnos-ui/core/services/transitions/collapse';
14
14
  export * from '@agnos-ui/core/services/transitions/baseTransitions';
15
15
  export * from '@agnos-ui/core/utils/writables';
16
+ import { writable, computed } from '@amadeus-it-group/tansu';
16
17
  import * as i0 from '@angular/core';
17
- import { inject, NgZone, Injectable, signal, DestroyRef, Injector, ElementRef, PLATFORM_ID, runInInjectionContext, afterNextRender, input, Directive, computed, InjectionToken, SkipSelf, Optional, booleanAttribute, numberAttribute, viewChild, ChangeDetectionStrategy, Component, createComponent, EnvironmentInjector, TemplateRef, reflectComponentType, ViewContainerRef } from '@angular/core';
18
+ import { inject, NgZone, Injectable, signal, DestroyRef, Injector, runInInjectionContext, Directive, ElementRef, PLATFORM_ID, afterNextRender, input, computed as computed$1, InjectionToken, SkipSelf, Optional, booleanAttribute, numberAttribute, viewChild, ChangeDetectionStrategy, Component, createComponent, EnvironmentInjector, TemplateRef, reflectComponentType, ViewContainerRef } from '@angular/core';
18
19
  import { toReadableStore } from '@agnos-ui/core/utils/stores';
19
20
  export * from '@agnos-ui/core/utils/stores';
20
21
  export * from '@agnos-ui/core/utils/func';
21
22
  import { multiDirective } from '@agnos-ui/core/utils/directive';
22
23
  export * from '@agnos-ui/core/utils/directive';
23
24
  import { isPlatformServer } from '@angular/common';
25
+ import { FACTORY_WIDGET_NAME } from '@agnos-ui/core/types';
24
26
  export * from '@agnos-ui/core/types';
25
27
  import { createWidgetsConfig } from '@agnos-ui/core/config';
26
28
  export * from '@agnos-ui/core/config';
27
- import { writable, computed as computed$1 } from '@amadeus-it-group/tansu';
28
29
  export * from '@agnos-ui/core/components/tree';
29
30
  import { Toaster } from '@agnos-ui/core/components/toast';
30
31
  export * from '@agnos-ui/core/components/toast';
@@ -165,6 +166,154 @@ const toAngularWritableSignal = (tansuSignal) => {
165
166
  return res;
166
167
  };
167
168
 
169
+ const createPatchSlots = (set) => {
170
+ let lastValue = {};
171
+ return (object) => {
172
+ const newValue = {};
173
+ let hasChange = false;
174
+ for (const key of Object.keys(object)) {
175
+ const objectKey = object[key];
176
+ if (objectKey != null) {
177
+ // only use defined slots
178
+ newValue[key] = objectKey;
179
+ }
180
+ if (objectKey != lastValue[key]) {
181
+ hasChange = true;
182
+ }
183
+ }
184
+ if (hasChange) {
185
+ lastValue = newValue;
186
+ set(newValue);
187
+ }
188
+ };
189
+ };
190
+ /**
191
+ * Call a widget factory using provided configs.
192
+ *
193
+ * The resulting widget can be easily hooked into the lifecycle of an Angular component through {@link BaseWidgetDirective}.
194
+ *
195
+ * @param factory - the widget factory to call
196
+ * @param options - the options
197
+ * @param options.defaultConfig - the default config of the widget
198
+ * @param options.widgetConfig - the config of the widget, overriding the defaultConfig
199
+ * @param options.events - the events of the widget
200
+ * @param options.afterInit - a callback to call after successful setup of the widget
201
+ * @param options.slotTemplates - a function to provide all slot templates using child queries
202
+ * @param options.slotChildren - a function to provide the default children slot using a view query
203
+ * @returns the widget
204
+ */
205
+ const callWidgetFactoryWithConfig = (factory, options) => {
206
+ let { defaultConfig, widgetConfig, events, afterInit, slotTemplates, slotChildren } = options ?? {};
207
+ const injector = inject(Injector);
208
+ const slots$ = writable({});
209
+ const props = {};
210
+ let initDone;
211
+ const patchSlots = createPatchSlots(slots$.set);
212
+ const res = {
213
+ initialized: new Promise((resolve) => {
214
+ initDone = resolve;
215
+ }),
216
+ updateSlots: () => {
217
+ if (slotTemplates) {
218
+ patchSlots(slotTemplates());
219
+ }
220
+ },
221
+ patch(newProps) {
222
+ // temporary function replaced in ngInit
223
+ Object.assign(props, newProps);
224
+ },
225
+ ngInit() {
226
+ runInInjectionContext(injector, () => {
227
+ const zoneWrapper = inject(ZoneWrapper);
228
+ factory = zoneWrapper.outsideNgZone(factory);
229
+ const defaultConfig$ = toReadableStore(defaultConfig);
230
+ events = zoneWrapper.insideNgZoneWrapFunctionsObject(events);
231
+ const widget = factory({
232
+ config: computed(() => ({
233
+ ...defaultConfig$(),
234
+ children: slotChildren?.(),
235
+ ...widgetConfig?.(),
236
+ ...slots$(),
237
+ ...events,
238
+ })),
239
+ props,
240
+ });
241
+ Object.assign(res, {
242
+ patch: zoneWrapper.outsideNgZone(widget.patch),
243
+ directives: zoneWrapper.outsideNgZoneWrapDirectivesObject(widget.directives),
244
+ api: zoneWrapper.outsideNgZoneWrapFunctionsObject(widget.api),
245
+ state: Object.fromEntries(Object.entries(widget.stores).map(([key, val]) => [key.slice(0, -1), toAngularSignal(val)])),
246
+ });
247
+ afterInit?.(res);
248
+ initDone();
249
+ });
250
+ },
251
+ };
252
+ return res;
253
+ };
254
+ function patchSimpleChanges(patchFn, changes) {
255
+ const obj = {};
256
+ for (const [key, simpleChange] of Object.entries(changes)) {
257
+ if (simpleChange !== undefined) {
258
+ obj[key] = simpleChange.currentValue;
259
+ }
260
+ }
261
+ patchFn(obj);
262
+ }
263
+ /**
264
+ * An abstract base class for widget directives, providing common functionality
265
+ * for Angular components that interact with widgets.
266
+ *
267
+ * @template W - The type of the widget.
268
+ */
269
+ class BaseWidgetDirective {
270
+ // eslint-disable-next-line @angular-eslint/prefer-inject
271
+ constructor(_widget) {
272
+ this._widget = _widget;
273
+ }
274
+ /**
275
+ * Retrieves the widget api
276
+ * @returns the widget api
277
+ */
278
+ get api() {
279
+ return this._widget.api;
280
+ }
281
+ /**
282
+ * Retrieves the widget state. Each property of the state is exposed through an Angular {@link https://angular.dev/api/core/Signal | Signal}
283
+ * @returns the widget state
284
+ */
285
+ get state() {
286
+ return this._widget.state;
287
+ }
288
+ /**
289
+ * Retrieves the widget directives
290
+ * @returns the widget directives
291
+ */
292
+ get directives() {
293
+ return this._widget.directives;
294
+ }
295
+ /**
296
+ * @inheritdoc
297
+ * @internal
298
+ */
299
+ ngOnChanges(changes) {
300
+ patchSimpleChanges(this._widget.patch, changes);
301
+ }
302
+ /** @internal */
303
+ ngOnInit() {
304
+ this._widget.ngInit();
305
+ }
306
+ /** @internal */
307
+ ngAfterContentChecked() {
308
+ this._widget.updateSlots();
309
+ }
310
+ static { this.ɵfac = function BaseWidgetDirective_Factory(__ngFactoryType__) { i0.ɵɵinvalidFactory(); }; }
311
+ static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: BaseWidgetDirective, features: [i0.ɵɵNgOnChangesFeature] }); }
312
+ }
313
+ (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(BaseWidgetDirective, [{
314
+ type: Directive
315
+ }], () => [{ type: undefined }], null); })();
316
+
168
317
  /**
169
318
  * A utility function to manage the lifecycle of a directive for a host element.
170
319
  *
@@ -319,7 +468,7 @@ class SlotComponent {
319
468
  /**
320
469
  * The slot context, to be used when the slot component uses other slots.
321
470
  */
322
- this.slotContext = computed(() => ({ state: this.state(), api: this.api(), directives: this.directives() }));
471
+ this.slotContext = computed$1(() => ({ state: this.state(), api: this.api(), directives: this.directives() }));
323
472
  }
324
473
  static { this.ɵfac = function SlotComponent_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || SlotComponent)(); }; }
325
474
  static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: SlotComponent, inputs: { state: [1, "state"], api: [1, "api"], directives: [1, "directives"] } }); }
@@ -328,150 +477,6 @@ class SlotComponent {
328
477
  type: Directive
329
478
  }], null, null); })();
330
479
 
331
- const createPatchSlots = (set) => {
332
- let lastValue = {};
333
- return (object) => {
334
- const newValue = {};
335
- let hasChange = false;
336
- for (const key of Object.keys(object)) {
337
- const objectKey = object[key];
338
- if (objectKey != null) {
339
- // only use defined slots
340
- newValue[key] = objectKey;
341
- }
342
- if (objectKey != lastValue[key]) {
343
- hasChange = true;
344
- }
345
- }
346
- if (hasChange) {
347
- lastValue = newValue;
348
- set(newValue);
349
- }
350
- };
351
- };
352
- /**
353
- * Call a widget factory using provided configs.
354
- *
355
- * @param parameter - the parameter
356
- * @param parameter.factory - the widget factory to call
357
- * @param parameter.defaultConfig - the default config of the widget
358
- * @param parameter.widgetConfig - the config of the widget, overriding the defaultConfig
359
- * @param parameter.events - the events of the widget
360
- * @param parameter.afterInit - a callback to call after successful setup of the widget
361
- * @param parameter.slotTemplates - a function to provide all slot templates using child queries
362
- * @param parameter.slotChildren - a function to provide the default children slot using a view query
363
- * @returns the widget
364
- */
365
- const callWidgetFactoryWithConfig = ({ factory, defaultConfig, widgetConfig, events, afterInit, slotTemplates, slotChildren, }) => {
366
- const injector = inject(Injector);
367
- const slots$ = writable({});
368
- const props = {};
369
- let initDone;
370
- const patchSlots = createPatchSlots(slots$.set);
371
- const res = {
372
- initialized: new Promise((resolve) => {
373
- initDone = resolve;
374
- }),
375
- updateSlots: () => {
376
- if (slotTemplates) {
377
- patchSlots(slotTemplates());
378
- }
379
- },
380
- patch(newProps) {
381
- // temporary function replaced in ngInit
382
- Object.assign(props, newProps);
383
- },
384
- ngInit() {
385
- runInInjectionContext(injector, () => {
386
- const zoneWrapper = inject(ZoneWrapper);
387
- factory = zoneWrapper.outsideNgZone(factory);
388
- const defaultConfig$ = toReadableStore(defaultConfig);
389
- events = zoneWrapper.insideNgZoneWrapFunctionsObject(events);
390
- const widget = factory({
391
- config: computed$1(() => ({
392
- ...defaultConfig$(),
393
- children: slotChildren?.(),
394
- ...widgetConfig?.(),
395
- ...slots$(),
396
- ...events,
397
- })),
398
- props,
399
- });
400
- Object.assign(res, {
401
- patch: zoneWrapper.outsideNgZone(widget.patch),
402
- directives: zoneWrapper.outsideNgZoneWrapDirectivesObject(widget.directives),
403
- api: zoneWrapper.outsideNgZoneWrapFunctionsObject(widget.api),
404
- state: Object.fromEntries(Object.entries(widget.stores).map(([key, val]) => [key.slice(0, -1), toAngularSignal(val)])),
405
- });
406
- afterInit?.(res);
407
- initDone();
408
- });
409
- },
410
- };
411
- return res;
412
- };
413
- function patchSimpleChanges(patchFn, changes) {
414
- const obj = {};
415
- for (const [key, simpleChange] of Object.entries(changes)) {
416
- if (simpleChange !== undefined) {
417
- obj[key] = simpleChange.currentValue;
418
- }
419
- }
420
- patchFn(obj);
421
- }
422
- /**
423
- * An abstract base class for widget directives, providing common functionality
424
- * for Angular components that interact with widgets.
425
- *
426
- * @template W - The type of the widget.
427
- */
428
- class BaseWidgetDirective {
429
- constructor(_widget) {
430
- this._widget = _widget;
431
- }
432
- /**
433
- * Retrieves the widget api
434
- * @returns the widget api
435
- */
436
- get api() {
437
- return this._widget.api;
438
- }
439
- /**
440
- * Retrieves the widget state. Each property of the state is exposed through an Angular {@link https://angular.dev/api/core/Signal | Signal}
441
- * @returns the widget state
442
- */
443
- get state() {
444
- return this._widget.state;
445
- }
446
- /**
447
- * Retrieves the widget directives
448
- * @returns the widget directives
449
- */
450
- get directives() {
451
- return this._widget.directives;
452
- }
453
- /**
454
- * @inheritdoc
455
- * @internal
456
- */
457
- ngOnChanges(changes) {
458
- patchSimpleChanges(this._widget.patch, changes);
459
- }
460
- /** @internal */
461
- ngOnInit() {
462
- this._widget.ngInit();
463
- }
464
- /** @internal */
465
- ngAfterContentChecked() {
466
- this._widget.updateSlots();
467
- }
468
- static { this.ɵfac = function BaseWidgetDirective_Factory(__ngFactoryType__) { i0.ɵɵinvalidFactory(); }; }
469
- static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: BaseWidgetDirective, features: [i0.ɵɵNgOnChangesFeature] }); }
470
- }
471
- (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(BaseWidgetDirective, [{
472
- type: Directive
473
- }], () => [{ type: undefined }], null); })();
474
-
475
480
  /**
476
481
  * A factory to create the utilities to allow widgets to be context-aware.
477
482
  *
@@ -572,30 +577,30 @@ const widgetsConfigFactory = (widgetsConfigInjectionToken = new InjectionToken('
572
577
  */
573
578
  const injectWidgetConfig = (widgetName) => {
574
579
  const widgetsConfig = inject(widgetsConfigInjectionToken, { optional: true });
575
- return computed$1(() => widgetsConfig?.()[widgetName]);
580
+ return computed(() => widgetsConfig?.()[widgetName]);
576
581
  };
577
582
  /**
578
583
  * Creates and initializes a widget using the provided factory and configuration options.
579
584
  *
585
+ * The resulting widget can be easily hooked into the lifecycle of an Angular component through {@link BaseWidgetDirective}.
586
+ *
580
587
  * @template W - The type of the widget.
581
- * @param params - The parameters for creating the widget.
582
- * @param params.factory - The factory function to create the widget.
583
- * @param params.widgetName - The name of the widget configuration to inject, if any.
584
- * @param params.defaultConfig - The default configuration for the widget.
585
- * @param params.events - The event handlers for the widget.
586
- * @param params.slotTemplates - A function that returns the slot templates for the widget.
587
- * @param params.slotChildren - A function that returns the slot children for the widget.
588
- * @param params.afterInit - A callback function to be called after the widget is initialized.
588
+ * @param factory - The factory function to create the widget.
589
+ * @param options - The options for creating the widget.
590
+ * @param options.defaultConfig - The default configuration for the widget.
591
+ * @param options.events - The event handlers for the widget.
592
+ * @param options.slotTemplates - A function that returns the slot templates for the widget.
593
+ * @param options.slotChildren - A function that returns the slot children for the widget.
594
+ * @param options.afterInit - A callback function to be called after the widget is initialized.
589
595
  * @returns The initialized widget.
590
596
  */
591
- const callWidgetFactory = ({ factory, widgetName = null, defaultConfig = {}, events, afterInit, slotTemplates, slotChildren, }) => callWidgetFactoryWithConfig({
592
- factory,
593
- widgetConfig: widgetName ? injectWidgetConfig(widgetName) : null,
594
- defaultConfig,
595
- events,
596
- afterInit,
597
- slotTemplates: slotTemplates,
598
- slotChildren,
597
+ const callWidgetFactory = (factory, options) => callWidgetFactoryWithConfig(factory, {
598
+ widgetConfig: factory[FACTORY_WIDGET_NAME] ? injectWidgetConfig(factory[FACTORY_WIDGET_NAME]) : undefined,
599
+ defaultConfig: options?.defaultConfig,
600
+ events: options?.events,
601
+ afterInit: options?.afterInit,
602
+ slotTemplates: options?.slotTemplates,
603
+ slotChildren: options?.slotChildren,
599
604
  });
600
605
  return {
601
606
  /**