@@ -410,10 +410,16 @@ class Threads {
410
410
}
411
411
412
412
// Remove the last message date time elements.
413
- let lastMessageDateTimeEle = document . getElementById ( "ch_thread_msg_datetime_" +
414
- this . threadsMessages [ this . threadsMessages . length - 1 ] [ 'id' ] ) ;
415
- if ( lastMessageDateTimeEle ) {
416
- lastMessageDateTimeEle . remove ( ) ;
413
+ if ( loadMoreMessages && messages . length ) {
414
+ if ( moment ( new Date ( messages [ 0 ] . createdAt ) ) . format ( 'DD/MM/YYYY' ) ==
415
+ moment ( new Date ( this . threadsMessages [ this . threadsMessages . length - 1 ] . createdAt ) ) . format ( 'DD/MM/YYYY' )
416
+ ) {
417
+ let lastMessageDateTimeEle = document . getElementById ( "ch_thread_msg_datetime_" +
418
+ this . threadsMessages [ this . threadsMessages . length - 1 ] [ 'id' ] ) ;
419
+ if ( lastMessageDateTimeEle ) {
420
+ lastMessageDateTimeEle . remove ( ) ;
421
+ }
422
+ }
417
423
}
418
424
419
425
this . threadsMessages = this . threadsMessages . concat ( messages ) ;
@@ -550,23 +556,21 @@ class Threads {
550
556
}
551
557
552
558
_createParentMessageBubble ( message , messagesBox ) {
553
-
554
559
let threadStart = document . getElementById ( "ch_thread_start" ) ;
555
560
if ( ! threadStart ) {
556
561
// Create message frame
557
- this . _createMessageBubble ( message , messagesBox , false ) ;
562
+ this . _createMessageBubble ( message , messagesBox , false , false , false ) ;
558
563
564
+ let parentMessage = document . getElementById ( "thread_" + message . id ) ;
559
565
// Create a line breaker b/w parent and chid message.
560
566
let threadStartAttributes = [ { "id" :"ch_thread_start" } , { "class" :"ch-thread-start ch-msg-bubble" } ] ;
561
- let threadStart = this . utility . createElement ( "div" , threadStartAttributes , null , messagesBox ) ;
567
+ let threadStart = this . utility . createElement ( "div" , threadStartAttributes , null , parentMessage ) ;
562
568
563
569
let threadLineSpanAttributes = [ ] ;
564
570
let threadLineSpan = this . utility . createElement ( "span" , threadLineSpanAttributes , LANGUAGE_PHRASES . START_OF_A_NEW_THREAD , threadStart ) ;
565
571
566
- // Set parent message and line breaker position
567
- let parentMessage = document . getElementById ( "thread_" + message . id ) ;
572
+ // Set parent message position
568
573
messagesBox . insertBefore ( parentMessage , messagesBox . childNodes [ 0 ] ) ;
569
- messagesBox . insertBefore ( threadStart , messagesBox . childNodes [ 1 ] ) ;
570
574
571
575
// Add date time on the top of the parent message.
572
576
let msgDateTimeAttributes = [ { "id" :"ch_thread_msg_datetime_" + message . id } , { "class" :"ch-msg-datetime" } ] ;
@@ -1073,7 +1077,7 @@ class Threads {
1073
1077
let messagesBox = document . getElementById ( "ch_thread_messages_box" ) ;
1074
1078
1075
1079
// Create message frame
1076
- this . _createMessageBubble ( msgData , messagesBox , false , true ) ;
1080
+ this . _createMessageBubble ( msgData , messagesBox , false , true , true ) ;
1077
1081
}
1078
1082
1079
1083
addNewMessage ( message , newConversation ) {
@@ -1115,7 +1119,7 @@ class Threads {
1115
1119
message . createdAt = new Date ( ) ;
1116
1120
1117
1121
// Create message frame
1118
- this . _createMessageBubble ( message , messagesBox , false ) ;
1122
+ this . _createMessageBubble ( message , messagesBox , true , true , false ) ;
1119
1123
}
1120
1124
1121
1125
_getMessages ( conversation , limit , skip , cb ) {
@@ -1126,31 +1130,52 @@ class Threads {
1126
1130
} ) ;
1127
1131
}
1128
1132
1129
- _isDateDiffShown ( messageId ) {
1130
- const messageIndex = this . threadsMessages . findIndex ( message => message . id == messageId ) ;
1131
- if ( messageIndex == - 1 ) {
1132
- return false ;
1133
- }
1133
+ _isDateDiffShown ( currentMsgId , lastMsgId , isNewMessage ) {
1134
+ const messageIndex = this . threadsMessages . findIndex ( message => message . id == currentMsgId ) ;
1135
+ if ( messageIndex == - 1 ) {
1136
+ return false ;
1137
+ }
1138
+ const currentMsgDate = moment ( new Date ( this . threadsMessages [ messageIndex ] . createdAt ) ) . format ( 'DD/MM/YYYY' ) ;
1139
+
1140
+ // Check if this new message
1141
+ // 1. Yes: this is new entered message.
1142
+ // 2. No: Means this is came from API.
1143
+ if ( isNewMessage ) {
1144
+ // This in only one message.
1145
+ if ( ! lastMsgId ) {
1146
+ return true ;
1147
+ }
1148
+ const lastMsgIndex = this . threadsMessages . findIndex ( message => message . id == lastMsgId ) ;
1149
+ if ( lastMsgIndex == - 1 ) {
1150
+ return true ;
1151
+ }
1152
+ const lastMsgDate = moment ( new Date ( this . threadsMessages [ lastMsgIndex ] . createdAt ) ) . format ( 'DD/MM/YYYY' ) ;
1153
+ if ( currentMsgDate != lastMsgDate ) {
1154
+ return true ;
1155
+ }
1156
+ return false ;
1157
+ }
1158
+
1159
+ if ( ! this . threadsMessages [ messageIndex + 1 ] ) {
1160
+ return true ;
1161
+ }
1134
1162
1135
- if ( ! this . threadsMessages [ messageIndex + 1 ] ) {
1136
- return true ;
1137
- }
1138
-
1139
- const currentMsgDate = moment ( new Date ( this . threadsMessages [ messageIndex ] . createdAt ) ) . format ( 'DD/MM/YYYY' ) ;
1140
- const nextMsgDate = moment ( new Date ( this . threadsMessages [ messageIndex + 1 ] . createdAt ) ) . format ( 'DD/MM/YYYY' ) ;
1141
-
1142
- if ( currentMsgDate != nextMsgDate ) {
1143
- return true ;
1144
- }
1145
- return false
1163
+ const nextMsgDate = moment ( new Date ( this . threadsMessages [ messageIndex + 1 ] . createdAt ) ) . format ( 'DD/MM/YYYY' ) ;
1164
+
1165
+ if ( currentMsgDate != nextMsgDate ) {
1166
+ return true ;
1167
+ }
1168
+ return false ;
1146
1169
}
1147
1170
1148
- _createMessageBubble ( message , messagesBox , showDateDiff = true , isPendingMessage = false ) {
1171
+ _createMessageBubble ( message , messagesBox , showDateDiff = true , isNewMessage = false , isPendingMessage = false ) {
1149
1172
// Create message list
1173
+ const lastMsgId = messagesBox . lastChild && messagesBox . lastChild . id . replace ( "thread_" , "" ) ;
1174
+
1150
1175
let msgBubbleEleAttributes = [ { "id" :"thread_" + message . id } , { "class" :"ch-msg-bubble" } ] ;
1151
1176
let msgBubbleEle = this . utility . createElement ( "div" , msgBubbleEleAttributes , null , messagesBox ) ;
1152
1177
1153
- if ( showDateDiff && this . _isDateDiffShown ( message . id ) ) {
1178
+ if ( showDateDiff && this . _isDateDiffShown ( message . id , lastMsgId , isNewMessage ) ) {
1154
1179
let msgDateTimeAttributes = [ { "id" :"ch_thread_msg_datetime_" + message . id } , { "class" :"ch-msg-datetime" } ] ;
1155
1180
this . utility . createElement ( "div" , msgDateTimeAttributes , this . utility . formatDate ( message . createdAt ) , msgBubbleEle ) ;
1156
1181
}
0 commit comments