| 1 | var wpThemes; |
| 2 | |
| 3 | (function($){ |
| 4 | var inputs = {}, Query; |
| 5 | |
| 6 | wpThemes = { |
| 7 | timeToTriggerQuery: 150, |
| 8 | minQueryAJAXDuration: 200, |
| 9 | outListBottomThreshold: 200, |
| 10 | noMoreResults: false, |
| 11 | |
| 12 | init : function() { |
| 13 | inputs.nonce = $('#_ajax_fetch_list_nonce').val(); |
| 14 | |
| 15 | // Parse Query |
| 16 | inputs.queryString = window.location.search; |
| 17 | inputs.queryArray = wpThemes.parseQuery( inputs.queryString.substring( 1 ) ); |
| 18 | |
| 19 | // Handle Inputs from Query |
| 20 | inputs.search = inputs.queryArray['s']; |
| 21 | inputs.features = inputs.queryArray['features']; |
| 22 | inputs.startPage = parseInt( inputs.queryArray['paged'] ); |
| 23 | inputs.tab = inputs.queryArray['tab']; |
| 24 | inputs.type = inputs.queryArray['type']; |
| 25 | |
| 26 | if ( isNaN( inputs.startPage ) ) |
| 27 | inputs.startPage = 2; |
| 28 | else |
| 29 | inputs.startPage++; |
| 30 | |
| 31 | // FIXME: Debug Features Array |
| 32 | // console.log("Features:" + inputs.features); |
| 33 | |
| 34 | // Link to output and start polling |
| 35 | inputs.outList = $('#availablethemes'); |
| 36 | |
| 37 | // Generate Query |
| 38 | wpThemes.query = new Query(); |
| 39 | |
| 40 | // Start Polling |
| 41 | $(window).scroll( function(){ wpThemes.maybeLoad(); }); |
| 42 | }, |
| 43 | delayedCallback : function( func, delay ) { |
| 44 | var timeoutTriggered, funcTriggered, funcArgs, funcContext; |
| 45 | |
| 46 | if ( ! delay ) |
| 47 | return func; |
| 48 | |
| 49 | setTimeout( function() { |
| 50 | if ( funcTriggered ) |
| 51 | return func.apply( funcContext, funcArgs ); |
| 52 | // Otherwise, wait. |
| 53 | timeoutTriggered = true; |
| 54 | }, delay); |
| 55 | |
| 56 | return function() { |
| 57 | if ( timeoutTriggered ) |
| 58 | return func.apply( this, arguments ); |
| 59 | // Otherwise, wait. |
| 60 | funcArgs = arguments; |
| 61 | funcContext = this; |
| 62 | funcTriggered = true; |
| 63 | }; |
| 64 | }, |
| 65 | ajax: function( callback ) { |
| 66 | var self = this, |
| 67 | response = wpThemes.delayedCallback( function( results, params ) { |
| 68 | self.process( results, params ); |
| 69 | if ( callback ) |
| 70 | callback( results, params ); |
| 71 | }, wpThemes.minQueryAJAXDuration ); |
| 72 | |
| 73 | this.query.ajax( response ); |
| 74 | }, |
| 75 | process: function( results, params ) { |
| 76 | // If no Results, for now, mark as no Matches, and bail. |
| 77 | // Alternately: inputs.outList.append(wpThemesL10n.noMatchesFound); |
| 78 | if ( ( results === undefined ) || |
| 79 | ( results.rows.indexOf( "no-items" ) != -1 ) ) { |
| 80 | this.noMoreResults = true; |
| 81 | } else { |
| 82 | inputs.outList.append(results.rows); |
| 83 | } |
| 84 | }, |
| 85 | maybeLoad: function() { |
| 86 | var self = this, |
| 87 | el = $(document), |
| 88 | bottom = el.scrollTop() + $(window).innerHeight(); |
| 89 | |
| 90 | /* // FIXME: Debug scroll trigger. |
| 91 | console.log('scrollTop:'+ el.scrollTop() + |
| 92 | '; scrollBottom:' + bottom + |
| 93 | '; height:' + el.height() + |
| 94 | '; checkVal:' + (el.height() - wpThemes.outListBottomThreshold)); |
| 95 | */ |
| 96 | |
| 97 | if ( this.noMoreResults || |
| 98 | !this.query.ready() || |
| 99 | ( bottom < inputs.outList.height() - wpThemes.outListBottomThreshold ) ) |
| 100 | return; |
| 101 | |
| 102 | setTimeout( function() { |
| 103 | var newTop = el.scrollTop(), |
| 104 | newBottom = newTop + $(window).innerHeight(); |
| 105 | |
| 106 | if ( !self.query.ready() || |
| 107 | ( newBottom < inputs.outList.height() - wpThemes.outListBottomThreshold ) ) |
| 108 | return; |
| 109 | |
| 110 | /* FIXME: Create/Add Spinner. |
| 111 | self.waiting.show(); // Show Spinner |
| 112 | el.scrollTop( newTop + self.waiting.outerHeight() ); // Scroll down? |
| 113 | self.ajax( function() { self.waiting.hide(); }); // Hide Spinner |
| 114 | */ |
| 115 | self.ajax(); |
| 116 | }, wpThemes.timeToTriggerQuery ); |
| 117 | }, |
| 118 | parseQuery: function( query ) { |
| 119 | var Params = {}; |
| 120 | if ( ! query ) {return Params;}// return empty object |
| 121 | var Pairs = query.split(/[;&]/); |
| 122 | for ( var i = 0; i < Pairs.length; i++ ) { |
| 123 | var KeyVal = Pairs[i].split('='); |
| 124 | if ( ! KeyVal || KeyVal.length != 2 ) {continue;} |
| 125 | var key = unescape( KeyVal[0] ); |
| 126 | var val = unescape( KeyVal[1] ); |
| 127 | val = val.replace(/\+/g, ' '); |
| 128 | key = key.replace(/\[.*\]$/g, ''); |
| 129 | |
| 130 | if ( Params[key] === undefined ) { |
| 131 | Params[key] = val; |
| 132 | } else { |
| 133 | var oldVal = Params[key]; |
| 134 | if ( ! jQuery.isArray( Params[key] ) ) |
| 135 | Params[key] = new Array( oldVal, val ); |
| 136 | else |
| 137 | Params[key].push( val ); |
| 138 | } |
| 139 | } |
| 140 | return Params; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | Query = function() { |
| 145 | this.failedRequest = false; |
| 146 | this.querying = false; |
| 147 | this.page = inputs.startPage; |
| 148 | } |
| 149 | |
| 150 | $.extend( Query.prototype, { |
| 151 | ready: function() { |
| 152 | return !( this.querying || this.failedRequest ); |
| 153 | }, |
| 154 | ajax: function( callback ) { |
| 155 | var self = this, |
| 156 | query = { |
| 157 | action: 'fetch-list', |
| 158 | paged: this.page, |
| 159 | s: inputs.search, |
| 160 | 'features[]': inputs.features, |
| 161 | 'list_args': list_args, |
| 162 | 'tab': inputs.tab, |
| 163 | 'type': inputs.type, |
| 164 | '_ajax_fetch_list_nonce': inputs.nonce |
| 165 | }; |
| 166 | |
| 167 | this.querying = true; |
| 168 | $.get( ajaxurl, query, function(r) { |
| 169 | self.page++; |
| 170 | self.querying = false; |
| 171 | self.failedRequest = !r; |
| 172 | callback( r, query ); |
| 173 | }, "json" ); |
| 174 | } |
| 175 | }); |
| 176 | |
| 177 | $(document).ready( wpThemes.init ); |
| 178 | |
| 179 | })(jQuery); |