Make WordPress Core

Changeset 31562


Ignore:
Timestamp:
02/26/2015 11:00:20 PM (10 years ago)
Author:
wonderboymusic
Message:

Persist search terms across grid/list modes:

  • In grid mode, when the page loads and s is in the URL, all attachments are loaded and then the search value is set, which will filter the attachments. If the page loads with the attachments already filtered, the library will have to be requery'd to get the full set, which will require weirder code.
  • When a user searches, the mode-switcher link for list view is updated dynamically to represent the current location.href in the proper mode= and s= context.

Fixes #30583.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/upload.php

    r31200 r31562  
    2626    wp_enqueue_script( 'media' );
    2727
    28     $vars = wp_edit_attachments_query_vars();
     28    $q = $_GET;
     29    // let JS handle this
     30    unset( $q['s'] );
     31    $vars = wp_edit_attachments_query_vars( $q );
    2932    $ignore = array( 'mode', 'post_type', 'post_status', 'posts_per_page' );
    3033    foreach ( $vars as $key => $value ) {
     
    6871    require_once( ABSPATH . 'wp-admin/admin-header.php' );
    6972    ?>
    70     <div class="wrap" id="wp-media-grid">
     73    <div class="wrap" id="wp-media-grid" data-search="<?php _admin_search_query() ?>">
    7174        <h2>
    7275        <?php
  • trunk/src/wp-includes/js/media/grid.js

    r31494 r31562  
    683683        this.bindRegionModeHandlers();
    684684        this.render();
     685        this.bindSearchHandler();
     686    },
     687
     688    /**
     689     * The views must interact with form controls that are not part of a frame
     690     */
     691    bindSearchHandler: function() {
     692        var search = this.$( '#media-search-input' ),
     693            currentSearch = this.options.container.data( 'search' ),
     694            searchView = this.browserView.toolbar.get( 'search' ).$el,
     695            listMode = this.$( '.view-list' ),
     696
     697            input  = _.debounce( function (e) {
     698                var val = $( e.currentTarget ).val(),
     699                    url = '';
     700
     701                if ( val ) {
     702                    url += '?search=' + val;
     703                }
     704                this.gridRouter.navigate( this.gridRouter.baseUrl( url ) );
     705            }, 1000 );
    685706
    686707        // Update the URL when entering search string (at most once per second)
    687         $( '#media-search-input' ).on( 'input', _.debounce( function(e) {
    688             var val = $( e.currentTarget ).val(), url = '';
    689             if ( val ) {
    690                 url += '?search=' + val;
     708        search.on( 'input', _.bind( input, this ) );
     709        searchView.val( currentSearch ).trigger( 'input' );
     710
     711        this.gridRouter.on( 'route:search', function () {
     712            var href = window.location.href;
     713            if ( href.indexOf( 'mode=' ) > -1 ) {
     714                href = href.replace( /mode=[^&]+/g, 'mode=list' );
     715            } else {
     716                href += href.indexOf( '?' ) > -1 ? '&mode=list' : '?mode=list';
    691717            }
    692             self.gridRouter.navigate( self.gridRouter.baseUrl( url ) );
    693         }, 1000 ) );
     718            href = href.replace( 'search=', 's=' );
     719            listMode.prop( 'href', href );
     720        } );
    694721    },
    695722
  • trunk/src/wp-includes/js/media/views/frame/manage.js

    r31494 r31562  
    8484        this.bindRegionModeHandlers();
    8585        this.render();
     86        this.bindSearchHandler();
     87    },
     88
     89    bindSearchHandler: function() {
     90        var search = this.$( '#media-search-input' ),
     91            currentSearch = this.options.container.data( 'search' ),
     92            searchView = this.browserView.toolbar.get( 'search' ).$el,
     93            listMode = this.$( '.view-list' ),
     94
     95            input  = _.debounce( function (e) {
     96                var val = $( e.currentTarget ).val(),
     97                    url = '';
     98
     99                if ( val ) {
     100                    url += '?search=' + val;
     101                }
     102                this.gridRouter.navigate( this.gridRouter.baseUrl( url ) );
     103            }, 1000 );
    86104
    87105        // Update the URL when entering search string (at most once per second)
    88         $( '#media-search-input' ).on( 'input', _.debounce( function(e) {
    89             var val = $( e.currentTarget ).val(), url = '';
    90             if ( val ) {
    91                 url += '?search=' + val;
     106        search.on( 'input', _.bind( input, this ) );
     107        searchView.val( currentSearch ).trigger( 'input' );
     108
     109        this.gridRouter.on( 'route:search', function () {
     110            var href = window.location.href;
     111            if ( href.indexOf( 'mode=' ) > -1 ) {
     112                href = href.replace( /mode=[^&]+/g, 'mode=list' );
     113            } else {
     114                href += href.indexOf( '?' ) > -1 ? '&mode=list' : '?mode=list';
    92115            }
    93             self.gridRouter.navigate( self.gridRouter.baseUrl( url ) );
    94         }, 1000 ) );
     116            href = href.replace( 'search=', 's=' );
     117            listMode.prop( 'href', href );
     118        } );
    95119    },
    96120
Note: See TracChangeset for help on using the changeset viewer.