--- no_pager/no_pager.js 2006/08/15 23:13:20 7 +++ no_pager/no_pager.js 2006/08/16 14:20:18 11 @@ -1,129 +1,120 @@ /* no pager, as seen at http://www.humanized.com/weblog/2006/04/28/reading_humanized/ + but using jquery + + 2006-08-16 Dobrica Pavlinusic */ -function favorite( image, feedID ){ - var makeFavorite; - if (image.src.indexOf("fav_off.gif") != -1){ - image.src = "static/images/fav_on.gif"; - makeFavorite = "True"; - } - else if (image.src.indexOf("fav_on.gif") != -1){ - image.src = "static/images/fav_off.gif"; - makeFavorite = "False"; - } - doSimpleXMLHttpRequest("favorite", {id: feedID, makeFavorite: makeFavorite}) - -} +var preloadDistance = 1000; +var isUpdating = false; +var id; +var checker; -function getMoreHistory(){ - var indexVal = getElement("history_index").value; - var xmlDoc = doSimpleXMLHttpRequest("index.cgi/snippet", {index: indexVal, q: document.getElementById('q').value }); - xmlDoc.addCallbacks(addMoreHistory, failure); -} +/* + function to log debugging information into debug div +*/ -function failure(){ - var message = P( null, "" ); - message.innerHTML = "Could not contact the Humanized server.
"; - message.innerHTML += "Please wait awhile and try again.

"; - message.innerHTML += "We apologize for the inconvenience."; - var el = getElement("more_history"); +function logDebug( m1, m2 ) { + var m = m1; + if (m2) m += m2; + if (m) $('#debug').append( ''+m+'
' ); +} +function log( m1, m2 ) { + var m = m1; + if (m2) m += m2; + m += '
' + if (m) $('#debug').append( m ); } -function addMoreHistory( xmlDoc ){ - var response = xmlDoc.responseText; +function load_page() { + setCookie(id+"_height", getScrollHeight() ); + setCookie(id+"_scroll", getScrollHeight() - _getWindowHeight() ); - var index_el = getElement("history_index"); - var indexVal = parseInt(index_el.value); + if ( isUpdating == true + || getPageHeight() - getScrollHeight() > preloadDistance + ) return; + + var history_index = parseInt( $('#history_index').val() ); + var q = $('#q').val(); + + if (! q || q == '') return; + + log("Getting segment: " + history_index + " for: "+q); + + setCookie( id, history_index ); + isUpdating = true; + + $('#status').html( + 'Loading page '+ ( history_index + 1 ) + '...' + ); + + $.get( + "index.cgi/snippet", + { + index: history_index, + q: q + }, + function ( response ) { + + if (response.length < 500) { + $('#footer').show(); + if ( history_index > 0 ) { + $('#more_history').html('

No more results.

'); + } else { + $('#more_history').html('

No results matched your search.

'); + } + $('#status').html( 'All results shown.' ); + return; + } + + response += "
"; + response += " More posts are being loaded...
"; + response += " If you are using the scroll bar, release the mouse to see more posts."; + response += "
" + $('#more_history').html( response ); + $('#history_index').val( history_index + 1 ); + isUpdating = false; + $('#status').html( $('#status_update').html() ); + } + ); -// minimum response length? - if (response.length < 300){ - - showElement("footer") - if( indexVal > 0 ){ - var out = H2({'style':'text-align:center; font-style:italic; padding: 2em;'}, - "No more results." - ); - }else{ - var out = H2({'style':'text-align:center; font-style:italic; padding: 2em;'}, - "No posts matched your search." - ); - } - replaceChildNodes( "more_history", out ) - return; - } - response += "
"; - response += " More posts are being loaded...
"; - response += " If you are using the scroll bar, release the mouse to see more posts."; - response += "
" - - index_el.value = indexVal + 1; - - var new_history = DIV(null,""); - new_history.innerHTML = response; - swapDOM( "more_history", new_history ); - - isUpdating = false; } -function updatePage(){ - if ( isUpdating == false - && mouseState == "up" - && getPageHeight() - getScrollHeight() < preloadDistance){ - log("Getting segment: ", $("history_index").value); - setCookie(id, $("history_index").value ); - isUpdating = true; - getMoreHistory(); - } - checker.cancel(); - checker = callLater(checkInterval, updatePage); -} - - -function onMouseDown(e ){ - mouseState = "down"; - setCookie(id+"_height", getScrollHeight() ); - setCookie(id+"_scroll", getScrollHeight() - _getWindowHeight() ); -} +/* + bind events and init no_pager +*/ -function onMouseUp(){ - mouseState = "up"; -} +$(document).ready( function() { -var checker; -var checkInterval = .2; -var preloadDistance = 1000; -var isUpdating = false; -var mouseState = "up"; -var id; + logDebug( 'no_page binding events' ); -function init( ){ - document.onmousedown = onMouseDown; - document.onmouseup = onMouseUp; + checker = setInterval(function () { + load_page(); + }, 100); - //createLoggingPane(true); - id = $("page_id").value; + id = $('#page_id').val(); logDebug("Page ID:", id); logDebug("Page Cookie:", getCookie(id)); - + var fromBackButton = false; - if( getCookie(id) ){ fromBackButton = true; } - else{ setCookie(id, "1") } + if ( getCookie(id) ) { + fromBackButton = true; + } else { + setCookie(id, "1"); + } logDebug("From Back Button?:", fromBackButton); if( fromBackButton ){ logDebug( "Number of segments loaded: ", getCookie(id) ); logDebug( "Should scroll to: ", getCookie(id+"_scroll") ); - $("spacer").style.height=getCookie(id+"_height")+"px"; - scroll( 0, getCookie(id+"_scroll") ); + $('#spacer').height( getCookie(id+"_height")+"px" ); + scroll( 0, getCookie(id+"_scroll") ); } - getElement("history_index").value = 0; - checker = callLater(0, updatePage); -} - -addLoadEvent(init); + $('#history_index').val( 0 ); + load_page(); +});