# HG changeset patch # User cavaliet # Date 1390318952 -3600 # Node ID 8c9ee4175638743cb9b408aa2eb09591286b13be # Parent 0371cf63df4942e55a94ab982e250cf1be1bb27f optimize pagination diff -r 0371cf63df49 -r 8c9ee4175638 src/jocondelab/static/jocondelab/js/front-common.js --- a/src/jocondelab/static/jocondelab/js/front-common.js Tue Jan 21 15:30:06 2014 +0100 +++ b/src/jocondelab/static/jocondelab/js/front-common.js Tue Jan 21 16:42:32 2014 +0100 @@ -387,7 +387,6 @@ var max_scroll_pages = 3, currentpage; function loadMorePages(query) { - console.log("loadMorePages currentpage = " + currentpage); $(".load-more").hide(); $win.off("scroll.ajaxload"); $(".notice-list").empty(); @@ -403,8 +402,18 @@ bindResultsMouseover(); $(".loading-please-wait").hide(); scrollLoad(query); - // Enable load page when scroll not available - startTimeOut(); + // We check if last element has attribute "last page" and, if so, we cancel loading next page + var attr = $(".notice-list").children().last().attr('data-last-notice'); + if(typeof attr !== 'undefined' && attr !== false){ + $win.off("scroll.ajaxload"); + $(".load-more").hide(); + $(".loading-please-wait").hide(); + window.stopTimeOut(); + } + else{ + // Enable load page when scroll not available + startTimeOut(); + } } }); } @@ -414,13 +423,12 @@ var loadingnext = false, page_count = parseInt($(".notice-list").attr("data-page-count")), max_page = Math.min(currentpage + max_scroll_pages, page_count); - console.log('scrollLoad max_page = ' + max_page); + $(".load-more").hide().off("click").click(function() { loadMorePages(query); return false; }); $win.on("scroll.ajaxload", function() { - console.log('$win.on("scroll.ajaxload") max_page = ' + max_page); if (loadingnext || currentpage >= max_page) { return; } @@ -431,9 +439,8 @@ } var winbottom = $win.scrollTop() + $win.height(), databottom = dbo.top + $datablock.height(); - console.log('AJAX currentpage ? loadingnext = ' + loadingnext); + if (winbottom >= databottom && !loadingnext) { - console.log('AJAX currentpage LOAD'); loadingnext = true; $(".loading-please-wait").show(); $.ajax({ @@ -441,23 +448,29 @@ data: _({ page: ++currentpage }).extend(query), dataType: "html", success: function(html) { - console.log('$datablock.append(html); more = ' + (currentpage >= max_page && currentpage < page_count)); - console.log("HTML == '' ? ", (html==""), (html.trim()==""), typeof html); + var last_page = false; if(html.trim()!=""){ - console.log(" APPEND HTML"); $datablock.append(html); loadingnext = false; bindResultsMouseover(); $(".loading-please-wait").hide(); if (currentpage >= max_page && currentpage < page_count) { - console.log(' $(".load-more").show();'); $(".load-more").show(); } + // We check if last element has attribute "last page" and, if so, we cancel loading next page + var attr = $datablock.children().last().attr('data-last-notice'); + if(typeof attr !== 'undefined' && attr !== false){ + last_page = true; + } } else{ + last_page = true; + } + if(last_page){ + $win.off("scroll.ajaxload"); $(".load-more").hide(); $(".loading-please-wait").hide(); - console.log(" NOTHING HTML"); + window.stopTimeOut(); } } }); diff -r 0371cf63df49 -r 8c9ee4175638 src/jocondelab/static/jocondelab/js/front-search.js --- a/src/jocondelab/static/jocondelab/js/front-search.js Tue Jan 21 15:30:06 2014 +0100 +++ b/src/jocondelab/static/jocondelab/js/front-search.js Tue Jan 21 16:42:32 2014 +0100 @@ -30,7 +30,6 @@ // Check if a dom element is the current viewport // el is dom element, not jquery object function isElementInViewport(el) { - console.log("isElementInViewport"); var rect = el.getBoundingClientRect(); return ( rect.top >= 0 && @@ -41,11 +40,12 @@ } var myTimeOut = false; window.startTimeOut = function(){ - console.log("window.startTimeOut"); - myTimeOut = setInterval(function(){ console.log("setInterval"); testIfLastLiInViewport() }, 3000); + myTimeOut = setInterval(function(){ testIfLastLiInViewport(); }, 3000); + } + window.stopTimeOut = function(){ + clearInterval(myTimeOut); } function testIfLastLiInViewport(){ - console.log("testIfLastLiInViewport"); var el = $($(".results ul")[0]).children().last()[0]; if( (typeof el!=="undefined") && isElementInViewport(el) ) { // Last li is visible, so we have to call next page manually @@ -54,18 +54,16 @@ } } else{ - clearInterval(myTimeOut); + window.stopTimeOut(); } } if (window.addEventListener) { - console.log("addEventListener 1"); //addEventListener('DOMContentLoaded', testIfLastLiInViewport(), false); addEventListener('load', startTimeOut(), false); //addEventListener('scroll', testIfLastLiInViewport(), false); //addEventListener('resize', testIfLastLiInViewport(), false); } else if (window.attachEvent) { - console.log("addEventListener 2"); //attachEvent('DOMContentLoaded', testIfLastLiInViewport()); attachEvent('load', startTimeOut()); //attachEvent('scroll', testIfLastLiInViewport()); diff -r 0371cf63df49 -r 8c9ee4175638 src/jocondelab/templates/jocondelab/partial/notice_list.html --- a/src/jocondelab/templates/jocondelab/partial/notice_list.html Tue Jan 21 15:30:06 2014 +0100 +++ b/src/jocondelab/templates/jocondelab/partial/notice_list.html Tue Jan 21 16:42:32 2014 +0100 @@ -1,7 +1,7 @@ {% load i18n %} {% for notice in notices %} -