-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.autopager-1.0.0.js
178 lines (153 loc) · 4.02 KB
/
jquery.autopager-1.0.0.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
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
/*
* jQuery.autopager v1.0.0
*
* Copyright (c) lagos
* Dual licensed under the MIT and GPL licenses.
*/
(function($) {
var window = this, options = {},
content, currentUrl, nextUrl,
active = false,
defaults = {
autoLoad: true,
page: 1,
content: '.content',
link: 'a[rel=next]',
insertBefore: null,
appendTo: null,
start: function() {},
load: function() {},
disabled: false
};
$.autopager = function(_options) {
var autopager = this.autopager;
if (typeof _options === 'string' && $.isFunction(autopager[_options])) {
var args = Array.prototype.slice.call(arguments, 1),
value = autopager[_options].apply(autopager, args);
return value === autopager || value === undefined ? this : value;
}
_options = $.extend({}, defaults, _options);
autopager.option(_options);
content = $(_options.content).filter(':last');
if (content.length) {
if (!_options.insertBefore && !_options.appendTo) {
var insertBefore = content.next();
if (insertBefore.length) {
set('insertBefore', insertBefore);
} else {
set('appendTo', content.parent());
}
}
}
setUrl();
return this;
};
$.extend($.autopager, {
option: function(key, value) {
var _options = key;
if (typeof key === "string") {
if (value === undefined) {
return options[key];
}
_options = {};
_options[key] = value;
}
$.each(_options, function(key, value) {
set(key, value);
});
return this;
},
enable: function() {
set('disabled', false);
return this;
},
disable: function() {
set('disabled', true);
return this;
},
destroy: function() {
this.autoLoad(false);
options = {};
content = currentUrl = nextUrl = undefined;
return this;
},
autoLoad: function(value) {
return this.option('autoLoad', value);
},
load: function() {
setUrl();
if (active || !nextUrl || options.disabled) {
return;
}
active = true;
options.start(currentHash(), nextHash());
$.get(nextUrl, insertContent);
return this;
}
});
function set(key, value) {
switch (key) {
case 'autoLoad':
if (value && !options.autoLoad) {
$(window).scroll(loadOnScroll);
} else if (!value && options.autoLoad) {
$(window).unbind('scroll', loadOnScroll);
}
break;
case 'insertBefore':
if (value) {
options.appendTo = null;
}
break
case 'appendTo':
if (value) {
options.insertBefore = null;
}
break
}
options[key] = value;
}
function setUrl(context) {
currentUrl = nextUrl || window.location.href;
nextUrl = $(options.link, context).attr('href');
}
function loadOnScroll() {
if (content.offset().top + content.height() < $(document).scrollTop() + $(window).height()) {
$.autopager.load();
}
}
function insertContent(res) {
if (typeof res === 'object') {
res = res.regions.search_api_ajax;
}
var _options = options,
nextPage = $('<div/>').append(res.replace(/<script(.|\s)*?\/script>/g, "")),
nextContent = nextPage.find(_options.content);
nextPager = nextPage.find(_options.pager);
set('page', _options.page + 1);
setUrl(nextPage);
if (nextContent.length) {
if (_options.insertBefore) {
nextContent.insertBefore(_options.insertBefore);
} else {
nextContent.appendTo(_options.appendTo);
}
$(_options.pager).replaceWith(nextPager);
_options.load.call(nextContent.get(), currentHash(), nextHash());
content = nextContent.filter(':last');
}
active = false;
}
function currentHash() {
return {
page: options.page,
url: currentUrl
};
}
function nextHash() {
return {
page: options.page + 1,
url: nextUrl
};
}
})(jQuery);