forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelevio.js
More file actions
117 lines (96 loc) · 4.11 KB
/
elevio.js
File metadata and controls
117 lines (96 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const ClientBase = require('./client_base');
const GTM = require('./gtm');
const BinarySocket = require('./socket_base');
const getLanguage = require('../language').get;
const localize = require('../localize').localize;
const createElement = require('../utility').createElement;
const isLoginPages = require('../utility').isLoginPages;
const Elevio = (() => {
const el_shell_id = 'elevio-shell';
let el_shell;
const init = () => {
if (isLoginPages()) return;
el_shell = document.getElementById(el_shell_id);
el_shell.addEventListener('click', () => injectElevio(true));
};
const injectElevio = (is_open = false) => {
const account_id = '5bbc2de0b7365';
window._elev = {}; // eslint-disable-line no-underscore-dangle
window._elev.account_id = account_id; // eslint-disable-line no-underscore-dangle
const script = document.createElement('script');
script.type = 'text/javascript';
script.async = 1;
script.src = `https://cdn.elev.io/sdk/bootloader/v4/elevio-bootloader.js?cid=${account_id}`;
script.id = 'loaded-elevio-script';
document.body.appendChild(script);
window._elev.q = []; // eslint-disable-line no-underscore-dangle
window._elev.on = function(z, y) { // eslint-disable-line no-underscore-dangle
window._elev.q.push([z, y]); // eslint-disable-line no-underscore-dangle
};
script.onload = () => loadElevio(is_open);
};
const loadElevio = (is_open = false) => {
if (!window._elev) return; // eslint-disable-line no-underscore-dangle
window._elev.on('load', (elev) => { // eslint-disable-line no-underscore-dangle
if (el_shell) {
el_shell.parentNode.removeChild(el_shell);
el_shell = undefined;
}
const available_elev_languages = ['es', 'id', 'pt', 'ru'];
const current_language = getLanguage().toLowerCase();
if (available_elev_languages.indexOf(current_language) !== -1) {
elev.setLanguage(current_language);
} else {
elev.setLanguage('en');
}
setUserInfo(elev);
setTranslations(elev);
addEventListenerGTM();
makeLauncherVisible();
if (is_open) {
elev.open();
}
});
};
const addEventListenerGTM = () => {
window._elev.on('widget:opened', () => { // eslint-disable-line no-underscore-dangle
GTM.pushDataLayer({ event: 'elevio_widget_opened' });
});
};
const makeLauncherVisible = () => {
// we have to add the style since the launcher element gets updates even after elevio's 'ready' event fired
const el_launcher_style = createElement('style', { html: '._elevio_launcher {display: block;}' });
document.head.appendChild(el_launcher_style);
};
const setUserInfo = (elev) => {
if (ClientBase.isLoggedIn()) {
BinarySocket.wait('get_settings').then((response) => {
const get_settings = response.get_settings || {};
const is_virtual = ClientBase.get('is_virtual');
const user_obj = {
email : ClientBase.get('email'),
first_name: is_virtual ? 'Virtual' : get_settings.first_name,
last_name : is_virtual ? ClientBase.get('loginid') : get_settings.last_name,
user_hash : get_settings.user_hash,
};
elev.setUser(user_obj);
});
}
};
const setTranslations = (elev) => {
elev.setTranslations({
modules: {
support: {
thankyou: localize('Thank you, we\'ll get back to you within 24 hours'),
},
},
});
};
const createComponent = (type) => window._elev.component({ type }); // eslint-disable-line no-underscore-dangle
return {
init,
injectElevio,
createComponent,
};
})();
module.exports = Elevio;