@hellotext/hellotext 2.5.6 → 2.5.8
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/README.md +5 -26
- package/dist/hellotext.js +1 -1
- package/dist/hellotext.js.LICENSE.txt +1 -1
- package/index.d.ts +15 -6
- package/lib/api/acks.cjs +17 -31
- package/lib/api/acks.js +17 -42
- package/lib/api/businesses.cjs +14 -28
- package/lib/api/businesses.js +13 -36
- package/lib/api/events.cjs +26 -40
- package/lib/api/events.js +28 -52
- package/lib/api/forms.cjs +24 -40
- package/lib/api/forms.js +24 -57
- package/lib/api/identifications.cjs +15 -29
- package/lib/api/identifications.js +15 -40
- package/lib/api/index.cjs +26 -46
- package/lib/api/index.js +28 -49
- package/lib/api/push/identities.cjs +43 -0
- package/lib/api/push/identities.js +35 -0
- package/lib/api/response.cjs +30 -52
- package/lib/api/response.js +31 -60
- package/lib/api/submissions.cjs +18 -34
- package/lib/api/submissions.js +18 -48
- package/lib/api/webchat/messages.cjs +44 -64
- package/lib/api/webchat/messages.js +44 -80
- package/lib/api/webchats.cjs +37 -56
- package/lib/api/webchats.js +37 -65
- package/lib/api/whatsapp_widgets.cjs +49 -72
- package/lib/api/whatsapp_widgets.js +48 -91
- package/lib/builders/input_builder.cjs +36 -48
- package/lib/builders/input_builder.js +36 -48
- package/lib/builders/logo_builder.cjs +10 -29
- package/lib/builders/logo_builder.js +20 -28
- package/lib/channels/application_channel.cjs +136 -178
- package/lib/channels/application_channel.js +137 -180
- package/lib/channels/webchat_channel.cjs +125 -170
- package/lib/channels/webchat_channel.js +125 -170
- package/lib/controllers/form_controller.cjs +82 -121
- package/lib/controllers/form_controller.js +82 -130
- package/lib/controllers/message_controller.cjs +211 -282
- package/lib/controllers/message_controller.js +209 -284
- package/lib/controllers/mixins/usePopover.cjs +3 -6
- package/lib/controllers/mixins/usePopover.js +17 -22
- package/lib/controllers/webchat/emoji_picker_controller.cjs +106 -150
- package/lib/controllers/webchat/emoji_picker_controller.js +105 -170
- package/lib/controllers/webchat/useBehaviour.js +5 -5
- package/lib/controllers/webchat/useOpeningSequence.js +6 -6
- package/lib/controllers/webchat/useTeaser.js +9 -9
- package/lib/controllers/webchat_controller.cjs +916 -1088
- package/lib/controllers/webchat_controller.js +926 -1152
- package/lib/core/configuration/forms.cjs +21 -35
- package/lib/core/configuration/forms.js +20 -35
- package/lib/core/configuration/locale.cjs +36 -71
- package/lib/core/configuration/locale.js +36 -70
- package/lib/core/configuration/push.cjs +16 -0
- package/lib/core/configuration/push.js +10 -0
- package/lib/core/configuration/webchat.cjs +142 -182
- package/lib/core/configuration/webchat.js +145 -189
- package/lib/core/configuration/whatsapp.cjs +76 -102
- package/lib/core/configuration/whatsapp.js +76 -105
- package/lib/core/configuration.cjs +62 -75
- package/lib/core/configuration.js +61 -75
- package/lib/core/event.cjs +36 -59
- package/lib/core/event.js +35 -61
- package/lib/core/sanitize_html.js +4 -4
- package/lib/errors/invalid_event.cjs +5 -26
- package/lib/errors/invalid_event.js +5 -26
- package/lib/errors/not_initialized_error.cjs +5 -26
- package/lib/errors/not_initialized_error.js +5 -26
- package/lib/hellotext.cjs +186 -211
- package/lib/hellotext.js +189 -236
- package/lib/index.js +1 -1
- package/lib/models/business.cjs +115 -152
- package/lib/models/business.js +118 -162
- package/lib/models/cookies.cjs +34 -51
- package/lib/models/cookies.js +34 -51
- package/lib/models/fingerprint.cjs +8 -23
- package/lib/models/fingerprint.js +25 -58
- package/lib/models/form.cjs +106 -142
- package/lib/models/form.js +105 -153
- package/lib/models/form_collection.cjs +63 -101
- package/lib/models/form_collection.js +61 -107
- package/lib/models/index.cjs +7 -0
- package/lib/models/index.js +1 -0
- package/lib/models/page.cjs +106 -129
- package/lib/models/page.js +106 -131
- package/lib/models/push.cjs +345 -0
- package/lib/models/push.js +339 -0
- package/lib/models/query.cjs +21 -41
- package/lib/models/query.js +21 -41
- package/lib/models/session.cjs +33 -65
- package/lib/models/session.js +30 -63
- package/lib/models/user.cjs +29 -51
- package/lib/models/user.js +29 -51
- package/lib/models/utm.cjs +15 -27
- package/lib/models/utm.js +17 -32
- package/lib/models/webchat.cjs +52 -76
- package/lib/models/webchat.js +52 -90
- package/lib/models/whatsapp_widget.cjs +41 -59
- package/lib/models/whatsapp_widget.js +41 -73
- package/lib/vanilla.cjs +4 -7
- package/lib/vanilla.js +2 -1
- package/package.json +2 -10
- package/src/api/index.js +5 -0
- package/src/api/push/identities.js +42 -0
- package/src/core/configuration/push.js +12 -0
- package/src/core/configuration.js +5 -0
- package/src/hellotext.js +15 -1
- package/src/index.bundle.js +0 -1
- package/src/models/business.js +1 -0
- package/src/models/index.js +1 -0
- package/src/models/push.js +397 -0
- package/src/vanilla.js +2 -1
- package/styles/index.css +0 -30
package/lib/models/business.cjs
CHANGED
|
@@ -7,11 +7,6 @@ exports.Business = void 0;
|
|
|
7
7
|
var _locales = _interopRequireDefault(require("../locales"));
|
|
8
8
|
var _businesses = _interopRequireDefault(require("../api/businesses"));
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
11
|
-
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); } }
|
|
12
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
13
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
14
|
-
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); }
|
|
15
10
|
const stylesheetAttribute = 'data-hellotext-stylesheet';
|
|
16
11
|
const stylesheetLoadTimeout = 10000;
|
|
17
12
|
|
|
@@ -38,6 +33,7 @@ const stylesheetLoadTimeout = 10000;
|
|
|
38
33
|
* @property {String} [locale] - Default dashboard locale for the business.
|
|
39
34
|
* @property {String} [style_url] - Stylesheet URL to inject for dashboard-managed surfaces.
|
|
40
35
|
* @property {BusinessWebchat|null} [webchat] - Dashboard webchat defaults.
|
|
36
|
+
* @property {{public_key: String}|null} [push] - Public VAPID key for push subscriptions.
|
|
41
37
|
* @property {String|Array<String>} [whitelist] - Domain whitelist configuration.
|
|
42
38
|
* @property {String} [subscription] - Current business subscription tier.
|
|
43
39
|
*/
|
|
@@ -45,12 +41,11 @@ const stylesheetLoadTimeout = 10000;
|
|
|
45
41
|
/**
|
|
46
42
|
* Public business context used by the SDK for tracking, forms, and webchat defaults.
|
|
47
43
|
*/
|
|
48
|
-
|
|
44
|
+
class Business {
|
|
49
45
|
/**
|
|
50
46
|
* @param {String} id - Public business id.
|
|
51
47
|
*/
|
|
52
|
-
|
|
53
|
-
_classCallCheck(this, Business);
|
|
48
|
+
constructor(id) {
|
|
54
49
|
this.id = id;
|
|
55
50
|
this.data = null;
|
|
56
51
|
this.stylesheet = null;
|
|
@@ -65,158 +60,126 @@ let Business = /*#__PURE__*/function () {
|
|
|
65
60
|
*
|
|
66
61
|
* @returns {Promise<BusinessData|null>}
|
|
67
62
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const response = await _businesses.default.get(this.id);
|
|
73
|
-
if (response.ok === false) {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
const business = await response.json();
|
|
77
|
-
if (!business) {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
this.setData(business);
|
|
81
|
-
if (business.locale) {
|
|
82
|
-
this.setLocale(business.locale);
|
|
83
|
-
}
|
|
84
|
-
return business;
|
|
85
|
-
} catch (_error) {
|
|
63
|
+
async hydrate() {
|
|
64
|
+
try {
|
|
65
|
+
const response = await _businesses.default.get(this.id);
|
|
66
|
+
if (response.ok === false) {
|
|
86
67
|
return null;
|
|
87
68
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
* @param {BusinessData} data
|
|
92
|
-
* @returns {void}
|
|
93
|
-
*/
|
|
94
|
-
}, {
|
|
95
|
-
key: "setData",
|
|
96
|
-
value: function setData(data) {
|
|
97
|
-
this.data = data;
|
|
98
|
-
if (typeof document !== 'undefined' && data.style_url) {
|
|
99
|
-
this.stylesheet = this.constructor.ensureStylesheet(data.style_url);
|
|
100
|
-
this.stylesheetLoaded = this.constructor.waitForStylesheet(this.stylesheet);
|
|
101
|
-
} else {
|
|
102
|
-
this.stylesheet = null;
|
|
103
|
-
this.stylesheetLoaded = Promise.resolve(false);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}, {
|
|
107
|
-
key: "subscription",
|
|
108
|
-
get: function () {
|
|
109
|
-
return this.data.subscription;
|
|
110
|
-
}
|
|
111
|
-
}, {
|
|
112
|
-
key: "country",
|
|
113
|
-
get: function () {
|
|
114
|
-
return this.data.country;
|
|
115
|
-
}
|
|
116
|
-
}, {
|
|
117
|
-
key: "enabledWhitelist",
|
|
118
|
-
get: function () {
|
|
119
|
-
return this.data.whitelist !== 'disabled';
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* @param {String} locale
|
|
124
|
-
* @returns {void}
|
|
125
|
-
*/
|
|
126
|
-
}, {
|
|
127
|
-
key: "setLocale",
|
|
128
|
-
value: function setLocale(locale) {
|
|
129
|
-
if (!_locales.default[locale]) {
|
|
130
|
-
return console.warn(`Locale ${locale} not found`);
|
|
131
|
-
}
|
|
132
|
-
if (!this.data) {
|
|
133
|
-
this.data = {};
|
|
69
|
+
const business = await response.json();
|
|
70
|
+
if (!business) {
|
|
71
|
+
return null;
|
|
134
72
|
}
|
|
135
|
-
this.
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
key: "locale",
|
|
139
|
-
get: function () {
|
|
140
|
-
return _locales.default[this.data.locale];
|
|
141
|
-
}
|
|
142
|
-
}, {
|
|
143
|
-
key: "features",
|
|
144
|
-
get: function () {
|
|
145
|
-
return this.data.features;
|
|
146
|
-
}
|
|
147
|
-
}], [{
|
|
148
|
-
key: "stylesheetSelector",
|
|
149
|
-
get: function () {
|
|
150
|
-
return `link[rel="stylesheet"][${stylesheetAttribute}]`;
|
|
151
|
-
}
|
|
152
|
-
}, {
|
|
153
|
-
key: "ensureStylesheet",
|
|
154
|
-
value: function ensureStylesheet(styleUrl) {
|
|
155
|
-
const href = this.normalizedStylesheetUrl(styleUrl);
|
|
156
|
-
const existingLink = this.stylesheetLinks.find(link => link.href === href);
|
|
157
|
-
if (existingLink) {
|
|
158
|
-
existingLink.setAttribute(stylesheetAttribute, 'true');
|
|
159
|
-
return existingLink;
|
|
73
|
+
this.setData(business);
|
|
74
|
+
if (business.locale) {
|
|
75
|
+
this.setLocale(business.locale);
|
|
160
76
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
linkTag.setAttribute(stylesheetAttribute, 'true');
|
|
165
|
-
this.waitForStylesheet(linkTag);
|
|
166
|
-
document.head.append(linkTag);
|
|
167
|
-
return linkTag;
|
|
77
|
+
return business;
|
|
78
|
+
} catch (_error) {
|
|
79
|
+
return null;
|
|
168
80
|
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {BusinessData} data
|
|
85
|
+
* @returns {void}
|
|
86
|
+
*/
|
|
87
|
+
setData(data) {
|
|
88
|
+
this.data = data;
|
|
89
|
+
if (typeof document !== 'undefined' && data.style_url) {
|
|
90
|
+
this.stylesheet = this.constructor.ensureStylesheet(data.style_url);
|
|
91
|
+
this.stylesheetLoaded = this.constructor.waitForStylesheet(this.stylesheet);
|
|
92
|
+
} else {
|
|
93
|
+
this.stylesheet = null;
|
|
94
|
+
this.stylesheetLoaded = Promise.resolve(false);
|
|
174
95
|
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
96
|
+
}
|
|
97
|
+
static get stylesheetSelector() {
|
|
98
|
+
return `link[rel="stylesheet"][${stylesheetAttribute}]`;
|
|
99
|
+
}
|
|
100
|
+
static ensureStylesheet(styleUrl) {
|
|
101
|
+
const href = this.normalizedStylesheetUrl(styleUrl);
|
|
102
|
+
const existingLink = this.stylesheetLinks.find(link => link.href === href);
|
|
103
|
+
if (existingLink) {
|
|
104
|
+
existingLink.setAttribute(stylesheetAttribute, 'true');
|
|
105
|
+
return existingLink;
|
|
179
106
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
107
|
+
const linkTag = document.createElement('link');
|
|
108
|
+
linkTag.rel = 'stylesheet';
|
|
109
|
+
linkTag.href = styleUrl;
|
|
110
|
+
linkTag.setAttribute(stylesheetAttribute, 'true');
|
|
111
|
+
this.waitForStylesheet(linkTag);
|
|
112
|
+
document.head.append(linkTag);
|
|
113
|
+
return linkTag;
|
|
114
|
+
}
|
|
115
|
+
static get stylesheetLinks() {
|
|
116
|
+
if (typeof document === 'undefined') return [];
|
|
117
|
+
return Array.from(document.querySelectorAll(this.stylesheetSelector));
|
|
118
|
+
}
|
|
119
|
+
static get latestStylesheet() {
|
|
120
|
+
return this.stylesheetLinks[this.stylesheetLinks.length - 1];
|
|
121
|
+
}
|
|
122
|
+
static normalizedStylesheetUrl(styleUrl) {
|
|
123
|
+
try {
|
|
124
|
+
return new URL(styleUrl, document.baseURI).href;
|
|
125
|
+
} catch (_error) {
|
|
126
|
+
return styleUrl;
|
|
188
127
|
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
128
|
+
}
|
|
129
|
+
static waitForStylesheet(linkTag) {
|
|
130
|
+
if (!linkTag) return Promise.resolve(false);
|
|
131
|
+
if (this.stylesheetIsLoaded(linkTag)) return Promise.resolve(true);
|
|
132
|
+
if (linkTag.dataset.hellotextStylesheetLoaded === 'false') return Promise.resolve(false);
|
|
133
|
+
if (linkTag._hellotextStylesheetLoaded) return linkTag._hellotextStylesheetLoaded;
|
|
134
|
+
linkTag._hellotextStylesheetLoaded = new Promise(resolve => {
|
|
135
|
+
let timeout;
|
|
136
|
+
const finish = loaded => {
|
|
137
|
+
clearTimeout(timeout);
|
|
138
|
+
linkTag.removeEventListener('load', handleLoad);
|
|
139
|
+
linkTag.removeEventListener('error', handleError);
|
|
140
|
+
linkTag.dataset.hellotextStylesheetLoaded = loaded ? 'true' : 'false';
|
|
141
|
+
resolve(loaded);
|
|
142
|
+
};
|
|
143
|
+
const handleLoad = () => finish(this.stylesheetIsLoaded(linkTag));
|
|
144
|
+
const handleError = () => finish(false);
|
|
145
|
+
linkTag.addEventListener('load', handleLoad);
|
|
146
|
+
linkTag.addEventListener('error', handleError);
|
|
147
|
+
timeout = setTimeout(() => finish(this.stylesheetIsLoaded(linkTag)), stylesheetLoadTimeout);
|
|
148
|
+
if (timeout.unref) timeout.unref();
|
|
149
|
+
});
|
|
150
|
+
return linkTag._hellotextStylesheetLoaded;
|
|
151
|
+
}
|
|
152
|
+
static stylesheetIsLoaded(linkTag) {
|
|
153
|
+
return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
|
|
154
|
+
}
|
|
155
|
+
get subscription() {
|
|
156
|
+
return this.data.subscription;
|
|
157
|
+
}
|
|
158
|
+
get country() {
|
|
159
|
+
return this.data.country;
|
|
160
|
+
}
|
|
161
|
+
get enabledWhitelist() {
|
|
162
|
+
return this.data.whitelist !== 'disabled';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param {String} locale
|
|
167
|
+
* @returns {void}
|
|
168
|
+
*/
|
|
169
|
+
setLocale(locale) {
|
|
170
|
+
if (!_locales.default[locale]) {
|
|
171
|
+
return console.warn(`Locale ${locale} not found`);
|
|
213
172
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
value: function stylesheetIsLoaded(linkTag) {
|
|
217
|
-
return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
|
|
173
|
+
if (!this.data) {
|
|
174
|
+
this.data = {};
|
|
218
175
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
176
|
+
this.data.locale = locale;
|
|
177
|
+
}
|
|
178
|
+
get locale() {
|
|
179
|
+
return _locales.default[this.data.locale];
|
|
180
|
+
}
|
|
181
|
+
get features() {
|
|
182
|
+
return this.data.features;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
222
185
|
exports.Business = Business;
|
package/lib/models/business.js
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
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); } }
|
|
2
|
-
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); }); }; }
|
|
3
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
4
|
-
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); } }
|
|
5
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
6
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
7
|
-
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); }
|
|
8
1
|
import locales from '../locales';
|
|
9
2
|
import BusinessesAPI from '../api/businesses';
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
const stylesheetAttribute = 'data-hellotext-stylesheet';
|
|
4
|
+
const stylesheetLoadTimeout = 10000;
|
|
12
5
|
|
|
13
6
|
/**
|
|
14
7
|
* @typedef {Object} BusinessCountry
|
|
@@ -33,6 +26,7 @@ var stylesheetLoadTimeout = 10000;
|
|
|
33
26
|
* @property {String} [locale] - Default dashboard locale for the business.
|
|
34
27
|
* @property {String} [style_url] - Stylesheet URL to inject for dashboard-managed surfaces.
|
|
35
28
|
* @property {BusinessWebchat|null} [webchat] - Dashboard webchat defaults.
|
|
29
|
+
* @property {{public_key: String}|null} [push] - Public VAPID key for push subscriptions.
|
|
36
30
|
* @property {String|Array<String>} [whitelist] - Domain whitelist configuration.
|
|
37
31
|
* @property {String} [subscription] - Current business subscription tier.
|
|
38
32
|
*/
|
|
@@ -40,12 +34,11 @@ var stylesheetLoadTimeout = 10000;
|
|
|
40
34
|
/**
|
|
41
35
|
* Public business context used by the SDK for tracking, forms, and webchat defaults.
|
|
42
36
|
*/
|
|
43
|
-
|
|
37
|
+
class Business {
|
|
44
38
|
/**
|
|
45
39
|
* @param {String} id - Public business id.
|
|
46
40
|
*/
|
|
47
|
-
|
|
48
|
-
_classCallCheck(this, Business);
|
|
41
|
+
constructor(id) {
|
|
49
42
|
this.id = id;
|
|
50
43
|
this.data = null;
|
|
51
44
|
this.stylesheet = null;
|
|
@@ -60,163 +53,126 @@ var Business = /*#__PURE__*/function () {
|
|
|
60
53
|
*
|
|
61
54
|
* @returns {Promise<BusinessData|null>}
|
|
62
55
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
var response = yield BusinessesAPI.get(this.id);
|
|
69
|
-
if (response.ok === false) {
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
var business = yield response.json();
|
|
73
|
-
if (!business) {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
this.setData(business);
|
|
77
|
-
if (business.locale) {
|
|
78
|
-
this.setLocale(business.locale);
|
|
79
|
-
}
|
|
80
|
-
return business;
|
|
81
|
-
} catch (_error) {
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
function hydrate() {
|
|
86
|
-
return _hydrate.apply(this, arguments);
|
|
56
|
+
async hydrate() {
|
|
57
|
+
try {
|
|
58
|
+
const response = await BusinessesAPI.get(this.id);
|
|
59
|
+
if (response.ok === false) {
|
|
60
|
+
return null;
|
|
87
61
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
* @param {BusinessData} data
|
|
92
|
-
* @returns {void}
|
|
93
|
-
*/
|
|
94
|
-
}, {
|
|
95
|
-
key: "setData",
|
|
96
|
-
value: function setData(data) {
|
|
97
|
-
this.data = data;
|
|
98
|
-
if (typeof document !== 'undefined' && data.style_url) {
|
|
99
|
-
this.stylesheet = this.constructor.ensureStylesheet(data.style_url);
|
|
100
|
-
this.stylesheetLoaded = this.constructor.waitForStylesheet(this.stylesheet);
|
|
101
|
-
} else {
|
|
102
|
-
this.stylesheet = null;
|
|
103
|
-
this.stylesheetLoaded = Promise.resolve(false);
|
|
62
|
+
const business = await response.json();
|
|
63
|
+
if (!business) {
|
|
64
|
+
return null;
|
|
104
65
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
get: function get() {
|
|
109
|
-
return this.data.subscription;
|
|
110
|
-
}
|
|
111
|
-
}, {
|
|
112
|
-
key: "country",
|
|
113
|
-
get: function get() {
|
|
114
|
-
return this.data.country;
|
|
115
|
-
}
|
|
116
|
-
}, {
|
|
117
|
-
key: "enabledWhitelist",
|
|
118
|
-
get: function get() {
|
|
119
|
-
return this.data.whitelist !== 'disabled';
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* @param {String} locale
|
|
124
|
-
* @returns {void}
|
|
125
|
-
*/
|
|
126
|
-
}, {
|
|
127
|
-
key: "setLocale",
|
|
128
|
-
value: function setLocale(locale) {
|
|
129
|
-
if (!locales[locale]) {
|
|
130
|
-
return console.warn("Locale ".concat(locale, " not found"));
|
|
66
|
+
this.setData(business);
|
|
67
|
+
if (business.locale) {
|
|
68
|
+
this.setLocale(business.locale);
|
|
131
69
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
this.data.locale = locale;
|
|
136
|
-
}
|
|
137
|
-
}, {
|
|
138
|
-
key: "locale",
|
|
139
|
-
get: function get() {
|
|
140
|
-
return locales[this.data.locale];
|
|
141
|
-
}
|
|
142
|
-
}, {
|
|
143
|
-
key: "features",
|
|
144
|
-
get: function get() {
|
|
145
|
-
return this.data.features;
|
|
146
|
-
}
|
|
147
|
-
}], [{
|
|
148
|
-
key: "stylesheetSelector",
|
|
149
|
-
get: function get() {
|
|
150
|
-
return "link[rel=\"stylesheet\"][".concat(stylesheetAttribute, "]");
|
|
70
|
+
return business;
|
|
71
|
+
} catch (_error) {
|
|
72
|
+
return null;
|
|
151
73
|
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.
|
|
166
|
-
document.head.append(linkTag);
|
|
167
|
-
return linkTag;
|
|
168
|
-
}
|
|
169
|
-
}, {
|
|
170
|
-
key: "stylesheetLinks",
|
|
171
|
-
get: function get() {
|
|
172
|
-
if (typeof document === 'undefined') return [];
|
|
173
|
-
return Array.from(document.querySelectorAll(this.stylesheetSelector));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {BusinessData} data
|
|
78
|
+
* @returns {void}
|
|
79
|
+
*/
|
|
80
|
+
setData(data) {
|
|
81
|
+
this.data = data;
|
|
82
|
+
if (typeof document !== 'undefined' && data.style_url) {
|
|
83
|
+
this.stylesheet = this.constructor.ensureStylesheet(data.style_url);
|
|
84
|
+
this.stylesheetLoaded = this.constructor.waitForStylesheet(this.stylesheet);
|
|
85
|
+
} else {
|
|
86
|
+
this.stylesheet = null;
|
|
87
|
+
this.stylesheetLoaded = Promise.resolve(false);
|
|
174
88
|
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
89
|
+
}
|
|
90
|
+
static get stylesheetSelector() {
|
|
91
|
+
return `link[rel="stylesheet"][${stylesheetAttribute}]`;
|
|
92
|
+
}
|
|
93
|
+
static ensureStylesheet(styleUrl) {
|
|
94
|
+
const href = this.normalizedStylesheetUrl(styleUrl);
|
|
95
|
+
const existingLink = this.stylesheetLinks.find(link => link.href === href);
|
|
96
|
+
if (existingLink) {
|
|
97
|
+
existingLink.setAttribute(stylesheetAttribute, 'true');
|
|
98
|
+
return existingLink;
|
|
179
99
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
100
|
+
const linkTag = document.createElement('link');
|
|
101
|
+
linkTag.rel = 'stylesheet';
|
|
102
|
+
linkTag.href = styleUrl;
|
|
103
|
+
linkTag.setAttribute(stylesheetAttribute, 'true');
|
|
104
|
+
this.waitForStylesheet(linkTag);
|
|
105
|
+
document.head.append(linkTag);
|
|
106
|
+
return linkTag;
|
|
107
|
+
}
|
|
108
|
+
static get stylesheetLinks() {
|
|
109
|
+
if (typeof document === 'undefined') return [];
|
|
110
|
+
return Array.from(document.querySelectorAll(this.stylesheetSelector));
|
|
111
|
+
}
|
|
112
|
+
static get latestStylesheet() {
|
|
113
|
+
return this.stylesheetLinks[this.stylesheetLinks.length - 1];
|
|
114
|
+
}
|
|
115
|
+
static normalizedStylesheetUrl(styleUrl) {
|
|
116
|
+
try {
|
|
117
|
+
return new URL(styleUrl, document.baseURI).href;
|
|
118
|
+
} catch (_error) {
|
|
119
|
+
return styleUrl;
|
|
188
120
|
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
121
|
+
}
|
|
122
|
+
static waitForStylesheet(linkTag) {
|
|
123
|
+
if (!linkTag) return Promise.resolve(false);
|
|
124
|
+
if (this.stylesheetIsLoaded(linkTag)) return Promise.resolve(true);
|
|
125
|
+
if (linkTag.dataset.hellotextStylesheetLoaded === 'false') return Promise.resolve(false);
|
|
126
|
+
if (linkTag._hellotextStylesheetLoaded) return linkTag._hellotextStylesheetLoaded;
|
|
127
|
+
linkTag._hellotextStylesheetLoaded = new Promise(resolve => {
|
|
128
|
+
let timeout;
|
|
129
|
+
const finish = loaded => {
|
|
130
|
+
clearTimeout(timeout);
|
|
131
|
+
linkTag.removeEventListener('load', handleLoad);
|
|
132
|
+
linkTag.removeEventListener('error', handleError);
|
|
133
|
+
linkTag.dataset.hellotextStylesheetLoaded = loaded ? 'true' : 'false';
|
|
134
|
+
resolve(loaded);
|
|
135
|
+
};
|
|
136
|
+
const handleLoad = () => finish(this.stylesheetIsLoaded(linkTag));
|
|
137
|
+
const handleError = () => finish(false);
|
|
138
|
+
linkTag.addEventListener('load', handleLoad);
|
|
139
|
+
linkTag.addEventListener('error', handleError);
|
|
140
|
+
timeout = setTimeout(() => finish(this.stylesheetIsLoaded(linkTag)), stylesheetLoadTimeout);
|
|
141
|
+
if (timeout.unref) timeout.unref();
|
|
142
|
+
});
|
|
143
|
+
return linkTag._hellotextStylesheetLoaded;
|
|
144
|
+
}
|
|
145
|
+
static stylesheetIsLoaded(linkTag) {
|
|
146
|
+
return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
|
|
147
|
+
}
|
|
148
|
+
get subscription() {
|
|
149
|
+
return this.data.subscription;
|
|
150
|
+
}
|
|
151
|
+
get country() {
|
|
152
|
+
return this.data.country;
|
|
153
|
+
}
|
|
154
|
+
get enabledWhitelist() {
|
|
155
|
+
return this.data.whitelist !== 'disabled';
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @param {String} locale
|
|
160
|
+
* @returns {void}
|
|
161
|
+
*/
|
|
162
|
+
setLocale(locale) {
|
|
163
|
+
if (!locales[locale]) {
|
|
164
|
+
return console.warn(`Locale ${locale} not found`);
|
|
213
165
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
value: function stylesheetIsLoaded(linkTag) {
|
|
217
|
-
return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
|
|
166
|
+
if (!this.data) {
|
|
167
|
+
this.data = {};
|
|
218
168
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
169
|
+
this.data.locale = locale;
|
|
170
|
+
}
|
|
171
|
+
get locale() {
|
|
172
|
+
return locales[this.data.locale];
|
|
173
|
+
}
|
|
174
|
+
get features() {
|
|
175
|
+
return this.data.features;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
222
178
|
export { Business };
|