|
|
@@ -353,7 +353,6 @@ function scrollToTs(container, ts) {
|
|
|
function onRoomUpdated() {
|
|
|
var chatFrag = document.getElementById(R.id.currentRoom.content),
|
|
|
prevMsg = null,
|
|
|
- firstTsCombo = 0,
|
|
|
currentGroup = 0,
|
|
|
currentMsgInGroup = 0;
|
|
|
|
|
|
@@ -363,16 +362,14 @@ function onRoomUpdated() {
|
|
|
document.getElementById(R.id.mainSection).classList.remove(R.klass.starred);
|
|
|
var currentScrollTopTs = getCurrentScrollTopTs(chatFrag);
|
|
|
|
|
|
- var addMessageInGroup = function(msg) {
|
|
|
+ /** @type {function(Message, Element=)} */
|
|
|
+ var addMessageInGroup = function(msg, domToReplace) {
|
|
|
var dom = msg.getDom();
|
|
|
- if (Math.abs(firstTsCombo -msg.ts) < 30 && prevMsg)
|
|
|
- prevMsg.getDom().classList.add(R.klass.msg.sameTs);
|
|
|
- firstTsCombo = msg.ts;
|
|
|
- if ((!prevMsg || prevMsg.ts <= SELECTED_ROOM.lastRead) && msg.ts > SELECTED_ROOM.lastRead)
|
|
|
- dom.classList.add(R.klass.msg.firstUnread);
|
|
|
- else
|
|
|
- dom.classList.remove(R.klass.msg.firstUnread);
|
|
|
- if (currentMsgInGroup >= MSG_GROUPS[currentGroup].messages.length) {
|
|
|
+ prevMsg && prevMsg.getDom().setClass(Math.abs(prevMsg.ts -msg.ts) < 60000, R.klass.msg.sameTs);
|
|
|
+ dom.setClass((!prevMsg || prevMsg.ts <= SELECTED_ROOM.lastRead) && msg.ts > SELECTED_ROOM.lastRead, R.klass.msg.firstUnread);
|
|
|
+ if (domToReplace) {
|
|
|
+ MSG_GROUPS[currentGroup].content.replaceChild(domToReplace, dom);
|
|
|
+ } else if (currentMsgInGroup >= MSG_GROUPS[currentGroup].messages.length) {
|
|
|
MSG_GROUPS[currentGroup].content.appendChild(dom);
|
|
|
MSG_GROUPS[currentGroup].messages.push(msg.id);
|
|
|
} else {
|
|
|
@@ -382,8 +379,8 @@ function onRoomUpdated() {
|
|
|
MSG_GROUPS[currentGroup].ts = MSG_GROUPS[currentGroup].ts ? msg.ts : Math.min(MSG_GROUPS[currentGroup].ts, msg.ts);
|
|
|
++currentMsgInGroup;
|
|
|
prevMsg = msg;
|
|
|
- },
|
|
|
- endGroupAndMoveToNext = function() {
|
|
|
+ };
|
|
|
+ var endGroupAndMoveToNext = function() {
|
|
|
while (MSG_GROUPS[currentGroup].content.children[currentMsgInGroup])
|
|
|
MSG_GROUPS[currentGroup].content.children[currentMsgInGroup].remove();
|
|
|
MSG_GROUPS[currentGroup].messages.splice(currentMsgInGroup);
|
|
|
@@ -393,35 +390,11 @@ function onRoomUpdated() {
|
|
|
} else {
|
|
|
currentGroup++;
|
|
|
}
|
|
|
+ prevMsg = null;
|
|
|
};
|
|
|
DATA.history[SELECTED_ROOM.id] && DATA.history[SELECTED_ROOM.id].messages.forEach(function(msg) {
|
|
|
var groupDomCreated = false;
|
|
|
if (MSG_GROUPS[currentGroup]) {
|
|
|
- if (currentMsgInGroup >= MSG_GROUPS[currentGroup].messages.length) {
|
|
|
- if (!msg.removed) {
|
|
|
- if (isSameGroup(MSG_GROUPS[currentGroup], msg)) {
|
|
|
- // New message belonging in this group
|
|
|
- addMessageInGroup(msg);
|
|
|
- return;
|
|
|
- }
|
|
|
- if (msg.ts < MSG_GROUPS[currentGroup].ts) {
|
|
|
- while (currentGroup > 0 && msg.ts < MSG_GROUPS[currentGroup].ts) {
|
|
|
- // New message in previous group, move group cursor before the group to be inserted
|
|
|
- --currentGroup;
|
|
|
- }
|
|
|
- while (MSG_GROUPS[currentGroup]) {
|
|
|
- MSG_GROUPS[currentGroup].remove();
|
|
|
- MSG_GROUPS.splice(currentGroup, 1);
|
|
|
- }
|
|
|
- } else {
|
|
|
- // New message in next group
|
|
|
- endGroupAndMoveToNext();
|
|
|
- }
|
|
|
- currentMsgInGroup = 0;
|
|
|
- firstTsCombo = msg.ts;
|
|
|
- }
|
|
|
- // else message got removed
|
|
|
- }
|
|
|
if (isSameGroup(MSG_GROUPS[currentGroup], msg)) {
|
|
|
if (MSG_GROUPS[currentGroup].messages[currentMsgInGroup] === msg.id) {
|
|
|
// same message
|
|
|
@@ -432,22 +405,32 @@ function onRoomUpdated() {
|
|
|
MSG_GROUPS[currentGroup].messages.splice(currentMsgInGroup, 1);
|
|
|
} else if (msg.uiNeedRefresh) {
|
|
|
// Check invalidated
|
|
|
- var newDom = msg.getDom();
|
|
|
- MSG_GROUPS[currentGroup].content.replaceChild(dom, newDom);
|
|
|
- dom = newDom;
|
|
|
+ addMessageInGroup(msg, dom);
|
|
|
+ } else {
|
|
|
+ ++currentMsgInGroup;
|
|
|
}
|
|
|
- if ((!prevMsg || prevMsg.ts <= SELECTED_ROOM.lastRead) && msg.ts > SELECTED_ROOM.lastRead)
|
|
|
- dom.classList.add(R.klass.msg.firstUnread);
|
|
|
- else
|
|
|
- dom.classList.remove(R.klass.msg.firstUnread);
|
|
|
- currentMsgInGroup++;
|
|
|
- prevMsg = msg;
|
|
|
} else if (!msg.removed) {
|
|
|
// Another message, insert it before
|
|
|
addMessageInGroup(msg);
|
|
|
}
|
|
|
return;
|
|
|
+ } else if (msg.removed) {
|
|
|
+ return;
|
|
|
+ } else if (msg.ts < MSG_GROUPS[currentGroup].ts) {
|
|
|
+ while (currentGroup > 0 && msg.ts < MSG_GROUPS[currentGroup].ts) {
|
|
|
+ // New message in previous group, move group cursor before the group to be inserted
|
|
|
+ --currentGroup;
|
|
|
+ }
|
|
|
+ while (MSG_GROUPS[currentGroup]) {
|
|
|
+ MSG_GROUPS[currentGroup].remove();
|
|
|
+ MSG_GROUPS.splice(currentGroup, 1);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // New message in next group
|
|
|
+ endGroupAndMoveToNext();
|
|
|
}
|
|
|
+ prevMsg = null;
|
|
|
+ currentMsgInGroup = 0;
|
|
|
}
|
|
|
if (msg.removed)
|
|
|
return;
|
|
|
@@ -513,9 +496,15 @@ function onRoomUpdated() {
|
|
|
return result;
|
|
|
});
|
|
|
if (currentScrollTopTs)
|
|
|
+ {
|
|
|
+ console.log("scroll to", new Date(currentScrollTopTs));
|
|
|
scrollToTs(chatFrag, currentScrollTopTs);
|
|
|
+ }
|
|
|
else
|
|
|
+ {
|
|
|
+ console.log("scroll to bottom");
|
|
|
chatFrag.scrollTop = chatFrag.scrollHeight -chatFrag.clientHeight;
|
|
|
+ }
|
|
|
|
|
|
updateTitle();
|
|
|
if (window.hasFocus)
|