embeddedaichatux 1.7.0 → 1.7.2

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 (2) hide show
  1. package/EmbeddedChat.js +91 -21
  2. package/package.json +1 -1
package/EmbeddedChat.js CHANGED
@@ -74,7 +74,7 @@
74
74
  this.addEventListeners();
75
75
  }
76
76
 
77
- updateIframes() {
77
+ async updateIframes() {
78
78
  const cleanedServerUrl = this.serverUrl.endsWith('/') ? this.serverUrl.slice(0, -1) : this.serverUrl;
79
79
  const baseIframeUrl = `${cleanedServerUrl}/ChatUX/${this.chatId}`;
80
80
  const minimizedPositionStyle = `position: fixed; right: 0; bottom: 0; width: ${this.width}px; height: ${this.minimizedHeight}; z-index: 100000; ${this.enableAnimation ? 'transition: height 0.3s ease, opacity 0.3s ease;' : ''}`;
@@ -87,6 +87,8 @@
87
87
  this.containerDiv.innerHTML = iframeHtml;
88
88
  this.iframe = this.containerDiv.querySelector("#embedded-chat");
89
89
  this.minimizedIframe = this.containerDiv.querySelector("#embedded-chat-minimized");
90
+
91
+ await this.waitForIframeLoad(this.iframe);
90
92
  }
91
93
 
92
94
  addEventListeners() {
@@ -110,9 +112,9 @@
110
112
  if (typeof e.data === "object" && (!e.data.chatId || e.data.chatId === this.chatId)) {
111
113
  const updates = {};
112
114
 
113
- // Preserve minimize on scroll functionality
114
- if (e.data.type === "setMinimizeOnScroll") {
115
- this.minimizeOnScroll = e.data.value === "true";
115
+ if (e.data.type === "setMinimizeOnScroll" || e.data.minimizeOnScroll !== undefined) {
116
+ const minimizeValue = e.data.type === "setMinimizeOnScroll" ? e.data.value : e.data.minimizeOnScroll;
117
+ this.minimizeOnScroll = minimizeValue === true || minimizeValue === "true";
116
118
  }
117
119
 
118
120
  // Collect updates for locale, width, and scale
@@ -138,7 +140,11 @@
138
140
  this.animateMinimize();
139
141
  }
140
142
  } else if (e.data.message === "show" || e.data.message === "maximize") {
141
- this.showMaximized();
143
+ const animate = e.data.animate === true;
144
+ this.showMaximized(animate);
145
+ } else if (e.data.message === "navigate" && e.data.url) {
146
+ // Handle the navigate message
147
+ window.location.href = e.data.url;
142
148
  }
143
149
  }
144
150
 
@@ -170,6 +176,31 @@
170
176
  }
171
177
  }
172
178
 
