optimize pagination
authorcavaliet
Tue, 21 Jan 2014 16:42:32 +0100
changeset 296 8c9ee4175638
parent 295 0371cf63df49
child 297 44993cacd25c
optimize pagination
src/jocondelab/static/jocondelab/js/front-common.js
src/jocondelab/static/jocondelab/js/front-search.js
src/jocondelab/templates/jocondelab/partial/notice_list.html
src/jocondelab/views/front_office.py
--- 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();
                     	}
                     }
                 });
--- 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());
--- 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 %}
-    <li class="notice-item" data-notice-id="{{notice.id}}">
+    <li class="notice-item" data-notice-id="{{notice.id}}"{% if last_page and forloop.last %} data-last-notice="true"{% endif %}>
         <div class="notice-contents">
             <a href="{% url 'front_notice' notice.id %}" class="update-ids" data-id="{{ notice.id }}" title="{{notice.imagetitle}}">
                 <img class="notice-image" alt="{{notice.imagetitle}}" src="{{notice.image}}" onerror="window.onResultImageError(this);" />
--- a/src/jocondelab/views/front_office.py	Tue Jan 21 15:30:06 2014 +0100
+++ b/src/jocondelab/views/front_office.py	Tue Jan 21 16:42:32 2014 +0100
@@ -84,6 +84,7 @@
         npp = request.GET.get('count', 12 if emptysearch else 30)
         context["lang"] = lang
         context["current_page"] = page
+        context["last_page"] = False
         
         if self.template_name is None:
             if is_ajax and page > 1:
@@ -194,6 +195,9 @@
                 logger.debug(context["count"])
                 # Now that we have the list of ids
                 ns = Notice.objects.filter(pk__in=ids).extra(select={'relative_url': '"core_noticeimage"."relative_url"'}).filter(image=True).filter(images__main=True).order_by('id')
+                # We check if we are in the last page of the "real" notices and not the +/- number of notices.
+                if len(ns) < npp:
+                    context["last_page"] = True
         
         notices = []
         termsbythesaurus = get_terms_by_thesaurus(ns, lang)