@epam/ai-dial-visualizer-connector 0.11.0-rc.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.
- package/LICENSE +201 -0
- package/README.md +13 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +242 -0
- package/package.json +21 -0
- package/src/index.d.ts +2 -0
- package/src/lib/VisualizerConnector.d.ts +88 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023 EPAM Systems, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# DIAL Visualizer Connector
|
|
2
|
+
|
|
3
|
+
DIAL Visualizer Connector is a library for connecting DIAL CHAT with custom visualizers - applications which could visualize some special type data (for example **plot data** for the **Plotly**).
|
|
4
|
+
|
|
5
|
+
## Public classes to use
|
|
6
|
+
|
|
7
|
+
`VisualizerConnector` - class which creates iframe with provided **VisualizerConnector**, allows to interact with **Visualizer** rendered in the iframe (send data). Types for configuration options is `VisualizerConnectorOptions`.
|
|
8
|
+
|
|
9
|
+
## Prerequisites
|
|
10
|
+
|
|
11
|
+
How to configure your DIAL CHAT to use **Custom Visualizers** you could find [here](../chat-visualizer-connector/README.md).
|
|
12
|
+
|
|
13
|
+
At **Visualizer** side should be used [DIAL Chat VIsualizer Connector](../chat-visualizer-connector/README.md)
|
package/index.esm.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { VisualizerConnectorEvents, Task, setStyles, visualizerConnectorLibName, DeferredRequest } from '@epam/ai-dial-shared';
|
|
2
|
+
|
|
3
|
+
/******************************************************************************
|
|
4
|
+
Copyright (c) Microsoft Corporation.
|
|
5
|
+
|
|
6
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
7
|
+
purpose with or without fee is hereby granted.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
***************************************************************************** */
|
|
17
|
+
|
|
18
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
19
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
20
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
21
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
22
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
23
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
24
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
29
|
+
var e = new Error(message);
|
|
30
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const defaultLoaderSVG = `<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
34
|
+
<path d="M24 39V46.5" stroke="#7F8792" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
|
|
35
|
+
<path opacity="0.4" d="M9 24H1.5" stroke="#7F8792" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
|
|
36
|
+
<path opacity="0.5" d="M8.0918 8.0918L13.3994 13.3994" stroke="#7F8792" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
|
|
37
|
+
<path opacity="0.6" d="M24 1.5V9" stroke="#7F8792" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
|
|
38
|
+
<path opacity="0.7" d="M39.9121 8.08594L37.2607 10.7373L34.6094 13.3887" stroke="#7F8792" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
|
|
39
|
+
<path opacity="0.8" d="M46.5 24H39" stroke="#7F8792" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
|
|
40
|
+
<path opacity="0.9" d="M34.6055 34.6055L39.9082 39.9082" stroke="#7F8792" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
|
|
41
|
+
<path opacity="0.3" d="M13.3936 34.6055L8.08594 39.9131" stroke="#7F8792" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
|
|
42
|
+
<animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0 0 0" to="360 0 0" dur="1s" repeatCount="indefinite" />
|
|
43
|
+
</svg>
|
|
44
|
+
`;
|
|
45
|
+
class VisualizerConnector {
|
|
46
|
+
/**
|
|
47
|
+
* Creates a VisualizerConnector
|
|
48
|
+
* @param root {HTMLElement | string} reference or selector to parent container where the iframe should be placed
|
|
49
|
+
* @param options {VisualizerConnectorOptions} visualizer connector options ( hostDomain, domain etc.) which will be used to create iframe
|
|
50
|
+
*/
|
|
51
|
+
constructor(root, options) {
|
|
52
|
+
this.loaderDisplayCss = 'flex';
|
|
53
|
+
/**
|
|
54
|
+
* Callback to post message event, contains mapping event to this.requests, mapping event to this.subscriptions
|
|
55
|
+
* If event.data.type === '{visualizerName}/READY' means that Visualizer ready to receive message -> this.iframeInteraction.complete()
|
|
56
|
+
* @param event {MessageEvent} post message event
|
|
57
|
+
*/
|
|
58
|
+
this.process = (event) => {
|
|
59
|
+
var _a, _b, _c, _d;
|
|
60
|
+
if (event.data.type ===
|
|
61
|
+
`${this.options.visualizerName}/${VisualizerConnectorEvents.ready}`) {
|
|
62
|
+
this.hideLoader();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (event.data.type ===
|
|
66
|
+
`${this.options.visualizerName}/${VisualizerConnectorEvents.readyToInteract}`) {
|
|
67
|
+
this.hideLoader();
|
|
68
|
+
this.iframeInteraction.complete();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (!((_a = event.data) === null || _a === void 0 ? void 0 : _a.type))
|
|
72
|
+
return;
|
|
73
|
+
// Try to map event, because type doesn't have requestId
|
|
74
|
+
if (!((_b = event.data) === null || _b === void 0 ? void 0 : _b.requestId)) {
|
|
75
|
+
this.processEvent(event.data.type, (_c = event.data) === null || _c === void 0 ? void 0 : _c.payload);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
for (const request of this.requests) {
|
|
79
|
+
if (request.match(event.data.type, event.data.requestId)) {
|
|
80
|
+
request.reply((_d = event.data) === null || _d === void 0 ? void 0 : _d.payload);
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// if request was replied -> should delete it from this.requests
|
|
85
|
+
this.requests = this.requests.filter((request) => !request.isReplied);
|
|
86
|
+
};
|
|
87
|
+
this.options = options;
|
|
88
|
+
this.root = this.getRoot(root);
|
|
89
|
+
this.requests = [];
|
|
90
|
+
this.subscriptions = [];
|
|
91
|
+
this.iframeInteraction = new Task();
|
|
92
|
+
this.iframe = this.initIframe();
|
|
93
|
+
this.loader = this.initLoader((options === null || options === void 0 ? void 0 : options.loaderStyles) || {}, options === null || options === void 0 ? void 0 : options.loaderClass, options === null || options === void 0 ? void 0 : options.loaderInnerHTML);
|
|
94
|
+
this.root.appendChild(this.loader);
|
|
95
|
+
this.root.appendChild(this.iframe);
|
|
96
|
+
setStyles(this.root, { position: 'relative' });
|
|
97
|
+
this.showLoader();
|
|
98
|
+
window.addEventListener('message', this.process);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Creates iframe add set initial options to it
|
|
102
|
+
* @returns {HTMLIFrameElement} reference to iframe element
|
|
103
|
+
*/
|
|
104
|
+
initIframe() {
|
|
105
|
+
const iframe = document.createElement('iframe');
|
|
106
|
+
iframe.src = this.options.domain;
|
|
107
|
+
iframe.sandbox.add('allow-same-origin');
|
|
108
|
+
iframe.sandbox.add('allow-scripts');
|
|
109
|
+
iframe.sandbox.add('allow-modals');
|
|
110
|
+
iframe.sandbox.add('allow-forms');
|
|
111
|
+
iframe.sandbox.add('allow-downloads');
|
|
112
|
+
iframe.style.height = '100%';
|
|
113
|
+
iframe.style.width = '100%';
|
|
114
|
+
iframe.style.border = 'none';
|
|
115
|
+
iframe.loading = 'lazy';
|
|
116
|
+
return iframe;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Creates loader and add styles
|
|
120
|
+
* @returns {HTMLElement} reference to loader element
|
|
121
|
+
*/
|
|
122
|
+
initLoader(styles, className, loaderInnerHTML) {
|
|
123
|
+
const loader = document.createElement('div');
|
|
124
|
+
loader.innerHTML = loaderInnerHTML !== null && loaderInnerHTML !== void 0 ? loaderInnerHTML : defaultLoaderSVG;
|
|
125
|
+
if (className) {
|
|
126
|
+
loader.className = className;
|
|
127
|
+
}
|
|
128
|
+
if (styles === null || styles === void 0 ? void 0 : styles.display) {
|
|
129
|
+
this.loaderDisplayCss = styles.display;
|
|
130
|
+
}
|
|
131
|
+
setStyles(loader, Object.assign({ position: 'absolute', background: 'white', display: this.loaderDisplayCss, alignItems: 'center', justifyContent: 'center', left: '0', right: '0', top: '0', bottom: '0', zIndex: '2' }, styles));
|
|
132
|
+
return loader;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Shows loader
|
|
136
|
+
*/
|
|
137
|
+
showLoader() {
|
|
138
|
+
setStyles(this.loader, {
|
|
139
|
+
display: this.loaderDisplayCss,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Hides loader
|
|
144
|
+
*/
|
|
145
|
+
hideLoader() {
|
|
146
|
+
setStyles(this.loader, {
|
|
147
|
+
display: 'none',
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Displays if iframe ready to interact
|
|
152
|
+
* @returns {Promise<boolean>} if the promise resolved -> iframe ready to interact
|
|
153
|
+
*/
|
|
154
|
+
ready() {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
return this.iframeInteraction.ready();
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* If user provides reference to container ? returns the container : query the selector and return reference to container
|
|
161
|
+
* @param {HTMLElement | string} root reference to parent container or selector where iframe should be placed
|
|
162
|
+
* @returns {HTMLElement} reference to container where iframe would be placed
|
|
163
|
+
*/
|
|
164
|
+
getRoot(root) {
|
|
165
|
+
if (typeof root === 'string') {
|
|
166
|
+
const element = document.querySelector(root);
|
|
167
|
+
if (!element) {
|
|
168
|
+
throw new Error(`[${visualizerConnectorLibName}] There is no element with selector ${root} to append iframe`);
|
|
169
|
+
}
|
|
170
|
+
return element;
|
|
171
|
+
}
|
|
172
|
+
return root;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Going through all event callbacks and call it if eventType is the same as in subscription
|
|
176
|
+
* @param eventType {string} Name of event that DIAL send
|
|
177
|
+
* @param payload {unknown} Payload of event
|
|
178
|
+
*/
|
|
179
|
+
processEvent(eventType, payload) {
|
|
180
|
+
for (const subscription of this.subscriptions) {
|
|
181
|
+
if (subscription.eventType === eventType) {
|
|
182
|
+
subscription.callback(payload);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Creates DeferredRequests, put into the this.requests
|
|
188
|
+
* We don't put something into the this.requests until `this.ready()`
|
|
189
|
+
* @param type Request name
|
|
190
|
+
* @param payload Request payload
|
|
191
|
+
* @param waitForReady Is this request should wait for Visualizer ready (default: true)
|
|
192
|
+
* @returns {Promise<unknown>} Return promise with response payload when resolved
|
|
193
|
+
*/
|
|
194
|
+
send(type, payload, waitForReady = true) {
|
|
195
|
+
var _a;
|
|
196
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
197
|
+
if (waitForReady) {
|
|
198
|
+
yield this.iframeInteraction.ready();
|
|
199
|
+
}
|
|
200
|
+
if (!this.iframe.contentWindow) {
|
|
201
|
+
throw new Error(`[${visualizerConnectorLibName}] There is no content window to send requests`);
|
|
202
|
+
}
|
|
203
|
+
const request = new DeferredRequest(`${this.options.visualizerName}/${type}`, {
|
|
204
|
+
payload,
|
|
205
|
+
timeout: (_a = this.options) === null || _a === void 0 ? void 0 : _a.requestTimeout,
|
|
206
|
+
}, visualizerConnectorLibName);
|
|
207
|
+
this.requests.push(request);
|
|
208
|
+
this.iframe.contentWindow.postMessage(request.toPostMessage(), this.options.domain);
|
|
209
|
+
return request.promise;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Add callback with eventType to this.subscriptions
|
|
214
|
+
* @param eventType Event type
|
|
215
|
+
* @param callback Callback which should associated to the event type
|
|
216
|
+
* @returns {() => void} Callback which removes event callback from the this.requests
|
|
217
|
+
*/
|
|
218
|
+
subscribe(eventType, callback) {
|
|
219
|
+
this.subscriptions.push({ eventType, callback });
|
|
220
|
+
return () => {
|
|
221
|
+
this.subscriptions = this.subscriptions.filter((sub) => sub.callback !== callback);
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Sets Visualizer options (hostDomain,loaderStyles, etc.)
|
|
226
|
+
* @param options {VisualizerConnectorOptions} Options that should be set into the Visualizer
|
|
227
|
+
*/
|
|
228
|
+
setVisualizerConnectorOptions(options) {
|
|
229
|
+
this.options = options;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Destroys Visualizer
|
|
233
|
+
*/
|
|
234
|
+
destroy() {
|
|
235
|
+
window.removeEventListener('message', this.process);
|
|
236
|
+
this.iframeInteraction.fail('Chat Visualizer destroyed');
|
|
237
|
+
this.root.removeChild(this.iframe);
|
|
238
|
+
this.root.removeChild(this.loader);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export { VisualizerConnector };
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@epam/ai-dial-visualizer-connector",
|
|
3
|
+
"description": "Package for connecting custom visualizer into Dial Chat",
|
|
4
|
+
"homepage": "https://epam-rail.com",
|
|
5
|
+
"version": "0.11.0-rc.0",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@epam/ai-dial-shared": "0.11.0-rc.0"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/epam/ai-dial-chat/issues"
|
|
12
|
+
},
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/epam/ai-dial-chat.git",
|
|
17
|
+
"directory": "libs/visualizer-connector"
|
|
18
|
+
},
|
|
19
|
+
"module": "./index.esm.js",
|
|
20
|
+
"main": "./index.esm.js"
|
|
21
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { DeferredRequest, Styles, Task, VisualizerConnectorOptions, VisualizerConnectorRequest, VisualizerConnectorRequests } from '@epam/ai-dial-shared';
|
|
2
|
+
interface Subscription {
|
|
3
|
+
eventType: string;
|
|
4
|
+
callback: (payload: unknown) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare class VisualizerConnector {
|
|
7
|
+
protected root: HTMLElement;
|
|
8
|
+
protected subscriptions: Subscription[];
|
|
9
|
+
protected iframe: HTMLIFrameElement;
|
|
10
|
+
protected loader: HTMLElement;
|
|
11
|
+
protected loaderDisplayCss: string;
|
|
12
|
+
protected iframeInteraction: Task;
|
|
13
|
+
protected requests: DeferredRequest[];
|
|
14
|
+
protected options: VisualizerConnectorOptions;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a VisualizerConnector
|
|
17
|
+
* @param root {HTMLElement | string} reference or selector to parent container where the iframe should be placed
|
|
18
|
+
* @param options {VisualizerConnectorOptions} visualizer connector options ( hostDomain, domain etc.) which will be used to create iframe
|
|
19
|
+
*/
|
|
20
|
+
constructor(root: HTMLElement | string, options: VisualizerConnectorOptions);
|
|
21
|
+
/**
|
|
22
|
+
* Creates iframe add set initial options to it
|
|
23
|
+
* @returns {HTMLIFrameElement} reference to iframe element
|
|
24
|
+
*/
|
|
25
|
+
protected initIframe(): HTMLIFrameElement;
|
|
26
|
+
/**
|
|
27
|
+
* Creates loader and add styles
|
|
28
|
+
* @returns {HTMLElement} reference to loader element
|
|
29
|
+
*/
|
|
30
|
+
protected initLoader(styles: Styles, className?: string, loaderInnerHTML?: string): HTMLElement;
|
|
31
|
+
/**
|
|
32
|
+
* Shows loader
|
|
33
|
+
*/
|
|
34
|
+
private showLoader;
|
|
35
|
+
/**
|
|
36
|
+
* Hides loader
|
|
37
|
+
*/
|
|
38
|
+
private hideLoader;
|
|
39
|
+
/**
|
|
40
|
+
* Displays if iframe ready to interact
|
|
41
|
+
* @returns {Promise<boolean>} if the promise resolved -> iframe ready to interact
|
|
42
|
+
*/
|
|
43
|
+
ready(): Promise<boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* If user provides reference to container ? returns the container : query the selector and return reference to container
|
|
46
|
+
* @param {HTMLElement | string} root reference to parent container or selector where iframe should be placed
|
|
47
|
+
* @returns {HTMLElement} reference to container where iframe would be placed
|
|
48
|
+
*/
|
|
49
|
+
protected getRoot(root: HTMLElement | string): HTMLElement;
|
|
50
|
+
/**
|
|
51
|
+
* Callback to post message event, contains mapping event to this.requests, mapping event to this.subscriptions
|
|
52
|
+
* If event.data.type === '{visualizerName}/READY' means that Visualizer ready to receive message -> this.iframeInteraction.complete()
|
|
53
|
+
* @param event {MessageEvent} post message event
|
|
54
|
+
*/
|
|
55
|
+
protected process: (event: MessageEvent<VisualizerConnectorRequest>) => void;
|
|
56
|
+
/**
|
|
57
|
+
* Going through all event callbacks and call it if eventType is the same as in subscription
|
|
58
|
+
* @param eventType {string} Name of event that DIAL send
|
|
59
|
+
* @param payload {unknown} Payload of event
|
|
60
|
+
*/
|
|
61
|
+
protected processEvent(eventType: string, payload?: unknown): void;
|
|
62
|
+
/**
|
|
63
|
+
* Creates DeferredRequests, put into the this.requests
|
|
64
|
+
* We don't put something into the this.requests until `this.ready()`
|
|
65
|
+
* @param type Request name
|
|
66
|
+
* @param payload Request payload
|
|
67
|
+
* @param waitForReady Is this request should wait for Visualizer ready (default: true)
|
|
68
|
+
* @returns {Promise<unknown>} Return promise with response payload when resolved
|
|
69
|
+
*/
|
|
70
|
+
send(type: VisualizerConnectorRequests, payload?: unknown, waitForReady?: boolean): Promise<unknown>;
|
|
71
|
+
/**
|
|
72
|
+
* Add callback with eventType to this.subscriptions
|
|
73
|
+
* @param eventType Event type
|
|
74
|
+
* @param callback Callback which should associated to the event type
|
|
75
|
+
* @returns {() => void} Callback which removes event callback from the this.requests
|
|
76
|
+
*/
|
|
77
|
+
subscribe(eventType: string, callback: (payload: unknown) => void): () => void;
|
|
78
|
+
/**
|
|
79
|
+
* Sets Visualizer options (hostDomain,loaderStyles, etc.)
|
|
80
|
+
* @param options {VisualizerConnectorOptions} Options that should be set into the Visualizer
|
|
81
|
+
*/
|
|
82
|
+
setVisualizerConnectorOptions(options: VisualizerConnectorOptions): void;
|
|
83
|
+
/**
|
|
84
|
+
* Destroys Visualizer
|
|
85
|
+
*/
|
|
86
|
+
destroy(): void;
|
|
87
|
+
}
|
|
88
|
+
export {};
|