This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backbone.InfiniteCarousel.js
85 lines (62 loc) · 3.02 KB
/
Backbone.InfiniteCarousel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(function($, _, Backbone) {
Backbone.InfiniteCarousel = Backbone.View.extend({
imageRenderer: Backbone.View.extend({
tagName: 'figure',
render: function() {
this.el.style.display = 'inline-block';
this.el.style.width = (100 / this.collection.pageSize).toFixed(2) + '%';
$('<img />').attr('src', this.model.get('image')).appendTo(this.el);
}
}),
animation: {},
events: {
'click button[rel = "prev"]': '_onPrevClick',
'click button[rel = "next"]': '_onNextClick'
},
initialize: function(options) {
_.extend(this, _.pick(options, 'imageView', 'animation'));
this._prevButton = $('<button rel="prev" disabled="true">«</button>');
this._nextButton = $('<button rel="next" disabled="true">»</button>');
this._track = $('<div style="width: 400%;"></div>');
$('<div style="width: 25%;"></div>').append(this.collection.getPage().map(this._renderImage, this)).appendTo(this._track);
var controller = this;
$('<div style="width: 25%;"></div>').append(this.collection.behind().map(this._renderImage, this)).prependTo(this._track).imagesLoaded(function() {
controller._prevButton.removeAttr('disabled');
controller._track.css('margin-left', '-100%');
});
$('<div style="width: 25%;"></div>').append(this.collection.ahead().map(this._renderImage, this)).appendTo(this._track).imagesLoaded(function() {
controller._nextButton.removeAttr('disabled');
});
this.render();
},
_renderImage: function(i) {
var v = new this.imageRenderer({ model: i, collection: this.collection });
v.render();
return v.el;
},
_onAnimationDone: function(collectionMethod, loadHandler) {
$('<div style="width: 25%;"></div>').append(this.collection[collectionMethod]().map(this._renderImage, this)).imagesLoaded($.proxy(loadHandler, this));
},
_onPrevLoaded: function(instance) {
$(instance.elements[0]).prependTo(this._track).parent().css('margin-left', '-100%').children().last().remove();
this._prevButton.removeAttr('disabled').parent().removeClass('loading');
},
_onPrevClick: function() {
this.collection.retreat();
this._prevButton.attr('disabled', true).parent().addClass('loading');
this._track.animate({ marginLeft: '+=100%' }, _.extend({}, this.animation, { complete: $.proxy(this._onAnimationDone, this, 'behind', this._onPrevLoaded) }));
},
_onNextLoaded: function(instance) {
$(instance.elements[0]).appendTo(this._track).parent().css('margin-left', '-100%').children().first().remove();
this._nextButton.removeAttr('disabled').parent().removeClass('loading');
},
_onNextClick: function() {
this.collection.advance();
this._nextButton.attr('disabled', true).parent().addClass('loading');
this._track.animate({ marginLeft: '-=100%' }, _.extend({}, this.animation, { complete: $.proxy(this._onAnimationDone, this, 'ahead', this._onNextLoaded) }));
},
render: function() {
this._track.appendTo(this.el).before(this._prevButton).after(this._nextButton);
}
});
})(jQuery, _, Backbone);