Make WordPress Core

Ticket #19815: 19815.6.diff

File 19815.6.diff, 7.0 KB (added by mikeschroder, 12 years ago)

Spinner should function, added additional cleanup and caching window selector.

  • wp-admin/includes/class-wp-theme-install-list-table.php

     
    99 */
    1010class WP_Theme_Install_List_Table extends WP_List_Table {
    1111
     12        function __construct() {
     13                parent::__construct( array(
     14                        'ajax' => true,
     15                ) );
     16        }
     17
    1218        function ajax_user_can() {
    1319                return current_user_can('install_themes');
    1420        }
     
    128134
    129135        function display() {
    130136
    131                 // wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
     137                wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
    132138?>
    133139                <div class="tablenav top themes">
    134140                        <div class="alignleft actions">
  • wp-admin/includes/class-wp-themes-list-table.php

     
    1111
    1212        var $search = array();
    1313        var $features = array();
     14       
     15        function __construct() {
     16                parent::__construct( array(
     17                        'ajax' => true,
     18                ) );
     19        }
    1420
    1521        function ajax_user_can() {
    1622                // Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
     
    4753                unset( $themes[$ct->name] );
    4854                uksort( $themes, "strnatcasecmp" );
    4955
    50                 $per_page = 24;
     56                $per_page = 999;
    5157                $page = $this->get_pagenum();
    5258
    5359                $start = ( $page - 1 ) * $per_page;
     
    101107        }
    102108
    103109        function display() {
    104                 // wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
     110                wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
    105111?>
    106112                <?php $this->tablenav( 'top' ); ?>
    107113
  • wp-admin/js/theme.dev.js

     
    5353        theme_viewer = new ThemeViewer();
    5454        theme_viewer.init();
    5555});
     56
     57var 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                        // 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                        };
     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);
     135                        if ( ( results === undefined ) ||
     136                                 ( results.rows.indexOf( "no-items" ) != -1 ) ) {
     137                                this.noMoreResults = true;
     138                        } else {
     139                                inputs.outList.append( results.rows );
     140                        }
     141                },
     142                maybeLoad: function() {
     143                        var self = this,
     144                                el = $(document),
     145                                bottom = el.scrollTop() + inputs.window.innerHeight();
     146                               
     147                        if ( this.noMoreResults ||
     148                                 !this.query.ready() ||
     149                                 ( bottom < inputs.outList.height() - wpThemes.outListBottomThreshold ) )
     150                                return;
     151
     152                        setTimeout( function() {
     153                                var newTop = el.scrollTop(),
     154                                        newBottom = newTop + inputs.window.innerHeight();
     155
     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 );
     164                },
     165                parseQuery: function( query ) {
     166                        var Params = {};
     167                        if ( ! query ) {return Params;}// return empty object
     168                        var Pairs = query.split(/[;&]/);
     169                        for ( var i = 0; i < Pairs.length; i++ ) {
     170                                var KeyVal = Pairs[i].split('=');
     171                                if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
     172                                var key = unescape( KeyVal[0] );
     173                                var val = unescape( KeyVal[1] );
     174                                val = val.replace(/\+/g, ' ');
     175                                key = key.replace(/\[.*\]$/g, '');
     176       
     177                                if ( Params[key] === undefined ) {
     178                                        Params[key] = val;
     179                                } else {
     180                                        var oldVal = Params[key];
     181                                        if ( ! jQuery.isArray( Params[key] ) )
     182                                                Params[key] = new Array( oldVal, val );
     183                                        else
     184                                                Params[key].push( val );
     185                                }
     186                        }
     187                        return Params;
     188                }
     189        }
     190
     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 );
     225
     226})(jQuery);
  • wp-admin/theme-install.php

     
    3333
    3434add_thickbox();
    3535wp_enqueue_script( 'theme-preview' );
     36wp_enqueue_script( 'theme' );
    3637
    3738$body_id = $tab;
    3839