| 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 ) ); |
| 76 | | |
| 77 | | // Handle Inputs from Query |
| 78 | | 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']; |
| 83 | | |
| 84 | | if ( isNaN( inputs.startPage ) ) |
| 85 | | inputs.startPage = 2; |
| 86 | | else |
| 87 | | inputs.startPage++; |
| 88 | | |
| 89 | | // Cache jQuery objects |
| 90 | | inputs.outList = $('#availablethemes'); |
| 91 | | inputs.waiting = $('div.tablenav.bottom').children( 'img.ajax-loading' ); |
| 92 | | inputs.window = $(window); |
| 93 | | |
| 94 | | // Generate Query |
| 95 | | wpThemes.query = new Query(); |
| 96 | | |
| 97 | | // Start Polling |
| 98 | | inputs.window.scroll( function(){ wpThemes.maybeLoad(); } ); |
| 99 | | }, |
| 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 | | }; |
| | 88 | // Parse GET query string |
| | 89 | queryArray = this.parseQuery( queryString.substring( 1 ) ); |
| | 90 | |
| | 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']; |
| | 97 | |
| | 98 | startPage = parseInt( queryArray['paged'] ); |
| | 99 | if ( !isNaN( startPage ) ) |
| | 100 | this.nextPage++; |
| | 101 | |
| | 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); |
| | 107 | |
| | 108 | setInterval(this.poll, this.scrollThrottleDelay); |
| 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 ); |
| | 110 | poll : function() { |
| | 111 | var t = ThemeScroller, |
| | 112 | bottom = t.$document.scrollTop() + t.$window.innerHeight(); |
| | 113 | |
| | 114 | if ( t.noMoreResults || |
| | 115 | t.querying || |
| | 116 | ( bottom < t.$outList.height() - t.outListBottomThreshold ) ) |
| | 117 | return; |
| 160 | | inputs.waiting.css( 'visibility', 'visible' ); // Show Spinner |
| 161 | | self.ajax( function() { inputs.waiting.css( 'visibility', 'hidden' ) } ); // Hide Spinner |
| 162 | | |
| 163 | | }, wpThemes.timeToTriggerQuery ); |
| | 144 | this.$spinner.css( 'visibility', 'visible' ); |
| | 145 | $.getJSON( ajaxurl, query ) |
| | 146 | .done( function( response ) { |
| | 147 | self.nextPage++; |
| | 148 | self.process( response ); |
| | 149 | self.$spinner.css( 'visibility', 'hidden' ); |
| | 150 | self.querying = false; |
| | 151 | }) |
| | 152 | .fail( function() { |
| | 153 | self.$spinner.css( 'visibility', 'hidden' ); |
| | 154 | self.querying = false; |
| | 155 | setTimeout( function() { self.maybeLoad(); }, self.failedRetryDelay) |
| | 156 | }); |
| 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 | | }; |
| 213 | | |
| 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 ); |
| | 184 | $(document).ready( function($) { ThemeScroller.init(); }); |