-
Notifications
You must be signed in to change notification settings - Fork 110
/
chat.js
287 lines (213 loc) · 7.73 KB
/
chat.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
$(function() {
$("#telegramSiteHelper_chatTextBox").bind('keyup', function(e) {
if (e.keyCode == 13) {
msg = $("#telegramSiteHelper_chatTextBox").val();
if (msg.length > 0) {
if(window.whileSending != true){
sendMsgs(msg)
}
}
}
});
StartCHAT();
});
var startByClick=0;
function StartCHAT() {
var tbChatHash = $.cookie("tbChatHash");
if (tbChatHash !== null) {
getMsgs();
} else {
startByClick=1;
}
}
function getMsgs() {
var tbChatHash = $.cookie("tbChatHash");
var lastMessageId = $("#telegramSiteHelper_lastMId").val();
var params = {};
params['lastMessageId'] = lastMessageId;
params['tbChatHash'] = tbChatHash;
console.log("getMsgs "+tbChatHash);
$.ajax({
url: "../telegramSiteHelper/tbGetMessages.php",
type: "POST",
dataType: "JSON",
data: params,
async: true,
timeout: 61000
}).done(function(answer) {
console.log(answer);
if (answer.status == 'ok') {
if (answer.msgs) {
if (answer.msgs.length > 0) {
if (lastMessageId != 0) {
$("#telegramSiteHelper_chatSound")[0].play();
}
$.each(answer.msgs, function(i, msg) {
var m = makeMsg(msg);
$("#telegramSiteHelper_lastMId").val(msg.msgId)
appendMessage(m);
});
refreshChatScroll();
}else{
if (lastMessageId == ""){
$("#telegramSiteHelper_lastMId").val("0")
}
}
}
console.log("OKEY "+answer.status);
getMsgs();
return (true);
} else if (answer.status == 'error') { // ЕСЛИ API возвратило status=error обрабатываем ошибки.
console.log("FAIL "+answer.status);
if(answer.error=="bad_chHash"){
$.cookie("tbChatHash",null);
alert("Please reload page!")
}
setTimeout(function() {
getMsgs();
}, 1500);
}
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log("FAIL "+textStatus);
console.log(jqXHR)
setTimeout(function() {
getMsgs();
}, 1500);
});
}
function sendMsgs(tbMessage) {
var tbChatHash = $.cookie("tbChatHash");
var params = {};
window.whileSending = true;
params['tbMessage'] = tbMessage;
params['tbChatHash'] = tbChatHash;
$.ajax({
url: "../telegramSiteHelper/tbSendMessage.php",
type: "POST",
dataType: "JSON",
data: params,
async: true,
timeout: 25000
}).done(function(answer) {
window.whileSending = false;
console.log(answer);
if (answer.status == 'ok') {
$("#telegramSiteHelper_chatTextBox").val("");
$("#telegramSiteHelper_lastMId").val(answer.lastMessageId);
var tbChatHash = $.cookie("tbChatHash")
if (tbChatHash == null) {
$.cookie("tbChatHash", answer.tbChatHash);
getMsgs();
}
/*var startByClick = $.cookie("startByClick");
if (startByClick = 1) {
$.cookie("startByClick", 0);
getMsgs();
}
*/
$.cookie("managerName", answer.managerName);
var msgObj = {}
msgObj.msgFrom = "c";
msgObj.msgText = tbMessage;
msgObj.msgId = answer.lastMessageId;
msgObj.msgTime = answer.lastMessageDate;
var m = makeMsg(msgObj);
appendMessage(m);
refreshChatScroll();
} else if (answer.status == 'error') {
if(answer.error=="NO_MANAGERS"){
alert("No availible managers! Sorry...");
}else{
alert("Error during send message. Try again after page refresh!");
}
}
}).fail(function(jqXHR, textStatus, errorThrown) {
window.whileSending = false; //
console.log(jqXHR);
alert("Error during send message. Try again after page refresh! "+ textStatus);
});
//####
}
function refreshChatScroll() {
var h = $("#telegramSiteHelper_chatMessages").height();
if (h > 300) {
var a = 300 - h;
} else {
a = 1;
}
$("#telegramSiteHelper_chatWrapper").animate({ scrollTop: h }, "2");
}
function makeMsg(msgObj) {
if (msgObj.msgFrom == "m") {
msgObj.mCreator = "Менеджер";
var managerName = $.cookie("managerName");
if (managerName != null) {
msgObj.mCreator = managerName;
}
msgObj.clr = "1";
} else {
msgObj.mCreator = "Я";
msgObj.clr = "2";
}
var m = "<li class=\"msg\" id=\"msg_" + msgObj.msgId + "\">";
m += "<b class=\"name clr" + msgObj.clr + "\">" + msgObj.mCreator + ":</b>";;
m += "<b class=\"time\">" + msgObj.msgTime + "</b>";
m += "<div class=\"clr\"></div>";
m += msgObj.msgText;
m += "</li>";
return (m);
}
function appendMessage(msgTxt) {
$("#telegramSiteHelper_firstChatMsg").hide();
$("#telegramSiteHelper_chatMessages").append(msgTxt);
}
/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};