Файл: js/mail/typing.js
Строк: 174
<?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;
var window_focus = true;
$(window).focus(function () {
window_focus = true;
}).blur(function () {
window_focus = false;
});
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) {0;
var self = this;
var $inputMsg, $typingElement, $typingDotsElement;
// handlers
var typingHandler, dotsHandler;
var checkTypingTimeout;
// jqXHR
var checkTypingXHR, typingXHR;
var isTyping = false,
isUserTyping = false;
$(document).ready(function () {
$inputMsg = $('#inputMsg');
// for altername_post_form.php
if ($inputMsg.length == 0)
$inputMsg = $('textarea[name="msg"]');
$typingElement = $('#typing');
$typingDotsElement = $('#typingDots');
self.initListeners();
});
this.initListeners = function () {
$inputMsg.on('input', function () {
if (!isTyping) {
isTyping = true;
if (typingXHR != undefined)
typingXHR.abort();
self.updateTyping();
}
if (typingHandler != undefined)
clearInterval(typingHandler);
typingHandler = setTimeout(function () {
isTyping = false;
if (typingXHR != undefined)
typingXHR.abort();
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 () {
typingXHR = $.ajax({
url: UPDATE_TYPING_URL,
type: 'post',
data: {
id_user: id_user,
is_typing: isTyping
},
success: function (json) {
handleJsonErrors(json);
}
});
}
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;
if (dotsHandler != undefined)
clearInterval(dotsHandler);
if (isUserTyping) {
dotsHandler = setInterval(function () {
self.updateDots();
}, 200);
$typingElement.show(100);
}
else
$typingElement.hide(100);
},
complete: function () {
if ($.isFunction(completeCallback))
completeCallback();
}
});
}
self.updateDots = function () {
var dots = $typingDotsElement.text();
var objs = {'': '.', '.': '..', '..': '...', '...': ''};
$typingDotsElement.text(objs[dots]);
}
}
?>