2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-01-19 00:16:24 +00:00
|
|
|
*/
|
2014-07-22 18:55:26 +00:00
|
|
|
(function () {
|
2015-01-30 06:59:08 +00:00
|
|
|
'use strict';
|
2015-03-05 23:44:53 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2014-07-22 18:55:26 +00:00
|
|
|
|
2015-01-30 06:59:08 +00:00
|
|
|
Whisper.MessageListView = Whisper.ListView.extend({
|
|
|
|
tagName: 'ul',
|
2015-08-27 00:48:02 +00:00
|
|
|
className: 'message-list',
|
2015-01-30 06:59:08 +00:00
|
|
|
itemView: Whisper.MessageView,
|
|
|
|
events: {
|
2015-11-11 00:03:19 +00:00
|
|
|
'scroll': 'onScroll',
|
2015-01-30 06:59:08 +00:00
|
|
|
},
|
2017-05-31 00:29:23 +00:00
|
|
|
initialize: function() {
|
|
|
|
Whisper.ListView.prototype.initialize.call(this);
|
|
|
|
|
|
|
|
this.triggerLazyScroll = _.debounce(function() {
|
|
|
|
this.$el.trigger('lazyScroll');
|
|
|
|
}.bind(this), 500);
|
|
|
|
},
|
2015-11-11 00:03:19 +00:00
|
|
|
onScroll: function() {
|
|
|
|
this.measureScrollPosition();
|
|
|
|
if (this.$el.scrollTop() === 0) {
|
|
|
|
this.$el.trigger('loadMore');
|
|
|
|
}
|
2017-05-27 00:24:51 +00:00
|
|
|
if (this.atBottom()) {
|
2017-05-19 01:33:35 +00:00
|
|
|
this.$el.trigger('atBottom');
|
|
|
|
} else if (this.bottomOffset > this.outerHeight) {
|
|
|
|
this.$el.trigger('farFromBottom');
|
|
|
|
}
|
2017-05-31 00:29:23 +00:00
|
|
|
|
|
|
|
this.triggerLazyScroll();
|
2015-08-27 00:30:20 +00:00
|
|
|
},
|
2017-05-27 00:24:51 +00:00
|
|
|
atBottom: function() {
|
2017-05-27 01:11:11 +00:00
|
|
|
return this.bottomOffset < 30;
|
2017-05-27 00:24:51 +00:00
|
|
|
},
|
2015-07-14 21:11:11 +00:00
|
|
|
measureScrollPosition: function() {
|
2015-08-27 23:25:33 +00:00
|
|
|
if (this.el.scrollHeight === 0) { // hidden
|
|
|
|
return;
|
|
|
|
}
|
2017-05-19 01:33:35 +00:00
|
|
|
this.outerHeight = this.$el.outerHeight();
|
|
|
|
this.scrollPosition = this.$el.scrollTop() + this.outerHeight;
|
2015-08-25 23:47:15 +00:00
|
|
|
this.scrollHeight = this.el.scrollHeight;
|
2017-05-30 17:39:42 +00:00
|
|
|
this.bottomOffset = this.scrollHeight - this.scrollPosition;
|
2015-08-25 23:47:15 +00:00
|
|
|
},
|
|
|
|
resetScrollPosition: function() {
|
2017-05-17 21:34:55 +00:00
|
|
|
this.$el.scrollTop(this.scrollPosition - this.$el.outerHeight());
|
2015-07-14 21:11:11 +00:00
|
|
|
},
|
|
|
|
scrollToBottomIfNeeded: function() {
|
2017-05-27 00:24:51 +00:00
|
|
|
if (this.atBottom()) {
|
2017-05-19 01:33:35 +00:00
|
|
|
this.scrollToBottom();
|
2017-05-17 21:34:02 +00:00
|
|
|
}
|
2015-11-11 00:03:19 +00:00
|
|
|
},
|
2017-05-19 01:33:35 +00:00
|
|
|
scrollToBottom: function() {
|
|
|
|
this.$el.scrollTop(this.el.scrollHeight);
|
|
|
|
this.measureScrollPosition();
|
|
|
|
},
|
2015-11-11 00:03:19 +00:00
|
|
|
addOne: function(model) {
|
2016-09-27 06:15:20 +00:00
|
|
|
var view;
|
2016-09-28 23:47:57 +00:00
|
|
|
if (model.isExpirationTimerUpdate()) {
|
2016-09-27 06:15:20 +00:00
|
|
|
view = new Whisper.ExpirationTimerUpdateView({model: model}).render();
|
2016-09-18 06:55:05 +00:00
|
|
|
} else if (model.get('type') === 'keychange') {
|
|
|
|
view = new Whisper.KeyChangeView({model: model}).render();
|
2016-09-27 06:15:20 +00:00
|
|
|
} else {
|
|
|
|
view = new this.itemView({model: model}).render();
|
2015-11-12 18:35:29 +00:00
|
|
|
this.listenTo(view, 'beforeChangeHeight', this.measureScrollPosition);
|
|
|
|
this.listenTo(view, 'afterChangeHeight', this.scrollToBottomIfNeeded);
|
2016-09-27 06:15:20 +00:00
|
|
|
}
|
2017-05-17 21:34:02 +00:00
|
|
|
|
2016-10-04 07:24:00 +00:00
|
|
|
var index = this.collection.indexOf(model);
|
2017-05-17 21:34:02 +00:00
|
|
|
this.measureScrollPosition();
|
2017-05-27 00:24:51 +00:00
|
|
|
|
|
|
|
if (model.get('unread') && !this.atBottom()) {
|
2017-05-19 01:33:35 +00:00
|
|
|
this.$el.trigger('newOffscreenMessage');
|
|
|
|
}
|
|
|
|
|
2016-10-04 07:24:00 +00:00
|
|
|
if (index === this.collection.length - 1) {
|
2016-09-27 06:15:20 +00:00
|
|
|
// add to the bottom.
|
|
|
|
this.$el.append(view.el);
|
2016-10-04 07:24:00 +00:00
|
|
|
} else if (index === 0) {
|
2016-10-05 13:43:31 +00:00
|
|
|
// add to top
|
2016-10-04 07:24:00 +00:00
|
|
|
this.$el.prepend(view.el);
|
2016-09-27 06:15:20 +00:00
|
|
|
} else {
|
2016-10-04 07:24:00 +00:00
|
|
|
// insert
|
|
|
|
var next = this.$('#' + this.collection.at(index + 1).id);
|
|
|
|
var prev = this.$('#' + this.collection.at(index - 1).id);
|
|
|
|
if (next.length > 0) {
|
|
|
|
view.$el.insertBefore(next);
|
|
|
|
} else if (prev.length > 0) {
|
|
|
|
view.$el.insertAfter(prev);
|
|
|
|
} else {
|
|
|
|
// scan for the right spot
|
|
|
|
var elements = this.$el.children();
|
2016-10-05 13:43:31 +00:00
|
|
|
if (elements.length > 0) {
|
|
|
|
for (var i = 0; i < elements.length; ++i) {
|
|
|
|
var m = this.collection.get(elements[i].id);
|
|
|
|
var m_index = this.collection.indexOf(m);
|
|
|
|
if (m_index > index) {
|
|
|
|
view.$el.insertBefore(elements[i]);
|
|
|
|
break;
|
|
|
|
}
|
2016-10-04 07:24:00 +00:00
|
|
|
}
|
2016-10-05 13:43:31 +00:00
|
|
|
} else {
|
|
|
|
this.$el.append(view.el);
|
2016-10-04 07:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-11 00:03:19 +00:00
|
|
|
}
|
2017-05-17 21:34:02 +00:00
|
|
|
this.scrollToBottomIfNeeded();
|
2015-11-11 00:03:19 +00:00
|
|
|
},
|
2015-01-30 06:59:08 +00:00
|
|
|
});
|
2014-07-22 18:55:26 +00:00
|
|
|
})();
|