forked from jasonmarkbeaton/tidio-chat-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtidio-chat-api.js
executable file
·349 lines (255 loc) · 8.91 KB
/
tidio-chat-api.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
** API Module - Color
*/
var tidioChatApiColor = {
renderChatColors: function(firstColor, secondColor) {
//rgbX - hex values with changed opacity
var rgb02 = this.changeHexOpacity(secondColor, 0.2);
var rgb03 = this.changeHexOpacity(secondColor, 0.3);
var rgb06 = this.changeHexOpacity(secondColor, 0.6);
var style = '';
//set primary color
style += '.tidio-chat-window header,#tidio-chat-discussion .operator .avatar-placeholder,.tidio-chat-window input[type="submit"]{background-color:' + secondColor + ';}';
// set primary color (webkit)
style += '.tidio-chat-window ::-webkit-scrollbar-thumb,.tidio-chat-window ::-webkit-scrollbar-thumb:window-inactive{background-color:' + secondColor + ';}';
//set color for message
style += '#tidio-chat-discussion .operator .message{color:' + secondColor + ';background-color:' + rgb02 + ';}';
//set color for message footer
style += '#tidio-chat-discussion .operator footer{color:' + rgb06 + ';background-color:' + rgb03 + ';}';
//button styles
style += this.getGradientStyle('#tidio-chat-button', firstColor, secondColor);
style += '#tidio-chat-button{border-bottom-color:' + firstColor + ';}';
return style;
},
changeHexOpacity: function(hex, opacity) {
var rgb = this.hexToRgb(hex);
opacity = 1 - opacity;
rgb.r = (255 - rgb.r) * opacity + rgb.r;
rgb.g = (255 - rgb.g) * opacity + rgb.g;
rgb.b = (255 - rgb.b) * opacity + rgb.b;
return this.rgbToHex(rgb.r, rgb.g, rgb.b);
},
componentToHex: function(c) {
c = Math.round(c);
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
},
rgbToHex: function(r, g, b) {
return "#" + this.componentToHex(r) + this.componentToHex(g) + this.componentToHex(b);
},
hexToRgb: function(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
},
setGradient: function($el, firstColor, secondColor, fallbackColor) {
if (typeof fallbackColor == 'undefined') {
fallbackColor = secondColor;
}
var mozBg = '-moz-linear-gradient(top, ' + firstColor + ' 1%, ' + secondColor + ' 100%)';
var webkitBgOld = '-webkit-gradient(linear, left top, left bottom, color-stop(1%,' + secondColor + '), color-stop(100%, ' + secondColor + '))';
var webkitBg = '-webkit-linear-gradient(top, ' + firstColor + ' 1%,' + secondColor + ' 100%)';
var operaBg = '-o-linear-gradient(top, ' + firstColor + ' 1%,' + secondColor + ' 100%)';
var msBg = '-ms-linear-gradient(top, ' + firstColor + ' 1%,' + secondColor + ' 100%)';
var linearBg = 'linear-gradient(to bottom, ' + firstColor + ' 1%,' + secondColor + ' 100%)';
var filterBg = "progid:DXImageTransform.Microsoft.gradient( startColorstr='" + firstColor + "', endColorstr='" + secondColor + "',GradientType=0 )";
$el.css('background', fallbackColor)
.css('background', mozBg)
.css('background', webkitBgOld)
.css('background', webkitBg)
.css('background', operaBg)
.css('background', msBg)
.css('background', linearBg)
.css('filter', filterBg);
},
getGradientStyle: function(elSelector, firstColor, secondColor, fallbackColor) {
if (typeof fallbackColor == 'undefined') {
fallbackColor = secondColor;
}
var style = elSelector + '{';
style += 'background: ' + fallbackColor + ';';
style += 'background: -moz-linear-gradient(top, ' + firstColor + ' 1%, ' + secondColor + ' 100%);';
style += 'background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,' + secondColor + '), color-stop(100%, ' + secondColor + '));';
style += 'background: -webkit-linear-gradient(top, ' + firstColor + ' 1%,' + secondColor + ' 100%);';
style += 'background: -o-linear-gradient(top, ' + firstColor + ' 1%,' + secondColor + ' 100%);';
style += 'background: -ms-linear-gradient(top, ' + firstColor + ' 1%,' + secondColor + ' 100%);';
style += 'background: linear-gradient(to bottom, ' + firstColor + ' 1%,' + secondColor + ' 100%);';
style += "background: progid:DXImageTransform.Microsoft.gradient( startColorstr='" + firstColor + "', endColorstr='" + secondColor + "',GradientType=0 );";
style += '}';
return style;
}
};
/*
** Tidio Chat API
*/
var tidioChatApi = {
chat_is_ready: false,
chat_is_ready_queue: [],
chat_display: true,
// Map of function which we can trigger,
// 0 - means that function is trigger immediately
// 1 - we waiting to moment when everything else is loaded
function_map: {
'setColorPallete': 1,
'languageSet': 0,
'badgeShow': 0,
'badgeHide': 0,
'messageFromVisitor': 0,
'messageFromOperator': 0,
'popUpOpen': 0,
'popUpHide': 0,
'chatDisplay': 1
},
$iframe: null,
$iframe_chat: null,
$iframeJQ: null,
listeners: {},
language: {},
init: function(){
tidioChatRender.listener('chatIsReady', function(){
if(tidioChatApi.chat_is_ready){
return false;
}
//
tidioChatApi.chat_is_ready = true;
tidioChatApi.$iframe = document.getElementById('tidio-chat').contentWindow.document;
tidioChatApi.$iframeJQ = tidioChatApi.$iframe.jQuery;
tidioChatApi.chatIsReadyQueueTrigger();
}, true);
},
// Function is checking if chat is ready, if not it push function to queue, and queue is trigger when everything is loaded
chatIsReadyPush: function(_func){
if(tidioChatApi.chat_is_ready){
_func();
return true;
}
//
this.chat_is_ready_queue.push(_func);
return false;
},
chatIsReadyQueueTrigger: function(){
for(var i=0,c=this.chat_is_ready_queue.length;i<c;++i){
this.chat_is_ready_queue[i]();
}
},
//
method: function(id, a, b){
if(typeof this.function_map[id]=='undefined'){
return false;
}
var map = this.function_map[id];
if(map===1){
tidioChatApi[id](a,b);
return false;
} else {
this.chatIsReadyPush(function(){
tidioChatApi[id](a,b);
});
}
},
on: function(id, a, b){
if(typeof a=='function'){
if(!this.listeners[id]){
this.listeners[id] = [];
}
this.listeners[id].push(a);
return true;
}
if(!this.listeners[id]){
return false;
}
if(typeof a=='undefined')
a = null;
if(typeof b=='undefined')
b = null;
for(var i=0,c=this.listeners[id].length;i<c;++i){
this.listeners[id][i](a,b)
}
return true;
},
/*
** Language
*/
languageSet: function(lang){
this.language = lang;
},
/*
** Badge
*/
badgeShow: function(){
tidioChatApi.$iframe.tidioChat.badgeDisplay(true);
},
badgeHide: function(){
tidioChatApi.$iframe.tidioChat.badgeDisplay(false);
},
/*
** Chat Display
*/
chatDisplay: function(display){
// rendering chat if was previous blocked
if(display && typeof tidioChatRender=='object' && !this.chat_display && !tidioChatRender.chat_created){
this.chat_display = true;
tidioChatRender.create(tidioChatRender.chat_data);
return '3';
}
if(this.chat_is_ready && typeof tidioChatRender=='object'){
if(display){
document.getElementById('tidio-chat').style.display = 'block';
} else {
document.getElementById('tidio-chat').style.display = 'none';
}
return false;
}
if(typeof tidioChatRender=='undefined'){
this.chat_display = display;
return false;
}
this.chatIsReadyPush(function(){
tidioChatApi.chatDisplay(display);
});
},
/*
** Message
*/
messageFromVisitor: function(msg){
tidioChatApi.$iframe.tidioChat.onMessageFromVisitor(msg, true);
},
messageFromOperator: function(msg){
tidioChatApi.$iframe.tidioChat.onMessageFromOperator({
message: msg
});
},
/*
** PopUp
*/
popUpOpen: function(){
tidioChatApi.$iframe.tidioChat.popupShow();
},
popUpHide: function(){
tidioChatApi.$iframe.tidioChat.popupHide();
},
/*
** Color Customization (extends: tidioChatApiColor)
*/
setColorPallete: function(color1, color2){
var pallete_css = tidioChatApiColor.renderChatColors(color1, color2);
this.setApiGlobalData('colorPallete', pallete_css);
//
this.chatIsReadyPush(function(){
var ele_head = tidioChatRender._iframe_doc.querySelector('head'),
ele_style = document.createElement('style');
ele_style.innerHTML = pallete_css;
ele_head.appendChild(ele_style);
});
},
setApiGlobalData: function(id, value){
if(!document.tidioChatGlobalData){
document.tidioChatGlobalData = {};
}
document.tidioChatGlobalData[id] = value;
}
};
document.tidioChatApi = tidioChatApi;