@chat21/chat21-web-widget 5.0.58 → 5.0.59-rc.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/CHANGELOG.md +3 -0
- package/package.json +2 -1
- package/src/build_launch.js +71 -0
- package/src/launch_template.js +417 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chat21/chat21-web-widget",
|
|
3
3
|
"author": "Tiledesk SRL",
|
|
4
|
-
"version": "5.0.
|
|
4
|
+
"version": "5.0.59-rc.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://www.tiledesk.com",
|
|
7
7
|
"repository": {
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"marked": "^4.0.16",
|
|
44
44
|
"ngx-logger": "^5.0.11",
|
|
45
45
|
"popper.js": "^1.16.1",
|
|
46
|
+
"replace": "^1.2.2",
|
|
46
47
|
"rxjs": "~6.5.1",
|
|
47
48
|
"source-map-explorer": "^2.5.3",
|
|
48
49
|
"tslib": "^2.0.0",
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var glob = require("glob")
|
|
2
|
+
var replace = require("replace");
|
|
3
|
+
|
|
4
|
+
initReplacement()
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function initReplacement(){
|
|
8
|
+
console.log('init replacement HEREEE')
|
|
9
|
+
fileToBeReplaced = {
|
|
10
|
+
'main' : {name: 'main', extension: '.js', regex: /(?<=\/main\.)(.+?)(?=\.js|$)/},
|
|
11
|
+
'runtime' : {name: 'runtime', extension: '.js', regex: /(?<=\/runtime\.)(.+?)(?=\.js|$)/},
|
|
12
|
+
'polyfills' : {name: 'polyfills', extension: '.js', regex: /(?<=\/polyfills\.)(.+?)(?=\.js|$)/},
|
|
13
|
+
'vendor' : {name: 'vendor', extension: '.js', regex: /(?<=\/vendor\.)(.+?)(?=\.js|$)/},
|
|
14
|
+
'styles' : {name: 'styles', extension: '.css', regex: /(?<=\/styles\.)(.+?)(?=\.css|$)/},
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
Object.keys(fileToBeReplaced).forEach(key => {
|
|
18
|
+
replaceFile(key, fileToBeReplaced[key])
|
|
19
|
+
})
|
|
20
|
+
// glob("./dist/main*", function (er, files) {
|
|
21
|
+
// // files is an array of filenames.
|
|
22
|
+
// // If the `nonull` option is set, and nothing
|
|
23
|
+
// // was found, then files is ["**/*.js"]
|
|
24
|
+
// // er is an error object or null.
|
|
25
|
+
// console.log('fileeeeee', files)
|
|
26
|
+
// hashCode = files[0].match(/(?<=\/main\.)(.+?)(?=\.js|$)/)[0]
|
|
27
|
+
// console.log('hashhh',hashCode )
|
|
28
|
+
// replace({
|
|
29
|
+
// regex: "main",
|
|
30
|
+
// replacement: "main."+hashCode ,
|
|
31
|
+
// paths: [ './dist/launch.js' ],
|
|
32
|
+
// recursive: true,
|
|
33
|
+
// silent: false,
|
|
34
|
+
// }, (error,changedFiles)=>{
|
|
35
|
+
// if (error) {
|
|
36
|
+
// return console.error('Error occurred:', error);
|
|
37
|
+
// }
|
|
38
|
+
// console.log('Modified files:', changedFiles.join(', '));
|
|
39
|
+
// });
|
|
40
|
+
|
|
41
|
+
// })
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
function replaceFile(name, element){
|
|
48
|
+
let hashCode = ''
|
|
49
|
+
glob("./dist/"+name+"*", function (er, files) {
|
|
50
|
+
// files is an array of filenames.
|
|
51
|
+
// If the `nonull` option is set, and nothing
|
|
52
|
+
// was found, then files is ["**/*.js"]
|
|
53
|
+
// er is an error object or null.
|
|
54
|
+
console.log('fileeeeee', files, element)
|
|
55
|
+
hashCode = files[0].match(element.regex)[0]
|
|
56
|
+
console.log('hashhh',hashCode ,name )
|
|
57
|
+
replace({
|
|
58
|
+
regex: '{{'+ name + '}}'+ element.extension,
|
|
59
|
+
replacement: name + "." + hashCode + element.extension ,
|
|
60
|
+
paths: [ './dist/launch.js' ],
|
|
61
|
+
recursive: true,
|
|
62
|
+
silent: false,
|
|
63
|
+
}, (error,changedFiles)=>{
|
|
64
|
+
if (error) {
|
|
65
|
+
return console.error('Error occurred:', error);
|
|
66
|
+
}
|
|
67
|
+
console.log('Modified files:', changedFiles);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
})
|
|
71
|
+
}
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/** */
|
|
2
|
+
ready(function() {
|
|
3
|
+
console.log('DOM is ready, call initWidget');
|
|
4
|
+
if(!window.tileDeskAsyncInit){
|
|
5
|
+
initAysncEvents();
|
|
6
|
+
}
|
|
7
|
+
initWidget();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
/** */
|
|
11
|
+
function ready(callbackFunction){
|
|
12
|
+
// if(document.readyState != 'loading'){
|
|
13
|
+
// console.log('in ifffffff', document.readyState)
|
|
14
|
+
// callbackFunction()
|
|
15
|
+
// }
|
|
16
|
+
// else{
|
|
17
|
+
// document.addEventListener("DOMContentLoaded", callbackFunction)
|
|
18
|
+
// }
|
|
19
|
+
document.addEventListener('scroll', start);
|
|
20
|
+
document.addEventListener('mousedown', start);
|
|
21
|
+
document.addEventListener('mousemove', start);
|
|
22
|
+
document.addEventListener('touchstart', start);
|
|
23
|
+
document.addEventListener('keydown', start);
|
|
24
|
+
|
|
25
|
+
function start(){
|
|
26
|
+
if(document.readyState==='complete'){
|
|
27
|
+
callbackFunction()
|
|
28
|
+
}else if(window.attachEvent){
|
|
29
|
+
window.attachEvent('onload',callbackFunction);
|
|
30
|
+
}else{
|
|
31
|
+
window.addEventListener('load',callbackFunction,false);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
document.removeEventListener('scroll', start);
|
|
35
|
+
document.removeEventListener('mousedown', start);
|
|
36
|
+
document.removeEventListener('mousemove', start);
|
|
37
|
+
document.removeEventListener('touchstart', start);
|
|
38
|
+
document.removeEventListener('scroll', start);
|
|
39
|
+
document.removeEventListener('keydown', start);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
/** */
|
|
47
|
+
function loadIframe(tiledeskScriptBaseLocation) {
|
|
48
|
+
var dev = window.location.hostname.includes('localhost')? true: false;
|
|
49
|
+
|
|
50
|
+
var containerDiv = document.createElement('div');
|
|
51
|
+
containerDiv.setAttribute('id','tiledesk-container');
|
|
52
|
+
containerDiv.classList.add("closed");
|
|
53
|
+
document.body.appendChild(containerDiv);
|
|
54
|
+
|
|
55
|
+
var iDiv = document.createElement('div');
|
|
56
|
+
iDiv.setAttribute('id','tiledeskdiv');
|
|
57
|
+
containerDiv.appendChild(iDiv);
|
|
58
|
+
|
|
59
|
+
var ifrm = document.createElement("iframe");
|
|
60
|
+
ifrm.setAttribute("frameborder", "0");
|
|
61
|
+
ifrm.setAttribute("border", "0");
|
|
62
|
+
ifrm.setAttribute("title", "Tiledesk Widget")
|
|
63
|
+
|
|
64
|
+
var srcTileDesk = '<html lang="en">';
|
|
65
|
+
srcTileDesk += '<head>';
|
|
66
|
+
srcTileDesk += '<meta charset="utf-8">';
|
|
67
|
+
srcTileDesk += '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />';
|
|
68
|
+
srcTileDesk += '<title>Tilechat Widget</title>';
|
|
69
|
+
srcTileDesk += '<base href="'+tiledeskScriptBaseLocation+ '/">';
|
|
70
|
+
srcTileDesk += '<link rel="icon" type="image/x-icon" href="favicon.ico">';
|
|
71
|
+
srcTileDesk += '<link rel="stylesheet" type="text/css" href="' + tiledeskScriptBaseLocation +'/assets/styles/tiledesk_v1.scss" media="all">';
|
|
72
|
+
srcTileDesk += '</head>';
|
|
73
|
+
srcTileDesk += '<body>';
|
|
74
|
+
srcTileDesk += '<chat-root></chat-root>';
|
|
75
|
+
srcTileDesk += '<script async type="text/javascript" src="'+tiledeskScriptBaseLocation+'/{{runtime}}.js"></script>';
|
|
76
|
+
srcTileDesk += '<script async type="text/javascript" src="'+tiledeskScriptBaseLocation+'/{{polyfills}}.js"></script>';
|
|
77
|
+
srcTileDesk += '<script async type="text/javascript" src="'+tiledeskScriptBaseLocation+'/{{vendor}}.js"></script>';
|
|
78
|
+
srcTileDesk += '<script async type="text/javascript" src="'+tiledeskScriptBaseLocation+'/{{main}}.js"></script>';
|
|
79
|
+
srcTileDesk += '<link type="text/css" rel="stylesheet" href="'+tiledeskScriptBaseLocation+'/{{styles}}.css" media="all"></link>';
|
|
80
|
+
srcTileDesk += '</body>';
|
|
81
|
+
srcTileDesk += '</html>';
|
|
82
|
+
|
|
83
|
+
ifrm.setAttribute('id','tiledeskiframe');
|
|
84
|
+
ifrm.setAttribute('tiledesk_context','parent');
|
|
85
|
+
|
|
86
|
+
/** */
|
|
87
|
+
window.tiledesk.on('onInit', function(event_data) {
|
|
88
|
+
// console.log("launch onInit isopen", tiledeskScriptBaseLocation, window.tiledesk.angularcomponent.component.g.isOpen);
|
|
89
|
+
if (window.tiledesk.angularcomponent.component.g.isOpen) {
|
|
90
|
+
containerDiv.classList.add("open");
|
|
91
|
+
containerDiv.classList.remove("closed");
|
|
92
|
+
iDiv.classList.remove("callout");
|
|
93
|
+
} else {
|
|
94
|
+
containerDiv.classList.add("closed");
|
|
95
|
+
containerDiv.classList.remove("open");
|
|
96
|
+
iDiv.classList.remove("messagePreview");
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
/** */
|
|
100
|
+
window.tiledesk.on('onOpen', function(event_data) {
|
|
101
|
+
containerDiv.classList.add("open");
|
|
102
|
+
containerDiv.classList.remove("closed");
|
|
103
|
+
iDiv.classList.remove("callout");
|
|
104
|
+
iDiv.classList.remove("messagePreview");
|
|
105
|
+
});
|
|
106
|
+
/** */
|
|
107
|
+
window.tiledesk.on('onClose', function(event_data) {
|
|
108
|
+
containerDiv.classList.add("closed");
|
|
109
|
+
containerDiv.classList.remove("open");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
/** */
|
|
113
|
+
window.tiledesk.on('onOpenEyeCatcher', function(event_data) {
|
|
114
|
+
iDiv.classList.add("callout");
|
|
115
|
+
});
|
|
116
|
+
/** */
|
|
117
|
+
window.tiledesk.on('onClosedEyeCatcher', function(event_data) {
|
|
118
|
+
iDiv.classList.remove("callout");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
/** */
|
|
122
|
+
window.tiledesk.on('onConversationUpdated', function(event_data) {
|
|
123
|
+
const messagePreview = window.tiledesk.angularcomponent.component.g.isOpenNewMessage
|
|
124
|
+
const isOpen = window.tiledesk.angularcomponent.component.g.isOpen
|
|
125
|
+
try {
|
|
126
|
+
if (!isOpen && messagePreview) {
|
|
127
|
+
iDiv.classList.add("messagePreview");
|
|
128
|
+
iDiv.classList.remove("callout");
|
|
129
|
+
// ----------------------------//
|
|
130
|
+
}
|
|
131
|
+
} catch(er) {
|
|
132
|
+
console.error("onConversationUpdated > error: " + er);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
window.tiledesk.on('onCloseMessagePreview', function(event_data) {
|
|
137
|
+
try {
|
|
138
|
+
iDiv.classList.remove("messagePreview");
|
|
139
|
+
} catch(er) {
|
|
140
|
+
console.error("onCloseMessagePreview > error: " + er);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
/**** BEGIN EVENST ****/
|
|
146
|
+
/** */
|
|
147
|
+
window.tiledesk.on('onNewConversation', function(event_data) {
|
|
148
|
+
// console.log("test-custom-auth.html onNewConversation >>>",event_data);
|
|
149
|
+
const tiledeskToken = window.tiledesk.angularcomponent.component.g.tiledeskToken;
|
|
150
|
+
// console.log(">>>> tiledeskToken >>>> ",event_data.detail.appConfigs.apiUrl+event_data.detail.default_settings.projectid);
|
|
151
|
+
if(tiledeskToken) {
|
|
152
|
+
var httpRequest = createCORSRequest('POST', event_data.detail.appConfigs.apiUrl+event_data.detail.default_settings.projectid+'/events',true); //set async to false because loadParams must return when the get is complete
|
|
153
|
+
httpRequest.setRequestHeader('Content-type', 'application/json');
|
|
154
|
+
httpRequest.setRequestHeader('Authorization',tiledeskToken);
|
|
155
|
+
httpRequest.send(JSON.stringify({ "name":"new_conversation",
|
|
156
|
+
"attributes": {
|
|
157
|
+
"request_id":event_data.detail.newConvId,
|
|
158
|
+
"department": event_data.detail.global.departmentSelected.id,
|
|
159
|
+
"participants": event_data.detail.global.participants,
|
|
160
|
+
"language": event_data.detail.global.lang,
|
|
161
|
+
"subtype":"info",
|
|
162
|
+
"fullname":event_data.detail.global.attributes.userFullname,
|
|
163
|
+
"email":event_data.detail.global.attributes.userEmail,
|
|
164
|
+
"attributes":event_data.detail.global.attributes
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
));
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/** @deprecated event */
|
|
172
|
+
window.tiledesk.on('onLoggedIn', function(event_data) {
|
|
173
|
+
// console.log("test-custom-auth.html onLoggedIn",event_data);
|
|
174
|
+
const tiledeskToken = window.tiledesk.angularcomponent.component.g.tiledeskToken;
|
|
175
|
+
// console.log("------------------->>>> tiledeskToken: ",window.tiledesk.angularcomponent.component.g);
|
|
176
|
+
if(tiledeskToken) {
|
|
177
|
+
var httpRequest = createCORSRequest('POST', event_data.detail.appConfigs.apiUrl+event_data.detail.default_settings.projectid+'/events',true); //set async to false because loadParams must return when the get is complete
|
|
178
|
+
httpRequest.setRequestHeader('Content-type','application/json');
|
|
179
|
+
httpRequest.setRequestHeader('Authorization',tiledeskToken);
|
|
180
|
+
httpRequest.send(JSON.stringify({"name":"logged_in","attributes": {"fullname":event_data.detail.global.attributes.userFullname, "email":event_data.detail.global.attributes.userEmail, "language": event_data.detail.global.lang, "attributes":event_data.detail.global.attributes}}));
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
/** */
|
|
185
|
+
window.tiledesk.on('onAuthStateChanged', function(event_data) {
|
|
186
|
+
// console.log("test-custom-auth.html onAuthStateChanged",event_data);
|
|
187
|
+
const tiledeskToken = window.tiledesk.angularcomponent.component.g.tiledeskToken;
|
|
188
|
+
// console.log("------------------->>>> tiledeskToken: ",window.tiledesk.angularcomponent.component.g);
|
|
189
|
+
if(tiledeskToken) {
|
|
190
|
+
var httpRequest = createCORSRequest('POST', event_data.detail.appConfigs.apiUrl+event_data.detail.default_settings.projectid+'/events',true); //set async to false because loadParams must return when the get is complete
|
|
191
|
+
httpRequest.setRequestHeader('Content-type','application/json');
|
|
192
|
+
httpRequest.setRequestHeader('Authorization',tiledeskToken);
|
|
193
|
+
httpRequest.send(JSON.stringify({"name":"auth_state_changed","attributes": {"user_id":event_data.detail.global.senderId, "isLogged":event_data.detail.global.isLogged, "event":event_data.detail.event, "subtype":"info", "fullname":event_data.detail.global.attributes.userFullname, "email":event_data.detail.global.attributes.userEmail, "language":event_data.detail.global.lang, "attributes":event_data.detail.global.attributes}}));
|
|
194
|
+
httpRequest.onload = function(event) {
|
|
195
|
+
if(event.target && event.target.status === 401){
|
|
196
|
+
window.tiledesk.hide()
|
|
197
|
+
window.tiledesk.dispose()
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
/**** END EVENST ****/
|
|
203
|
+
|
|
204
|
+
iDiv.appendChild(ifrm);
|
|
205
|
+
|
|
206
|
+
if(tiledeskScriptBaseLocation.includes('localhost')){
|
|
207
|
+
ifrm.contentWindow.document.open();
|
|
208
|
+
ifrm.contentWindow.document.write(srcTileDesk);
|
|
209
|
+
ifrm.contentWindow.document.close();
|
|
210
|
+
}else {
|
|
211
|
+
ifrm.srcdoc = srcTileDesk
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
function initAysncEvents() {
|
|
219
|
+
console.log('INIT ASYNC EVENTS')
|
|
220
|
+
|
|
221
|
+
window.tileDeskAsyncInit = function() {
|
|
222
|
+
// console.log('launch tiledeskAsyncInit:::', window.Tiledesk.q)
|
|
223
|
+
window.tiledesk.on('onLoadParams', function(event_data) {
|
|
224
|
+
if (window.Tiledesk && window.Tiledesk.q && window.Tiledesk.q.length>0) {
|
|
225
|
+
window.Tiledesk.q.forEach(f => {
|
|
226
|
+
if (f.length>=1) {
|
|
227
|
+
var functionName = f[0];
|
|
228
|
+
if (functionName==="onLoadParams") {
|
|
229
|
+
//CALLING ONLY FUNCTION 'onLoadParams'
|
|
230
|
+
if (f.length==2) {
|
|
231
|
+
var functionCallback = f[1];
|
|
232
|
+
if(typeof functionCallback === "function") {
|
|
233
|
+
window.tiledesk.on(functionName, functionCallback);
|
|
234
|
+
functionCallback(event_data);
|
|
235
|
+
} else {
|
|
236
|
+
console.error("initAysncEvents --> functionCallback is not a function.");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}else if(functionName=='setParameter'){
|
|
240
|
+
//CALLING ONLY METHOD 'setParameter' AND CHECK IF IT HAS OBJECT ARG
|
|
241
|
+
if (f.length==2) {
|
|
242
|
+
var functionArgs = f[1];
|
|
243
|
+
if(typeof functionArgs === "object") {
|
|
244
|
+
window.tiledesk[functionName](functionArgs);
|
|
245
|
+
} else {
|
|
246
|
+
console.error("initAysncEvents --> functionArgs is not a object.");
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
window.tiledesk.on('onBeforeInit', function(event_data) {
|
|
256
|
+
if (window.Tiledesk && window.Tiledesk.q && window.Tiledesk.q.length>0) {
|
|
257
|
+
// console.log("w.q", window.Tiledesk.q);
|
|
258
|
+
window.Tiledesk.q.forEach(f => {
|
|
259
|
+
if (f.length>=1) {
|
|
260
|
+
var functionName = f[0];
|
|
261
|
+
if (functionName==="onLoadParams" || functionName==="setParameter") {
|
|
262
|
+
//SKIP FUNCTION WITH NAMES 'onLoadParams' AND METHOD 'setParameter'
|
|
263
|
+
} else if (functionName.startsWith("on")) {
|
|
264
|
+
// CALLING METHOD THAT STARTS WITH 'on'
|
|
265
|
+
if (f.length==2) {
|
|
266
|
+
var functionCallback = f[1];
|
|
267
|
+
if(typeof functionCallback === "function"){
|
|
268
|
+
window.tiledesk.on(functionName, functionCallback); //potrei usare window.Tiledesk ?!?
|
|
269
|
+
if (functionName==="onBeforeInit") {
|
|
270
|
+
functionCallback(event_data)
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
console.error("functionCallback is not a function.");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
} else {
|
|
277
|
+
//CALLING REMAININGS METHOD and CHECK IF CONTAINS ARG TO PASS THROUGH THE METHOD
|
|
278
|
+
if (f.length==2) {
|
|
279
|
+
let args = f[1]
|
|
280
|
+
window.tiledesk[functionName](args);
|
|
281
|
+
} else {
|
|
282
|
+
window.tiledesk[functionName]();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// RICHIAMATO DOPO L'INIT DEL WIDGET
|
|
292
|
+
window.Tiledesk = function() {
|
|
293
|
+
if (arguments.length>=1) {
|
|
294
|
+
var functionName = arguments[0];
|
|
295
|
+
if (arguments.length==2) {
|
|
296
|
+
var functionCallback = arguments[1];
|
|
297
|
+
}
|
|
298
|
+
var methodOrProperty = window.tiledesk[functionName];
|
|
299
|
+
if(typeof methodOrProperty==="function"){
|
|
300
|
+
return window.tiledesk[functionName](functionCallback);
|
|
301
|
+
}else { //property
|
|
302
|
+
return window.tiledesk[functionName];
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
*
|
|
314
|
+
*/
|
|
315
|
+
function initWidget() {
|
|
316
|
+
var tiledeskroot = document.createElement('chat-root');
|
|
317
|
+
var tiledeskScriptLocation = document.getElementById("tiledesk-jssdk").src;
|
|
318
|
+
//var currentScript = document.currentScript;
|
|
319
|
+
//var tiledeskScriptLocation = '';
|
|
320
|
+
//setInterval(function(){
|
|
321
|
+
//tiledeskScriptLocation = currentScript.src;
|
|
322
|
+
var tiledeskScriptBaseLocation = tiledeskScriptLocation.replace("/launch.js","");
|
|
323
|
+
window.tiledesk = new function() {
|
|
324
|
+
//this.type = "macintosh";
|
|
325
|
+
this.tiledeskroot = tiledeskroot;
|
|
326
|
+
this.on = function (event_name, handler) {
|
|
327
|
+
tiledeskroot.addEventListener(event_name, handler);
|
|
328
|
+
};
|
|
329
|
+
this.getBaseLocation = function() {
|
|
330
|
+
return tiledeskScriptBaseLocation;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
try {
|
|
335
|
+
window.tileDeskAsyncInit();
|
|
336
|
+
}catch(er) {
|
|
337
|
+
if (typeof window.tileDeskAsyncInit == "undefined") {
|
|
338
|
+
console.log("tileDeskAsyncInit() doesn't exists");
|
|
339
|
+
} else {
|
|
340
|
+
console.log(er);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
document.body.appendChild(tiledeskroot);
|
|
344
|
+
initCSSWidget(tiledeskScriptBaseLocation);
|
|
345
|
+
loadIframe(tiledeskScriptBaseLocation);
|
|
346
|
+
//},2000);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
function initCSSWidget(tiledeskScriptBaseLocation) {
|
|
354
|
+
var cssId = 'iframeCss'; // you could encode the css path itself to generate id..
|
|
355
|
+
// if (!document.getElementById(cssId))
|
|
356
|
+
// {
|
|
357
|
+
var head = document.getElementsByTagName('head')[0];
|
|
358
|
+
var link = document.createElement('link');
|
|
359
|
+
link.id = cssId;
|
|
360
|
+
link.rel = 'stylesheet';
|
|
361
|
+
link.type = 'text/css';
|
|
362
|
+
link.href = tiledeskScriptBaseLocation+'/iframe-style.css';
|
|
363
|
+
link.media = 'print';
|
|
364
|
+
link.onload = function(){
|
|
365
|
+
link.media = 'all'
|
|
366
|
+
}
|
|
367
|
+
head.appendChild(link);
|
|
368
|
+
// }
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
//DEPRECATED
|
|
373
|
+
function signInWithCustomToken() {
|
|
374
|
+
let json = JSON.stringify({
|
|
375
|
+
"id_project": "5b55e806c93dde00143163dd"
|
|
376
|
+
});
|
|
377
|
+
var httpRequest = createCORSRequest('POST', 'https://tiledesk-server-pre.herokuapp.com/auth/signinAnonymously',true);
|
|
378
|
+
if (!httpRequest) {
|
|
379
|
+
throw new Error('CORS not supported');
|
|
380
|
+
}
|
|
381
|
+
httpRequest.setRequestHeader('Content-type','application/json');
|
|
382
|
+
httpRequest.send(json);
|
|
383
|
+
httpRequest.onload = function() {
|
|
384
|
+
if (httpRequest.readyState === XMLHttpRequest.DONE) {
|
|
385
|
+
if (httpRequest.status === 200) {
|
|
386
|
+
try {
|
|
387
|
+
var response = JSON.parse(httpRequest.responseText);
|
|
388
|
+
window.tiledesk.signInWithCustomToken(response);
|
|
389
|
+
}
|
|
390
|
+
catch(err) {
|
|
391
|
+
console.error(err.message);
|
|
392
|
+
}
|
|
393
|
+
return true;
|
|
394
|
+
} else {
|
|
395
|
+
alert('There was a problem with the request.');
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
httpRequest.onerror = function() {
|
|
400
|
+
console.error('There was an error!');
|
|
401
|
+
return false;
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
function createCORSRequest(method, url, async) {
|
|
407
|
+
var xhr = new XMLHttpRequest();
|
|
408
|
+
if ("withCredentials" in xhr) {
|
|
409
|
+
xhr.open(method, url, async);
|
|
410
|
+
} else if (typeof XDomainRequest != "undefined") {
|
|
411
|
+
xhr = new XDomainRequest();
|
|
412
|
+
xhr.open(method, url);
|
|
413
|
+
} else {
|
|
414
|
+
xhr = null;
|
|
415
|
+
}
|
|
416
|
+
return xhr;
|
|
417
|
+
}
|