@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.
Files changed (174) hide show
  1. package/dist/ts-chart-sdk.d.ts +449 -31
  2. package/lib/index.d.ts +3 -0
  3. package/lib/index.d.ts.map +1 -1
  4. package/lib/index.js +3 -0
  5. package/lib/index.js.map +1 -1
  6. package/lib/main/custom-chart-context.d.ts +17 -5
  7. package/lib/main/custom-chart-context.d.ts.map +1 -1
  8. package/lib/main/custom-chart-context.js +74 -34
  9. package/lib/main/custom-chart-context.js.map +1 -1
  10. package/lib/main/custom-chart-context.spec.js +256 -257
  11. package/lib/main/custom-chart-context.spec.js.map +1 -1
  12. package/lib/main/post-message-event-bridge.d.ts +3 -3
  13. package/lib/main/post-message-event-bridge.d.ts.map +1 -1
  14. package/lib/main/post-message-event-bridge.js +24 -36
  15. package/lib/main/post-message-event-bridge.js.map +1 -1
  16. package/lib/main/post-message-event-bridge.spec.js +0 -2
  17. package/lib/main/post-message-event-bridge.spec.js.map +1 -1
  18. package/lib/main/util.d.ts +2 -0
  19. package/lib/main/util.d.ts.map +1 -0
  20. package/lib/main/util.js +9 -0
  21. package/lib/main/util.js.map +1 -0
  22. package/lib/react/use-custom-chart-context.spec.js +128 -121
  23. package/lib/react/use-custom-chart-context.spec.js.map +1 -1
  24. package/lib/test/test-conditional-formatting-utils.d.ts +5 -0
  25. package/lib/test/test-conditional-formatting-utils.d.ts.map +1 -0
  26. package/lib/test/test-conditional-formatting-utils.js +43 -0
  27. package/lib/test/test-conditional-formatting-utils.js.map +1 -0
  28. package/lib/types/answer-column.types.d.ts +54 -1
  29. package/lib/types/answer-column.types.d.ts.map +1 -1
  30. package/lib/types/answer-column.types.js +47 -0
  31. package/lib/types/answer-column.types.js.map +1 -1
  32. package/lib/types/common.types.d.ts +51 -4
  33. package/lib/types/common.types.d.ts.map +1 -1
  34. package/lib/types/common.types.js +15 -1
  35. package/lib/types/common.types.js.map +1 -1
  36. package/lib/types/conditional-formatting.types.d.ts +155 -0
  37. package/lib/types/conditional-formatting.types.d.ts.map +1 -0
  38. package/lib/types/conditional-formatting.types.js +60 -0
  39. package/lib/types/conditional-formatting.types.js.map +1 -0
  40. package/lib/types/configurator.types.d.ts +5 -0
  41. package/lib/types/configurator.types.d.ts.map +1 -1
  42. package/lib/types/ts-to-chart-event.types.d.ts +3 -0
  43. package/lib/types/ts-to-chart-event.types.d.ts.map +1 -1
  44. package/lib/types/ts-to-chart-event.types.js.map +1 -1
  45. package/lib/types/visual-prop.types.d.ts +3 -0
  46. package/lib/types/visual-prop.types.d.ts.map +1 -1
  47. package/lib/utils/conditional-formatting/conditional-formatting.d.ts +10 -0
  48. package/lib/utils/conditional-formatting/conditional-formatting.d.ts.map +1 -0
  49. package/lib/utils/conditional-formatting/conditional-formatting.js +103 -0
  50. package/lib/utils/conditional-formatting/conditional-formatting.js.map +1 -0
  51. package/lib/utils/conditional-formatting/conditional-formatting.spec.d.ts +2 -0
  52. package/lib/utils/conditional-formatting/conditional-formatting.spec.d.ts.map +1 -0
  53. package/lib/utils/conditional-formatting/conditional-formatting.spec.js +142 -0
  54. package/lib/utils/conditional-formatting/conditional-formatting.spec.js.map +1 -0
  55. package/lib/utils/date-formatting.d.ts +19 -0
  56. package/lib/utils/date-formatting.d.ts.map +1 -0
  57. package/lib/utils/date-formatting.js +56 -0
  58. package/lib/utils/date-formatting.js.map +1 -0
  59. package/lib/utils/date-formatting.spec.d.ts +2 -0
  60. package/lib/utils/date-formatting.spec.d.ts.map +1 -0
  61. package/lib/utils/date-formatting.spec.js +124 -0
  62. package/lib/utils/date-formatting.spec.js.map +1 -0
  63. package/package.json +10 -7
  64. package/src/index.ts +3 -0
  65. package/src/main/custom-chart-context.spec.ts +291 -266
  66. package/src/main/custom-chart-context.ts +164 -46
  67. package/src/main/post-message-event-bridge.spec.ts +0 -3
  68. package/src/main/post-message-event-bridge.ts +36 -62
  69. package/src/main/util.ts +11 -0
  70. package/src/react/use-custom-chart-context.spec.tsx +149 -122
  71. package/src/test/test-conditional-formatting-utils.ts +95 -0
  72. package/src/types/answer-column.types.ts +98 -0
  73. package/src/types/common.types.ts +82 -10
  74. package/src/types/conditional-formatting.types.ts +172 -0
  75. package/src/types/configurator.types.ts +21 -0
  76. package/src/types/ts-to-chart-event.types.ts +15 -0
  77. package/src/types/visual-prop.types.ts +7 -0
  78. package/src/utils/conditional-formatting/conditional-formatting.spec.ts +469 -0
  79. package/src/utils/conditional-formatting/conditional-formatting.ts +229 -0
  80. package/src/utils/date-formatting.spec.ts +168 -0
  81. package/src/utils/date-formatting.ts +109 -0
  82. package/cjs/index.d.ts +0 -9
  83. package/cjs/index.d.ts.map +0 -1
  84. package/cjs/index.js +0 -25
  85. package/cjs/index.js.map +0 -1
  86. package/cjs/main/custom-chart-context.d.ts +0 -47
  87. package/cjs/main/custom-chart-context.d.ts.map +0 -1
  88. package/cjs/main/custom-chart-context.js +0 -326
  89. package/cjs/main/custom-chart-context.js.map +0 -1
  90. package/cjs/main/custom-chart-context.spec.d.ts +0 -2
  91. package/cjs/main/custom-chart-context.spec.d.ts.map +0 -1
  92. package/cjs/main/custom-chart-context.spec.js +0 -699
  93. package/cjs/main/custom-chart-context.spec.js.map +0 -1
  94. package/cjs/main/post-message-event-bridge.d.ts +0 -6
  95. package/cjs/main/post-message-event-bridge.d.ts.map +0 -1
  96. package/cjs/main/post-message-event-bridge.js +0 -46
  97. package/cjs/main/post-message-event-bridge.js.map +0 -1
  98. package/cjs/main/post-message-event-bridge.spec.d.ts +0 -2
  99. package/cjs/main/post-message-event-bridge.spec.d.ts.map +0 -1
  100. package/cjs/main/post-message-event-bridge.spec.js +0 -124
  101. package/cjs/main/post-message-event-bridge.spec.js.map +0 -1
  102. package/cjs/react/custom-chart-context-service.d.ts +0 -33
  103. package/cjs/react/custom-chart-context-service.d.ts.map +0 -1
  104. package/cjs/react/custom-chart-context-service.js +0 -118
  105. package/cjs/react/custom-chart-context-service.js.map +0 -1
  106. package/cjs/react/custom-chart.context.d.ts +0 -26
  107. package/cjs/react/custom-chart.context.d.ts.map +0 -1
  108. package/cjs/react/custom-chart.context.js +0 -105
  109. package/cjs/react/custom-chart.context.js.map +0 -1
  110. package/cjs/react/custom-chart.spec.d.ts +0 -2
  111. package/cjs/react/custom-chart.spec.d.ts.map +0 -1
  112. package/cjs/react/custom-chart.spec.js +0 -72
  113. package/cjs/react/custom-chart.spec.js.map +0 -1
  114. package/cjs/react/mocks/custom-chart-context-mock.d.ts +0 -3
  115. package/cjs/react/mocks/custom-chart-context-mock.d.ts.map +0 -1
  116. package/cjs/react/mocks/custom-chart-context-mock.js +0 -100
  117. package/cjs/react/mocks/custom-chart-context-mock.js.map +0 -1
  118. package/cjs/react/use-custom-chart-context.d.ts +0 -4
  119. package/cjs/react/use-custom-chart-context.d.ts.map +0 -1
  120. package/cjs/react/use-custom-chart-context.js +0 -95
  121. package/cjs/react/use-custom-chart-context.js.map +0 -1
  122. package/cjs/react/use-custom-chart-context.spec.d.ts +0 -2
  123. package/cjs/react/use-custom-chart-context.spec.d.ts.map +0 -1
  124. package/cjs/react/use-custom-chart-context.spec.js +0 -391
  125. package/cjs/react/use-custom-chart-context.spec.js.map +0 -1
  126. package/cjs/react/use-custom-chart-context.util.d.ts +0 -6
  127. package/cjs/react/use-custom-chart-context.util.d.ts.map +0 -1
  128. package/cjs/react/use-custom-chart-context.util.js +0 -60
  129. package/cjs/react/use-custom-chart-context.util.js.map +0 -1
  130. package/cjs/react/use-custom-chart-types.d.ts +0 -23
  131. package/cjs/react/use-custom-chart-types.d.ts.map +0 -1
  132. package/cjs/react/use-custom-chart-types.js +0 -3
  133. package/cjs/react/use-custom-chart-types.js.map +0 -1
  134. package/cjs/test/test-utils.d.ts +0 -7
  135. package/cjs/test/test-utils.d.ts.map +0 -1
  136. package/cjs/test/test-utils.js +0 -14
  137. package/cjs/test/test-utils.js.map +0 -1
  138. package/cjs/types/answer-column.types.d.ts +0 -69
  139. package/cjs/types/answer-column.types.d.ts.map +0 -1
  140. package/cjs/types/answer-column.types.js +0 -59
  141. package/cjs/types/answer-column.types.js.map +0 -1
  142. package/cjs/types/chart-to-ts-event.types.d.ts +0 -98
  143. package/cjs/types/chart-to-ts-event.types.d.ts.map +0 -1
  144. package/cjs/types/chart-to-ts-event.types.js +0 -32
  145. package/cjs/types/chart-to-ts-event.types.js.map +0 -1
  146. package/cjs/types/common.types.d.ts +0 -51
  147. package/cjs/types/common.types.d.ts.map +0 -1
  148. package/cjs/types/common.types.js +0 -3
  149. package/cjs/types/common.types.js.map +0 -1
  150. package/cjs/types/configurator.types.d.ts +0 -15
  151. package/cjs/types/configurator.types.d.ts.map +0 -1
  152. package/cjs/types/configurator.types.js +0 -3
  153. package/cjs/types/configurator.types.js.map +0 -1
  154. package/cjs/types/ts-to-chart-event.types.d.ts +0 -91
  155. package/cjs/types/ts-to-chart-event.types.d.ts.map +0 -1
  156. package/cjs/types/ts-to-chart-event.types.js +0 -18
  157. package/cjs/types/ts-to-chart-event.types.js.map +0 -1
  158. package/cjs/types/visual-prop.types.d.ts +0 -73
  159. package/cjs/types/visual-prop.types.d.ts.map +0 -1
  160. package/cjs/types/visual-prop.types.js +0 -3
  161. package/cjs/types/visual-prop.types.js.map +0 -1
  162. package/lib/dist/ts-chart-sdk.d.ts +0 -312
  163. package/lib/react/custom-chart-context-service.d.ts +0 -33
  164. package/lib/react/custom-chart-context-service.d.ts.map +0 -1
  165. package/lib/react/custom-chart-context-service.js +0 -91
  166. package/lib/react/custom-chart-context-service.js.map +0 -1
  167. package/lib/react/custom-chart.context.d.ts +0 -26
  168. package/lib/react/custom-chart.context.d.ts.map +0 -1
  169. package/lib/react/custom-chart.context.js +0 -78
  170. package/lib/react/custom-chart.context.js.map +0 -1
  171. package/lib/react/custom-chart.spec.d.ts +0 -2
  172. package/lib/react/custom-chart.spec.d.ts.map +0 -1
  173. package/lib/react/custom-chart.spec.js +0 -67
  174. 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 { ChartConfigEditorDefinition } from '../types/configurator.types';
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 { VisualPropEditorDefinition } from '../types/visual-prop.types';
46
- import * as PostMessageEventBridge from './post-message-event-bridge';
47
-
48
- let isInitialized = false;
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: (chartConfig: ChartConfig[]) => Query[];
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?: 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?: 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
- PostMessageEventBridge.destroyMessageListener(this.eventProcessor);
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 PostMessageEventBridge.postMessageToHostApp(
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
- PostMessageEventBridge.initMessageListener(this.eventProcessor);
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 = (event: MessageEvent) => {
472
- if (event.data.source !== 'ts-host-app') {
473
- return;
474
- }
569
+ private eventProcessor = (data: any) => {
570
+ console.log('Chart Context: message received:', data.eventType, data);
475
571
 
476
- console.log(
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
- if (!_.isNil(event.ports[0])) {
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
- (payload: VisualPropsValidateEventPayload): ValidationResponse => {
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
- (payload: ChartConfigValidateEventPayload): ValidationResponse => {
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.chartContextProps.chartConfigEditorDefinition,
899
+ this.getChartConfigEditorDefinition(),
784
900
  visualPropEditorDefinition:
785
- this.chartContextProps.visualPropEditorDefinition,
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 = (event: MessageEvent): any => {
913
+ private executeEventListenerCBs = (data: any): any => {
796
914
  // do basic sanity
797
- const payload = event.data.payload;
915
+ const payload = data.payload;
798
916
  let response;
799
- if (_.isArray(this.eventListeners[event.data.eventType])) {
800
- this.eventListeners[event.data.eventType].forEach((callback) => {
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: ${event.data.eventType}`,
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
- event.data.eventType,
933
+ data.eventType,
816
934
  response,
817
- this.eventListeners[event.data.eventType]?.length,
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
- * method to listen to messages using postMessage from the parent.
7
- *
8
- * @param {any} handleMessageEvent
9
- */
10
- const init = (handleMessageEvent: any) => {
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
- * stop listening to the messages
15
+ * method to listen to messages using postMessage from the parent.
16
16
  *
17
17
  * @param {any} handleMessageEvent
18
18
  */
19
- const destroy = (handleMessageEvent: any) => {
20
- window.removeEventListener('message', handleMessageEvent);
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
- new Promise((resolve, reject) => {
38
- const channel = new MessageChannel();
39
- channel.port1.onmessage = (res: any) => {
40
- channel.port1.close();
41
- const { hasError, error } = res.data;
42
- if (hasError) {
43
- console.log('ChartContext: message failure:', res.data);
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
- eventPayload,
79
- );
80
- reject(err);
81
- }
82
- });
83
-
84
- export {
85
- init as initMessageListener,
86
- destroy as destroyMessageListener,
87
- postMessageToHostApp,
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 };
@@ -0,0 +1,11 @@
1
+ export function timeout(promise: Promise<any>, ms: number, message?: string) {
2
+ return Promise.race([
3
+ promise,
4
+ new Promise((resolve, reject) => {
5
+ setTimeout(
6
+ () => reject(new Error(message || 'Operation timed out.')),
7
+ ms,
8
+ );
9
+ }),
10
+ ]);
11
+ }