@hellotext/hellotext 1.8.4 → 1.8.5
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/__tests__/core/configuration/webchat_test.js +163 -0
- package/__tests__/hellotext_test.js +97 -101
- package/dist/hellotext.js +1 -1
- package/docs/webchat.md +101 -0
- package/jest.config.js +1 -0
- package/jest.setup.js +9 -0
- package/lib/api/index.js +6 -0
- package/lib/api/web_chat/messages.js +74 -0
- package/lib/api/web_chats.js +53 -0
- package/lib/api/webchat/messages.js +85 -0
- package/lib/api/webchats.js +53 -0
- package/lib/builders/logo_builder.js +2 -1
- package/lib/channels/application_channel.js +68 -0
- package/lib/channels/web_chat_channel.js +97 -0
- package/lib/channels/webchat_channel.js +112 -0
- package/lib/controllers/mixins/usePopover.js +61 -0
- package/lib/controllers/web_chat/pagination_controller.js +81 -0
- package/lib/controllers/webchat/emoji_picker_controller.js +134 -0
- package/lib/controllers/webchat_controller.js +446 -0
- package/lib/core/configuration/web_chat.js +168 -0
- package/lib/core/configuration/webchat.js +185 -0
- package/lib/core/configuration.js +6 -1
- package/lib/core/event.js +1 -1
- package/lib/hellotext.js +18 -9
- package/lib/index.js +4 -0
- package/lib/models/business.js +9 -1
- package/lib/models/index.js +7 -0
- package/lib/models/web_chat.js +51 -0
- package/lib/models/webchat.js +51 -0
- package/package.json +8 -7
- package/src/api/index.js +5 -0
- package/src/api/webchat/messages.js +55 -0
- package/src/api/webchats.js +30 -0
- package/src/builders/logo_builder.js +8 -4
- package/src/channels/application_channel.js +45 -0
- package/src/channels/webchat_channel.js +77 -0
- package/src/controllers/mixins/usePopover.js +50 -0
- package/src/controllers/webchat/emoji_picker_controller.js +82 -0
- package/src/controllers/webchat_controller.js +448 -0
- package/src/core/configuration/webchat.js +176 -0
- package/src/core/configuration.js +5 -0
- package/src/core/event.js +10 -1
- package/src/hellotext.js +7 -2
- package/src/index.js +5 -0
- package/src/models/business.js +11 -1
- package/src/models/index.js +1 -0
- package/src/models/webchat.js +28 -0
- package/styles/index.css +4 -0
- package/webpack.config.js +1 -1
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _stimulus = require("@hotwired/stimulus");
|
|
8
|
+
var _dom = require("@floating-ui/dom");
|
|
9
|
+
var _messages = _interopRequireDefault(require("../api/webchat/messages"));
|
|
10
|
+
var _webchat_channel = _interopRequireDefault(require("../channels/webchat_channel"));
|
|
11
|
+
var _hellotext = _interopRequireDefault(require("../hellotext"));
|
|
12
|
+
var _webchat = require("../core/configuration/webchat");
|
|
13
|
+
var _usePopover = require("./mixins/usePopover");
|
|
14
|
+
var _logo_builder = require("../builders/logo_builder");
|
|
15
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
17
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
18
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
20
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
21
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
22
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
23
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
24
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
25
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
26
|
+
function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
|
|
27
|
+
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
|
|
28
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
29
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
30
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
31
|
+
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
32
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
33
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
34
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
35
|
+
var _default = /*#__PURE__*/function (_Controller) {
|
|
36
|
+
_inherits(_default, _Controller);
|
|
37
|
+
var _super = _createSuper(_default);
|
|
38
|
+
function _default() {
|
|
39
|
+
_classCallCheck(this, _default);
|
|
40
|
+
return _super.apply(this, arguments);
|
|
41
|
+
}
|
|
42
|
+
_createClass(_default, [{
|
|
43
|
+
key: "initialize",
|
|
44
|
+
value: function initialize() {
|
|
45
|
+
this.messagesAPI = new _messages.default(this.idValue);
|
|
46
|
+
this.webChatChannel = new _webchat_channel.default(this.idValue, _hellotext.default.session, this.conversationIdValue);
|
|
47
|
+
this.files = [];
|
|
48
|
+
this.onMessageReceived = this.onMessageReceived.bind(this);
|
|
49
|
+
this.onConversationAssignment = this.onConversationAssignment.bind(this);
|
|
50
|
+
this.onAgentOnline = this.onAgentOnline.bind(this);
|
|
51
|
+
this.onMessageReaction = this.onMessageReaction.bind(this);
|
|
52
|
+
this.onScroll = this.onScroll.bind(this);
|
|
53
|
+
_get(_getPrototypeOf(_default.prototype), "initialize", this).call(this);
|
|
54
|
+
}
|
|
55
|
+
}, {
|
|
56
|
+
key: "connect",
|
|
57
|
+
value: function connect() {
|
|
58
|
+
(0, _usePopover.usePopover)(this);
|
|
59
|
+
this.popoverTarget.classList.add(..._webchat.Webchat.classes);
|
|
60
|
+
this.triggerTarget.classList.add(..._webchat.Webchat.triggerClasses);
|
|
61
|
+
this.setupFloatingUI({
|
|
62
|
+
trigger: this.triggerTarget,
|
|
63
|
+
popover: this.popoverTarget
|
|
64
|
+
});
|
|
65
|
+
this.webChatChannel.onMessage(this.onMessageReceived);
|
|
66
|
+
this.webChatChannel.onConversationAssignment(this.onConversationAssignment);
|
|
67
|
+
this.webChatChannel.onAgentOnline(this.onAgentOnline);
|
|
68
|
+
this.webChatChannel.onReaction(this.onMessageReaction);
|
|
69
|
+
this.messagesContainerTarget.addEventListener('scroll', this.onScroll);
|
|
70
|
+
if (!_hellotext.default.business.features.white_label) {
|
|
71
|
+
this.toolbarTarget.appendChild(_logo_builder.LogoBuilder.build());
|
|
72
|
+
}
|
|
73
|
+
if (localStorage.getItem("hellotext--webchat--".concat(this.idValue)) === 'opened') {
|
|
74
|
+
this.openValue = true;
|
|
75
|
+
}
|
|
76
|
+
_hellotext.default.eventEmitter.dispatch('webchat:mounted');
|
|
77
|
+
_get(_getPrototypeOf(_default.prototype), "connect", this).call(this);
|
|
78
|
+
}
|
|
79
|
+
}, {
|
|
80
|
+
key: "disconnect",
|
|
81
|
+
value: function disconnect() {
|
|
82
|
+
this.messagesContainerTarget.removeEventListener('scroll', this.onScroll);
|
|
83
|
+
this.floatingUICleanup();
|
|
84
|
+
_get(_getPrototypeOf(_default.prototype), "disconnect", this).call(this);
|
|
85
|
+
}
|
|
86
|
+
}, {
|
|
87
|
+
key: "onScroll",
|
|
88
|
+
value: function () {
|
|
89
|
+
var _onScroll = _asyncToGenerator(function* () {
|
|
90
|
+
if (this.messagesContainerTarget.scrollTop > 300 || !this.nextPageValue || this.fetchingNextPage) return;
|
|
91
|
+
this.fetchingNextPage = true;
|
|
92
|
+
var response = yield this.messagesAPI.index({
|
|
93
|
+
page: this.nextPageValue,
|
|
94
|
+
session: _hellotext.default.session
|
|
95
|
+
});
|
|
96
|
+
var {
|
|
97
|
+
next: nextPage,
|
|
98
|
+
messages
|
|
99
|
+
} = yield response.json();
|
|
100
|
+
this.nextPageValue = nextPage;
|
|
101
|
+
this.oldScrollHeight = this.messagesContainerTarget.scrollHeight;
|
|
102
|
+
messages.forEach(message => {
|
|
103
|
+
var {
|
|
104
|
+
body,
|
|
105
|
+
attachments
|
|
106
|
+
} = message;
|
|
107
|
+
var div = document.createElement('div');
|
|
108
|
+
div.innerHTML = body;
|
|
109
|
+
var element = this.messageTemplateTarget.cloneNode(true);
|
|
110
|
+
element.setAttribute('data-hellotext--webchat-target', 'message');
|
|
111
|
+
element.style.removeProperty('display');
|
|
112
|
+
element.querySelector('[data-body]').innerHTML = div.innerHTML;
|
|
113
|
+
if (message.state === 'received') {
|
|
114
|
+
element.classList.add('received');
|
|
115
|
+
} else {
|
|
116
|
+
element.classList.remove('received');
|
|
117
|
+
}
|
|
118
|
+
if (attachments) {
|
|
119
|
+
attachments.forEach(attachmentUrl => {
|
|
120
|
+
var image = this.attachmentImageTarget.cloneNode(true);
|
|
121
|
+
image.removeAttribute('data-hellotext--webchat-target');
|
|
122
|
+
image.src = attachmentUrl;
|
|
123
|
+
image.style.display = 'block';
|
|
124
|
+
element.querySelector('[data-attachment-container]').appendChild(image);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
element.setAttribute('data-body', body);
|
|
128
|
+
this.messagesContainerTarget.prepend(element);
|
|
129
|
+
});
|
|
130
|
+
this.messagesContainerTarget.scroll({
|
|
131
|
+
top: this.messagesContainerTarget.scrollHeight - this.oldScrollHeight,
|
|
132
|
+
behavior: 'instant'
|
|
133
|
+
});
|
|
134
|
+
this.fetchingNextPage = false;
|
|
135
|
+
});
|
|
136
|
+
function onScroll() {
|
|
137
|
+
return _onScroll.apply(this, arguments);
|
|
138
|
+
}
|
|
139
|
+
return onScroll;
|
|
140
|
+
}()
|
|
141
|
+
}, {
|
|
142
|
+
key: "onClickOutside",
|
|
143
|
+
value: function onClickOutside(event) {
|
|
144
|
+
if (_webchat.Webchat.behaviour === _webchat.behaviors.POPOVER && this.openValue && event.target.nodeType && this.element.contains(event.target) === false) {
|
|
145
|
+
this.openValue = false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}, {
|
|
149
|
+
key: "onPopoverOpened",
|
|
150
|
+
value: function onPopoverOpened() {
|
|
151
|
+
this.inputTarget.focus();
|
|
152
|
+
if (!this.scrolled) {
|
|
153
|
+
this.messagesContainerTarget.scroll({
|
|
154
|
+
top: this.messagesContainerTarget.scrollHeight,
|
|
155
|
+
behavior: 'instant'
|
|
156
|
+
});
|
|
157
|
+
this.scrolled = true;
|
|
158
|
+
}
|
|
159
|
+
_hellotext.default.eventEmitter.dispatch('webchat:opened');
|
|
160
|
+
localStorage.setItem("hellotext--webchat--".concat(this.idValue), 'opened');
|
|
161
|
+
if (this.unreadCounterTarget.style.display === 'none') return;
|
|
162
|
+
this.unreadCounterTarget.style.display = 'none';
|
|
163
|
+
this.unreadCounterTarget.innerText = '0';
|
|
164
|
+
this.messagesAPI.markAsSeen();
|
|
165
|
+
}
|
|
166
|
+
}, {
|
|
167
|
+
key: "onPopoverClosed",
|
|
168
|
+
value: function onPopoverClosed() {
|
|
169
|
+
_hellotext.default.eventEmitter.dispatch('webchat:closed');
|
|
170
|
+
setTimeout(() => {
|
|
171
|
+
this.inputTarget.value = "";
|
|
172
|
+
});
|
|
173
|
+
localStorage.setItem("hellotext--webchat--".concat(this.idValue), 'closed');
|
|
174
|
+
}
|
|
175
|
+
}, {
|
|
176
|
+
key: "onMessageReaction",
|
|
177
|
+
value: function onMessageReaction(message) {
|
|
178
|
+
var {
|
|
179
|
+
message: messageId,
|
|
180
|
+
reaction,
|
|
181
|
+
type
|
|
182
|
+
} = message;
|
|
183
|
+
var messageElement = this.messageTargets.find(element => element.dataset.id === messageId);
|
|
184
|
+
var reactionsContainer = messageElement.querySelector('[data-reactions]');
|
|
185
|
+
if (type === 'reaction.destroy') {
|
|
186
|
+
var reactionElement = reactionsContainer.querySelector("[data-id=\"".concat(reaction.id, "\"]"));
|
|
187
|
+
return reactionElement.remove();
|
|
188
|
+
}
|
|
189
|
+
if (reactionsContainer.querySelector("[data-id=\"".concat(reaction.id, "\"]"))) {
|
|
190
|
+
var _reactionElement = reactionsContainer.querySelector("[data-id=\"".concat(reaction.id, "\"]"));
|
|
191
|
+
_reactionElement.innerText = reaction.emoji;
|
|
192
|
+
} else {
|
|
193
|
+
var _reactionElement2 = document.createElement('span');
|
|
194
|
+
_reactionElement2.innerText = reaction.emoji;
|
|
195
|
+
_reactionElement2.setAttribute('data-id', reaction.id);
|
|
196
|
+
reactionsContainer.appendChild(_reactionElement2);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}, {
|
|
200
|
+
key: "onMessageReceived",
|
|
201
|
+
value: function onMessageReceived(message) {
|
|
202
|
+
var {
|
|
203
|
+
body,
|
|
204
|
+
attachments
|
|
205
|
+
} = message;
|
|
206
|
+
var div = document.createElement('div');
|
|
207
|
+
div.innerHTML = body;
|
|
208
|
+
var element = this.messageTemplateTarget.cloneNode(true);
|
|
209
|
+
element.style.display = 'flex';
|
|
210
|
+
element.querySelector('[data-body]').innerHTML = div.innerHTML;
|
|
211
|
+
element.setAttribute('data-hellotext--webchat-target', 'message');
|
|
212
|
+
if (attachments) {
|
|
213
|
+
attachments.forEach(attachmentUrl => {
|
|
214
|
+
var image = this.attachmentImageTarget.cloneNode(true);
|
|
215
|
+
image.src = attachmentUrl;
|
|
216
|
+
image.style.display = 'block';
|
|
217
|
+
element.querySelector('[data-attachment-container]').appendChild(image);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
this.messagesContainerTarget.appendChild(element);
|
|
221
|
+
_hellotext.default.eventEmitter.dispatch('webchat:message:received', _objectSpread(_objectSpread({}, message), {}, {
|
|
222
|
+
body: element.querySelector('[data-body]').innerText
|
|
223
|
+
}));
|
|
224
|
+
element.scrollIntoView({
|
|
225
|
+
behavior: 'smooth'
|
|
226
|
+
});
|
|
227
|
+
this.setOfflineTimeout();
|
|
228
|
+
if (this.openValue) return;
|
|
229
|
+
this.unreadCounterTarget.style.display = 'flex';
|
|
230
|
+
var unreadCount = (parseInt(this.unreadCounterTarget.innerText) || 0) + 1;
|
|
231
|
+
this.unreadCounterTarget.innerText = unreadCount > 99 ? '99+' : unreadCount;
|
|
232
|
+
}
|
|
233
|
+
}, {
|
|
234
|
+
key: "onConversationAssignment",
|
|
235
|
+
value: function onConversationAssignment(conversation) {
|
|
236
|
+
var {
|
|
237
|
+
to: user
|
|
238
|
+
} = conversation;
|
|
239
|
+
this.titleTarget.innerText = user.name;
|
|
240
|
+
if (user.online) {
|
|
241
|
+
this.onlineStatusTarget.style.display = 'flex';
|
|
242
|
+
} else {
|
|
243
|
+
this.onlineStatusTarget.style.display = 'none';
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}, {
|
|
247
|
+
key: "onAgentOnline",
|
|
248
|
+
value: function onAgentOnline() {
|
|
249
|
+
this.onlineStatusTarget.style.display = 'flex';
|
|
250
|
+
this.setOfflineTimeout();
|
|
251
|
+
}
|
|
252
|
+
}, {
|
|
253
|
+
key: "sendMessage",
|
|
254
|
+
value: function () {
|
|
255
|
+
var _sendMessage = _asyncToGenerator(function* () {
|
|
256
|
+
var formData = new FormData();
|
|
257
|
+
var message = {
|
|
258
|
+
body: this.inputTarget.value,
|
|
259
|
+
attachments: this.files
|
|
260
|
+
};
|
|
261
|
+
formData.append('message[body]', this.inputTarget.value);
|
|
262
|
+
this.files.forEach(file => {
|
|
263
|
+
formData.append('message[attachments][]', file);
|
|
264
|
+
});
|
|
265
|
+
formData.append('session', _hellotext.default.session);
|
|
266
|
+
var element = this.messageTemplateTarget.cloneNode(true);
|
|
267
|
+
element.classList.add('received');
|
|
268
|
+
element.style.removeProperty('display');
|
|
269
|
+
element.setAttribute('data-hellotext--webchat-target', 'message');
|
|
270
|
+
element.querySelector('[data-body]').innerText = this.inputTarget.value;
|
|
271
|
+
var attachments = this.attachmentContainerTarget.querySelectorAll('img');
|
|
272
|
+
if (attachments.length > 0) {
|
|
273
|
+
attachments.forEach(attachment => {
|
|
274
|
+
element.querySelector('[data-attachment-container]').appendChild(attachment);
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
this.messagesContainerTarget.appendChild(element);
|
|
278
|
+
element.scrollIntoView({
|
|
279
|
+
behavior: 'smooth'
|
|
280
|
+
});
|
|
281
|
+
this.inputTarget.value = "";
|
|
282
|
+
this.files = [];
|
|
283
|
+
this.attachmentContainerTarget.innerHTML = "";
|
|
284
|
+
this.attachmentContainerTarget.classList.add("hidden");
|
|
285
|
+
this.inputTarget.focus();
|
|
286
|
+
var response = yield this.messagesAPI.create(formData);
|
|
287
|
+
if (response.failed) {
|
|
288
|
+
return element.classList.add('failed');
|
|
289
|
+
}
|
|
290
|
+
var data = yield response.json();
|
|
291
|
+
element.setAttribute('data-id', data.id);
|
|
292
|
+
message.id = data.id;
|
|
293
|
+
_hellotext.default.eventEmitter.dispatch('webchat:message:sent', message);
|
|
294
|
+
if (data.conversation !== this.conversationIdValue) {
|
|
295
|
+
this.conversationIdValue = data.conversation;
|
|
296
|
+
this.webChatChannel.updateSubscriptionWith(this.conversationIdValue);
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
function sendMessage() {
|
|
300
|
+
return _sendMessage.apply(this, arguments);
|
|
301
|
+
}
|
|
302
|
+
return sendMessage;
|
|
303
|
+
}()
|
|
304
|
+
}, {
|
|
305
|
+
key: "openAttachment",
|
|
306
|
+
value: function openAttachment() {
|
|
307
|
+
this.attachmentInputTarget.click();
|
|
308
|
+
}
|
|
309
|
+
}, {
|
|
310
|
+
key: "onFileInputChange",
|
|
311
|
+
value: function onFileInputChange() {
|
|
312
|
+
this.errorMessageContainerTarget.classList.add('hidden');
|
|
313
|
+
this.files = Array.from(this.attachmentInputTarget.files);
|
|
314
|
+
var fileMaxSizeTooMuch = this.files.find(file => {
|
|
315
|
+
var type = file.type.split("/")[0];
|
|
316
|
+
if (['image', 'video', 'audio'].includes(type)) {
|
|
317
|
+
return this.mediaValue[type].max_size < file.size;
|
|
318
|
+
} else {
|
|
319
|
+
return this.mediaValue.document.max_size < file.size;
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
if (fileMaxSizeTooMuch) {
|
|
323
|
+
var type = fileMaxSizeTooMuch.type.split("/")[0];
|
|
324
|
+
var mediaType = ['image', 'audio', 'video'].includes(type) ? type : 'document';
|
|
325
|
+
this.errorMessageContainerTarget.innerText = this.fileSizeErrorMessageValue.replace('%{limit}', this.byteToMegabyte(this.mediaValue[mediaType].max_size));
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
this.errorMessageContainerTarget.innerText = "";
|
|
329
|
+
this.files.forEach(file => this.createAttachmentElement(file));
|
|
330
|
+
this.inputTarget.focus();
|
|
331
|
+
}
|
|
332
|
+
}, {
|
|
333
|
+
key: "createAttachmentElement",
|
|
334
|
+
value: function createAttachmentElement(file) {
|
|
335
|
+
var element = this.attachmentElement();
|
|
336
|
+
this.attachmentContainerTarget.classList.remove('hidden');
|
|
337
|
+
element.setAttribute('data-name', file.name);
|
|
338
|
+
if (file.type.startsWith("image/")) {
|
|
339
|
+
var thumbnail = this.attachmentImageTarget.cloneNode(true);
|
|
340
|
+
thumbnail.src = URL.createObjectURL(file);
|
|
341
|
+
thumbnail.style.display = 'block';
|
|
342
|
+
element.appendChild(thumbnail);
|
|
343
|
+
this.attachmentContainerTarget.appendChild(element);
|
|
344
|
+
this.attachmentContainerTarget.style.display = 'flex';
|
|
345
|
+
} else {
|
|
346
|
+
element.querySelector("main").classList.add(...this.widthClasses, "h-20", "rounded-md", "bg-gray-200", "p-1");
|
|
347
|
+
element.querySelector("p[data-attachment-name]").innerText = file.name;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}, {
|
|
351
|
+
key: "removeAttachment",
|
|
352
|
+
value: function removeAttachment(_ref) {
|
|
353
|
+
var {
|
|
354
|
+
currentTarget
|
|
355
|
+
} = _ref;
|
|
356
|
+
var attachment = currentTarget.closest("[data-hellotext--webchat-target='attachment']");
|
|
357
|
+
this.files = this.files.filter(file => file.name !== attachment.dataset.name);
|
|
358
|
+
attachment.remove();
|
|
359
|
+
this.inputTarget.focus();
|
|
360
|
+
}
|
|
361
|
+
}, {
|
|
362
|
+
key: "attachmentTargetDisconnected",
|
|
363
|
+
value: function attachmentTargetDisconnected() {
|
|
364
|
+
if (this.attachmentTargets.length === 0) {
|
|
365
|
+
this.attachmentContainerTarget.innerHTML = "";
|
|
366
|
+
this.attachmentContainerTarget.style.display = 'none';
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}, {
|
|
370
|
+
key: "attachmentElement",
|
|
371
|
+
value: function attachmentElement() {
|
|
372
|
+
var element = this.attachmentTemplateTarget.cloneNode(true);
|
|
373
|
+
element.removeAttribute("hidden");
|
|
374
|
+
element.style.display = 'flex';
|
|
375
|
+
element.setAttribute("data-hellotext--webchat-target", "attachment");
|
|
376
|
+
return element;
|
|
377
|
+
}
|
|
378
|
+
}, {
|
|
379
|
+
key: "onEmojiSelect",
|
|
380
|
+
value: function onEmojiSelect(_ref2) {
|
|
381
|
+
var {
|
|
382
|
+
detail: emoji
|
|
383
|
+
} = _ref2;
|
|
384
|
+
var value = this.inputTarget.value;
|
|
385
|
+
var start = this.inputTarget.selectionStart;
|
|
386
|
+
var end = this.inputTarget.selectionEnd;
|
|
387
|
+
this.inputTarget.value = value.slice(0, start) + emoji + value.slice(end);
|
|
388
|
+
this.inputTarget.selectionStart = this.inputTarget.selectionEnd = start + emoji.length;
|
|
389
|
+
this.inputTarget.focus();
|
|
390
|
+
}
|
|
391
|
+
}, {
|
|
392
|
+
key: "setOfflineTimeout",
|
|
393
|
+
value: function setOfflineTimeout() {
|
|
394
|
+
clearTimeout(this.offlineTimeout);
|
|
395
|
+
this.offlineTimeout = setTimeout(() => {
|
|
396
|
+
this.onlineStatusTarget.style.display = 'none';
|
|
397
|
+
}, this.fiveMinutes);
|
|
398
|
+
}
|
|
399
|
+
}, {
|
|
400
|
+
key: "byteToMegabyte",
|
|
401
|
+
value: function byteToMegabyte(bytes) {
|
|
402
|
+
return Math.ceil(bytes / 1024 / 1024);
|
|
403
|
+
}
|
|
404
|
+
}, {
|
|
405
|
+
key: "fiveMinutes",
|
|
406
|
+
get: function get() {
|
|
407
|
+
return 300000;
|
|
408
|
+
}
|
|
409
|
+
}, {
|
|
410
|
+
key: "middlewares",
|
|
411
|
+
get: function get() {
|
|
412
|
+
return [(0, _dom.offset)(5), (0, _dom.shift)({
|
|
413
|
+
padding: 24
|
|
414
|
+
}), (0, _dom.flip)()];
|
|
415
|
+
}
|
|
416
|
+
}]);
|
|
417
|
+
return _default;
|
|
418
|
+
}(_stimulus.Controller);
|
|
419
|
+
exports.default = _default;
|
|
420
|
+
_default.values = {
|
|
421
|
+
id: String,
|
|
422
|
+
conversationId: String,
|
|
423
|
+
media: Object,
|
|
424
|
+
fileSizeErrorMessage: String,
|
|
425
|
+
placement: {
|
|
426
|
+
type: String,
|
|
427
|
+
default: "bottom-end"
|
|
428
|
+
},
|
|
429
|
+
open: {
|
|
430
|
+
type: Boolean,
|
|
431
|
+
default: false
|
|
432
|
+
},
|
|
433
|
+
autoPlacement: {
|
|
434
|
+
type: Boolean,
|
|
435
|
+
default: false
|
|
436
|
+
},
|
|
437
|
+
disabled: {
|
|
438
|
+
type: Boolean,
|
|
439
|
+
default: false
|
|
440
|
+
},
|
|
441
|
+
nextPage: {
|
|
442
|
+
type: Number,
|
|
443
|
+
default: undefined
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
_default.targets = ['trigger', 'popover', 'input', 'attachmentInput', 'attachmentButton', 'errorMessageContainer', 'attachmentTemplate', 'attachmentContainer', 'attachment', 'messageTemplate', 'messagesContainer', 'title', 'onlineStatus', 'attachmentImage', 'footer', 'toolbar', 'message', 'unreadCounter'];
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.behaviors = exports.WebChat = void 0;
|
|
7
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
8
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
9
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
10
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
11
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'} Placement
|
|
14
|
+
* @description Valid placements for the webchat.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @enum {Placement}
|
|
19
|
+
*/
|
|
20
|
+
var placements = {
|
|
21
|
+
TOP_LEFT: 'top-left',
|
|
22
|
+
TOP_RIGHT: 'top-right',
|
|
23
|
+
BOTTOM_LEFT: 'bottom-left',
|
|
24
|
+
BOTTOM_RIGHT: 'bottom-right'
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {'modal' | 'popover'} Behaviour
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @enum {Behaviour}
|
|
33
|
+
*/
|
|
34
|
+
var behaviors = {
|
|
35
|
+
MODAL: 'modal',
|
|
36
|
+
POPOVER: 'popover'
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @typedef {Object} Style
|
|
41
|
+
* @property {string} background - The background color or style (e.g., a hex code or color name).
|
|
42
|
+
* @property {string} foreground - The foreground color or style (e.g., a hex code or color name).
|
|
43
|
+
* @property {string} typography - The typography style (e.g., font family).
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @class WebChat
|
|
48
|
+
* @classdesc
|
|
49
|
+
* Configuration for webchat
|
|
50
|
+
* @property {String} id - the id of the webchat.
|
|
51
|
+
* @property {String} container - the container to append the webchat to, defaults to 'body'
|
|
52
|
+
* @property {Placement} placement - the placement of the webchat within the container, defaults to "bottom-right".
|
|
53
|
+
* @property {String} classes - additional classes to apply to the webchat popup.
|
|
54
|
+
* @property {String} triggerClasses - additional classes to apply to the webchat popup trigger.
|
|
55
|
+
* @property {Behaviour} behaviour - the behaviour of the webchat, defaults to 'popover'.
|
|
56
|
+
* @property {Style} style - the style of the webchat.
|
|
57
|
+
*/
|
|
58
|
+
exports.behaviors = behaviors;
|
|
59
|
+
var WebChat = /*#__PURE__*/function () {
|
|
60
|
+
function WebChat() {
|
|
61
|
+
_classCallCheck(this, WebChat);
|
|
62
|
+
}
|
|
63
|
+
_createClass(WebChat, null, [{
|
|
64
|
+
key: "container",
|
|
65
|
+
get: function get() {
|
|
66
|
+
return this._container;
|
|
67
|
+
},
|
|
68
|
+
set: function set(value) {
|
|
69
|
+
this._container = value;
|
|
70
|
+
}
|
|
71
|
+
}, {
|
|
72
|
+
key: "placement",
|
|
73
|
+
get: function get() {
|
|
74
|
+
return this._placement;
|
|
75
|
+
},
|
|
76
|
+
set: function set(value) {
|
|
77
|
+
if (!Object.values(placements).includes(value)) {
|
|
78
|
+
throw new Error("Invalid placement value: ".concat(value));
|
|
79
|
+
}
|
|
80
|
+
this._placement = value;
|
|
81
|
+
}
|
|
82
|
+
}, {
|
|
83
|
+
key: "classes",
|
|
84
|
+
get: function get() {
|
|
85
|
+
if (typeof this._classes === 'string') {
|
|
86
|
+
return this._classes.split(',').map(c => c.trim());
|
|
87
|
+
} else {
|
|
88
|
+
return this._classes;
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
set: function set(value) {
|
|
92
|
+
if (!Array.isArray(value) && typeof value !== 'string') {
|
|
93
|
+
throw new Error('classes must be an array or a string');
|
|
94
|
+
}
|
|
95
|
+
this._classes = value;
|
|
96
|
+
}
|
|
97
|
+
}, {
|
|
98
|
+
key: "triggerClasses",
|
|
99
|
+
get: function get() {
|
|
100
|
+
if (typeof this._triggerClasses === 'string') {
|
|
101
|
+
return this._triggerClasses.split(',').map(c => c.trim());
|
|
102
|
+
} else {
|
|
103
|
+
return this._triggerClasses;
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
set: function set(value) {
|
|
107
|
+
if (!Array.isArray(value) && typeof value !== 'string') {
|
|
108
|
+
throw new Error('triggerClasses must be an array or a string');
|
|
109
|
+
}
|
|
110
|
+
this._triggerClasses = value;
|
|
111
|
+
}
|
|
112
|
+
}, {
|
|
113
|
+
key: "id",
|
|
114
|
+
get: function get() {
|
|
115
|
+
return this._id;
|
|
116
|
+
},
|
|
117
|
+
set: function set(value) {
|
|
118
|
+
this._id = value;
|
|
119
|
+
}
|
|
120
|
+
}, {
|
|
121
|
+
key: "isSet",
|
|
122
|
+
get: function get() {
|
|
123
|
+
return !!this._id;
|
|
124
|
+
}
|
|
125
|
+
}, {
|
|
126
|
+
key: "style",
|
|
127
|
+
get: function get() {
|
|
128
|
+
return this._style;
|
|
129
|
+
},
|
|
130
|
+
set: function set(value) {
|
|
131
|
+
if (typeof value !== 'object') {
|
|
132
|
+
throw new Error('Style must be an object');
|
|
133
|
+
}
|
|
134
|
+
this._style = value;
|
|
135
|
+
}
|
|
136
|
+
}, {
|
|
137
|
+
key: "behaviour",
|
|
138
|
+
get: function get() {
|
|
139
|
+
return this._behaviour;
|
|
140
|
+
},
|
|
141
|
+
set: function set(value) {
|
|
142
|
+
if (!Object.values(behaviors).includes(value)) {
|
|
143
|
+
throw new Error("Invalid behaviour value: ".concat(value));
|
|
144
|
+
}
|
|
145
|
+
this._behaviour = value;
|
|
146
|
+
}
|
|
147
|
+
}, {
|
|
148
|
+
key: "assign",
|
|
149
|
+
value: function assign(props) {
|
|
150
|
+
if (props) {
|
|
151
|
+
Object.entries(props).forEach(_ref => {
|
|
152
|
+
var [key, value] = _ref;
|
|
153
|
+
this[key] = value;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
}]);
|
|
159
|
+
return WebChat;
|
|
160
|
+
}();
|
|
161
|
+
exports.WebChat = WebChat;
|
|
162
|
+
WebChat._id = void 0;
|
|
163
|
+
WebChat._container = 'body';
|
|
164
|
+
WebChat._placement = 'bottom-right';
|
|
165
|
+
WebChat._classes = [];
|
|
166
|
+
WebChat._triggerClasses = [];
|
|
167
|
+
WebChat._style = {};
|
|
168
|
+
WebChat._behaviour = behaviors.POPOVER;
|