Файл: js/mail/typing.js
Строк: 182
<?php
var SCRIPTS_URL = '/ajax/mail/',
CHECK_TYPING_URL = SCRIPTS_URL + 'check_typing.php',
UPDATE_TYPING_URL = SCRIPTS_URL + 'update_typing.php';
var CHECK_TYPING_TIMEOUT = 2000,
TYPING_WAIT_TIME = 10;
function handleJsonErrors(json) {
if (json == undefined || json.error != undefined) {
showError(json == undefined ? 'Произошла ошибка' : json.error);
return true;
}
return false;
}
function showError(error) {
alert(error);
}
var MailTyping = function (id_user) {
var self = this;
var $inputMsg, $typingElement, $typingDotsElement;
// handlers
var typingHandler, dotsHandler;
var checkTypingTimeout;
// jqXHR
var checkTypingXHR, typingXHR;
var isTyping = false,
isUserTyping = false,
lastTypingTime;
$(document).ready(function () {
$inputMsg = $('#inputMsg');
// for altername_post_form.php
if ($inputMsg.length == 0)
$inputMsg = $('textarea[name="msg"]');
// for mail by iskatel'
if ($inputMsg.length == 0)
$inputMsg = $('#messages');
$typingElement = $('#typing');
$typingDotsElement = $('#typingDots');
self.initListeners();
});
this.initListeners = function () {
$inputMsg.on('input', function () {
if (!isTyping) {
isTyping = true;
self.updateTyping();
}
if (typingHandler != undefined)
clearInterval(typingHandler);
typingHandler = setTimeout(function () {
isTyping = false;
self.updateTyping();
}, 1000);
});
self.setCheckTypingTimeout();
}
this.setCheckTypingTimeout = function () {
self.abortCheckTyping();
checkTypingTimeout = setTimeout(function () {
self.checkTyping(function () {
self.setCheckTypingTimeout();
});
}, CHECK_TYPING_TIMEOUT);
}
this.abortCheckTyping = function () {
if (checkTypingTimeout != undefined)
clearInterval(checkTypingTimeout);
if (checkTypingXHR != undefined)
checkTypingXHR.abort();
}
this.updateTyping = function () {
if (typingXHR != undefined)
typingXHR.abort();
typingXHR = $.ajax({
url: UPDATE_TYPING_URL,
type: 'post',
data: {
id_user: id_user,
is_typing: isTyping
},
success: function (json) {
if (handleJsonErrors(json))
return;
}
});
}
this.checkTyping = function (completeCallback) {
checkTypingXHR = $.ajax({
url: CHECK_TYPING_URL,
type: 'post',
data: {
id_user: id_user,
is_typing: isUserTyping
},
success: function (json) {
if (handleJsonErrors(json))
return;
isUserTyping = json.is_typing;
var mailTypingId = json.mailTypingId;
if (mailTypingId != id_user && isTyping)
self.updateTyping();
if (dotsHandler != undefined)
clearInterval(dotsHandler);
lastTypingTime = json.time;
if (lastTypingTime < unixTimestamp() - TYPING_WAIT_TIME)
isUserTyping = false;
self.updateUserIsTyping();
},
complete: function () {
if ($.isFunction(completeCallback))
completeCallback();
}
});
}
self.updateUserIsTyping = function () {
if (isUserTyping) {
dotsHandler = setInterval(function () {
self.updateDots();
}, 200);
$typingElement.show(100);
}
else
$typingElement.hide(100);
}
self.updateDots = function () {
var dots = $typingDotsElement.text();
var objs = {'': '.', '.': '..', '..': '...', '...': ''};
$typingDotsElement.text(objs[dots]);
}
}
?>