@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.
Files changed (113) hide show
  1. package/README.md +5 -26
  2. package/dist/hellotext.js +1 -1
  3. package/dist/hellotext.js.LICENSE.txt +1 -1
  4. package/index.d.ts +15 -6
  5. package/lib/api/acks.cjs +17 -31
  6. package/lib/api/acks.js +17 -42
  7. package/lib/api/businesses.cjs +14 -28
  8. package/lib/api/businesses.js +13 -36
  9. package/lib/api/events.cjs +26 -40
  10. package/lib/api/events.js +28 -52
  11. package/lib/api/forms.cjs +24 -40
  12. package/lib/api/forms.js +24 -57
  13. package/lib/api/identifications.cjs +15 -29
  14. package/lib/api/identifications.js +15 -40
  15. package/lib/api/index.cjs +26 -46
  16. package/lib/api/index.js +28 -49
  17. package/lib/api/push/identities.cjs +43 -0
  18. package/lib/api/push/identities.js +35 -0
  19. package/lib/api/response.cjs +30 -52
  20. package/lib/api/response.js +31 -60
  21. package/lib/api/submissions.cjs +18 -34
  22. package/lib/api/submissions.js +18 -48
  23. package/lib/api/webchat/messages.cjs +44 -64
  24. package/lib/api/webchat/messages.js +44 -80
  25. package/lib/api/webchats.cjs +37 -56
  26. package/lib/api/webchats.js +37 -65
  27. package/lib/api/whatsapp_widgets.cjs +49 -72
  28. package/lib/api/whatsapp_widgets.js +48 -91
  29. package/lib/builders/input_builder.cjs +36 -48
  30. package/lib/builders/input_builder.js +36 -48
  31. package/lib/builders/logo_builder.cjs +10 -29
  32. package/lib/builders/logo_builder.js +20 -28
  33. package/lib/channels/application_channel.cjs +136 -178
  34. package/lib/channels/application_channel.js +137 -180
  35. package/lib/channels/webchat_channel.cjs +125 -170
  36. package/lib/channels/webchat_channel.js +125 -170
  37. package/lib/controllers/form_controller.cjs +82 -121
  38. package/lib/controllers/form_controller.js +82 -130
  39. package/lib/controllers/message_controller.cjs +211 -282
  40. package/lib/controllers/message_controller.js +209 -284
  41. package/lib/controllers/mixins/usePopover.cjs +3 -6
  42. package/lib/controllers/mixins/usePopover.js +17 -22
  43. package/lib/controllers/webchat/emoji_picker_controller.cjs +106 -150
  44. package/lib/controllers/webchat/emoji_picker_controller.js +105 -170
  45. package/lib/controllers/webchat/useBehaviour.js +5 -5
  46. package/lib/controllers/webchat/useOpeningSequence.js +6 -6
  47. package/lib/controllers/webchat/useTeaser.js +9 -9
  48. package/lib/controllers/webchat_controller.cjs +916 -1088
  49. package/lib/controllers/webchat_controller.js +926 -1152
  50. package/lib/core/configuration/forms.cjs +21 -35
  51. package/lib/core/configuration/forms.js +20 -35
  52. package/lib/core/configuration/locale.cjs +36 -71
  53. package/lib/core/configuration/locale.js +36 -70
  54. package/lib/core/configuration/push.cjs +16 -0
  55. package/lib/core/configuration/push.js +10 -0
  56. package/lib/core/configuration/webchat.cjs +142 -182
  57. package/lib/core/configuration/webchat.js +145 -189
  58. package/lib/core/configuration/whatsapp.cjs +76 -102
  59. package/lib/core/configuration/whatsapp.js +76 -105
  60. package/lib/core/configuration.cjs +62 -75
  61. package/lib/core/configuration.js +61 -75
  62. package/lib/core/event.cjs +36 -59
  63. package/lib/core/event.js +35 -61
  64. package/lib/core/sanitize_html.js +4 -4
  65. package/lib/errors/invalid_event.cjs +5 -26
  66. package/lib/errors/invalid_event.js +5 -26
  67. package/lib/errors/not_initialized_error.cjs +5 -26
  68. package/lib/errors/not_initialized_error.js +5 -26
  69. package/lib/hellotext.cjs +186 -211
  70. package/lib/hellotext.js +189 -236
  71. package/lib/index.js +1 -1
  72. package/lib/models/business.cjs +115 -152
  73. package/lib/models/business.js +118 -162
  74. package/lib/models/cookies.cjs +34 -51
  75. package/lib/models/cookies.js +34 -51
  76. package/lib/models/fingerprint.cjs +8 -23
  77. package/lib/models/fingerprint.js +25 -58
  78. package/lib/models/form.cjs +106 -142
  79. package/lib/models/form.js +105 -153
  80. package/lib/models/form_collection.cjs +63 -101
  81. package/lib/models/form_collection.js +61 -107
  82. package/lib/models/index.cjs +7 -0
  83. package/lib/models/index.js +1 -0
  84. package/lib/models/page.cjs +106 -129
  85. package/lib/models/page.js +106 -131
  86. package/lib/models/push.cjs +345 -0
  87. package/lib/models/push.js +339 -0
  88. package/lib/models/query.cjs +21 -41
  89. package/lib/models/query.js +21 -41
  90. package/lib/models/session.cjs +33 -65
  91. package/lib/models/session.js +30 -63
  92. package/lib/models/user.cjs +29 -51
  93. package/lib/models/user.js +29 -51
  94. package/lib/models/utm.cjs +15 -27
  95. package/lib/models/utm.js +17 -32
  96. package/lib/models/webchat.cjs +52 -76
  97. package/lib/models/webchat.js +52 -90
  98. package/lib/models/whatsapp_widget.cjs +41 -59
  99. package/lib/models/whatsapp_widget.js +41 -73
  100. package/lib/vanilla.cjs +4 -7
  101. package/lib/vanilla.js +2 -1
  102. package/package.json +2 -10
  103. package/src/api/index.js +5 -0
  104. package/src/api/push/identities.js +42 -0
  105. package/src/core/configuration/push.js +12 -0
  106. package/src/core/configuration.js +5 -0
  107. package/src/hellotext.js +15 -1
  108. package/src/index.bundle.js +0 -1
  109. package/src/models/business.js +1 -0
  110. package/src/models/index.js +1 -0
  111. package/src/models/push.js +397 -0
  112. package/src/vanilla.js +2 -1
  113. package/styles/index.css +0 -30
