-
Notifications
You must be signed in to change notification settings - Fork 5
/
jqm.infinitescroll.js
100 lines (82 loc) · 3.73 KB
/
jqm.infinitescroll.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
------------------------------------
jQuery Mobile Infinite Scroll Plugin
------------------------------------
Copyright 2013 - Kevin Pheasey
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function($) {
var defaults = {
'windowLocationTrigger': 0.75, //percentage of how far downt he page the user needs to be before the next list is loaded
'pathToNextPage': undefined,
'showLoadingUi': false
};
var methods = {
setPathToNextPage: function(navElement, instance) {
var pathToNextPage = undefined;
if ($(navElement, instance).length > 0) { // nav element may not exist
pathToNextPage = $(navElement, instance).attr('href');
} else {
pathToNextPage = undefined;
}
return pathToNextPage;
},
isScrolledToBottom: function(limit) {
if ($(window).scrollTop() == 0 || ($(window).scrollTop() > 1 && (($(window).scrollTop() + $(window).height()) / $(document).height()) > limit)) {
return true;
} else {
return false;
}
}
};
$.fn.infinitescroll = function(options, callback) {
var opts = $.extend(defaults, options);
opts['instance'] = $(this);
opts['newItems'] = undefined;
$(opts.navElement, $.mobile.activePage).hide(); // hide the navigation element
$(window).on('scrollstop.infinitescroll', function() {
if (methods.isScrolledToBottom(opts.windowLocationTrigger)) {
$(window).off('scrollstop.infinitescroll');// stop checking the scrolling
opts.pathToNextPage = methods.setPathToNextPage(opts.navElement, $.mobile.activePage);
if (opts.pathToNextPage == undefined) {
return false;
}
if (opts.showLoadingUi){
$.mobile.loading('show');
}
$.ajax(opts.pathToNextPage).done(function(data) {
$.mobile.loading('hide');
opts.newItems = $(data).find(opts.itemsToLoad);
opts.newItems.each(function() {
opts.instance.append(this);
});
if (typeof callback === 'function') { // make sure the callback is a function
callback.call(this, opts.newItems); // brings the scope to the callback
}
var newNavElement = $(data).find(opts.navElement);
if (newNavElement.length > 0) {
$(opts.navElement).attr('href', newNavElement.attr('href'));
opts.instance.infinitescroll(opts, callback);
} else {
$(opts.navElement).removeAttr('id');
}
});
}
});
if ($(window).height() > $('[data-role="content"]', $.mobile.activePage).height()) {
$(window).trigger('scrollstop.infinitescroll');
}
};
})(jQuery);
$(document).on('pagebeforehide', function() {
$(window).off('scrollstop.infinitescroll resize.infinitescroll');
});