2016-08-16 21:11:42 +00:00
|
|
|
function updatemsgtime(msgnode) {
|
2016-08-15 10:37:51 +00:00
|
|
|
var spans = msgnode.getElementsByTagName("span");
|
|
|
|
var ctime = null;
|
|
|
|
for (var i = 0; i < spans.length; i++)
|
|
|
|
if (spans[i].className.split(' ').indexOf("converttime") > -1)
|
|
|
|
ctime = spans[i];
|
|
|
|
if (ctime != null)
|
2016-08-16 21:11:42 +00:00
|
|
|
ctime.innerText = moment($(ctime).data("val"), "YYYY-MM-DDTHH:mm:ssZ").fromNow();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlereceivedmessage(msgnode) {
|
|
|
|
updatemsgtime(msgnode);
|
2016-08-15 10:37:51 +00:00
|
|
|
msgnode.scrollIntoView(false);
|
|
|
|
}
|
|
|
|
|
2016-08-16 10:51:06 +00:00
|
|
|
var unreadCount = 0;
|
|
|
|
|
|
|
|
var updateUnreadCount = function () {
|
|
|
|
if (unreadCount > 0)
|
|
|
|
document.title = "(" + unreadCount + ") Chat";
|
|
|
|
else
|
|
|
|
document.title = "Chat";
|
2016-08-16 21:11:42 +00:00
|
|
|
var msgs = document.getElementById("channelmessages").getElementsByClassName("chmessage");
|
2016-08-16 10:51:06 +00:00
|
|
|
for (var i = msgs.length - 1; i >= 0; i--) {
|
|
|
|
if (i >= msgs.length - unreadCount)
|
2016-08-17 10:48:31 +00:00
|
|
|
msgs[i].classList.add("unreadmsg");
|
2016-08-16 10:51:06 +00:00
|
|
|
else
|
2016-08-17 10:48:31 +00:00
|
|
|
msgs[i].classList.remove("unreadmsg");
|
2016-08-16 21:11:42 +00:00
|
|
|
updatemsgtime(msgs[i]);
|
2016-08-16 10:51:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var addUnread = function addUnread() {
|
2016-08-18 20:35:23 +00:00
|
|
|
if (shouldread || document.getElementById("msginput").disabled)
|
|
|
|
return;
|
2016-08-16 10:51:06 +00:00
|
|
|
unreadCount++;
|
|
|
|
updateUnreadCount();
|
|
|
|
};
|
|
|
|
|
|
|
|
var resetUnread = function resetUnread() {
|
|
|
|
unreadCount = 0;
|
|
|
|
updateUnreadCount();
|
|
|
|
};
|
|
|
|
|
2016-08-17 10:48:31 +00:00
|
|
|
var shouldpoll = false;
|
2016-08-17 12:31:29 +00:00
|
|
|
var canswitchconversations = true;
|
2016-08-17 10:48:31 +00:00
|
|
|
function poll() {
|
|
|
|
setTimeout(function () {
|
|
|
|
if (!shouldpoll)
|
|
|
|
return;
|
|
|
|
$.ajax({
|
|
|
|
url: "/receivemessage", data: document.getElementById("convidp").innerText, success: function (data) {
|
|
|
|
var msgs = document.getElementById("channelmessages");
|
|
|
|
msgs.innerHTML += data;
|
|
|
|
var msgelement = msgs.children[msgs.children.length - 1];
|
|
|
|
handlereceivedmessage(msgelement);
|
2016-08-18 10:08:37 +00:00
|
|
|
console.log("Can switch conversations now");
|
2016-08-17 12:31:29 +00:00
|
|
|
canswitchconversations = true;
|
2016-08-18 20:35:23 +00:00
|
|
|
addUnread();
|
2016-08-17 12:31:29 +00:00
|
|
|
|
2016-08-17 10:48:31 +00:00
|
|
|
}, error: function (data) {
|
|
|
|
if (data.responseText) {
|
|
|
|
if (data.responseText.indexOf("ERROR") == -1)
|
|
|
|
showError(data.responseText);
|
|
|
|
else
|
|
|
|
console.log("Got empty string error...");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
showError("Can't connect to the server!");
|
|
|
|
}, dataType: "text", complete: poll, method: "POST"
|
|
|
|
});
|
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
|
|
|
|
function startPoll() {
|
|
|
|
if (!shouldpoll) {
|
|
|
|
shouldpoll = true;
|
|
|
|
poll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopPoll() {
|
|
|
|
shouldpoll = false;
|
|
|
|
}
|
|
|
|
|
2016-08-16 21:11:42 +00:00
|
|
|
var readTimer = null;
|
2016-08-18 20:35:23 +00:00
|
|
|
var shouldread = false;
|
2016-08-16 10:51:06 +00:00
|
|
|
$(document).ready(function () {
|
2016-08-18 20:35:23 +00:00
|
|
|
$('#msginput').on("focus", function () {
|
|
|
|
readTimer == null ? readTimer = setTimeout(function () {
|
|
|
|
resetUnread();
|
|
|
|
}, 3000) : readTimer;
|
|
|
|
shouldread = true;
|
|
|
|
});
|
2016-08-16 10:51:06 +00:00
|
|
|
$('#msginput').on("keydown", resetUnread);
|
2016-08-18 20:35:23 +00:00
|
|
|
$('#msginput').on("blur", function () {
|
|
|
|
readTimer != null ? clearTimeout(readTimer) : readTimer;
|
|
|
|
shouldread = false;
|
|
|
|
});
|
2016-08-16 10:51:06 +00:00
|
|
|
});
|