Ticket #19815: 19815.8.diff
File 19815.8.diff, 7.3 KB (added by , 13 years ago) |
---|
-
wp-admin/js/theme.dev.js
45 45 init: init 46 46 }; 47 47 48 48 return api; 49 49 } 50 50 })(jQuery); 51 51 … … 54 54 theme_viewer.init(); 55 55 }); 56 56 57 var wpThemes;57 var ThemeScroller; 58 58 59 59 (function($){ 60 var inputs = {}, Query; 60 ThemeScroller = { 61 // Inputs 62 nonce: '', 63 search: '', 64 tab: '', 65 type: '', 66 nextPage: 2, 67 features: {}, 61 68 62 wpThemes = {63 timeToTriggerQuery: 150,64 minQueryAJAXDuration: 200,69 // Preferences 70 scrollThrottleDelay: 250, 71 failedRetryDelay: 4000, 65 72 outListBottomThreshold: 200, 73 74 // Flags 66 75 noMoreResults: false, 67 76 scrolling: false, 77 querying: false, 78 68 79 init : function() { 80 var self = this, 81 startPage, 82 queryArray = {}, 83 queryString = window.location.search; 84 85 // We're using infinite scrolling, so hide all pagination. 69 86 $('.pagination-links').hide(); 70 87 71 inputs.nonce = $('#_ajax_fetch_list_nonce').val(); 72 73 // Parse Query 74 inputs.queryString = window.location.search; 75 inputs.queryArray = wpThemes.parseQuery( inputs.queryString.substring( 1 ) ); 88 // Parse GET query string 89 queryArray = this.parseQuery( queryString.substring( 1 ) ); 76 90 77 // Handle Inputs from Query78 inputs.search = inputs.queryArray['s'];79 inputs.features = inputs.queryArray['features'];80 inputs.startPage = parseInt( inputs.queryArray['paged'] );81 inputs.tab = inputs.queryArray['tab'];82 inputs.type = inputs.queryArray['type'];91 // Handle inputs 92 this.nonce = $('#_ajax_fetch_list_nonce').val(); 93 this.search = queryArray['s']; 94 this.features = queryArray['features']; 95 this.tab = queryArray['tab']; 96 this.type = queryArray['type']; 83 97 84 if ( isNaN( inputs.startPage ) ) 85 inputs.startPage = 2; 86 else 87 inputs.startPage++; 98 startPage = parseInt( queryArray['paged'] ); 99 if ( !isNaN( startPage ) ) 100 this.nextPage++; 88 101 89 // Cache jQuery objects 90 inputs.outList = $('#availablethemes'); 91 inputs.waiting = $('div.tablenav.bottom').children( 'img.ajax-loading' ); 92 inputs.window = $(window); 102 // Cache jQuery selectors 103 this.$outList = $('#availablethemes'); 104 this.$spinner = $('div.tablenav.bottom').children( 'img.ajax-loading' ); 105 this.$window = $(window); 106 this.$document = $(document); 93 107 94 // Generate Query95 wpThemes.query = new Query();96 97 108 // Start Polling 98 inputs.window.scroll( function(){ wpThemes.maybeLoad(); } ); 109 this.$window.scroll( function(){ self.scrolling = true; }); 110 setInterval( function() { 111 if ( self.scrolling ) { 112 self.scrolling = false; 113 self.maybeLoad(); 114 } 115 }, this.scrollThrottleDelay ); 99 116 }, 100 delayedCallback : function( func, delay ) { 101 var timeoutTriggered, funcTriggered, funcArgs, funcContext; 102 103 if ( ! delay ) 104 return func; 105 106 setTimeout( function() { 107 if ( funcTriggered ) 108 return func.apply( funcContext, funcArgs ); 109 // Otherwise, wait. 110 timeoutTriggered = true; 111 }, delay); 112 113 return function() { 114 if ( timeoutTriggered ) 115 return func.apply( this, arguments ); 116 // Otherwise, wait. 117 funcArgs = arguments; 118 funcContext = this; 119 funcTriggered = true; 120 }; 121 }, 122 ajax: function( callback ) { 123 var self = this, 124 response = wpThemes.delayedCallback( function( results, params ) { 125 self.process( results, params ); 126 if ( callback ) 127 callback( results, params ); 128 }, wpThemes.minQueryAJAXDuration ); 129 130 this.query.ajax( response ); 131 }, 132 process: function( results, params ) { 133 // If no Results, for now, mark as no Matches, and bail. 134 // Alternately: inputs.outList.append(wpThemesL10n.noMatchesFound); 117 process: function( results ) { 135 118 if ( ( results === undefined ) || 136 119 ( results.rows.indexOf( "no-items" ) != -1 ) ) { 137 120 this.noMoreResults = true; 138 121 } else { 139 inputs.outList.append( results.rows );122 this.$outList.append( results.rows ); 140 123 } 141 124 }, 142 125 maybeLoad: function() { 143 126 var self = this, 144 el = $(document), 145 bottom = el.scrollTop() + inputs.window.innerHeight(); 146 127 bottom = this.$document.scrollTop() + this.$window.innerHeight(); 128 147 129 if ( this.noMoreResults || 148 !this.query.ready()||149 ( bottom < inputs.outList.height() - wpThemes.outListBottomThreshold ) )130 this.querying || 131 ( bottom < this.$outList.height() - this.outListBottomThreshold ) ) 150 132 return; 151 133 152 setTimeout( function() { 153 var newTop = el.scrollTop(), 154 newBottom = newTop + inputs.window.innerHeight(); 134 this.querying = true; 135 var query = { 136 action: 'fetch-list', 137 tab: this.tab, 138 paged: this.nextPage, 139 s: this.search, 140 type: this.type, 141 _ajax_fetch_list_nonce: this.nonce, 142 'features[]': this.features, 143 'list_args': list_args 144 }; 155 145 156 if ( !self.query.ready() || 157 ( newBottom < inputs.outList.height() - wpThemes.outListBottomThreshold ) ) 158 return; 159 160 inputs.waiting.css( 'visibility', 'visible' ); // Show Spinner 161 self.ajax( function() { inputs.waiting.css( 'visibility', 'hidden' ) } ); // Hide Spinner 162 163 }, wpThemes.timeToTriggerQuery ); 146 this.$spinner.css( 'visibility', 'visible' ); 147 $.getJSON( ajaxurl, query ) 148 .done( function( response ) { 149 self.nextPage++; 150 self.process( response ); 151 self.$spinner.css( 'visibility', 'hidden' ); 152 self.querying = false; 153 }) 154 .fail( function() { 155 self.$spinner.css( 'visibility', 'hidden' ); 156 self.querying = false; 157 setTimeout( function() { self.maybeLoad(); }, self.failedRetryDelay) 158 }); 164 159 }, 165 160 parseQuery: function( query ) { 166 161 var Params = {}; 167 if ( ! query ) {return Params;}// return empty object162 if ( !query ) { return Params; } 168 163 var Pairs = query.split(/[;&]/); 169 164 for ( var i = 0; i < Pairs.length; i++ ) { 170 165 var KeyVal = Pairs[i].split('='); 171 if ( ! KeyVal || KeyVal.length != 2 ) { continue;}166 if ( ! KeyVal || KeyVal.length != 2 ) { continue; } 172 167 var key = unescape( KeyVal[0] ); 173 168 var val = unescape( KeyVal[1] ); 174 169 val = val.replace(/\+/g, ' '); 175 170 key = key.replace(/\[.*\]$/g, ''); 176 171 177 172 if ( Params[key] === undefined ) { 178 173 Params[key] = val; 179 174 } else { 180 175 var oldVal = Params[key]; 181 if ( ! 176 if ( !jQuery.isArray( Params[key] ) ) 182 177 Params[key] = new Array( oldVal, val ); 183 178 else 184 179 Params[key].push( val ); … … 188 183 } 189 184 } 190 185 191 Query = function() { 192 this.failedRequest = false; 193 this.querying = false; 194 this.page = inputs.startPage; 195 } 196 197 $.extend( Query.prototype, { 198 ready: function() { 199 return !( this.querying || this.failedRequest ); 200 }, 201 ajax: function( callback ) { 202 var self = this, 203 query = { 204 action: 'fetch-list', 205 tab: inputs.tab, 206 paged: this.page, 207 s: inputs.search, 208 type: inputs.type, 209 _ajax_fetch_list_nonce: inputs.nonce, 210 'features[]': inputs.features, 211 'list_args': list_args 212 }; 186 $(document).ready( function($) { ThemeScroller.init(); }); 213 187 214 this.querying = true;215 $.get( ajaxurl, query, function(r) {216 self.page++;217 self.querying = false;218 self.failedRequest = !r;219 callback( r, query );220 }, "json" );221 }222 });223 224 $(document).ready( wpThemes.init );225 226 188 })(jQuery);