<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[19971] trunk/wp-admin/js/theme.dev.js: Second pass at infinite scroll for themes, including polling, fixed paging, and fewer ajax calls.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/19971">19971</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2012-02-21 19:44:10 +0000 (Tue, 21 Feb 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Second pass at infinite scroll for themes, including polling, fixed paging, and fewer ajax calls. props DH-Shredder, helenyhou, garyc40. see <a href="http://core.trac.wordpress.org/ticket/19815">#19815</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminjsthemedevjs">trunk/wp-admin/js/theme.dev.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminjsthemedevjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/theme.dev.js (19970 => 19971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/theme.dev.js        2012-02-21 19:00:06 UTC (rev 19970)
+++ trunk/wp-admin/js/theme.dev.js        2012-02-21 19:44:10 UTC (rev 19971)
</span><span class="lines">@@ -45,7 +45,7 @@
</span><span class="cx">                         init: init
</span><span class="cx">                 };
</span><span class="cx"> 
</span><del>-          return api;
</del><ins>+        return api;
</ins><span class="cx">         }
</span><span class="cx"> })(jQuery);
</span><span class="cx"> 
</span><span class="lines">@@ -54,173 +54,145 @@
</span><span class="cx">         theme_viewer.init();
</span><span class="cx"> });
</span><span class="cx"> 
</span><del>-var wpThemes;
</del><ins>+var ThemeScroller;
</ins><span class="cx"> 
</span><span class="cx"> (function($){
</span><del>-        var inputs = {}, Query;
</del><ins>+        ThemeScroller = {
+                // Inputs
+                nonce: '',
+                search: '',
+                tab: '',
+                type: '',
+                nextPage: 2,
+                features: {},
</ins><span class="cx"> 
</span><del>-        wpThemes = {
-                timeToTriggerQuery: 150,
-                minQueryAJAXDuration: 200,
-                outListBottomThreshold: 200,
-                noMoreResults: false,
-                
-                init : function() {
-                        $('.pagination-links').hide();
</del><ins>+                // Preferences
+                scrollPollingDelay: 500,
+                failedRetryDelay: 4000,
+                outListBottomThreshold: 300,
</ins><span class="cx"> 
</span><del>-                        inputs.nonce = $('#_ajax_fetch_list_nonce').val();
-        
-                        // Parse Query
-                        inputs.queryString = window.location.search;                        
-                        inputs.queryArray = wpThemes.parseQuery( inputs.queryString.substring( 1 ) );
</del><ins>+                // Flags
+                scrolling: false,
+                querying: false,
</ins><span class="cx"> 
</span><del>-                        // Handle Inputs from Query
-                        inputs.search = inputs.queryArray['s'];
-                        inputs.features = inputs.queryArray['features'];
-                        inputs.startPage = parseInt( inputs.queryArray['paged'] );        
-                        inputs.tab = inputs.queryArray['tab'];
-                        inputs.type = inputs.queryArray['type'];
</del><ins>+                init: function() {
+                        var self = this,
+                                startPage,
+                                queryArray = {},
+                                queryString = window.location.search;
</ins><span class="cx"> 
</span><del>-                        if ( isNaN( inputs.startPage ) )
-                                inputs.startPage = 2;
-                        else
-                                inputs.startPage++;
</del><ins>+                        // We're using infinite scrolling, so hide all pagination.
+                        $('.pagination-links').hide();
</ins><span class="cx"> 
</span><del>-                        // Cache jQuery objects
-                        inputs.outList = $('#availablethemes');
-                        inputs.waiting = $('div.tablenav.bottom').children( 'img.ajax-loading' );
-                        inputs.window = $(window);
</del><ins>+                        // Parse GET query string
+                        queryArray = this.parseQuery( queryString.substring( 1 ) );
</ins><span class="cx"> 
</span><del>-                        // Generate Query
-                        wpThemes.query = new Query();
</del><ins>+                        // Handle inputs
+                        this.nonce = $('#_ajax_fetch_list_nonce').val();
+                        this.search = queryArray['s'];
+                        this.features = queryArray['features'];
+                        this.tab = queryArray['tab'];
+                        this.type = queryArray['type'];
</ins><span class="cx"> 
</span><del>-                        // Start Polling
-                        inputs.window.scroll( function(){ wpThemes.maybeLoad(); } );
-                },
-                delayedCallback : function( func, delay ) {
-                        var timeoutTriggered, funcTriggered, funcArgs, funcContext;
</del><ins>+                        startPage = parseInt( queryArray['paged'], 10 );
+                        if ( ! isNaN( startPage ) )
+                                this.nextPage = ( startPage + 1 );
</ins><span class="cx"> 
</span><del>-                        if ( ! delay )
-                                return func;
</del><ins>+                        // Cache jQuery selectors
+                        this.$outList = $('#availablethemes');
+                        this.$spinner = $('div.tablenav.bottom').children( 'img.ajax-loading' );
+                        this.$window = $(window);
+                        this.$document = $(document);
</ins><span class="cx"> 
</span><del>-                        setTimeout( function() {
-                                if ( funcTriggered )
-                                        return func.apply( funcContext, funcArgs );
-                                // Otherwise, wait.
-                                timeoutTriggered = true;
-                        }, delay);
-
-                        return function() {
-                                if ( timeoutTriggered )
-                                        return func.apply( this, arguments );
-                                // Otherwise, wait.
-                                funcArgs = arguments;
-                                funcContext = this;
-                                funcTriggered = true;
-                        };
</del><ins>+                        if ( $('.tablenav-pages').length )
+                                this.pollInterval =
+                                        setInterval( function() { 
+                                                return self.poll();
+                                        }, this.scrollPollingDelay );
</ins><span class="cx">                 },
</span><del>-                ajax: function( callback ) {
-                        var self = this,
-                                response = wpThemes.delayedCallback( function( results, params ) {
-                                        self.process( results, params );
-                                        if ( callback )
-                                                callback( results, params );
-                                }, wpThemes.minQueryAJAXDuration );
</del><ins>+                poll: function() {
+                        var bottom = this.$document.scrollTop() + this.$window.innerHeight();
</ins><span class="cx"> 
</span><del>-                        this.query.ajax( response );
</del><ins>+                        if ( this.querying ||
+                                ( bottom &lt; this.$outList.height() - this.outListBottomThreshold ) )
+                                return;
+
+                        this.ajax();
</ins><span class="cx">                 },
</span><del>-                process: function( results, params ) {
-                        // If no Results, for now, mark as no Matches, and bail.
-                        // Alternately: inputs.outList.append(wpThemesL10n.noMatchesFound);
</del><ins>+                process: function( results ) {
</ins><span class="cx">                         if ( ( results === undefined ) ||
</span><del>-                                 ( results.rows.indexOf( &quot;no-items&quot; ) != -1 ) ) {
-                                this.noMoreResults = true;
-                        } else {
-                                inputs.outList.append( results.rows );
-                        }
-                },
-                maybeLoad: function() {
-                        var self = this,
-                                el = $(document),
-                                bottom = el.scrollTop() + inputs.window.innerHeight();
-                                
-                        if ( this.noMoreResults ||
-                                 !this.query.ready() || 
-                                 ( bottom &lt; inputs.outList.height() - wpThemes.outListBottomThreshold ) )
</del><ins>+                                ( results.rows.indexOf( 'no-items' ) != -1 ) ) {
+                                clearInterval( this.pollInterval );
</ins><span class="cx">                                 return;
</span><ins>+                        }
</ins><span class="cx"> 
</span><del>-                        setTimeout( function() {
-                                var newTop = el.scrollTop(),
-                                        newBottom = newTop + inputs.window.innerHeight();
</del><ins>+                        var totalPages = parseInt( results.total_pages, 10 );
+                        if ( this.nextPage &gt; totalPages )
+                                clearInterval( this.pollInterval );
</ins><span class="cx"> 
</span><del>-                                if ( !self.query.ready() ||
-                                         ( newBottom &lt; inputs.outList.height() - wpThemes.outListBottomThreshold ) )
-                                        return;
</del><ins>+                        if ( this.nextPage &lt;= ( totalPages + 1 ) )
+                                this.$outList.append( results.rows );
+                },
+                ajax: function() {
+                        var self = this;
+                        this.querying = true;
</ins><span class="cx"> 
</span><del>-                                inputs.waiting.css( 'visibility', 'visible' ); // Show Spinner
-                                self.ajax( function() { inputs.waiting.css( 'visibility', 'hidden' ) } ); // Hide Spinner
-                                
-                        }, wpThemes.timeToTriggerQuery );
</del><ins>+                        var query = {
+                                action: 'fetch-list',
+                                tab: this.tab,
+                                paged: this.nextPage,
+                                s: this.search,
+                                type: this.type,
+                                _ajax_fetch_list_nonce: this.nonce,
+                                'features[]': this.features,
+                                'list_args': list_args
+                        };
+
+                        this.$spinner.css( 'visibility', 'visible' );
+                        $.getJSON( ajaxurl, query )
+                                .done( function( response ) {
+                                        self.nextPage++;
+                                        self.process( response );
+                                        self.$spinner.css( 'visibility', 'hidden' );
+                                        self.querying = false;
+                                })
+                                .fail( function() {
+                                        self.$spinner.css( 'visibility', 'hidden' );
+                                        self.querying = false;
+                                        setTimeout( function() { self.ajax(); }, self.failedRetryDelay )
+                                });
</ins><span class="cx">                 },
</span><span class="cx">                 parseQuery: function( query ) {
</span><del>-                        var Params = {};
-                        if ( ! query ) {return Params;}// return empty object
-                        var Pairs = query.split(/[;&amp;]/);
-                        for ( var i = 0; i &lt; Pairs.length; i++ ) {
-                                var KeyVal = Pairs[i].split('=');
-                                if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
-                                var key = unescape( KeyVal[0] );
-                                var val = unescape( KeyVal[1] );
-                                val = val.replace(/\+/g, ' ');
-                                key = key.replace(/\[.*\]$/g, '');
-        
-                                if ( Params[key] === undefined ) {
-                                        Params[key] = val;
</del><ins>+                        var params = {};
+                        if ( ! query )
+                                return params;
+
+                        var pairs = query.split( /[;&amp;]/ );
+                        for ( var i = 0; i &lt; pairs.length; i++ ) {
+                                var keyVal = pairs[i].split( '=' );
+
+                                if ( ! keyVal || keyVal.length != 2 )
+                                        continue;
+
+                                var key = unescape( keyVal[0] );
+                                var val = unescape( keyVal[1] );
+                                val = val.replace( /\+/g, ' ' );
+                                key = key.replace( /\[.*\]$/g, '' );
+
+                                if ( params[key] === undefined ) {
+                                        params[key] = val;
</ins><span class="cx">                                 } else {
</span><del>-                                        var oldVal = Params[key];
-                                        if ( ! jQuery.isArray( Params[key] ) )
-                                                Params[key] = new Array( oldVal, val );
</del><ins>+                                        var oldVal = params[key];
+                                        if ( ! $.isArray( params[key] ) )
+                                                params[key] = new Array( oldVal, val );
</ins><span class="cx">                                         else
</span><del>-                                                Params[key].push( val );
</del><ins>+                                                params[key].push( val );
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><del>-                        return Params;
</del><ins>+                        return params;
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        Query = function() {
-                this.failedRequest = false;
-                this.querying = false;
-                this.page = inputs.startPage;
-        }
-        
-        $.extend( Query.prototype, {
-                ready: function() {
-                        return !( this.querying || this.failedRequest );
-                },
-                ajax: function( callback ) {
-                        var self = this,
-                        query = {
-                                action: 'fetch-list',
-                                tab: inputs.tab,
-                                paged: this.page,
-                                s: inputs.search,
-                                type: inputs.type,
-                                _ajax_fetch_list_nonce: inputs.nonce,
-                                'features[]': inputs.features,
-                                'list_args': list_args
-                        };
</del><ins>+        $(document).ready( function( $ ) { ThemeScroller.init(); });
</ins><span class="cx"> 
</span><del>-                        this.querying = true;
-                        $.get( ajaxurl, query, function(r) {
-                                self.page++;
-                                self.querying = false;
-                                self.failedRequest = !r;
-                                callback( r, query );
-                        }, &quot;json&quot; );
-                }
-        });
-
-        $(document).ready( wpThemes.init );
-
</del><span class="cx"> })(jQuery);
</span></span></pre>
</div>
</div>

</body>
</html>