| 56 | |
| 57 | var wpThemes; |
| 58 | |
| 59 | (function($){ |
| 60 | var inputs = {}, Query; |
| 61 | |
| 62 | wpThemes = { |
| 63 | timeToTriggerQuery: 150, |
| 64 | minQueryAJAXDuration: 200, |
| 65 | outListBottomThreshold: 200, |
| 66 | noMoreResults: false, |
| 67 | |
| 68 | init : function() { |
| 69 | $( '.pagination-links' ).hide(); |
| 70 | |
| 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 | // FIXME: Debug Features Array |
| 90 | // console.log("Features:" + inputs.features); |
| 91 | |
| 92 | // Link to output and start polling |
| 93 | inputs.outList = $('#availablethemes'); |
| 94 | |
| 95 | // Generate Query |
| 96 | wpThemes.query = new Query(); |
| 97 | |
| 98 | // Start Polling |
| 99 | $(window).scroll( function(){ wpThemes.maybeLoad(); }); |
| 100 | }, |
| 101 | delayedCallback : function( func, delay ) { |
| 102 | var timeoutTriggered, funcTriggered, funcArgs, funcContext; |
| 103 | |
| 104 | if ( ! delay ) |
| 105 | return func; |
| 106 | |
| 107 | setTimeout( function() { |
| 108 | if ( funcTriggered ) |
| 109 | return func.apply( funcContext, funcArgs ); |
| 110 | // Otherwise, wait. |
| 111 | timeoutTriggered = true; |
| 112 | }, delay); |
| 113 | |
| 114 | return function() { |
| 115 | if ( timeoutTriggered ) |
| 116 | return func.apply( this, arguments ); |
| 117 | // Otherwise, wait. |
| 118 | funcArgs = arguments; |
| 119 | funcContext = this; |
| 120 | funcTriggered = true; |
| 121 | }; |
| 122 | }, |
| 123 | ajax: function( callback ) { |
| 124 | var self = this, |
| 125 | response = wpThemes.delayedCallback( function( results, params ) { |
| 126 | self.process( results, params ); |
| 127 | if ( callback ) |
| 128 | callback( results, params ); |
| 129 | }, wpThemes.minQueryAJAXDuration ); |
| 130 | |
| 131 | this.query.ajax( response ); |
| 132 | }, |
| 133 | process: function( results, params ) { |
| 134 | // If no Results, for now, mark as no Matches, and bail. |
| 135 | // Alternately: inputs.outList.append(wpThemesL10n.noMatchesFound); |
| 136 | if ( ( results === undefined ) || |
| 137 | ( results.rows.indexOf( "no-items" ) != -1 ) ) { |
| 138 | this.noMoreResults = true; |
| 139 | } else { |
| 140 | inputs.outList.append(results.rows); |
| 141 | } |
| 142 | }, |
| 143 | maybeLoad: function() { |
| 144 | var self = this, |
| 145 | el = $(document), |
| 146 | bottom = el.scrollTop() + $(window).innerHeight(); |
| 147 | |
| 148 | /* // FIXME: Debug scroll trigger. |
| 149 | console.log('scrollTop:'+ el.scrollTop() + |
| 150 | '; scrollBottom:' + bottom + |
| 151 | '; height:' + el.height() + |
| 152 | '; checkVal:' + (el.height() - wpThemes.outListBottomThreshold)); |
| 153 | */ |
| 154 | |
| 155 | if ( this.noMoreResults || |
| 156 | !this.query.ready() || |
| 157 | ( bottom < inputs.outList.height() - wpThemes.outListBottomThreshold ) ) |
| 158 | return; |
| 159 | |
| 160 | setTimeout( function() { |
| 161 | var newTop = el.scrollTop(), |
| 162 | newBottom = newTop + $(window).innerHeight(); |
| 163 | |
| 164 | if ( !self.query.ready() || |
| 165 | ( newBottom < inputs.outList.height() - wpThemes.outListBottomThreshold ) ) |
| 166 | return; |
| 167 | |
| 168 | /* FIXME: Create/Add Spinner. |
| 169 | self.waiting.show(); // Show Spinner |
| 170 | el.scrollTop( newTop + self.waiting.outerHeight() ); // Scroll down? |
| 171 | self.ajax( function() { self.waiting.hide(); }); // Hide Spinner |
| 172 | */ |
| 173 | self.ajax(); |
| 174 | }, wpThemes.timeToTriggerQuery ); |
| 175 | }, |
| 176 | parseQuery: function( query ) { |
| 177 | var Params = {}; |
| 178 | if ( ! query ) {return Params;}// return empty object |
| 179 | var Pairs = query.split(/[;&]/); |
| 180 | for ( var i = 0; i < Pairs.length; i++ ) { |
| 181 | var KeyVal = Pairs[i].split('='); |
| 182 | if ( ! KeyVal || KeyVal.length != 2 ) {continue;} |
| 183 | var key = unescape( KeyVal[0] ); |
| 184 | var val = unescape( KeyVal[1] ); |
| 185 | val = val.replace(/\+/g, ' '); |
| 186 | key = key.replace(/\[.*\]$/g, ''); |
| 187 | |
| 188 | if ( Params[key] === undefined ) { |
| 189 | Params[key] = val; |
| 190 | } else { |
| 191 | var oldVal = Params[key]; |
| 192 | if ( ! jQuery.isArray( Params[key] ) ) |
| 193 | Params[key] = new Array( oldVal, val ); |
| 194 | else |
| 195 | Params[key].push( val ); |
| 196 | } |
| 197 | } |
| 198 | return Params; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | Query = function() { |
| 203 | this.failedRequest = false; |
| 204 | this.querying = false; |
| 205 | this.page = inputs.startPage; |
| 206 | } |
| 207 | |
| 208 | $.extend( Query.prototype, { |
| 209 | ready: function() { |
| 210 | return !( this.querying || this.failedRequest ); |
| 211 | }, |
| 212 | ajax: function( callback ) { |
| 213 | var self = this, |
| 214 | query = { |
| 215 | action: 'fetch-list', |
| 216 | paged: this.page, |
| 217 | s: inputs.search, |
| 218 | 'features[]': inputs.features, |
| 219 | 'list_args': list_args, |
| 220 | 'tab': inputs.tab, |
| 221 | 'type': inputs.type, |
| 222 | '_ajax_fetch_list_nonce': inputs.nonce |
| 223 | }; |
| 224 | |
| 225 | this.querying = true; |
| 226 | $.get( ajaxurl, query, function(r) { |
| 227 | self.page++; |
| 228 | self.querying = false; |
| 229 | self.failedRequest = !r; |
| 230 | callback( r, query ); |
| 231 | }, "json" ); |
| 232 | } |
| 233 | }); |
| 234 | |
| 235 | $(document).ready( wpThemes.init ); |
| 236 | |
| 237 | })(jQuery); |