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)
|
|
|
|
msgs[i].style.backgroundColor = "darkgray";
|
|
|
|
else
|
|
|
|
msgs[i].style = "";
|
2016-08-16 21:11:42 +00:00
|
|
|
updatemsgtime(msgs[i]);
|
2016-08-16 10:51:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var addUnread = function addUnread() {
|
|
|
|
unreadCount++;
|
|
|
|
updateUnreadCount();
|
|
|
|
};
|
|
|
|
|
|
|
|
var resetUnread = function resetUnread() {
|
|
|
|
unreadCount = 0;
|
|
|
|
updateUnreadCount();
|
|
|
|
};
|
|
|
|
|
2016-08-16 21:11:42 +00:00
|
|
|
var readTimer = null;
|
2016-08-16 10:51:06 +00:00
|
|
|
$(document).ready(function () {
|
2016-08-16 21:11:42 +00:00
|
|
|
$('#msginput').on("focus", function () { readTimer == null ? readTimer = setTimeout(function () { resetUnread(); }, 3000) : readTimer; });
|
2016-08-16 10:51:06 +00:00
|
|
|
$('#msginput').on("keydown", resetUnread);
|
2016-08-16 21:11:42 +00:00
|
|
|
$('#msginput').on("blur", function () { readTimer != null ? clearTimeout(readTimer) : readTimer; });
|
|
|
|
|
|
|
|
if (isLoggedIn())
|
|
|
|
(function poll() {
|
|
|
|
setTimeout(function () {
|
|
|
|
$.ajax({
|
|
|
|
url: "/receivemessage", success: function (data) {
|
|
|
|
var msgs = document.getElementById("channelmessages");
|
|
|
|
msgs.innerHTML += data;
|
|
|
|
var msgelement = msgs.children[msgs.children.length - 1];
|
|
|
|
handlereceivedmessage(msgelement);
|
|
|
|
if (justsentmsgread)
|
|
|
|
justsentmsgread = false;
|
|
|
|
else
|
|
|
|
addUnread();
|
|
|
|
}, error: function (data) {
|
|
|
|
showError(data.responseText);
|
|
|
|
}, dataType: "text", complete: poll
|
|
|
|
});
|
|
|
|
}, 100);
|
|
|
|
})();
|
2016-08-16 10:51:06 +00:00
|
|
|
});
|