@thoughtspot/ts-chart-sdk 0.0.1-alpha.9 → 0.0.2-alpha.10
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/dist/ts-chart-sdk.d.ts +449 -31
- package/lib/index.d.ts +3 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -1
- package/lib/main/custom-chart-context.d.ts +17 -5
- package/lib/main/custom-chart-context.d.ts.map +1 -1
- package/lib/main/custom-chart-context.js +74 -34
- package/lib/main/custom-chart-context.js.map +1 -1
- package/lib/main/custom-chart-context.spec.js +256 -257
- package/lib/main/custom-chart-context.spec.js.map +1 -1
- package/lib/main/post-message-event-bridge.d.ts +3 -3
- package/lib/main/post-message-event-bridge.d.ts.map +1 -1
- package/lib/main/post-message-event-bridge.js +24 -36
- package/lib/main/post-message-event-bridge.js.map +1 -1
- package/lib/main/post-message-event-bridge.spec.js +0 -2
- package/lib/main/post-message-event-bridge.spec.js.map +1 -1
- package/lib/main/util.d.ts +2 -0
- package/lib/main/util.d.ts.map +1 -0
- package/lib/main/util.js +9 -0
- package/lib/main/util.js.map +1 -0
- package/lib/react/use-custom-chart-context.spec.js +128 -121
- package/lib/react/use-custom-chart-context.spec.js.map +1 -1
- package/lib/test/test-conditional-formatting-utils.d.ts +5 -0
- package/lib/test/test-conditional-formatting-utils.d.ts.map +1 -0
- package/lib/test/test-conditional-formatting-utils.js +43 -0
- package/lib/test/test-conditional-formatting-utils.js.map +1 -0
- package/lib/types/answer-column.types.d.ts +54 -1
- package/lib/types/answer-column.types.d.ts.map +1 -1
- package/lib/types/answer-column.types.js +47 -0
- package/lib/types/answer-column.types.js.map +1 -1
- package/lib/types/common.types.d.ts +51 -4
- package/lib/types/common.types.d.ts.map +1 -1
- package/lib/types/common.types.js +15 -1
- package/lib/types/common.types.js.map +1 -1
- package/lib/types/conditional-formatting.types.d.ts +155 -0
- package/lib/types/conditional-formatting.types.d.ts.map +1 -0
- package/lib/types/conditional-formatting.types.js +60 -0
- package/lib/types/conditional-formatting.types.js.map +1 -0
- package/lib/types/configurator.types.d.ts +5 -0
- package/lib/types/configurator.types.d.ts.map +1 -1
- package/lib/types/ts-to-chart-event.types.d.ts +3 -0
- package/lib/types/ts-to-chart-event.types.d.ts.map +1 -1
- package/lib/types/ts-to-chart-event.types.js.map +1 -1
- package/lib/types/visual-prop.types.d.ts +3 -0
- package/lib/types/visual-prop.types.d.ts.map +1 -1
- package/lib/utils/conditional-formatting/conditional-formatting.d.ts +10 -0
- package/lib/utils/conditional-formatting/conditional-formatting.d.ts.map +1 -0
- package/lib/utils/conditional-formatting/conditional-formatting.js +103 -0
- package/lib/utils/conditional-formatting/conditional-formatting.js.map +1 -0
- package/lib/utils/conditional-formatting/conditional-formatting.spec.d.ts +2 -0
- package/lib/utils/conditional-formatting/conditional-formatting.spec.d.ts.map +1 -0
- package/lib/utils/conditional-formatting/conditional-formatting.spec.js +142 -0
- package/lib/utils/conditional-formatting/conditional-formatting.spec.js.map +1 -0
- package/lib/utils/date-formatting.d.ts +19 -0
- package/lib/utils/date-formatting.d.ts.map +1 -0
- package/lib/utils/date-formatting.js +56 -0
- package/lib/utils/date-formatting.js.map +1 -0
- package/lib/utils/date-formatting.spec.d.ts +2 -0
- package/lib/utils/date-formatting.spec.d.ts.map +1 -0
- package/lib/utils/date-formatting.spec.js +124 -0
- package/lib/utils/date-formatting.spec.js.map +1 -0
- package/package.json +10 -7
- package/src/index.ts +3 -0
- package/src/main/custom-chart-context.spec.ts +291 -266
- package/src/main/custom-chart-context.ts +164 -46
- package/src/main/post-message-event-bridge.spec.ts +0 -3
- package/src/main/post-message-event-bridge.ts +36 -62
- package/src/main/util.ts +11 -0
- package/src/react/use-custom-chart-context.spec.tsx +149 -122
- package/src/test/test-conditional-formatting-utils.ts +95 -0
- package/src/types/answer-column.types.ts +98 -0
- package/src/types/common.types.ts +82 -10
- package/src/types/conditional-formatting.types.ts +172 -0
- package/src/types/configurator.types.ts +21 -0
- package/src/types/ts-to-chart-event.types.ts +15 -0
- package/src/types/visual-prop.types.ts +7 -0
- package/src/utils/conditional-formatting/conditional-formatting.spec.ts +469 -0
- package/src/utils/conditional-formatting/conditional-formatting.ts +229 -0
- package/src/utils/date-formatting.spec.ts +168 -0
- package/src/utils/date-formatting.ts +109 -0
- package/cjs/index.d.ts +0 -9
- package/cjs/index.d.ts.map +0 -1
- package/cjs/index.js +0 -25
- package/cjs/index.js.map +0 -1
- package/cjs/main/custom-chart-context.d.ts +0 -47
- package/cjs/main/custom-chart-context.d.ts.map +0 -1
- package/cjs/main/custom-chart-context.js +0 -326
- package/cjs/main/custom-chart-context.js.map +0 -1
- package/cjs/main/custom-chart-context.spec.d.ts +0 -2
- package/cjs/main/custom-chart-context.spec.d.ts.map +0 -1
- package/cjs/main/custom-chart-context.spec.js +0 -699
- package/cjs/main/custom-chart-context.spec.js.map +0 -1
- package/cjs/main/post-message-event-bridge.d.ts +0 -6
- package/cjs/main/post-message-event-bridge.d.ts.map +0 -1
- package/cjs/main/post-message-event-bridge.js +0 -46
- package/cjs/main/post-message-event-bridge.js.map +0 -1
- package/cjs/main/post-message-event-bridge.spec.d.ts +0 -2
- package/cjs/main/post-message-event-bridge.spec.d.ts.map +0 -1
- package/cjs/main/post-message-event-bridge.spec.js +0 -124
- package/cjs/main/post-message-event-bridge.spec.js.map +0 -1
- package/cjs/react/custom-chart-context-service.d.ts +0 -33
- package/cjs/react/custom-chart-context-service.d.ts.map +0 -1
- package/cjs/react/custom-chart-context-service.js +0 -118
- package/cjs/react/custom-chart-context-service.js.map +0 -1
- package/cjs/react/custom-chart.context.d.ts +0 -26
- package/cjs/react/custom-chart.context.d.ts.map +0 -1
- package/cjs/react/custom-chart.context.js +0 -105
- package/cjs/react/custom-chart.context.js.map +0 -1
- package/cjs/react/custom-chart.spec.d.ts +0 -2
- package/cjs/react/custom-chart.spec.d.ts.map +0 -1
- package/cjs/react/custom-chart.spec.js +0 -72
- package/cjs/react/custom-chart.spec.js.map +0 -1
- package/cjs/react/mocks/custom-chart-context-mock.d.ts +0 -3
- package/cjs/react/mocks/custom-chart-context-mock.d.ts.map +0 -1
- package/cjs/react/mocks/custom-chart-context-mock.js +0 -100
- package/cjs/react/mocks/custom-chart-context-mock.js.map +0 -1
- package/cjs/react/use-custom-chart-context.d.ts +0 -4
- package/cjs/react/use-custom-chart-context.d.ts.map +0 -1
- package/cjs/react/use-custom-chart-context.js +0 -95
- package/cjs/react/use-custom-chart-context.js.map +0 -1
- package/cjs/react/use-custom-chart-context.spec.d.ts +0 -2
- package/cjs/react/use-custom-chart-context.spec.d.ts.map +0 -1
- package/cjs/react/use-custom-chart-context.spec.js +0 -391
- package/cjs/react/use-custom-chart-context.spec.js.map +0 -1
- package/cjs/react/use-custom-chart-context.util.d.ts +0 -6
- package/cjs/react/use-custom-chart-context.util.d.ts.map +0 -1
- package/cjs/react/use-custom-chart-context.util.js +0 -60
- package/cjs/react/use-custom-chart-context.util.js.map +0 -1
- package/cjs/react/use-custom-chart-types.d.ts +0 -23
- package/cjs/react/use-custom-chart-types.d.ts.map +0 -1
- package/cjs/react/use-custom-chart-types.js +0 -3
- package/cjs/react/use-custom-chart-types.js.map +0 -1
- package/cjs/test/test-utils.d.ts +0 -7
- package/cjs/test/test-utils.d.ts.map +0 -1
- package/cjs/test/test-utils.js +0 -14
- package/cjs/test/test-utils.js.map +0 -1
- package/cjs/types/answer-column.types.d.ts +0 -69
- package/cjs/types/answer-column.types.d.ts.map +0 -1
- package/cjs/types/answer-column.types.js +0 -59
- package/cjs/types/answer-column.types.js.map +0 -1
- package/cjs/types/chart-to-ts-event.types.d.ts +0 -98
- package/cjs/types/chart-to-ts-event.types.d.ts.map +0 -1
- package/cjs/types/chart-to-ts-event.types.js +0 -32
- package/cjs/types/chart-to-ts-event.types.js.map +0 -1
- package/cjs/types/common.types.d.ts +0 -51
- package/cjs/types/common.types.d.ts.map +0 -1
- package/cjs/types/common.types.js +0 -3
- package/cjs/types/common.types.js.map +0 -1
- package/cjs/types/configurator.types.d.ts +0 -15
- package/cjs/types/configurator.types.d.ts.map +0 -1
- package/cjs/types/configurator.types.js +0 -3
- package/cjs/types/configurator.types.js.map +0 -1
- package/cjs/types/ts-to-chart-event.types.d.ts +0 -91
- package/cjs/types/ts-to-chart-event.types.d.ts.map +0 -1
- package/cjs/types/ts-to-chart-event.types.js +0 -18
- package/cjs/types/ts-to-chart-event.types.js.map +0 -1
- package/cjs/types/visual-prop.types.d.ts +0 -73
- package/cjs/types/visual-prop.types.d.ts.map +0 -1
- package/cjs/types/visual-prop.types.js +0 -3
- package/cjs/types/visual-prop.types.js.map +0 -1
- package/lib/dist/ts-chart-sdk.d.ts +0 -312
- package/lib/react/custom-chart-context-service.d.ts +0 -33
- package/lib/react/custom-chart-context-service.d.ts.map +0 -1
- package/lib/react/custom-chart-context-service.js +0 -91
- package/lib/react/custom-chart-context-service.js.map +0 -1
- package/lib/react/custom-chart.context.d.ts +0 -26
- package/lib/react/custom-chart.context.d.ts.map +0 -1
- package/lib/react/custom-chart.context.js +0 -78
- package/lib/react/custom-chart.context.js.map +0 -1
- package/lib/react/custom-chart.spec.d.ts +0 -2
- package/lib/react/custom-chart.spec.d.ts.map +0 -1
- package/lib/react/custom-chart.spec.js +0 -67
- package/lib/react/custom-chart.spec.js.map +0 -1
|
@@ -21,10 +21,14 @@ import {
|
|
|
21
21
|
AppConfig,
|
|
22
22
|
ChartConfig,
|
|
23
23
|
ChartModel,
|
|
24
|
+
SuccessValidationResponse,
|
|
24
25
|
ValidationResponse,
|
|
25
26
|
VisualProps,
|
|
26
27
|
} from '../types/common.types';
|
|
27
|
-
import {
|
|
28
|
+
import {
|
|
29
|
+
ChartConfigEditorDefinition,
|
|
30
|
+
ConfigEditorDefinitionSetter,
|
|
31
|
+
} from '../types/configurator.types';
|
|
28
32
|
import {
|
|
29
33
|
AxisMenuCustomActionPayload,
|
|
30
34
|
ChartConfigValidateEventPayload,
|
|
@@ -42,10 +46,21 @@ import {
|
|
|
42
46
|
VisualPropsUpdateEventPayload,
|
|
43
47
|
VisualPropsValidateEventPayload,
|
|
44
48
|
} from '../types/ts-to-chart-event.types';
|
|
45
|
-
import {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
import {
|
|
50
|
+
VisualEditorDefinitionSetter,
|
|
51
|
+
VisualPropEditorDefinition,
|
|
52
|
+
} from '../types/visual-prop.types';
|
|
53
|
+
import {
|
|
54
|
+
globalThis,
|
|
55
|
+
initMessageListener,
|
|
56
|
+
postMessageToHostApp,
|
|
57
|
+
} from './post-message-event-bridge';
|
|
58
|
+
|
|
59
|
+
export type AllowedConfigurations = {
|
|
60
|
+
allowColumnNumberFormatting: boolean;
|
|
61
|
+
allowColumnConditionalFormatting: boolean;
|
|
62
|
+
allowMeasureNamesAndValues: boolean;
|
|
63
|
+
};
|
|
49
64
|
|
|
50
65
|
export type CustomChartContextProps = {
|
|
51
66
|
/**
|
|
@@ -63,7 +78,10 @@ export type CustomChartContextProps = {
|
|
|
63
78
|
* @returns {@link Array<Query>}
|
|
64
79
|
* @version SDK: 0.1 | ThoughtSpot:
|
|
65
80
|
*/
|
|
66
|
-
getQueriesFromChartConfig: (
|
|
81
|
+
getQueriesFromChartConfig: (
|
|
82
|
+
chartConfig: ChartConfig[],
|
|
83
|
+
chartModel: ChartModel,
|
|
84
|
+
) => Query[];
|
|
67
85
|
/**
|
|
68
86
|
* Main Render function that will render the chart based on the chart context provided
|
|
69
87
|
*
|
|
@@ -106,7 +124,9 @@ export type CustomChartContextProps = {
|
|
|
106
124
|
*
|
|
107
125
|
* @version SDK: 0.1 | ThoughtSpot:
|
|
108
126
|
*/
|
|
109
|
-
chartConfigEditorDefinition?:
|
|
127
|
+
chartConfigEditorDefinition?:
|
|
128
|
+
| ConfigEditorDefinitionSetter
|
|
129
|
+
| ChartConfigEditorDefinition[];
|
|
110
130
|
|
|
111
131
|
/**
|
|
112
132
|
* Definition to help edit/customize the visual properties from chart settings editor
|
|
@@ -117,9 +137,18 @@ export type CustomChartContextProps = {
|
|
|
117
137
|
* @returns {@link VisualPropEditorDefinition}
|
|
118
138
|
* @version SDK: 0.1 | ThoughtSpot:
|
|
119
139
|
*/
|
|
120
|
-
visualPropEditorDefinition?:
|
|
140
|
+
visualPropEditorDefinition?:
|
|
141
|
+
| VisualEditorDefinitionSetter
|
|
142
|
+
| VisualPropEditorDefinition;
|
|
143
|
+
|
|
144
|
+
// Whether user wants thoughtspot default number and conditional formatting
|
|
145
|
+
allowedConfigurations?: AllowedConfigurations;
|
|
121
146
|
};
|
|
122
147
|
|
|
148
|
+
export type ValidationFunctions =
|
|
149
|
+
| CustomChartContextProps['validateVisualProps']
|
|
150
|
+
| CustomChartContextProps['validateConfig'];
|
|
151
|
+
|
|
123
152
|
/**
|
|
124
153
|
* Default configuration options for all the chart context properties
|
|
125
154
|
*/
|
|
@@ -127,6 +156,11 @@ const DEFAULT_CHART_CONTEXT_PROPS: Partial<CustomChartContextProps> = {
|
|
|
127
156
|
validateConfig: () => ({ isValid: true }),
|
|
128
157
|
validateVisualProps: () => ({ isValid: true }),
|
|
129
158
|
chartConfigEditorDefinition: undefined,
|
|
159
|
+
allowedConfigurations: {
|
|
160
|
+
allowColumnNumberFormatting: false,
|
|
161
|
+
allowColumnConditionalFormatting: false,
|
|
162
|
+
allowMeasureNamesAndValues: false,
|
|
163
|
+
},
|
|
130
164
|
};
|
|
131
165
|
|
|
132
166
|
export class CustomChartContext {
|
|
@@ -141,6 +175,8 @@ export class CustomChartContext {
|
|
|
141
175
|
*/
|
|
142
176
|
private componentId = '';
|
|
143
177
|
|
|
178
|
+
private removeListener: () => void = _.noop;
|
|
179
|
+
|
|
144
180
|
/**
|
|
145
181
|
* host app url
|
|
146
182
|
*
|
|
@@ -217,6 +253,8 @@ export class CustomChartContext {
|
|
|
217
253
|
*/
|
|
218
254
|
private axisMenuActionHandler: AxisMenuActionHandler = {};
|
|
219
255
|
|
|
256
|
+
public containerEl: HTMLElement | null = null;
|
|
257
|
+
|
|
220
258
|
/**
|
|
221
259
|
* Constructor to only accept context props as payload
|
|
222
260
|
*
|
|
@@ -305,8 +343,8 @@ export class CustomChartContext {
|
|
|
305
343
|
* @version SDK: 0.1 | ThoughtSpot:
|
|
306
344
|
*/
|
|
307
345
|
public destroy() {
|
|
308
|
-
|
|
309
|
-
isInitialized = false;
|
|
346
|
+
this.removeListener();
|
|
347
|
+
globalThis.isInitialized = false;
|
|
310
348
|
}
|
|
311
349
|
|
|
312
350
|
/**
|
|
@@ -358,6 +396,48 @@ export class CustomChartContext {
|
|
|
358
396
|
return processedPayload;
|
|
359
397
|
}
|
|
360
398
|
|
|
399
|
+
/**
|
|
400
|
+
* Funtions return the chart config editor definition
|
|
401
|
+
* @param {ChartConfig[]} currentChartConfig
|
|
402
|
+
* @param {VisualProps}
|
|
403
|
+
* @returns {ChartConfigEditorDefinition[]}
|
|
404
|
+
*/
|
|
405
|
+
private getChartConfigEditorDefinition = (
|
|
406
|
+
currentState: Partial<ChartModel> = {},
|
|
407
|
+
) => {
|
|
408
|
+
if (_.isFunction(this.chartContextProps.chartConfigEditorDefinition)) {
|
|
409
|
+
return this.chartContextProps.chartConfigEditorDefinition(
|
|
410
|
+
{
|
|
411
|
+
...this.chartModel,
|
|
412
|
+
...currentState,
|
|
413
|
+
},
|
|
414
|
+
this,
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
return this.chartContextProps.chartConfigEditorDefinition;
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Funtions returns the visual prop editor definition
|
|
422
|
+
* @param {ChartConfig[]} currentChartConfig
|
|
423
|
+
* @param {VisualProps}
|
|
424
|
+
* @returns {VisualPropEditorDefinition}
|
|
425
|
+
*/
|
|
426
|
+
private getVisualPropEditorDefinition = (
|
|
427
|
+
currentState: Partial<ChartModel> = {},
|
|
428
|
+
) => {
|
|
429
|
+
if (_.isFunction(this.chartContextProps.visualPropEditorDefinition)) {
|
|
430
|
+
return this.chartContextProps.visualPropEditorDefinition(
|
|
431
|
+
{
|
|
432
|
+
...this.chartModel,
|
|
433
|
+
...currentState,
|
|
434
|
+
},
|
|
435
|
+
this,
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
return this.chartContextProps.visualPropEditorDefinition;
|
|
439
|
+
};
|
|
440
|
+
|
|
361
441
|
/**
|
|
362
442
|
* Function to store the axis menu custom action callback mapped with action id
|
|
363
443
|
* @param {[OpenAxisMenuEventPayload]} eventPayload Event payload bound
|
|
@@ -417,6 +497,24 @@ export class CustomChartContext {
|
|
|
417
497
|
}
|
|
418
498
|
}
|
|
419
499
|
|
|
500
|
+
private validationsResponseProcessor(
|
|
501
|
+
currentValidationState: Partial<ChartModel>,
|
|
502
|
+
validationResponse: ValidationResponse,
|
|
503
|
+
) {
|
|
504
|
+
const visualPropEditorDefinition = this.getVisualPropEditorDefinition(
|
|
505
|
+
currentValidationState,
|
|
506
|
+
);
|
|
507
|
+
const chartConfigEditorDefinition = this.getChartConfigEditorDefinition(
|
|
508
|
+
currentValidationState,
|
|
509
|
+
);
|
|
510
|
+
|
|
511
|
+
return {
|
|
512
|
+
...validationResponse,
|
|
513
|
+
visualPropEditorDefinition,
|
|
514
|
+
chartConfigEditorDefinition,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
|
|
420
518
|
/**
|
|
421
519
|
* Function to emit Chart to TS Events to the TS application.
|
|
422
520
|
|
|
@@ -429,7 +527,7 @@ export class CustomChartContext {
|
|
|
429
527
|
eventType: T,
|
|
430
528
|
...eventPayload: ChartToTSEventsPayloadMap[T]
|
|
431
529
|
): Promise<any> {
|
|
432
|
-
if (!isInitialized) {
|
|
530
|
+
if (!globalThis.isInitialized) {
|
|
433
531
|
console.log(
|
|
434
532
|
'Chart Context: not initialized the context, something went wrong',
|
|
435
533
|
);
|
|
@@ -439,7 +537,7 @@ export class CustomChartContext {
|
|
|
439
537
|
eventType,
|
|
440
538
|
eventPayload,
|
|
441
539
|
);
|
|
442
|
-
return
|
|
540
|
+
return postMessageToHostApp(
|
|
443
541
|
this.componentId,
|
|
444
542
|
this.hostUrl,
|
|
445
543
|
processedPayload?.[0] ?? null,
|
|
@@ -452,13 +550,13 @@ export class CustomChartContext {
|
|
|
452
550
|
* Process all the functions via the eventProcess callback
|
|
453
551
|
*/
|
|
454
552
|
private registerEventProcessor = () => {
|
|
455
|
-
if (isInitialized) {
|
|
553
|
+
if (globalThis.isInitialized) {
|
|
456
554
|
console.error(
|
|
457
555
|
'The context is already initialized. you cannot have multiple contexts',
|
|
458
556
|
);
|
|
459
557
|
throw new Error(ErrorType.MultipleContextsNotSupported);
|
|
460
558
|
}
|
|
461
|
-
|
|
559
|
+
this.removeListener = initMessageListener(this.eventProcessor);
|
|
462
560
|
|
|
463
561
|
this.registerEvents();
|
|
464
562
|
};
|
|
@@ -468,29 +566,13 @@ export class CustomChartContext {
|
|
|
468
566
|
*
|
|
469
567
|
* @param event : Message Event Object
|
|
470
568
|
*/
|
|
471
|
-
private eventProcessor = (
|
|
472
|
-
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
569
|
+
private eventProcessor = (data: any) => {
|
|
570
|
+
console.log('Chart Context: message received:', data.eventType, data);
|
|
475
571
|
|
|
476
|
-
|
|
477
|
-
'Chart Context: message received:',
|
|
478
|
-
event.data.eventType,
|
|
479
|
-
event.data,
|
|
480
|
-
);
|
|
481
|
-
|
|
482
|
-
const messageResponse = this.executeEventListenerCBs(event);
|
|
572
|
+
const messageResponse = this.executeEventListenerCBs(data);
|
|
483
573
|
|
|
484
574
|
// respond back to parent to confirm/ack the receipt
|
|
485
|
-
|
|
486
|
-
if (!_.isNil(messageResponse)) {
|
|
487
|
-
event.ports[0].postMessage({
|
|
488
|
-
...(messageResponse as any),
|
|
489
|
-
});
|
|
490
|
-
} else {
|
|
491
|
-
event.ports[0].postMessage({});
|
|
492
|
-
}
|
|
493
|
-
}
|
|
575
|
+
return messageResponse;
|
|
494
576
|
};
|
|
495
577
|
|
|
496
578
|
/**
|
|
@@ -525,13 +607,27 @@ export class CustomChartContext {
|
|
|
525
607
|
*/
|
|
526
608
|
this.onInternal(
|
|
527
609
|
TSToChartEvent.VisualPropsValidate,
|
|
528
|
-
(
|
|
610
|
+
(
|
|
611
|
+
payload: VisualPropsValidateEventPayload,
|
|
612
|
+
):
|
|
613
|
+
| (ValidationResponse & SuccessValidationResponse)
|
|
614
|
+
| ValidationResponse => {
|
|
529
615
|
if (this.chartContextProps.validateVisualProps) {
|
|
530
616
|
const validationResponse =
|
|
531
617
|
this.chartContextProps.validateVisualProps(
|
|
532
618
|
payload.visualProps,
|
|
533
619
|
this.chartModel,
|
|
534
620
|
);
|
|
621
|
+
if (validationResponse.isValid) {
|
|
622
|
+
const currentVisualState = {
|
|
623
|
+
visualProps: payload.visualProps,
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
return this.validationsResponseProcessor(
|
|
627
|
+
currentVisualState,
|
|
628
|
+
validationResponse,
|
|
629
|
+
);
|
|
630
|
+
}
|
|
535
631
|
return validationResponse;
|
|
536
632
|
}
|
|
537
633
|
// this will never be true
|
|
@@ -546,13 +642,29 @@ export class CustomChartContext {
|
|
|
546
642
|
*/
|
|
547
643
|
this.onInternal(
|
|
548
644
|
TSToChartEvent.ChartConfigValidate,
|
|
549
|
-
(
|
|
645
|
+
(
|
|
646
|
+
payload: ChartConfigValidateEventPayload,
|
|
647
|
+
):
|
|
648
|
+
| (ValidationResponse & SuccessValidationResponse)
|
|
649
|
+
| ValidationResponse => {
|
|
550
650
|
if (this.chartContextProps.validateConfig) {
|
|
551
651
|
const validationResponse =
|
|
552
652
|
this.chartContextProps.validateConfig(
|
|
553
653
|
payload.chartConfig,
|
|
554
654
|
this.chartModel,
|
|
555
655
|
);
|
|
656
|
+
if (validationResponse.isValid) {
|
|
657
|
+
const currentConfigState = {
|
|
658
|
+
config: {
|
|
659
|
+
...this.chartModel.config,
|
|
660
|
+
chartConfig: payload.chartConfig,
|
|
661
|
+
},
|
|
662
|
+
};
|
|
663
|
+
return this.validationsResponseProcessor(
|
|
664
|
+
currentConfigState,
|
|
665
|
+
validationResponse,
|
|
666
|
+
);
|
|
667
|
+
}
|
|
556
668
|
return validationResponse;
|
|
557
669
|
}
|
|
558
670
|
// this will never be true
|
|
@@ -570,6 +682,7 @@ export class CustomChartContext {
|
|
|
570
682
|
const queries =
|
|
571
683
|
this.chartContextProps.getQueriesFromChartConfig(
|
|
572
684
|
payload.config,
|
|
685
|
+
this.chartModel,
|
|
573
686
|
);
|
|
574
687
|
return {
|
|
575
688
|
queries,
|
|
@@ -743,13 +856,16 @@ export class CustomChartContext {
|
|
|
743
856
|
this.hostUrl = payload.hostUrl;
|
|
744
857
|
this.chartModel = payload.chartModel;
|
|
745
858
|
this.appConfig = payload.appConfig ?? {};
|
|
859
|
+
this.containerEl = payload.containerElSelector
|
|
860
|
+
? document.querySelector(payload.containerElSelector)
|
|
861
|
+
: null;
|
|
746
862
|
|
|
747
863
|
return this.publishChartContextPropsToHost();
|
|
748
864
|
};
|
|
749
865
|
|
|
750
866
|
private initializationComplete = (): void => {
|
|
751
867
|
// context is now initialized
|
|
752
|
-
isInitialized = true;
|
|
868
|
+
globalThis.isInitialized = true;
|
|
753
869
|
|
|
754
870
|
// TODO: following can be done behind a promise
|
|
755
871
|
this.triggerInitResolve();
|
|
@@ -780,9 +896,11 @@ export class CustomChartContext {
|
|
|
780
896
|
isConfigValid: isValid,
|
|
781
897
|
defaultChartConfig,
|
|
782
898
|
chartConfigEditorDefinition:
|
|
783
|
-
this.
|
|
899
|
+
this.getChartConfigEditorDefinition(),
|
|
784
900
|
visualPropEditorDefinition:
|
|
785
|
-
this.
|
|
901
|
+
this.getVisualPropEditorDefinition(),
|
|
902
|
+
allowedConfigurations:
|
|
903
|
+
this.chartContextProps.allowedConfigurations,
|
|
786
904
|
};
|
|
787
905
|
};
|
|
788
906
|
|
|
@@ -792,12 +910,12 @@ export class CustomChartContext {
|
|
|
792
910
|
* @param event : Message Event Object
|
|
793
911
|
* @returns response to be sent back to the message sender (host)
|
|
794
912
|
*/
|
|
795
|
-
private executeEventListenerCBs = (
|
|
913
|
+
private executeEventListenerCBs = (data: any): any => {
|
|
796
914
|
// do basic sanity
|
|
797
|
-
const payload =
|
|
915
|
+
const payload = data.payload;
|
|
798
916
|
let response;
|
|
799
|
-
if (_.isArray(this.eventListeners[
|
|
800
|
-
this.eventListeners[
|
|
917
|
+
if (_.isArray(this.eventListeners[data.eventType])) {
|
|
918
|
+
this.eventListeners[data.eventType].forEach((callback) => {
|
|
801
919
|
// this is a problem today if we have multiple callbacks
|
|
802
920
|
// registered. only the last response will be sent back to the
|
|
803
921
|
// server
|
|
@@ -806,15 +924,15 @@ export class CustomChartContext {
|
|
|
806
924
|
} else {
|
|
807
925
|
response = {
|
|
808
926
|
hasError: true,
|
|
809
|
-
error: `Event type not recognised or processed: ${
|
|
927
|
+
error: `Event type not recognised or processed: ${data.eventType}`,
|
|
810
928
|
};
|
|
811
929
|
}
|
|
812
930
|
|
|
813
931
|
console.log(
|
|
814
932
|
'ChartContext: Response:',
|
|
815
|
-
|
|
933
|
+
data.eventType,
|
|
816
934
|
response,
|
|
817
|
-
this.eventListeners[
|
|
935
|
+
this.eventListeners[data.eventType]?.length,
|
|
818
936
|
);
|
|
819
937
|
return response;
|
|
820
938
|
};
|
|
@@ -66,7 +66,6 @@ describe('postMessageToHostApp', () => {
|
|
|
66
66
|
);
|
|
67
67
|
|
|
68
68
|
// Verify that the MessageChannel was used correctly
|
|
69
|
-
expect(channel.port1.close).toHaveBeenCalled();
|
|
70
69
|
expect(messageChannelMock.MessageChannel).toHaveBeenCalled();
|
|
71
70
|
});
|
|
72
71
|
|
|
@@ -121,8 +120,6 @@ describe('postMessageToHostApp', () => {
|
|
|
121
120
|
[channel.port2],
|
|
122
121
|
);
|
|
123
122
|
|
|
124
|
-
// Verify that the MessageChannel was used correctly
|
|
125
|
-
expect(channel.port1.close).toHaveBeenCalled();
|
|
126
123
|
expect(messageChannelMock.MessageChannel).toHaveBeenCalled();
|
|
127
124
|
});
|
|
128
125
|
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
+
import { onMessage, sendMessage } from 'promise-postmessage';
|
|
1
2
|
import { ChartToTSEvent } from '../types/chart-to-ts-event.types';
|
|
3
|
+
import { timeout } from './util';
|
|
2
4
|
|
|
3
5
|
const TIMEOUT_THRESHOLD = 30000; // 30sec
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
window.addEventListener('message', handleMessageEvent);
|
|
12
|
-
};
|
|
7
|
+
const elSelector = new URL(import.meta.url).searchParams.get('elSelector');
|
|
8
|
+
const target =
|
|
9
|
+
(elSelector && (document.querySelector(elSelector) as HTMLElement)) ||
|
|
10
|
+
window.parent;
|
|
11
|
+
|
|
12
|
+
const globalThis = (target === window.parent ? window : target) as any;
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* method to listen to messages using postMessage from the parent.
|
|
16
16
|
*
|
|
17
17
|
* @param {any} handleMessageEvent
|
|
18
18
|
*/
|
|
19
|
-
const
|
|
20
|
-
|
|
19
|
+
const init = (handleMessageEvent: (data: any) => Promise<any> | any) => {
|
|
20
|
+
return onMessage(handleMessageEvent, target, 'child');
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
/**
|
|
@@ -28,61 +28,35 @@ const destroy = (handleMessageEvent: any) => {
|
|
|
28
28
|
* @param {ChartToTSEvent} eventType type of the event
|
|
29
29
|
* @returns Promise
|
|
30
30
|
*/
|
|
31
|
-
const postMessageToHostApp = (
|
|
31
|
+
const postMessageToHostApp = async (
|
|
32
32
|
componentId: string,
|
|
33
33
|
hostUrl: string,
|
|
34
34
|
eventPayload: any,
|
|
35
35
|
eventType: ChartToTSEvent,
|
|
36
|
-
): Promise<any> =>
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
reject(error);
|
|
45
|
-
} else {
|
|
46
|
-
console.log('ChartContext: message success:', res.data);
|
|
47
|
-
resolve(res.data);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
setTimeout(() => {
|
|
52
|
-
reject(
|
|
53
|
-
new Error(
|
|
54
|
-
`ChartContext: postMessage operation timed out. ${eventType}`,
|
|
55
|
-
eventPayload,
|
|
56
|
-
),
|
|
57
|
-
);
|
|
58
|
-
}, TIMEOUT_THRESHOLD);
|
|
59
|
-
|
|
60
|
-
try {
|
|
61
|
-
window.parent.window.postMessage(
|
|
62
|
-
{
|
|
63
|
-
componentId,
|
|
64
|
-
payload: {
|
|
65
|
-
...eventPayload,
|
|
66
|
-
},
|
|
67
|
-
eventType,
|
|
68
|
-
source: 'ts-chart-sdk',
|
|
36
|
+
): Promise<any> => {
|
|
37
|
+
const resp = await timeout(
|
|
38
|
+
sendMessage(
|
|
39
|
+
target,
|
|
40
|
+
{
|
|
41
|
+
componentId,
|
|
42
|
+
payload: {
|
|
43
|
+
...eventPayload,
|
|
69
44
|
},
|
|
70
|
-
hostUrl,
|
|
71
|
-
[channel.port2],
|
|
72
|
-
);
|
|
73
|
-
} catch (err) {
|
|
74
|
-
console.error(
|
|
75
|
-
'ChartContext: error in emitting event:',
|
|
76
|
-
err,
|
|
77
45
|
eventType,
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
46
|
+
source: 'ts-chart-sdk',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
origin: hostUrl,
|
|
50
|
+
endpoint: 'child',
|
|
51
|
+
},
|
|
52
|
+
),
|
|
53
|
+
TIMEOUT_THRESHOLD,
|
|
54
|
+
'ChartContext: postMessage operation timed out.',
|
|
55
|
+
);
|
|
56
|
+
if (resp?.hasError) {
|
|
57
|
+
throw new Error(resp.error);
|
|
58
|
+
}
|
|
59
|
+
return resp;
|
|
88
60
|
};
|
|
61
|
+
|
|
62
|
+
export { init as initMessageListener, postMessageToHostApp, globalThis };
|
package/src/main/util.ts
ADDED