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 = _getWindowHeight() * 2; |
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 |
var next_page = $( '#next_page_' + v.page ); |
55 |
next_page.hide(); |
56 |
$( '#results' ).append( '<!-- appended page ' + v.page + ' -->' + response ); |
57 |
|
58 |
var resp_v; |
59 |
eval( 'resp_v=' + $('#json').val() ); |
60 |
//console.log( 'resp_v = ', resp_v ); |
61 |
|
62 |
// record ID so we can detect back-button |
63 |
v.id = resp_v.id; |
64 |
|
65 |
// restore status line |
66 |
$('#status').html( resp_v.status || old_status ); |
67 |
|
68 |
isUpdating = false; |
69 |
} |
70 |
); |
71 |
|
72 |
|
73 |
} |
74 |
|
75 |
/* |
76 |
bind events and init no_pager |
77 |
*/ |
78 |
|
79 |
$(document).ready( function() { |
80 |
|
81 |
$.log.info('no_page binding events'); |
82 |
|
83 |
eval( 'v=' + $('#json').val() ); |
84 |
|
85 |
var cookie_id = getCookie(cookie+'_id') || 0; |
86 |
|
87 |
//console.debug( 'ready', v ); |
88 |
$.log.debug("Page Cookie:" + cookie_id); |
89 |
|
90 |
/* FIXME |
91 |
var fromBackButton = false; |
92 |
if ( cookie_id == v.id ) { |
93 |
fromBackButton = true; |
94 |
} else { |
95 |
setCookie(cookie+'_id', v.id); |
96 |
} |
97 |
|
98 |
$.log.debug("from Back button? " + fromBackButton); |
99 |
if ( fromBackButton ) { |
100 |
$.log.info( 'Load ' + getCookie(cookie+'_page') + ' pages'); |
101 |
$.log.debug( 'Scroll to:' + getCookie(cookie+'_scroll') ); |
102 |
$('#spacer').height( getCookie(cookie+'_height')+"px" ); |
103 |
scroll( 0, getCookie(cookie+'_scroll') ); |
104 |
} |
105 |
*/ |
106 |
|
107 |
$(window).mousedown( function () { |
108 |
isMouseDown = true; |
109 |
}); |
110 |
$(window).mouseup( function () { |
111 |
isMouseDown = false; |
112 |
}); |
113 |
|
114 |
checker = setInterval(function () { |
115 |
load_next_page(); |
116 |
}, 100); |
117 |
|
118 |
|
119 |
}); |
120 |
|
121 |
|
122 |
|
123 |
// These three functions are taken from code displayed on |
124 |
// http://www.netspade.com/articles/2005/11/16/javascript-cookies/ |
125 |
|
126 |
/** |
127 |
* Sets a Cookie with the given name and value. |
128 |
* |
129 |
* name Name of the cookie |
130 |
* value Value of the cookie |
131 |
* [expires] Expiration date of the cookie (default: end of current session) |
132 |
* [path] Path where the cookie is valid (default: path of calling document) |
133 |
* [domain] Domain where the cookie is valid |
134 |
* (default: domain of calling document) |
135 |
* [secure] Boolean value indicating if the cookie transmission requires a |
136 |
* secure transmission |
137 |
*/ |
138 |
function setCookie(name, value, expires, path, domain, secure) { |
139 |
document.cookie= name + "=" + escape(value) + |
140 |
((expires) ? "; expires=" + expires.toGMTString() : "") + |
141 |
((path) ? "; path=" + path : "") + |
142 |
((domain) ? "; domain=" + domain : "") + |
143 |
((secure) ? "; secure" : ""); |
144 |
} |
145 |
|
146 |
/** |
147 |
* Gets the value of the specified cookie. |
148 |
* |
149 |
* name Name of the desired cookie. |
150 |
* |
151 |
* Returns a string containing value of specified cookie, |
152 |
* or null if cookie does not exist. |
153 |
*/ |
154 |
function getCookie(name) { |
155 |
var dc = document.cookie; |
156 |
var prefix = name + "="; |
157 |
var begin = dc.indexOf("; " + prefix); |
158 |
if (begin == -1) { |
159 |
begin = dc.indexOf(prefix); |
160 |
if (begin != 0) return null; |
161 |
} else { |
162 |
begin += 2; |
163 |
} |
164 |
var end = document.cookie.indexOf(";", begin); |
165 |
if (end == -1) { |
166 |
end = dc.length; |
167 |
} |
168 |
return unescape(dc.substring(begin + prefix.length, end)); |
169 |
} |
170 |
|
171 |
/** |
172 |
* Deletes the specified cookie. |
173 |
* |
174 |
* name name of the cookie |
175 |
* [path] path of the cookie (must be same as path used to create cookie) |
176 |
* [domain] domain of the cookie (must be same as domain used to create cookie) |
177 |
*/ |
178 |
function deleteCookie(name, path, domain) { |
179 |
if (getCookie(name)) { |
180 |
document.cookie = name + "=" + |
181 |
((path) ? "; path=" + path : "") + |
182 |
((domain) ? "; domain=" + domain : "") + |
183 |
"; expires=Thu, 01-Jan-70 00:00:01 GMT"; |
184 |
} |
185 |
} |
186 |
|
187 |
// These two functions are based on code from |
188 |
// http://www.quirksmode.org/viewport/compatibility.html |
189 |
|
190 |
function getPageHeight(){ |
191 |
var y; |
192 |
var test1 = document.body.scrollHeight; |
193 |
var test2 = document.body.offsetHeight |
194 |
if (test1 > test2) // all but Explorer Mac |
195 |
{ |
196 |
y = document.body.scrollHeight; |
197 |
} |
198 |
else // Explorer Mac; |
199 |
//would also work in Explorer 6 Strict, Mozilla and Safari |
200 |
{ |
201 |
y = document.body.offsetHeight; |
202 |
} |
203 |
return parseInt(y); |
204 |
} |
205 |
|
206 |
function _getWindowHeight(){ |
207 |
var frameWidth; |
208 |
var frameHeight; |
209 |
if (self.innerWidth) |
210 |
{ |
211 |
frameWidth = self.innerWidth; |
212 |
frameHeight = self.innerHeight; |
213 |
} |
214 |
else if (document.documentElement && document.documentElement.clientWidth) |
215 |
{ |
216 |
frameWidth = document.documentElement.clientWidth; |
217 |
frameHeight = document.documentElement.clientHeight; |
218 |
} |
219 |
else if (document.body) |
220 |
{ |
221 |
frameWidth = document.body.clientWidth; |
222 |
frameHeight = document.body.clientHeight; |
223 |
} |
224 |
return parseInt(frameHeight); |
225 |
} |
226 |
|
227 |
|
228 |
function getScrollHeight(){ |
229 |
var y; |
230 |
// all except Explorer |
231 |
if (self.pageYOffset) |
232 |
{ |
233 |
y = self.pageYOffset; |
234 |
} |
235 |
else if (document.documentElement && document.documentElement.scrollTop) |
236 |
{ |
237 |
y = document.documentElement.scrollTop; |
238 |
} |
239 |
else if (document.body) // all other Explorers |
240 |
{ |
241 |
y = document.body.scrollTop; |
242 |
} |
243 |
return parseInt(y)+_getWindowHeight(); |
244 |
} |
245 |
|