@@ -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
- let Business = /*#__PURE__*/function () {
44
+ class Business {
49
45
  /**
50
46
  * @param {String} id - Public business id.
51
47
  */
52
- function Business(id) {
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
- _createClass(Business, [{
69
- key: "hydrate",
70
- value: async function hydrate() {
71
- try {
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.data.locale = locale;
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
- const linkTag = document.createElement('link');
162
- linkTag.rel = 'stylesheet';
163
- linkTag.href = styleUrl;
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
- key: "stylesheetLinks",
171
- get: function () {
172
- if (typeof document === 'undefined') return [];
173
- return Array.from(document.querySelectorAll(this.stylesheetSelector));
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
- key: "latestStylesheet",
177
- get: function () {
178
- return this.stylesheetLinks[this.stylesheetLinks.length - 1];
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
- key: "normalizedStylesheetUrl",
182
- value: function normalizedStylesheetUrl(styleUrl) {
183
- try {
184
- return new URL(styleUrl, document.baseURI).href;
185
- } catch (_error) {
186
- return styleUrl;
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
- key: "waitForStylesheet",
191
- value: function waitForStylesheet(linkTag) {
192
- if (!linkTag) return Promise.resolve(false);
193
- if (this.stylesheetIsLoaded(linkTag)) return Promise.resolve(true);
194
- if (linkTag.dataset.hellotextStylesheetLoaded === 'false') return Promise.resolve(false);
195
- if (linkTag._hellotextStylesheetLoaded) return linkTag._hellotextStylesheetLoaded;
196
- linkTag._hellotextStylesheetLoaded = new Promise(resolve => {
197
- let timeout;
198
- const finish = loaded => {
199
- clearTimeout(timeout);
200
- linkTag.removeEventListener('load', handleLoad);
201
- linkTag.removeEventListener('error', handleError);
202
- linkTag.dataset.hellotextStylesheetLoaded = loaded ? 'true' : 'false';
203
- resolve(loaded);
204
- };
205
- const handleLoad = () => finish(this.stylesheetIsLoaded(linkTag));
206
- const handleError = () => finish(false);
207
- linkTag.addEventListener('load', handleLoad);
208
- linkTag.addEventListener('error', handleError);
209
- timeout = setTimeout(() => finish(this.stylesheetIsLoaded(linkTag)), stylesheetLoadTimeout);
210
- if (timeout.unref) timeout.unref();
211
- });
212
- return linkTag._hellotextStylesheetLoaded;
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
- key: "stylesheetIsLoaded",
216
- value: function stylesheetIsLoaded(linkTag) {
217
- return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
173
+ if (!this.data) {
174
+ this.data = {};
218
175
  }
219
- }]);
220
- return Business;
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;
@@ -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
- var stylesheetAttribute = 'data-hellotext-stylesheet';
11
- var stylesheetLoadTimeout = 10000;
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
- var Business = /*#__PURE__*/function () {
37
+ class Business {
44
38
  /**
45
39
  * @param {String} id - Public business id.
46
40
  */
47
- function Business(id) {
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
- _createClass(Business, [{
64
- key: "hydrate",
65
- value: function () {
66
- var _hydrate = _asyncToGenerator(function* () {
67
- try {
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
- return hydrate;
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
- key: "subscription",
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
- if (!this.data) {
133
- this.data = {};
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
- key: "ensureStylesheet",
154
- value: function ensureStylesheet(styleUrl) {
155
- var href = this.normalizedStylesheetUrl(styleUrl);
156
- var existingLink = this.stylesheetLinks.find(link => link.href === href);
157
- if (existingLink) {
158
- existingLink.setAttribute(stylesheetAttribute, 'true');
159
- return existingLink;
160
- }
161
- var linkTag = document.createElement('link');
162
- linkTag.rel = 'stylesheet';
163
- linkTag.href = styleUrl;
164
- linkTag.setAttribute(stylesheetAttribute, 'true');
165
- this.waitForStylesheet(linkTag);
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
- key: "latestStylesheet",
177
- get: function get() {
178
- return this.stylesheetLinks[this.stylesheetLinks.length - 1];
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
- key: "normalizedStylesheetUrl",
182
- value: function normalizedStylesheetUrl(styleUrl) {
183
- try {
184
- return new URL(styleUrl, document.baseURI).href;
185
- } catch (_error) {
186
- return styleUrl;
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
- key: "waitForStylesheet",
191
- value: function waitForStylesheet(linkTag) {
192
- if (!linkTag) return Promise.resolve(false);
193
- if (this.stylesheetIsLoaded(linkTag)) return Promise.resolve(true);
194
- if (linkTag.dataset.hellotextStylesheetLoaded === 'false') return Promise.resolve(false);
195
- if (linkTag._hellotextStylesheetLoaded) return linkTag._hellotextStylesheetLoaded;
196
- linkTag._hellotextStylesheetLoaded = new Promise(resolve => {
197
- var timeout;
198
- var finish = loaded => {
199
- clearTimeout(timeout);
200
- linkTag.removeEventListener('load', handleLoad);
201
- linkTag.removeEventListener('error', handleError);
202
- linkTag.dataset.hellotextStylesheetLoaded = loaded ? 'true' : 'false';
203
- resolve(loaded);
204
- };
205
- var handleLoad = () => finish(this.stylesheetIsLoaded(linkTag));
206
- var handleError = () => finish(false);
207
- linkTag.addEventListener('load', handleLoad);
208
- linkTag.addEventListener('error', handleError);
209
- timeout = setTimeout(() => finish(this.stylesheetIsLoaded(linkTag)), stylesheetLoadTimeout);
210
- if (timeout.unref) timeout.unref();
211
- });
212
- return linkTag._hellotextStylesheetLoaded;
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
- key: "stylesheetIsLoaded",
216
- value: function stylesheetIsLoaded(linkTag) {
217
- return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
166
+ if (!this.data) {
167
+ this.data = {};
218
168
  }
219
- }]);
220
- return Business;
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 };