@hellotext/hellotext 2.4.53 → 2.5.1
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 +1 -0
- package/dist/hellotext.js +1 -1
- package/dist/webchat-emoji-en.js +1 -1
- package/dist/webchat-emoji-es.js +1 -1
- package/dist/webchat-emoji.js +1 -1
- package/index.d.ts +18 -0
- package/lib/api/index.cjs +6 -0
- package/lib/api/index.js +6 -0
- package/lib/api/whatsapp_widgets.cjs +87 -0
- package/lib/api/whatsapp_widgets.js +99 -0
- package/lib/core/configuration/whatsapp.cjs +150 -0
- package/lib/core/configuration/whatsapp.js +147 -0
- package/lib/core/configuration.cjs +6 -1
- package/lib/core/configuration.js +5 -0
- package/lib/core/index.cjs +7 -0
- package/lib/core/index.js +1 -0
- package/lib/hellotext.cjs +11 -0
- package/lib/hellotext.js +12 -1
- package/lib/models/index.cjs +8 -1
- package/lib/models/index.js +2 -1
- package/lib/models/webchat.cjs +10 -0
- package/lib/models/webchat.js +10 -0
- package/lib/models/whatsapp_widget.cjs +77 -0
- package/lib/models/whatsapp_widget.js +84 -0
- package/package.json +6 -3
- package/src/api/index.js +5 -0
- package/src/api/whatsapp_widgets.js +71 -0
- package/src/core/configuration/whatsapp.js +143 -0
- package/src/core/configuration.js +5 -0
- package/src/core/index.js +1 -0
- package/src/hellotext.js +18 -0
- package/src/models/index.js +1 -0
- package/src/models/webchat.js +11 -0
- package/src/models/whatsapp_widget.js +68 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.WhatsAppWidget = void 0;
|
|
7
|
+
var _core = require("../core");
|
|
8
|
+
var _api = _interopRequireDefault(require("../api"));
|
|
9
|
+
var _business = require("./business");
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
12
|
+
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); } }
|
|
13
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
14
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
15
|
+
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); }
|
|
16
|
+
let WhatsAppWidget = /*#__PURE__*/function () {
|
|
17
|
+
function WhatsAppWidget(data) {
|
|
18
|
+
_classCallCheck(this, WhatsAppWidget);
|
|
19
|
+
this.data = data;
|
|
20
|
+
this.mounted = false;
|
|
21
|
+
this.rendered = Promise.resolve(false);
|
|
22
|
+
}
|
|
23
|
+
_createClass(WhatsAppWidget, [{
|
|
24
|
+
key: "render",
|
|
25
|
+
value: async function render() {
|
|
26
|
+
if (!this.data.html) return false;
|
|
27
|
+
const container = this.containerToAppendTo;
|
|
28
|
+
if (!container) {
|
|
29
|
+
console.warn(`Hellotext WhatsApp widget was not mounted because the container ${_core.Configuration.whatsapp.container} was not found.`);
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (!(await this.stylesheetLoaded)) {
|
|
33
|
+
console.warn('Hellotext WhatsApp widget was not mounted because its stylesheet failed to load.');
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
container.appendChild(this.data.html);
|
|
37
|
+
this.markCoexistingWidgets();
|
|
38
|
+
this.mounted = true;
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
}, {
|
|
42
|
+
key: "containerToAppendTo",
|
|
43
|
+
get: function () {
|
|
44
|
+
try {
|
|
45
|
+
return document.querySelector(_core.Configuration.whatsapp.container);
|
|
46
|
+
} catch (_) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}, {
|
|
51
|
+
key: "stylesheetLoaded",
|
|
52
|
+
get: function () {
|
|
53
|
+
return _business.Business.waitForStylesheet(_business.Business.latestStylesheet);
|
|
54
|
+
}
|
|
55
|
+
}, {
|
|
56
|
+
key: "markCoexistingWidgets",
|
|
57
|
+
value: function markCoexistingWidgets() {
|
|
58
|
+
const webchat = document.querySelector('.hellotext--webchat:not(.hellotext--whatsapp-widget)');
|
|
59
|
+
const whatsapp = this.data.html;
|
|
60
|
+
if (!webchat || !whatsapp) return;
|
|
61
|
+
webchat.classList.add('hellotext--with-whatsapp-widget');
|
|
62
|
+
whatsapp.classList.add('hellotext--with-webchat');
|
|
63
|
+
}
|
|
64
|
+
}], [{
|
|
65
|
+
key: "load",
|
|
66
|
+
value: async function load(id) {
|
|
67
|
+
const widget = new WhatsAppWidget({
|
|
68
|
+
id,
|
|
69
|
+
html: await _api.default.whatsappWidgets.get(id)
|
|
70
|
+
});
|
|
71
|
+
widget.rendered = widget.render();
|
|
72
|
+
return widget;
|
|
73
|
+
}
|
|
74
|
+
}]);
|
|
75
|
+
return WhatsAppWidget;
|
|
76
|
+
}();
|
|
77
|
+
exports.WhatsAppWidget = WhatsAppWidget;
|
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
import { Configuration } from '../core';
|
|
9
|
+
import API from '../api';
|
|
10
|
+
import { Business } from './business';
|
|
11
|
+
var WhatsAppWidget = /*#__PURE__*/function () {
|
|
12
|
+
function WhatsAppWidget(data) {
|
|
13
|
+
_classCallCheck(this, WhatsAppWidget);
|
|
14
|
+
this.data = data;
|
|
15
|
+
this.mounted = false;
|
|
16
|
+
this.rendered = Promise.resolve(false);
|
|
17
|
+
}
|
|
18
|
+
_createClass(WhatsAppWidget, [{
|
|
19
|
+
key: "render",
|
|
20
|
+
value: function () {
|
|
21
|
+
var _render = _asyncToGenerator(function* () {
|
|
22
|
+
if (!this.data.html) return false;
|
|
23
|
+
var container = this.containerToAppendTo;
|
|
24
|
+
if (!container) {
|
|
25
|
+
console.warn("Hellotext WhatsApp widget was not mounted because the container ".concat(Configuration.whatsapp.container, " was not found."));
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
if (!(yield this.stylesheetLoaded)) {
|
|
29
|
+
console.warn('Hellotext WhatsApp widget was not mounted because its stylesheet failed to load.');
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
container.appendChild(this.data.html);
|
|
33
|
+
this.markCoexistingWidgets();
|
|
34
|
+
this.mounted = true;
|
|
35
|
+
return true;
|
|
36
|
+
});
|
|
37
|
+
function render() {
|
|
38
|
+
return _render.apply(this, arguments);
|
|
39
|
+
}
|
|
40
|
+
return render;
|
|
41
|
+
}()
|
|
42
|
+
}, {
|
|
43
|
+
key: "containerToAppendTo",
|
|
44
|
+
get: function get() {
|
|
45
|
+
try {
|
|
46
|
+
return document.querySelector(Configuration.whatsapp.container);
|
|
47
|
+
} catch (_) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}, {
|
|
52
|
+
key: "stylesheetLoaded",
|
|
53
|
+
get: function get() {
|
|
54
|
+
return Business.waitForStylesheet(Business.latestStylesheet);
|
|
55
|
+
}
|
|
56
|
+
}, {
|
|
57
|
+
key: "markCoexistingWidgets",
|
|
58
|
+
value: function markCoexistingWidgets() {
|
|
59
|
+
var webchat = document.querySelector('.hellotext--webchat:not(.hellotext--whatsapp-widget)');
|
|
60
|
+
var whatsapp = this.data.html;
|
|
61
|
+
if (!webchat || !whatsapp) return;
|
|
62
|
+
webchat.classList.add('hellotext--with-whatsapp-widget');
|
|
63
|
+
whatsapp.classList.add('hellotext--with-webchat');
|
|
64
|
+
}
|
|
65
|
+
}], [{
|
|
66
|
+
key: "load",
|
|
67
|
+
value: function () {
|
|
68
|
+
var _load = _asyncToGenerator(function* (id) {
|
|
69
|
+
var widget = new WhatsAppWidget({
|
|
70
|
+
id,
|
|
71
|
+
html: yield API.whatsappWidgets.get(id)
|
|
72
|
+
});
|
|
73
|
+
widget.rendered = widget.render();
|
|
74
|
+
return widget;
|
|
75
|
+
});
|
|
76
|
+
function load(_x) {
|
|
77
|
+
return _load.apply(this, arguments);
|
|
78
|
+
}
|
|
79
|
+
return load;
|
|
80
|
+
}()
|
|
81
|
+
}]);
|
|
82
|
+
return WhatsAppWidget;
|
|
83
|
+
}();
|
|
84
|
+
export { WhatsAppWidget };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hellotext/hellotext",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "Hellotext JavaScript Client",
|
|
5
5
|
"source": "src/index.js",
|
|
6
6
|
"main": "lib/index.cjs",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"jest-environment-jsdom": "^29.3.1",
|
|
56
56
|
"prettier": "2.8.2",
|
|
57
57
|
"style-loader": "^3.3.1",
|
|
58
|
-
"webpack": "^5.
|
|
58
|
+
"webpack": "^5.107.2",
|
|
59
59
|
"webpack-cli": "^5.0.1",
|
|
60
60
|
"whatwg-fetch": "^3.6.20"
|
|
61
61
|
},
|
|
@@ -96,5 +96,8 @@
|
|
|
96
96
|
"@hotwired/stimulus": "^3.0.0",
|
|
97
97
|
"emoji-mart": "^5.6.0"
|
|
98
98
|
},
|
|
99
|
-
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
|
|
99
|
+
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610",
|
|
100
|
+
"resolutions": {
|
|
101
|
+
"@babel/traverse": "7.23.2"
|
|
102
|
+
}
|
|
100
103
|
}
|
package/src/api/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import EventsAPI from './events'
|
|
|
3
3
|
import FormsAPI from './forms'
|
|
4
4
|
import IdentificationsAPI from './identifications'
|
|
5
5
|
import WebchatsAPI from './webchats'
|
|
6
|
+
import WhatsAppWidgetsAPI from './whatsapp_widgets'
|
|
6
7
|
import AcksAPI from './acks'
|
|
7
8
|
|
|
8
9
|
// Browsers keep `fetch(..., { keepalive: true })` requests alive during page
|
|
@@ -45,6 +46,10 @@ export default class API {
|
|
|
45
46
|
return WebchatsAPI
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
static get whatsappWidgets() {
|
|
50
|
+
return WhatsAppWidgetsAPI
|
|
51
|
+
}
|
|
52
|
+
|
|
48
53
|
static get identifications() {
|
|
49
54
|
return IdentificationsAPI
|
|
50
55
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Configuration, Locale } from '../core'
|
|
2
|
+
import Hellotext from '../hellotext'
|
|
3
|
+
|
|
4
|
+
class WhatsAppWidgetsAPI {
|
|
5
|
+
static get endpoint() {
|
|
6
|
+
return Configuration.endpoint('public/widgets/whatsapp')
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static async get(id) {
|
|
10
|
+
const url = new URL(`${this.endpoint}/${id}`)
|
|
11
|
+
|
|
12
|
+
url.searchParams.append('locale', Locale.toString())
|
|
13
|
+
url.searchParams.append('placement', Configuration.whatsapp.placement)
|
|
14
|
+
this.appendWhatsAppOverrides(url)
|
|
15
|
+
|
|
16
|
+
const response = await this.fetchWidget(url)
|
|
17
|
+
|
|
18
|
+
if (!response.ok) return null
|
|
19
|
+
|
|
20
|
+
const data = await this.parseWidgetResponse(response)
|
|
21
|
+
|
|
22
|
+
if (!data) return null
|
|
23
|
+
|
|
24
|
+
if (!Hellotext.business.data) {
|
|
25
|
+
Hellotext.business.setData(data.business)
|
|
26
|
+
Hellotext.business.setLocale(data.locale)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return new DOMParser().parseFromString(data.html, 'text/html').querySelector('article')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static appendWhatsAppOverrides(url) {
|
|
33
|
+
const { appearance, body, number } = Configuration.whatsapp
|
|
34
|
+
|
|
35
|
+
this.appendIfSupplied(
|
|
36
|
+
url,
|
|
37
|
+
'whatsapp[appearance][launcher][icon_url]',
|
|
38
|
+
appearance.launcher?.iconUrl,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
this.appendIfSupplied(url, 'whatsapp[number]', number)
|
|
42
|
+
this.appendIfSupplied(url, 'whatsapp[body]', body)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static appendIfSupplied(url, key, value) {
|
|
46
|
+
if (value === undefined || value === null) return
|
|
47
|
+
|
|
48
|
+
url.searchParams.append(key, String(value))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static async fetchWidget(url) {
|
|
52
|
+
try {
|
|
53
|
+
return await fetch(url, {
|
|
54
|
+
method: 'GET',
|
|
55
|
+
headers: Hellotext.headers,
|
|
56
|
+
})
|
|
57
|
+
} catch (_) {
|
|
58
|
+
return { ok: false }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static async parseWidgetResponse(response) {
|
|
63
|
+
try {
|
|
64
|
+
return await response.json()
|
|
65
|
+
} catch (_) {
|
|
66
|
+
return null
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export default WhatsAppWidgetsAPI
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'} Placement
|
|
3
|
+
* @description Valid placements for the WhatsApp widget.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @enum {Placement}
|
|
8
|
+
*/
|
|
9
|
+
const placements = {
|
|
10
|
+
TOP_LEFT: 'top-left',
|
|
11
|
+
TOP_RIGHT: 'top-right',
|
|
12
|
+
BOTTOM_LEFT: 'bottom-left',
|
|
13
|
+
BOTTOM_RIGHT: 'bottom-right'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {Object} WhatsAppWidgetAppearance
|
|
18
|
+
* @property {Object} [launcher] - Launcher appearance overrides.
|
|
19
|
+
* @property {string} [launcher.iconUrl] - Image URL used for the launcher icon.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @class WhatsApp
|
|
24
|
+
* @classdesc Configuration for the standalone WhatsApp widget.
|
|
25
|
+
* @property {String} id - The id of the WhatsApp widget.
|
|
26
|
+
* @property {String} container - The container to append the widget to, defaults to 'body'.
|
|
27
|
+
* @property {Placement} placement - The placement of the widget, defaults to 'bottom-right'.
|
|
28
|
+
* @property {String} number - WhatsApp number used by the widget link.
|
|
29
|
+
* @property {String} body - Prefilled WhatsApp compose text.
|
|
30
|
+
* @property {WhatsAppWidgetAppearance} appearance - Appearance overrides.
|
|
31
|
+
*/
|
|
32
|
+
class WhatsApp {
|
|
33
|
+
static _id
|
|
34
|
+
static _container = 'body'
|
|
35
|
+
static _placement = 'bottom-right'
|
|
36
|
+
static _appearance = {}
|
|
37
|
+
static _number = null
|
|
38
|
+
static _body = null
|
|
39
|
+
|
|
40
|
+
static set id(value) {
|
|
41
|
+
this._id = value
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static get id() {
|
|
45
|
+
return this._id
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static set container(value) {
|
|
49
|
+
this._container = value
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static get container() {
|
|
53
|
+
return this._container
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static set placement(value) {
|
|
57
|
+
if (!Object.values(placements).includes(value)) {
|
|
58
|
+
throw new Error(`Invalid placement value: ${value}`)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
this._placement = value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static get placement() {
|
|
65
|
+
return this._placement
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static get appearance() {
|
|
69
|
+
return this._appearance
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static set appearance(value) {
|
|
73
|
+
if (!this.isPlainObject(value)) {
|
|
74
|
+
throw new Error('Appearance must be an object')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
Object.entries(value).forEach(([key, nestedValue]) => {
|
|
78
|
+
if (key !== 'launcher') {
|
|
79
|
+
throw new Error(`Invalid appearance property: ${key}`)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!this.isPlainObject(nestedValue)) {
|
|
83
|
+
throw new Error(`Appearance ${key} must be an object`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
Object.entries(nestedValue).forEach(([nestedKey, propertyValue]) => {
|
|
87
|
+
if (nestedKey !== 'iconUrl') {
|
|
88
|
+
throw new Error(`Invalid appearance launcher property: ${nestedKey}`)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (propertyValue == null) {
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (typeof propertyValue !== 'string') {
|
|
96
|
+
throw new Error(`Invalid appearance ${key}.${nestedKey} value: ${propertyValue}`)
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
this._appearance = value
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static get number() {
|
|
105
|
+
return this._number
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static set number(value) {
|
|
109
|
+
if (value != null && typeof value !== 'string') {
|
|
110
|
+
throw new Error(`Invalid number value: ${value}`)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this._number = value
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static get body() {
|
|
117
|
+
return this._body
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static set body(value) {
|
|
121
|
+
if (value != null && typeof value !== 'string') {
|
|
122
|
+
throw new Error(`Invalid body value: ${value}`)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
this._body = value
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static assign(props) {
|
|
129
|
+
if (props) {
|
|
130
|
+
Object.entries(props).forEach(([key, value]) => {
|
|
131
|
+
this[key] = value
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return this
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static isPlainObject(value) {
|
|
139
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { WhatsApp }
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Forms } from './configuration/forms'
|
|
2
2
|
import { Locale } from './configuration/locale'
|
|
3
3
|
import { Webchat } from './configuration/webchat'
|
|
4
|
+
import { WhatsApp } from './configuration/whatsapp'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* @class Configuration
|
|
@@ -10,6 +11,7 @@ import { Webchat } from './configuration/webchat'
|
|
|
10
11
|
* @property {String} [session] - session id
|
|
11
12
|
* @property {Forms} [forms] - form configuration
|
|
12
13
|
* @property {Webchat} [webchat] - webchat configuration
|
|
14
|
+
* @property {WhatsApp} [whatsappWidget] - WhatsApp widget configuration
|
|
13
15
|
* @property {Locale} [locale] - locale configuration
|
|
14
16
|
*/
|
|
15
17
|
class Configuration {
|
|
@@ -21,6 +23,7 @@ class Configuration {
|
|
|
21
23
|
|
|
22
24
|
static forms = Forms
|
|
23
25
|
static webchat = Webchat
|
|
26
|
+
static whatsapp = WhatsApp
|
|
24
27
|
|
|
25
28
|
/**
|
|
26
29
|
*
|
|
@@ -42,6 +45,8 @@ class Configuration {
|
|
|
42
45
|
this.forms = Forms.assign(value)
|
|
43
46
|
} else if (key === 'webchat') {
|
|
44
47
|
this.webchat = Webchat.assign(value)
|
|
48
|
+
} else if (key === 'whatsappWidget') {
|
|
49
|
+
this.whatsapp = WhatsApp.assign(value)
|
|
45
50
|
} else {
|
|
46
51
|
this[key] = value
|
|
47
52
|
}
|
package/src/core/index.js
CHANGED
package/src/hellotext.js
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
Session,
|
|
11
11
|
User,
|
|
12
12
|
Webchat,
|
|
13
|
+
WhatsAppWidget,
|
|
13
14
|
} from './models'
|
|
14
15
|
|
|
15
16
|
import { NotInitializedError } from './errors'
|
|
@@ -19,6 +20,7 @@ class Hellotext {
|
|
|
19
20
|
static forms
|
|
20
21
|
static business
|
|
21
22
|
static webchat
|
|
23
|
+
static whatsapp
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
26
|
* initialize the module.
|
|
@@ -44,6 +46,13 @@ class Hellotext {
|
|
|
44
46
|
(businessData && businessData.webchat) || {},
|
|
45
47
|
config.webchat || {},
|
|
46
48
|
)
|
|
49
|
+
const whatsappConfig =
|
|
50
|
+
config.whatsappWidget === false
|
|
51
|
+
? false
|
|
52
|
+
: this.mergeWhatsAppConfig(
|
|
53
|
+
(businessData && businessData.whatsapp) || {},
|
|
54
|
+
config.whatsappWidget || {},
|
|
55
|
+
)
|
|
47
56
|
|
|
48
57
|
const hasExplicitBehaviourOverride =
|
|
49
58
|
config.webchat &&
|
|
@@ -56,6 +65,11 @@ class Hellotext {
|
|
|
56
65
|
this.webchat = await Webchat.load(webchatConfig.id)
|
|
57
66
|
}
|
|
58
67
|
|
|
68
|
+
if (whatsappConfig && whatsappConfig.id) {
|
|
69
|
+
Configuration.whatsapp.assign(whatsappConfig)
|
|
70
|
+
this.whatsapp = await WhatsAppWidget.load(whatsappConfig.id)
|
|
71
|
+
}
|
|
72
|
+
|
|
59
73
|
if (typeof MutationObserver !== 'undefined') {
|
|
60
74
|
this.forms.collectExistingFormsOnPage()
|
|
61
75
|
}
|
|
@@ -65,6 +79,10 @@ class Hellotext {
|
|
|
65
79
|
return this.deepMergePlainObjects(dashboardConfig, localConfig)
|
|
66
80
|
}
|
|
67
81
|
|
|
82
|
+
static mergeWhatsAppConfig(dashboardConfig, localConfig) {
|
|
83
|
+
return this.deepMergePlainObjects(dashboardConfig, localConfig)
|
|
84
|
+
}
|
|
85
|
+
|
|
68
86
|
static deepMergePlainObjects(base, override) {
|
|
69
87
|
const result = { ...base }
|
|
70
88
|
|
package/src/models/index.js
CHANGED
package/src/models/webchat.js
CHANGED
|
@@ -30,6 +30,7 @@ class Webchat {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
this.containerToAppendTo.appendChild(this.data.html)
|
|
33
|
+
this.markCoexistingWidgets()
|
|
33
34
|
this.mounted = true
|
|
34
35
|
|
|
35
36
|
return true
|
|
@@ -69,6 +70,16 @@ class Webchat {
|
|
|
69
70
|
get stylesheetLoaded() {
|
|
70
71
|
return Business.waitForStylesheet(Business.latestStylesheet)
|
|
71
72
|
}
|
|
73
|
+
|
|
74
|
+
markCoexistingWidgets() {
|
|
75
|
+
const webchat = this.data.html
|
|
76
|
+
const whatsapp = document.querySelector('.hellotext--whatsapp-widget')
|
|
77
|
+
|
|
78
|
+
if (!webchat || !whatsapp) return
|
|
79
|
+
|
|
80
|
+
webchat.classList.add('hellotext--with-whatsapp-widget')
|
|
81
|
+
whatsapp.classList.add('hellotext--with-webchat')
|
|
82
|
+
}
|
|
72
83
|
}
|
|
73
84
|
|
|
74
85
|
export { Webchat }
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Configuration } from '../core'
|
|
2
|
+
|
|
3
|
+
import API from '../api'
|
|
4
|
+
import { Business } from './business'
|
|
5
|
+
|
|
6
|
+
class WhatsAppWidget {
|
|
7
|
+
static async load(id) {
|
|
8
|
+
const widget = new WhatsAppWidget({
|
|
9
|
+
id,
|
|
10
|
+
html: await API.whatsappWidgets.get(id)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
widget.rendered = widget.render()
|
|
14
|
+
|
|
15
|
+
return widget
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
constructor(data) {
|
|
19
|
+
this.data = data
|
|
20
|
+
this.mounted = false
|
|
21
|
+
this.rendered = Promise.resolve(false)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async render() {
|
|
25
|
+
if (!this.data.html) return false
|
|
26
|
+
|
|
27
|
+
const container = this.containerToAppendTo
|
|
28
|
+
if (!container) {
|
|
29
|
+
console.warn(`Hellotext WhatsApp widget was not mounted because the container ${Configuration.whatsapp.container} was not found.`)
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!await this.stylesheetLoaded) {
|
|
34
|
+
console.warn('Hellotext WhatsApp widget was not mounted because its stylesheet failed to load.')
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
container.appendChild(this.data.html)
|
|
39
|
+
this.markCoexistingWidgets()
|
|
40
|
+
this.mounted = true
|
|
41
|
+
|
|
42
|
+
return true
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get containerToAppendTo() {
|
|
46
|
+
try {
|
|
47
|
+
return document.querySelector(Configuration.whatsapp.container)
|
|
48
|
+
} catch (_) {
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get stylesheetLoaded() {
|
|
54
|
+
return Business.waitForStylesheet(Business.latestStylesheet)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
markCoexistingWidgets() {
|
|
58
|
+
const webchat = document.querySelector('.hellotext--webchat:not(.hellotext--whatsapp-widget)')
|
|
59
|
+
const whatsapp = this.data.html
|
|
60
|
+
|
|
61
|
+
if (!webchat || !whatsapp) return
|
|
62
|
+
|
|
63
|
+
webchat.classList.add('hellotext--with-whatsapp-widget')
|
|
64
|
+
whatsapp.classList.add('hellotext--with-webchat')
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { WhatsAppWidget }
|