@fluid-topics/ft-wc-utils 2.0.31 → 2.0.32
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,5 +1,6 @@
|
|
|
1
1
|
import { LitElement } from "lit";
|
|
2
2
|
import { adoptStyles } from "@lit/reactive-element/css-tag.js";
|
|
3
|
+
import { customElement } from "./decorators";
|
|
3
4
|
/**
|
|
4
5
|
* Typescript would not let us use the mixin directly when upgrading Lit from 2.2.8 to 2.7.2
|
|
5
6
|
*
|
|
@@ -30,6 +31,8 @@ export class ScopedRegistryLitElement extends LitElement {
|
|
|
30
31
|
Object.entries(definitions).forEach(([tagName, klass]) => this.defineScopedElement(tagName, klass));
|
|
31
32
|
}
|
|
32
33
|
static defineScopedElement(tagName, klass) {
|
|
34
|
+
// eslint-disable-next-line ft/custom-elements
|
|
35
|
+
customElement(tagName)(klass);
|
|
33
36
|
if (this.canDefineScopedElement(tagName)) {
|
|
34
37
|
this.registry.define(tagName, klass);
|
|
35
38
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { LitElement } from "lit";
|
|
2
|
+
import { Constructor } from "./generic-types";
|
|
3
|
+
export type FtAnalyticsActionEventDetail = {
|
|
4
|
+
eventName: string;
|
|
5
|
+
actionType: string;
|
|
6
|
+
componentType: string;
|
|
7
|
+
componentId: string;
|
|
8
|
+
data?: Record<string, unknown>;
|
|
9
|
+
};
|
|
10
|
+
export declare class FtAnalyticsActionEvent extends CustomEvent<FtAnalyticsActionEventDetail> {
|
|
11
|
+
constructor(detail: FtAnalyticsActionEventDetail);
|
|
12
|
+
}
|
|
13
|
+
declare global {
|
|
14
|
+
interface DocumentEventMap {
|
|
15
|
+
"ft-analytics-action": FtAnalyticsActionEvent;
|
|
16
|
+
}
|
|
17
|
+
interface HTMLElementEventMap {
|
|
18
|
+
"ft-analytics-action": FtAnalyticsActionEvent;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export interface withAnalyticsInterface<Actions extends Record<string, unknown>> {
|
|
22
|
+
configuredAnalyticsActions: Partial<Record<keyof Actions, {
|
|
23
|
+
eventName: string;
|
|
24
|
+
}>>;
|
|
25
|
+
emitAnalyticsAction(actionType: keyof Actions, data?: Record<string, unknown>): void;
|
|
26
|
+
}
|
|
27
|
+
export type withAnalyticsType<T extends Constructor<LitElement>, Actions extends Record<string, unknown>> = T & Constructor<withAnalyticsInterface<Actions>> & {
|
|
28
|
+
readonly analyticsActions: Actions;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Mixin that enables a web component to participate in the analytics emitter model.
|
|
32
|
+
*
|
|
33
|
+
* Each component declares the analytics actions it can emit by passing an action map
|
|
34
|
+
* to the mixin factory. The map's keys are action types and the values carry a UI
|
|
35
|
+
* label used by the page designer for design-time discovery:
|
|
36
|
+
*
|
|
37
|
+
* const analyticsActions = {
|
|
38
|
+
* "clicked": { label: "Button clicked" },
|
|
39
|
+
* "submitted": { label: "Form submitted" },
|
|
40
|
+
* }
|
|
41
|
+
* export class FtFoo extends withAnalytics(FtLitElement, analyticsActions) { … }
|
|
42
|
+
*
|
|
43
|
+
* Applying the mixin adds:
|
|
44
|
+
* - `static readonly analyticsActions`: the action map, exposed on the class for
|
|
45
|
+
* design-time discovery (e.g. the page designer's GrapesJS trait reads
|
|
46
|
+
* `el.constructor.analyticsActions` to render one row per available action type).
|
|
47
|
+
* - `configuredAnalyticsActions`: per-instance mapping of action type → `{ eventName }`,
|
|
48
|
+
* set via the `configuredAnalyticsActions` attribute (JSON string). Written by the
|
|
49
|
+
* page designer's analytics-events trait when a user enables tracking for a row.
|
|
50
|
+
* - `emitAnalyticsAction(actionType, data?)`: dispatches an `ft-analytics-action` custom
|
|
51
|
+
* event (bubbles, composed) only when the given action type has a non-empty
|
|
52
|
+
* `eventName` mapping in `configuredAnalyticsActions`.
|
|
53
|
+
*
|
|
54
|
+
* The optional `data` payload is a placeholder for per-interaction context (e.g. the
|
|
55
|
+
* clicked button's label, the submitted query, the selected option). It is currently
|
|
56
|
+
* carried on the event detail but not forwarded to the analytics backend — reserved
|
|
57
|
+
* for when `FtCustomTriggerEvent` gains a field for per-event properties.
|
|
58
|
+
*
|
|
59
|
+
* Composition: a parent component can re-expose its child's actions by
|
|
60
|
+
* spreading them into its own action map.
|
|
61
|
+
*/
|
|
62
|
+
export declare function withAnalytics<TBase extends Constructor<LitElement>, Actions extends Record<string, unknown>>(Base: TBase, analyticsActions: Actions): withAnalyticsType<TBase, Actions>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { jsonProperty } from "./decorators";
|
|
8
|
+
export class FtAnalyticsActionEvent extends CustomEvent {
|
|
9
|
+
constructor(detail) {
|
|
10
|
+
super("ft-analytics-action", { composed: true, bubbles: true, detail });
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Mixin that enables a web component to participate in the analytics emitter model.
|
|
15
|
+
*
|
|
16
|
+
* Each component declares the analytics actions it can emit by passing an action map
|
|
17
|
+
* to the mixin factory. The map's keys are action types and the values carry a UI
|
|
18
|
+
* label used by the page designer for design-time discovery:
|
|
19
|
+
*
|
|
20
|
+
* const analyticsActions = {
|
|
21
|
+
* "clicked": { label: "Button clicked" },
|
|
22
|
+
* "submitted": { label: "Form submitted" },
|
|
23
|
+
* }
|
|
24
|
+
* export class FtFoo extends withAnalytics(FtLitElement, analyticsActions) { … }
|
|
25
|
+
*
|
|
26
|
+
* Applying the mixin adds:
|
|
27
|
+
* - `static readonly analyticsActions`: the action map, exposed on the class for
|
|
28
|
+
* design-time discovery (e.g. the page designer's GrapesJS trait reads
|
|
29
|
+
* `el.constructor.analyticsActions` to render one row per available action type).
|
|
30
|
+
* - `configuredAnalyticsActions`: per-instance mapping of action type → `{ eventName }`,
|
|
31
|
+
* set via the `configuredAnalyticsActions` attribute (JSON string). Written by the
|
|
32
|
+
* page designer's analytics-events trait when a user enables tracking for a row.
|
|
33
|
+
* - `emitAnalyticsAction(actionType, data?)`: dispatches an `ft-analytics-action` custom
|
|
34
|
+
* event (bubbles, composed) only when the given action type has a non-empty
|
|
35
|
+
* `eventName` mapping in `configuredAnalyticsActions`.
|
|
36
|
+
*
|
|
37
|
+
* The optional `data` payload is a placeholder for per-interaction context (e.g. the
|
|
38
|
+
* clicked button's label, the submitted query, the selected option). It is currently
|
|
39
|
+
* carried on the event detail but not forwarded to the analytics backend — reserved
|
|
40
|
+
* for when `FtCustomTriggerEvent` gains a field for per-event properties.
|
|
41
|
+
*
|
|
42
|
+
* Composition: a parent component can re-expose its child's actions by
|
|
43
|
+
* spreading them into its own action map.
|
|
44
|
+
*/
|
|
45
|
+
export function withAnalytics(Base, analyticsActions) {
|
|
46
|
+
class FtAnalyticsAware extends Base {
|
|
47
|
+
constructor() {
|
|
48
|
+
super(...arguments);
|
|
49
|
+
this.configuredAnalyticsActions = {};
|
|
50
|
+
}
|
|
51
|
+
emitAnalyticsAction(actionType, data) {
|
|
52
|
+
var _a, _b;
|
|
53
|
+
const eventName = (_b = (_a = this.configuredAnalyticsActions) === null || _a === void 0 ? void 0 : _a[actionType]) === null || _b === void 0 ? void 0 : _b.eventName;
|
|
54
|
+
if (!eventName) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const action = analyticsActions[actionType];
|
|
58
|
+
this.dispatchEvent(new FtAnalyticsActionEvent({
|
|
59
|
+
eventName,
|
|
60
|
+
actionType: actionType,
|
|
61
|
+
componentType: this.tagName.toLowerCase(),
|
|
62
|
+
componentId: this.id || (action === null || action === void 0 ? void 0 : action.defaultComponentId) || this.tagName.toLowerCase(),
|
|
63
|
+
data,
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
FtAnalyticsAware.analyticsActions = analyticsActions;
|
|
68
|
+
__decorate([
|
|
69
|
+
jsonProperty({})
|
|
70
|
+
], FtAnalyticsAware.prototype, "configuredAnalyticsActions", void 0);
|
|
71
|
+
return FtAnalyticsAware;
|
|
72
|
+
}
|