forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.pageless.js
More file actions
219 lines (197 loc) · 6.86 KB
/
Copy pathjquery.pageless.js
File metadata and controls
219 lines (197 loc) · 6.86 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// =======================================================================
// PageLess - endless page
//
// Pageless is a jQuery plugin.
// As you scroll down you see more results coming back at you automatically.
// It provides an automatic pagination in an accessible way : if javascript
// is disabled your standard pagination is supposed to work.
//
// Licensed under the MIT:
// http://www.opensource.org/licenses/mit-license.php
//
// Parameters:
// currentPage: current page (params[:page])
// distance: distance to the end of page in px when ajax query is fired
// loader: selector of the loader div (ajax activity indicator)
// loaderHtml: html code of the div if loader not used
// loaderImage: image inside the loader
// loaderMsg: displayed ajax message
// pagination: selector of the paginator divs.
// if javascript is disabled paginator is provided
// params: paramaters for the ajax query, you can pass auth_token here
// totalPages: total number of pages
// url: URL used to request more data
//
// Callback Parameters:
// scrape: A function to modify the incoming data.
// complete: A function to call when a new page has been loaded (optional)
// end: A function to call when the last page has been loaded (optional)
//
// Usage:
// $('#results').pageless({ totalPages: 10
// , url: '/articles/'
// , loaderMsg: 'Loading more results'
// });
//
// Requires: jquery
//
// Author: Jean-Sébastien Ney (https://github.com/jney)
//
// Contributors:
// Alexander Lang (https://github.com/langalex)
// Lukas Rieder (https://github.com/Overbryd)
//
// Thanks to:
// * codemonky.com/post/34940898
// * www.unspace.ca/discover/pageless/
// * famspam.com/facebox
// =======================================================================
define([
'jquery' /* jQuery, $ */
], function($) {
var FALSE = !1
, TRUE = !FALSE
, NAMESPACE = '.pageless'
, SCROLL = 'scroll' + NAMESPACE
, RESIZE = 'resize' + NAMESPACE
, BARE_INSTANCE = null;
var createClosure = function (opts) {
var element
, isLoading = FALSE
, loader
, settings = { container: window
, currentPage: 1
, distance: 100
, pagination: '.pagination'
, params: {}
, url: location.href
, loaderImage: "/images/load.gif"
, animate: true
}
, container
, $container;
var activate = function(opts) {
$.isFunction(opts) ? opts.call() : init(opts);
};
var loaderHtml = function () {
return settings.loaderHtml || '\
<div id="pageless-loader" style="display:none;text-align:center;width:100%;">\
<div class="msg" style="color:#e9e9e9;font-size:2em"></div>\
<img src="' + settings.loaderImage + '" alt="loading more results" style="margin:10px auto" />\
</div>';
};
// settings params: totalPages
var init = function (opts) {
if (opts) $.extend(settings, opts);
container = settings.container;
$container = $(container);
// for accessibility we can keep pagination links
// but since we have javascript enabled we remove pagination links
if(settings.pagination) $(settings.pagination).remove();
// start the listener
startListener();
};
var applyContext = function ($el, opts) {
var $loader = $(opts.loader, $el);
element = $el;
// loader element
if (opts.loader && $loader.length) {
loader = $loader;
} else {
loader = $(loaderHtml());
$el.append(loader);
// if we use the default loader, set the message
if (!opts.loaderHtml) {
$('#pageless-loader .msg').html(opts.loaderMsg);
}
}
loading(isLoading);
};
//
var loading = function (bool) {
isLoading = bool;
if (!loader) { return; }
if (isLoading) {
if (loader.parents().first().is(':visible') && settings.animate) {
// visible parent, animate it
loader.fadeIn('normal');
} else {
// invisible parent, just show so it's visible when parent is shown
loader.show();
}
} else {
if (loader.parents().first().is(':visible') && settings.animate) {
// visible parent, animate it
loader.fadeOut('normal');
} else {
// invisible parent, just hide so it remains invisible when parent is
// shown
loader.hide();
}
}
};
// distance to end of the container
var distanceToBottom = function () {
return (container === window)
? $(document).height()
- $container.scrollTop()
- $container.height()
: $container[0].scrollHeight
- $container.scrollTop()
- $container.height();
};
var stopListener = function() {
$container.unbind(NAMESPACE);
};
// * bind a scroll event
// * trigger is once in case of reload
var startListener = function() {
$container.bind(SCROLL+' '+RESIZE, watch)
.trigger(SCROLL);
};
var watch = function() {
// listener was stopped or we've run out of pages
if (settings.totalPages <= settings.currentPage) {
stopListener();
// if there is a afterStopListener callback we call it
if (settings.end) settings.end.call();
return;
}
// if slider past our scroll offset, then fire a request for more data
if(!isLoading && (distanceToBottom() < settings.distance)) {
loading(TRUE);
// move to next page
settings.currentPage++;
// set up ajax query params
$.extend( settings.params
, { page: settings.currentPage });
// finally ajax query
$.get( settings.url
, settings.params
, function (data, text, xhr) {
var data = $.isFunction(settings.scrape) ? settings.scrape(data, xhr) : data;
loader ? loader.before(data) : element.append(data);
loading(FALSE);
// if there is a complete callback we call it
if (settings.complete) settings.complete.call();
}, 'html');
}
};
return {activate: activate, applyContext: applyContext};
};
$.pageless = function(opts) {
if (BARE_INSTANCE === null) {
BARE_INSTANCE = createClosure(opts);
BARE_INSTANCE.activate(opts);
}
return BARE_INSTANCE;
};
$.fn.pageless = function(opts) {
if (!this.hasOwnProperty('pagelessInstance')) {
this.pagelessInstance = createClosure(opts);
this.pagelessInstance.activate(opts);
}
this.pagelessInstance.applyContext($(this), opts);
return this;
};
});