-
Notifications
You must be signed in to change notification settings - Fork 13
/
infinite_scroll.js
80 lines (69 loc) · 2.33 KB
/
infinite_scroll.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
(function(window, Ember, $){
var InfiniteScroll = {
PAGE: 1, // default start page
PER_PAGE: 25 // default per page
};
InfiniteScroll.ControllerMixin = Ember.Mixin.create({
loadingMore: false,
page: InfiniteScroll.PAGE,
perPage: InfiniteScroll.PER_PAGE,
actions: {
getMore: function(){
if (this.get('loadingMore')) return;
this.set('loadingMore', true);
this.get('target').send('getMore');
},
gotMore: function(items, nextPage){
this.set('loadingMore', false);
this.pushObjects(items);
this.set('page', nextPage);
}
}
});
InfiniteScroll.RouteMixin = Ember.Mixin.create({
actions: {
getMore: function() {
throw new Error("Must override Route action `getMore`.");
},
fetchPage: function() {
throw new Error("Must override Route action `getMore`.");
}
}
});
InfiniteScroll.ViewMixin = Ember.Mixin.create({
setupInfiniteScrollListener: function(){
$('.inf-scroll-outer-container').on('scroll.infinite', Ember.run.bind(this, this.didScroll));
},
teardownInfiniteScrollListener: function(){
$('.inf-scroll-outer-container').off('scroll.infinite');
},
didScroll: function(){
if (this.isScrolledToRight() || this.isScrolledToBottom()) {
this.get('controller').send('getMore');
}
},
isScrolledToRight: function(){
var distanceToViewportLeft = (
$('.inf-scroll-inner-container').width() - $('.inf-scroll-outer-container').width());
var viewPortLeft = $('.inf-scroll-outer-container').scrollLeft();
if (viewPortLeft === 0) {
// if we are at the left of the page, don't do
// the infinite scroll thing
return false;
}
return (viewPortLeft - distanceToViewportLeft);
},
isScrolledToBottom: function(){
var distanceToViewportTop = (
$('.inf-scroll-inner-container').height() - $('.inf-scroll-outer-container').height());
var viewPortTop = $('.inf-scroll-outer-container').scrollTop();
if (viewPortTop === 0) {
// if we are at the top of the page, don't do
// the infinite scroll thing
return false;
}
return (viewPortTop >= distanceToViewportTop);
}
});
window.InfiniteScroll = InfiniteScroll;
})(this, Ember, jQuery);