ChatServer/pages/js/receivemessage.js
NorbiPeti 23134e74f0 Many fixes and other things
- Fixed handling the case of no messages in conversation
- Changed sending new messages to HTML instead of JSON - So the message
format is at one place
- Fixed new messages showing the conversation ID to the user at the top
- Fixed unsent messages
- Fixed unread indicator on own message
- Fixed error hider timer
- Fixed message receiving when not logged in
2016-08-16 23:12:20 +02:00

68 lines
2.4 KiB
JavaScript

function updatemsgtime(msgnode) {
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)
ctime.innerText = moment($(ctime).data("val"), "YYYY-MM-DDTHH:mm:ssZ").fromNow();
}
function handlereceivedmessage(msgnode) {
updatemsgtime(msgnode);
msgnode.scrollIntoView(false);
}
var unreadCount = 0;
var updateUnreadCount = function () {
if (unreadCount > 0)
document.title = "(" + unreadCount + ") Chat";
else
document.title = "Chat";
var msgs = document.getElementById("channelmessages").getElementsByClassName("chmessage");
for (var i = msgs.length - 1; i >= 0; i--) {
if (i >= msgs.length - unreadCount)
msgs[i].style.backgroundColor = "darkgray";
else
msgs[i].style = "";
updatemsgtime(msgs[i]);
}
};
var addUnread = function addUnread() {
unreadCount++;
updateUnreadCount();
};
var resetUnread = function resetUnread() {
unreadCount = 0;
updateUnreadCount();
};
var readTimer = null;
$(document).ready(function () {
$('#msginput').on("focus", function () { readTimer == null ? readTimer = setTimeout(function () { resetUnread(); }, 3000) : readTimer; });
$('#msginput').on("keydown", resetUnread);
$('#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);
})();
});