|
|
@@ -361,6 +361,38 @@ function onRoomUpdated() {
|
|
|
else
|
|
|
document.getElementById(R.id.mainSection).classList.remove(R.klass.starred);
|
|
|
var currentScrollTopTs = getCurrentScrollTopTs(chatFrag);
|
|
|
+
|
|
|
+ var addMessageInGroup = function(msg) {
|
|
|
+ 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) {
|
|
|
+ MSG_GROUPS[currentGroup].content.appendChild(dom);
|
|
|
+ MSG_GROUPS[currentGroup].messages.push(msg.id);
|
|
|
+ } else {
|
|
|
+ MSG_GROUPS[currentGroup].content.insertBefore(dom, MSG_GROUPS[currentGroup].content.children[currentMsgInGroup]);
|
|
|
+ MSG_GROUPS[currentGroup].messages.splice(currentMsgInGroup, 0, msg.id);
|
|
|
+ }
|
|
|
+ MSG_GROUPS[currentGroup].ts = MSG_GROUPS[currentGroup].ts ? msg.ts : Math.min(MSG_GROUPS[currentGroup].ts, msg.ts);
|
|
|
+ ++currentMsgInGroup;
|
|
|
+ prevMsg = msg;
|
|
|
+ },
|
|
|
+ endGroupAndMoveToNext = function() {
|
|
|
+ while (MSG_GROUPS[currentGroup].content.children[currentMsgInGroup])
|
|
|
+ MSG_GROUPS[currentGroup].content.children[currentMsgInGroup].remove();
|
|
|
+ MSG_GROUPS[currentGroup].messages.splice(currentMsgInGroup);
|
|
|
+ if (!MSG_GROUPS[currentGroup].messages.length) {
|
|
|
+ MSG_GROUPS[currentGroup].remove();
|
|
|
+ MSG_GROUPS[currentGroup] = null;
|
|
|
+ } else {
|
|
|
+ currentGroup++;
|
|
|
+ }
|
|
|
+ };
|
|
|
DATA.history[SELECTED_ROOM.id] && DATA.history[SELECTED_ROOM.id].messages.forEach(function(msg) {
|
|
|
var groupDomCreated = false;
|
|
|
if (MSG_GROUPS[currentGroup]) {
|
|
|
@@ -368,18 +400,7 @@ function onRoomUpdated() {
|
|
|
if (!msg.removed) {
|
|
|
if (isSameGroup(MSG_GROUPS[currentGroup], msg)) {
|
|
|
// New message belonging in this group
|
|
|
- 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);
|
|
|
- MSG_GROUPS[currentGroup].content.appendChild(dom);
|
|
|
- MSG_GROUPS[currentGroup].messages.push(msg.id);
|
|
|
- ++currentMsgInGroup;
|
|
|
- prevMsg = msg;
|
|
|
+ addMessageInGroup(msg);
|
|
|
return;
|
|
|
}
|
|
|
if (msg.ts < MSG_GROUPS[currentGroup].ts) {
|
|
|
@@ -393,15 +414,7 @@ function onRoomUpdated() {
|
|
|
}
|
|
|
} else {
|
|
|
// New message in next group
|
|
|
- while (MSG_GROUPS[currentGroup].content.children[currentMsgInGroup])
|
|
|
- MSG_GROUPS[currentGroup].content.children[currentMsgInGroup].remove();
|
|
|
- MSG_GROUPS[currentGroup].messages.splice(currentMsgInGroup);
|
|
|
- if (!MSG_GROUPS[currentGroup].messages.length) {
|
|
|
- MSG_GROUPS[currentGroup].remove();
|
|
|
- MSG_GROUPS[currentGroup] = null;
|
|
|
- } else {
|
|
|
- currentGroup++;
|
|
|
- }
|
|
|
+ endGroupAndMoveToNext();
|
|
|
}
|
|
|
currentMsgInGroup = 0;
|
|
|
firstTsCombo = msg.ts;
|
|
|
@@ -430,16 +443,7 @@ function onRoomUpdated() {
|
|
|
prevMsg = msg;
|
|
|
} else if (!msg.removed) {
|
|
|
// Another message, insert it before
|
|
|
- var dom = msg.getDom();
|
|
|
- MSG_GROUPS[currentGroup].content.insertBefore(dom, MSG_GROUPS[currentGroup].content.children[currentMsgInGroup]);
|
|
|
- MSG_GROUPS[currentGroup].messages.splice(currentMsgInGroup, 0, msg.id);
|
|
|
- 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);
|
|
|
- // Check invalidated
|
|
|
- currentMsgInGroup++;
|
|
|
- prevMsg = msg;
|
|
|
+ addMessageInGroup(msg);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
@@ -447,32 +451,18 @@ function onRoomUpdated() {
|
|
|
if (msg.removed)
|
|
|
return;
|
|
|
if (MSG_GROUPS[currentGroup] && !isSameGroup(MSG_GROUPS[currentGroup], msg)) { // Next group is not the good one, insert a new group
|
|
|
- MSG_GROUPS.splice(currentGroup, 0, null);
|
|
|
+ // New message in next group
|
|
|
+ endGroupAndMoveToNext();
|
|
|
}
|
|
|
if (!MSG_GROUPS[currentGroup]) {
|
|
|
MSG_GROUPS[currentGroup] = createMessageGroupDom(DATA.context.getUser(msg.userId), msg.username, msg instanceof MeMessage, msg.ts);
|
|
|
- groupDomCreated = true;
|
|
|
- }
|
|
|
- /** @type {Element} */
|
|
|
- var dom = msg.getDom();
|
|
|
- firstTsCombo = msg.ts;
|
|
|
- if ((!prevMsg || prevMsg.ts <= SELECTED_ROOM.lastRead) && msg.ts > SELECTED_ROOM.lastRead) {
|
|
|
- dom.classList.add(R.klass.msg.firstUnread);
|
|
|
- dom.insertBefore(UNREAD_INDICATOR_DOM, dom.firstChild);
|
|
|
- } else {
|
|
|
- dom.classList.remove(R.klass.msg.firstUnread);
|
|
|
- }
|
|
|
- MSG_GROUPS[currentGroup].content.appendChild(dom);
|
|
|
- MSG_GROUPS[currentGroup].messages.push(msg.id);
|
|
|
- prevMsg = msg;
|
|
|
-
|
|
|
- if (groupDomCreated) {
|
|
|
if (MSG_GROUPS[currentGroup +1])
|
|
|
chatFrag.insertBefore(MSG_GROUPS[currentGroup], MSG_GROUPS[currentGroup +1]);
|
|
|
else
|
|
|
chatFrag.appendChild(MSG_GROUPS[currentGroup]);
|
|
|
- currentMsgInGroup = 1;
|
|
|
+ currentMsgInGroup = 0;
|
|
|
}
|
|
|
+ addMessageInGroup(msg);
|
|
|
return;
|
|
|
});
|
|
|
if (MSG_GROUPS[currentGroup]) {
|