@buni.ai/chatbot-angular 1.0.24 → 1.0.25

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/index.js CHANGED
@@ -60,6 +60,31 @@ class BuniChatWidget {
60
60
  this.chatTargetOrigin = null;
61
61
  this.handshakeComplete = false;
62
62
  this.outboundMessageQueue = [];
63
+ this.displayMode = "hidden";
64
+ this.serverBehavior = {};
65
+ }
66
+ isMinimalModeEnabled() {
67
+ var _a, _b;
68
+ if (typeof ((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.enableMinimalMode) === "boolean") {
69
+ return this.options.config.enableMinimalMode;
70
+ }
71
+ return Boolean(this.serverBehavior.enableMinimalMode);
72
+ }
73
+ syncServerBehaviorFromData(data) {
74
+ if (!data || typeof data !== "object") {
75
+ return;
76
+ }
77
+ const behaviorCandidate = data.behavior && typeof data.behavior === "object"
78
+ ? data.behavior
79
+ : data;
80
+ if (typeof behaviorCandidate.enableMinimalMode === "boolean") {
81
+ this.serverBehavior.enableMinimalMode = behaviorCandidate.enableMinimalMode;
82
+ }
83
+ if (behaviorCandidate.defaultMode === "full" ||
84
+ behaviorCandidate.defaultMode === "minimal" ||
85
+ behaviorCandidate.defaultMode === "hidden") {
86
+ this.serverBehavior.defaultMode = behaviorCandidate.defaultMode;
87
+ }
63
88
  }
64
89
  async initialize(options) {
65
90
  this.options = options;
@@ -223,6 +248,15 @@ class BuniChatWidget {
223
248
  params.set("startButtonText", config.startButtonText);
224
249
  if (config.preChatFormFields)
225
250
  params.set("preChatFormFields", JSON.stringify(config.preChatFormFields));
251
+ if (config.enableMinimalMode !== undefined) {
252
+ params.set("enableMinimalMode", String(config.enableMinimalMode));
253
+ }
254
+ if (config.defaultMode) {
255
+ params.set("defaultMode", config.defaultMode);
256
+ }
257
+ if (config.autoMessages && Array.isArray(config.autoMessages)) {
258
+ params.set("autoMessages", JSON.stringify(config.autoMessages));
259
+ }
226
260
  chatIframe.src = `${this.getBaseUrl()}/embed/chat?${params.toString()}`;
227
261
  // Chat iframe styling - initially hidden
228
262
  chatIframe.style.cssText = `
@@ -263,13 +297,15 @@ class BuniChatWidget {
263
297
  this.triggerIframe = triggerIframe;
264
298
  this.chatIframe = chatIframe;
265
299
  this.state.isMinimized = true;
300
+ this.state.displayMode = "hidden";
301
+ this.displayMode = "hidden";
266
302
  this.state.isLoaded = true;
267
303
  resolve();
268
304
  };
269
305
  };
270
306
  // Listen for trigger and connection events
271
307
  window.addEventListener("message", (event) => {
272
- var _a, _b;
308
+ var _a, _b, _c, _d, _e, _f;
273
309
  const payload = event.data;
274
310
  if (!payload || typeof payload !== "object")
275
311
  return;
@@ -280,20 +316,53 @@ class BuniChatWidget {
280
316
  // Check if message is from chat iframe
281
317
  if (event.source === ((_a = this.chatIframe) === null || _a === void 0 ? void 0 : _a.contentWindow) &&
282
318
  event.origin === this.getBaseUrl()) {
319
+ this.syncServerBehaviorFromData(payload.data);
283
320
  // Connection is ready, show the trigger button
284
321
  if (container && !config.hideDefaultTrigger) {
285
322
  container.style.display = "block";
286
323
  }
287
324
  // Emit connection_ready event for consumers
288
- this.emit("connection_ready", { timestamp: Date.now() });
325
+ this.emit("connection_ready", {
326
+ timestamp: Date.now(),
327
+ behavior: (_b = payload.data) === null || _b === void 0 ? void 0 : _b.behavior,
328
+ });
289
329
  if (this.options.onConnectionReady &&
290
330
  typeof this.options.onConnectionReady === "function") {
291
- this.options.onConnectionReady({ timestamp: Date.now() });
331
+ this.options.onConnectionReady({
332
+ timestamp: Date.now(),
333
+ behavior: (_c = payload.data) === null || _c === void 0 ? void 0 : _c.behavior,
334
+ });
335
+ }
336
+ }
337
+ break;
338
+ case "chatbot:close":
339
+ if (event.source === ((_d = this.chatIframe) === null || _d === void 0 ? void 0 : _d.contentWindow) &&
340
+ event.origin === this.getBaseUrl()) {
341
+ this.closeChat();
342
+ this.displayMode = "hidden";
343
+ this.state.displayMode = "hidden";
344
+ }
345
+ break;
346
+ case "chatbot:minimize":
347
+ if (event.source === ((_e = this.chatIframe) === null || _e === void 0 ? void 0 : _e.contentWindow) &&
348
+ event.origin === this.getBaseUrl()) {
349
+ if (this.isMinimalModeEnabled()) {
350
+ if (!this.state.isOpen) {
351
+ void this.openChat();
352
+ }
353
+ this.postMessageToWidget("minimize");
354
+ this.displayMode = "minimal";
355
+ this.state.displayMode = "minimal";
356
+ }
357
+ else {
358
+ this.closeChat();
359
+ this.displayMode = "hidden";
360
+ this.state.displayMode = "hidden";
292
361
  }
293
362
  }
294
363
  break;
295
364
  case "trigger_clicked":
296
- if (event.source === ((_b = this.triggerIframe) === null || _b === void 0 ? void 0 : _b.contentWindow) &&
365
+ if (event.source === ((_f = this.triggerIframe) === null || _f === void 0 ? void 0 : _f.contentWindow) &&
297
366
  event.origin === window.location.origin) {
298
367
  this.openChat();
299
368
  }
@@ -303,13 +372,11 @@ class BuniChatWidget {
303
372
  // Trigger the load event
304
373
  // if by 10 seconds the ready message is not received, we want to manually, set the container to visible
305
374
  setTimeout(() => {
306
- if (!this.state.isLoaded) {
307
- if (container &&
308
- !config.hideDefaultTrigger &&
309
- container.style.display === "none" &&
310
- this.state.isOpen === false) {
311
- container.style.display = "block";
312
- }
375
+ if (container &&
376
+ !config.hideDefaultTrigger &&
377
+ container.style.display === "none" &&
378
+ this.state.isOpen === false) {
379
+ container.style.display = "block";
313
380
  }
314
381
  }, 10000);
315
382
  triggerIframe.src = "about:blank";
@@ -486,6 +553,8 @@ class BuniChatWidget {
486
553
  this.chatIframe.style.visibility = "visible";
487
554
  this.state.isMinimized = false;
488
555
  this.state.isOpen = true;
556
+ this.displayMode = "full";
557
+ this.state.displayMode = "full";
489
558
  this.emit("maximized", { timestamp: Date.now() });
490
559
  }
491
560
  closeChat() {
@@ -518,6 +587,8 @@ class BuniChatWidget {
518
587
  this.triggerIframe.style.display = "block";
519
588
  this.state.isMinimized = true;
520
589
  this.state.isOpen = false;
590
+ this.displayMode = "hidden";
591
+ this.state.displayMode = "hidden";
521
592
  this.emit("minimized", { timestamp: Date.now() });
522
593
  }
523
594
  getPositionStyles(position, isMinimized = false) {
@@ -609,8 +680,18 @@ class BuniChatWidget {
609
680
  }
610
681
  break;
611
682
  case "minimized":
612
- // User clicked minimize in chat - close chat and show trigger
613
- this.closeChat();
683
+ this.syncServerBehaviorFromData(data);
684
+ if (this.isMinimalModeEnabled()) {
685
+ if (!this.state.isOpen) {
686
+ void this.openChat();
687
+ }
688
+ this.displayMode = "minimal";
689
+ this.state.displayMode = "minimal";
690
+ }
691
+ else {
692
+ // Backward-compatible behavior.
693
+ this.closeChat();
694
+ }
614
695
  break;
615
696
  case "new_unread_message":
616
697
  // Update unread count in trigger
@@ -745,10 +826,21 @@ class BuniChatWidget {
745
826
  }
746
827
  }
747
828
  minimize() {
829
+ if (this.isMinimalModeEnabled()) {
830
+ if (!this.state.isOpen) {
831
+ void this.openChat();
832
+ }
833
+ this.postMessageToWidget("minimize");
834
+ this.displayMode = "minimal";
835
+ this.state.displayMode = "minimal";
836
+ return;
837
+ }
748
838
  this.closeChat();
749
839
  }
750
840
  maximize() {
751
- this.openChat();
841
+ void this.openChat();
842
+ this.displayMode = "full";
843
+ this.state.displayMode = "full";
752
844
  }
753
845
  setCustomerData(data) {
754
846
  this.customerData = { ...this.customerData, ...data };
@@ -789,6 +881,8 @@ class BuniChatWidget {
789
881
  }
790
882
  close() {
791
883
  this.closeChat();
884
+ this.displayMode = "hidden";
885
+ this.state.displayMode = "hidden";
792
886
  }
793
887
  on(event, callback) {
794
888
  if (!this.eventListeners.has(event)) {
@@ -824,7 +918,10 @@ class BuniChatWidget {
824
918
  }
825
919
  }
826
920
  getState() {
827
- return { ...this.state };
921
+ return {
922
+ ...this.state,
923
+ displayMode: this.displayMode,
924
+ };
828
925
  }
829
926
  isReady() {
830
927
  return this.state.isLoaded;