1 |
/* |
2 |
no pager, as seen at |
3 |
http://www.humanized.com/weblog/2006/04/28/reading_humanized/ |
4 |
but using jquery |
5 |
|
6 |
2006-08-16 Dobrica Pavlinusic <dpavlin@rot13.org> |
7 |
*/ |
8 |
|
9 |
var preloadDistance = 1000; |
10 |
var isUpdating = false; |
11 |
var isMouseDown = false; |
12 |
var checker; |
13 |
var v; |
14 |
var cookie = 'no_pager'; |
15 |
|
16 |
function load_next_page() { |
17 |
|
18 |
setCookie(cookie+"_height", getScrollHeight() ); |
19 |
setCookie(cookie+"_scroll", getScrollHeight() - _getWindowHeight() ); |
20 |
|
21 |
if ( isUpdating == true |
22 |
|| isMouseDown == true |
23 |
|| getPageHeight() - getScrollHeight() > preloadDistance |
24 |
|| v.page >= v.max_page |
25 |
) return; |
26 |
|
27 |
v.page++; |
28 |
|
29 |
$.log.info( |
30 |
'get page: ' + v.page, |
31 |
'search: ' + v.search, |
32 |
'PageHeight:' + getPageHeight(), |
33 |
'ScrollHeight:' + getScrollHeight() |
34 |
); |
35 |
|
36 |
setCookie( cookie+'_page', v.page ); |
37 |
isUpdating = true; |
38 |
|
39 |
var old_status = $('#status').html; |
40 |
|
41 |
$('#status').html( |
42 |
'Loading page '+ v.page + '...' |
43 |
); |
44 |
|
45 |
console.log( 'v = ', v ); |
46 |
|
47 |
$.get( |
48 |
"/Frey::View::NoPager/results_as_markup", |
49 |
{ |
50 |
page: v.page, |
51 |
search: v.search |
52 |
}, |
53 |
function ( response ) { |
54 |
$('#next_page').html( response ); |
55 |
|
56 |
var resp_v; |
57 |
eval( 'resp_v=' + $('#json').val() ); |
58 |
console.log( 'resp_v = ', resp_v ); |
59 |
|
60 |
// restore status line |
61 |
$('#status').html( resp_v.status || old_status ); |
62 |
|
63 |
isUpdating = false; |
64 |
} |
65 |
); |
66 |
|
67 |
|
68 |
} |
69 |
|
70 |
/* |
71 |
bind events and init no_pager |
72 |
*/ |
73 |
|
74 |
$(document).ready( function() { |
75 |
|
76 |
$.log.info('no_page binding events'); |
77 |
|
78 |
eval( 'v=' + $('#json').val() ); |
79 |
|
80 |
var cookie_id = getCookie(cookie+'_id') || 0; |
81 |
|
82 |
console.debug( v ); |
83 |
$.log.debug("Page Cookie:" + cookie_id); |
84 |
|
85 |
var fromBackButton = false; |
86 |
if ( cookie_id == v.id ) { |
87 |
fromBackButton = true; |
88 |
} else { |
89 |
setCookie(cookie+'_id', v.id); |
90 |
} |
91 |
|
92 |
$.log.debug("from Back button? " + fromBackButton); |
93 |
if ( fromBackButton ) { |
94 |
$.log.info( 'Load ' + getCookie(cookie+'_page') + ' pages'); |
95 |
$.log.debug( 'Scroll to:' + getCookie(cookie+'_scroll') ); |
96 |
$('#spacer').height( getCookie(cookie+'_height')+"px" ); |
97 |
scroll( 0, getCookie(cookie+'_scroll') ); |
98 |
} |
99 |
|
100 |
$(window).mousedown( function () { |
101 |
isMouseDown = true; |
102 |
}); |
103 |
$(window).mouseup( function () { |
104 |
isMouseDown = false; |
105 |
}); |
106 |
|
107 |
checker = setInterval(function () { |
108 |
load_next_page(); |
109 |
}, 100); |
110 |
|
111 |
|
112 |
}); |
113 |
|
114 |
|
115 |
|
116 |
// These three functions are taken from code displayed on |
117 |
// http://www.netspade.com/articles/2005/11/16/javascript-cookies/ |
118 |
|
119 |
/** |
120 |
* Sets a Cookie with the given name and value. |
121 |
* |
122 |
* name Name of the cookie |
123 |
* value Value of the cookie |
124 |
* [expires] Expiration date of the cookie (default: end of current session) |
125 |
* [path] Path where the cookie is valid (default: path of calling document) |
126 |
* [domain] Domain where the cookie is valid |
127 |
* (default: domain of calling document) |
128 |
* [secure] Boolean value indicating if the cookie transmission requires a |
129 |
* secure transmission |
130 |
*/ |
131 |
function setCookie(name, value, expires, path, domain, secure) { |
132 |
document.cookie= name + "=" + escape(value) + |
133 |
((expires) ? "; expires=" + expires.toGMTString() : "") + |
134 |
((path) ? "; path=" + path : "") + |
135 |
((domain) ? "; domain=" + domain : "") + |
136 |
((secure) ? "; secure" : ""); |
137 |
} |
138 |
|
139 |
/** |
140 |
* Gets the value of the specified cookie. |
141 |
* |
142 |
* name Name of the desired cookie. |
143 |
* |
144 |
* Returns a string containing value of specified cookie, |
145 |
* or null if cookie does not exist. |
146 |
*/ |
147 |
function getCookie(name) { |
148 |
var dc = document.cookie; |
149 |
var prefix = name + "="; |
150 |
var begin = dc.indexOf("; " + prefix); |
151 |
if (begin == -1) { |
152 |
begin = dc.indexOf(prefix); |
153 |
if (begin != 0) return null; |
154 |
} else { |
155 |
begin += 2; |
156 |
} |
157 |
var end = document.cookie.indexOf(";", begin); |
158 |
if (end == -1) { |
159 |
end = dc.length; |
160 |
} |
161 |
return unescape(dc.substring(begin + prefix.length, end)); |
162 |
} |
163 |
|
164 |
/** |
165 |
* Deletes the specified cookie. |
166 |
* |
167 |
* name name of the cookie |
168 |
* [path] path of the cookie (must be same as path used to create cookie) |
169 |
* [domain] domain of the cookie (must be same as domain used to create cookie) |
170 |
*/ |
171 |
function deleteCookie(name, path, domain) { |
172 |
if (getCookie(name)) { |
173 |
document.cookie = name + "=" + |
174 |
((path) ? "; path=" + path : "") + |
175 |
((domain) ? "; domain=" + domain : "") + |
176 |
"; expires=Thu, 01-Jan-70 00:00:01 GMT"; |
177 |
} |
178 |
} |
179 |
|
180 |
// These two functions are based on code from |
181 |
// http://www.quirksmode.org/viewport/compatibility.html |
182 |
|
183 |
function getPageHeight(){ |
184 |
var y; |
185 |
var test1 = document.body.scrollHeight; |
186 |
var test2 = document.body.offsetHeight |
187 |
if (test1 > test2) // all but Explorer Mac |
188 |
{ |
189 |
y = document.body.scrollHeight; |
190 |
} |
191 |
else // Explorer Mac; |
192 |
//would also work in Explorer 6 Strict, Mozilla and Safari |
193 |
{ |
194 |
y = document.body.offsetHeight; |
195 |
} |
196 |
return parseInt(y); |
197 |
} |
198 |
|
199 |
function _getWindowHeight(){ |
200 |
var frameWidth; |
201 |
var frameHeight; |
202 |
if (self.innerWidth) |
203 |
{ |
204 |
frameWidth = self.innerWidth; |
205 |
frameHeight = self.innerHeight; |
206 |
} |
207 |
else if (document.documentElement && document.documentElement.clientWidth) |
208 |
{ |
209 |
frameWidth = document.documentElement.clientWidth; |
210 |
frameHeight = document.documentElement.clientHeight; |
211 |
} |
212 |
else if (document.body) |
213 |
{ |
214 |
frameWidth = document.body.clientWidth; |
215 |
frameHeight = document.body.clientHeight; |
216 |
} |
217 |
return parseInt(frameHeight); |
218 |
} |
219 |
|
220 |
|
221 |
function getScrollHeight(){ |
222 |
var y; |
223 |
// all except Explorer |
224 |
if (self.pageYOffset) |
225 |
{ |
226 |
y = self.pageYOffset; |
227 |
} |
228 |
else if (document.documentElement && document.documentElement.scrollTop) |
229 |
{ |
230 |
y = document.documentElement.scrollTop; |
231 |
} |
232 |
else if (document.body) // all other Explorers |
233 |
{ |
234 |
y = document.body.scrollTop; |
235 |
} |
236 |
return parseInt(y)+_getWindowHeight(); |
237 |
} |
238 |
|