Browse Source

Refs TG-8 refactor

isundil 6 years ago
parent
commit
fdd6af5efc
4 changed files with 79 additions and 194 deletions
  1. 1 1
      Makefile
  2. 35 46
      cli/ui.js
  3. 7 0
      cli/utils.js
  4. 36 147
      srv/public/mimouchat.min.js

+ 1 - 1
Makefile

@@ -12,6 +12,7 @@ SRC=		srv/src/context.js			\
 			\
 			cli/msgFormatter/msgFormatter.js	\
 			\
+			cli/utils.js				\
 			cli/httpRequest.js			\
 			cli/confirmDialog.js		\
 			cli/resources.js			\
@@ -27,7 +28,6 @@ SRC=		srv/src/context.js			\
 			cli/emojiEngine.js			\
 			cli/roomInfo.js				\
 			cli/uiMessage.js			\
-			cli/utils.js				\
 			cli/config.js				\
 			cli/rawTCPSocket.js			\
 			cli/nativeCallback.js		\

+ 35 - 46
cli/ui.js

@@ -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)

+ 7 - 0
cli/utils.js

@@ -32,3 +32,10 @@ function isSameDay(ts1, ts2) {
     return d1.getTime() === d2.getTime();
 }
 
+Element.prototype.setClass = function(condition, klass) {
+    if (condition)
+        this.classList.add(klass);
+    else
+        this.classList.remove(klass);
+}
+

File diff suppressed because it is too large
+ 36 - 147
srv/public/mimouchat.min.js


Some files were not shown because too many files changed in this diff