Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 60e87d3

Browse files
committed
Make Chats with Unread Messages Float to Top
solves #1401
1 parent 4f795d0 commit 60e87d3

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

js/collections/chatConversationsCl.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,34 @@ module.exports = Backbone.Collection.extend({
99

1010
model: ChatConversationMd,
1111

12-
comparator: function(convo) {
13-
return -convo.get('timestamp');
12+
initialize: function() {
13+
this.on('change:unread', function(model){
14+
// Only re-sort collection if unread count was incremented
15+
if (model.hasChanged('unread') && model.get('unread') > 0) {
16+
this.sort();
17+
}
18+
});
19+
},
20+
21+
comparator: function(a, b) {
22+
// Sort by unread messages first
23+
if (a.get('unread') > 0 && b.get('unread') == 0) {
24+
return -1;
25+
}
26+
27+
if (a.get('unread') == 0 && b.get('unread') > 0) {
28+
return 1;
29+
}
30+
31+
// then sort by timestamp
32+
if (a.get('timestamp') > b.get('timestamp')) {
33+
return -1;
34+
}
35+
36+
if (a.get('timestamp') < b.get('timestamp')) {
37+
return 1;
38+
}
39+
40+
return 0;
1441
}
1542
});

js/views/chatVw.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ module.exports = baseVw.extend({
6666

6767
this.setAggregateUnreadCount();
6868
});
69+
70+
this.listenTo(this.chatConversationsCl, 'sort', (cl) => {
71+
if (this.chatHeadsVw) {
72+
this.chatHeadsVw.setCollection(
73+
this.chatConversationsCl
74+
);
75+
76+
this.chatHeadsVw.render();
77+
78+
this.$chatHeadsContainer.animate({
79+
scrollTop: 0
80+
});
81+
}
82+
});
6983

7084
this.listenTo(this.chatConversationsCl, 'add remove', (md, cl, opts) => {
7185
if (opts.add) {
@@ -114,8 +128,8 @@ module.exports = baseVw.extend({
114128
filteredMd && filteredMd.set(md.attributes);
115129
}
116130

117-
md.hasChanged('unread') && this.setAggregateUnreadCount();
118-
});
131+
md.hasChanged('unread') && this.setAggregateUnreadCount();
132+
});
119133
},
120134

121135
setAggregateUnreadCount: function() {

0 commit comments

Comments
 (0)