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