179
+ animateMaximize() {
180
+ if (this.mode !== 'ContactForm') {
181
+ // Start the animation for minimizing iframe
182
+ this.minimizedIframe.style.height = `${this.height}px`;
183
+ this.minimizedIframe.style.opacity = '0';
184
+
185
+ setTimeout(() => {
186
+ // Hide the minimized iframe
187
+ this.minimizedIframe.style.display = "none";
188
+ this.minimizedIframe.style.opacity = '1'; // Reset opacity
189
+
190
+ // Show and animate the main iframe
191
+ this.iframe.style.display = "block";
192
+ this.iframe.style.height = this.minimizedHeight; // Start with minimized height
193
+ this.iframe.style.opacity = '0'; // Start with zero opacity
194
+
195
+ // Trigger the maximize animation
196
+ setTimeout(() => {
197
+ this.iframe.style.height = `${this.height}px`; // Animate to full height
198
+ this.iframe.style.opacity = '1'; // Animate opacity to full
199
+ }, 10); // Small delay to allow display changes to take effect
200
+ }, this.enableAnimation ? 300 : 0); // Match the minimize animation duration
201
+ }
202
+ }
203
+
173
204
  applyIframeUpdates({ locale, width, scale } = {}) {
174
205
  // Update locale if provided and different (but do not update the iframe URL)
175
206
  if (locale && typeof locale === 'string' && locale !== this.locale) {
@@ -212,26 +243,44 @@
212
243
  }
213
244
 
214
245
 
215
- showMaximized() {
216
- this.minimizedIframe.style.display = "none";
217
-
218
- this.minimizedIframe.style.height = `${this.height}px`;
219
- this.minimizedIframe.style.opacity = ''; // Reset opacity to unset
220
-
221
- this.iframe.style.display = "block";
246
+ showMaximized(animate = false) {
247
+ const schedulePostMessage = () => {
248
+ if (this.iframe.contentWindow) {
249
+ this.iframe.contentWindow.postMessage({ message: "show" }, "*");
250
+ } else {
251
+ // Schedule a single retry with a delay
252
+ setTimeout(() => {
253
+ if (this.iframe.contentWindow) {
254
+ this.iframe.contentWindow.postMessage({ message: "show" }, "*");
255
+ } else {
256
+ console.warn("iframe contentWindow is not available.");
257
+ }
258
+ }, 500);
259
+ }
260
+ };
222
261
 
223
- if (this.isSafari() && !this.hasRefreshed) {
224
- // Force refresh by resetting the iframe's src
225
- const currentSrc = this.iframe.src;
226
- this.iframe.src = ''; // Temporarily clear the src
227
- this.iframe.src = currentSrc; // Reset to the original src to trigger a reload
228
- this.hasRefreshed = true; // Prevent endless loop
262
+ if (animate) {
263
+ this.animateMaximize();
229
264
  }
265
+ else {
266
+ this.minimizedIframe.style.display = "none";
267
+ this.minimizedIframe.style.height = `${this.height}px`;
268
+ this.minimizedIframe.style.opacity = ''; // Reset opacity to unset
269
+
270
+ this.iframe.style.display = "block";
271
+
272
+ if (this.isSafari() && !this.hasRefreshed) {
273
+ const currentSrc = this.iframe.src;
274
+ this.iframe.src = ''; // Temporarily clear the src
275
+ this.iframe.src = currentSrc; // Reset to the original src to trigger a reload
276
+ this.hasRefreshed = true; // Prevent endless loop
277
+ }
230
278
 
231
- this.iframe.style.height = `${this.height}px`;
232
- this.iframe.style.opacity = '1';
279
+ this.iframe.style.height = `${this.height}px`;
280
+ this.iframe.style.opacity = '1';
233
281
 
234
- this.iframe.contentWindow.postMessage({ message: "show" }, "*");
282
+ schedulePostMessage();
283
+ }
235
284
  }
236
285
 
237
286
  applyScale(scale) {
@@ -300,6 +349,27 @@
300
349
 
301
350
  return `${baseIframeUrl}?${urlParams.toString()}`;
302
351
  }
352
+
353
+ waitForIframeLoad(iframe, timeout = 5000) {
354
+ return new Promise((resolve, reject) => {
355
+ // Check if the iframe is already loaded
356
+ if (iframe.contentDocument || iframe.contentWindow) {
357
+ resolve();
358
+ return;
359
+ }
360
+
361
+ // Set up a timeout to avoid waiting indefinitely
362
+ const timeoutId = setTimeout(() => {
363
+ reject(new Error('Iframe did not load within the timeout period.'));
364
+ }, timeout);
365
+
366
+ // Add the load event listener
367
+ iframe.addEventListener('load', () => {
368
+ clearTimeout(timeoutId); // Clear the timeout
369
+ resolve();
370
+ }, { once: true }); // Ensure the listener is triggered only once
371
+ });
372
+ }
303
373
  }
304
374
 
305
375
  export function initEmbeddedChat(containerDiv, chatId, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "embeddedaichatux",
3
- "version": "1.7.0",
3
+ "version": "1.7.2",
4
4
  "description": "A lightweight and customizable embedded AI chat UI component that seamlessly integrates into web applications, offering minimized and expanded views, with iframe-based content rendering.",
5
5
  "main": "EmbeddedChat.js",
6
6
  "scripts": {