2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-01-19 01:16:24 +01:00
|
|
|
*/
|
2014-07-22 20:55:26 +02:00
|
|
|
(function () {
|
2015-01-30 07:59:08 +01:00
|
|
|
'use strict';
|
2015-03-06 00:44:53 +01:00
|
|
|
window.Whisper = window.Whisper || {};
|
2014-07-22 20:55:26 +02:00
|
|
|
|
2015-01-30 07:59:08 +01:00
|
|
|
/*
|
|
|
|
* Generic list view that watches a given collection, wraps its members in
|
|
|
|
* a given child view and adds the child view elements to its own element.
|
|
|
|
*/
|
|
|
|
Whisper.ListView = Backbone.View.extend({
|
|
|
|
tagName: 'ul',
|
|
|
|
itemView: Backbone.View,
|
2015-05-28 20:50:31 +02:00
|
|
|
initialize: function(options) {
|
2015-01-30 07:59:08 +01:00
|
|
|
this.listenTo(this.collection, 'add', this.addOne);
|
|
|
|
this.listenTo(this.collection, 'reset', this.addAll);
|
2015-05-28 20:50:31 +02:00
|
|
|
|
|
|
|
if (options.window) {
|
|
|
|
var $window = this.$(options.window);
|
|
|
|
$window.scroll(this.onScroll.bind(this));
|
|
|
|
$window.resize(this.onResize.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onResize: function(e) {
|
|
|
|
this.resizing = true;
|
|
|
|
clearTimeout(this.resizeTimer);
|
|
|
|
resizeTimer = setTimeout(function() {
|
|
|
|
resizing = false;
|
|
|
|
}, 500);
|
|
|
|
this.$el.scrollTop(this.scrollPercent * this.$el.height());
|
|
|
|
},
|
|
|
|
|
|
|
|
onScroll: function(e) {
|
|
|
|
if (!this.resizing) {
|
|
|
|
this.scrollTop = this.$el.scrollTop();
|
|
|
|
this.scrollPercent = this.scrollTop / this.$el.height();
|
|
|
|
}
|
2015-01-30 07:59:08 +01:00
|
|
|
},
|
2014-07-22 20:55:26 +02:00
|
|
|
|
2015-01-30 07:59:08 +01:00
|
|
|
addOne: function(model) {
|
2015-02-18 08:58:00 +01:00
|
|
|
if (this.itemView) {
|
2015-01-30 07:59:08 +01:00
|
|
|
var view = new this.itemView({model: model});
|
|
|
|
this.$el.append(view.render().el);
|
|
|
|
this.$el.trigger('add');
|
|
|
|
}
|
|
|
|
},
|
2014-07-22 20:55:26 +02:00
|
|
|
|
2015-01-30 07:59:08 +01:00
|
|
|
addAll: function() {
|
|
|
|
this.$el.html('');
|
|
|
|
this.collection.each(this.addOne, this);
|
2015-02-11 00:27:26 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
this.addAll();
|
|
|
|
return this;
|
2015-01-30 07:59:08 +01:00
|
|
|
}
|
|
|
|
});
|
2014-07-22 20:55:26 +02:00
|
|
|
})();
|