Make WordPress Core

Ticket #37233: 37233.comment:25.1.patch

File 37233.comment:25.1.patch, 3.6 KB (added by rahulsprajapati, 9 years ago)

Handling the back button using "popstate" and trigger the "input" event for search text box.

  • src/wp-admin/js/updates.js

    diff --git a/src/wp-admin/js/updates.js b/src/wp-admin/js/updates.js
    index e1d963a..c0a36da 100644
    a b  
    20332033                 * @since 4.6.0
    20342034                 */
    20352035                $pluginInstallSearch.on( 'keyup input', _.debounce( function( event, eventtype ) {
    2036                         var $searchTab = $( '.plugin-install-search' ), data, searchLocation;
     2036                        var $searchTab = $( '.plugin-install-search' ), data, searchLocation, currentLocation = location.href, urlSearchString = wp.urlParam( 's', currentLocation );
    20372037
    20382038                        data = {
    20392039                                _ajax_nonce: wp.updates.ajaxNonce,
     
    20422042                                type:        $( '#typeselector' ).val(),
    20432043                                pagenow:     pagenow
    20442044                        };
    2045                         searchLocation = location.href.split( '?' )[ 0 ] + '?' + $.param( _.omit( data, [ '_ajax_nonce', 'pagenow' ] ) );
     2045                        searchLocation = currentLocation.split( '?' )[ 0 ] + '?' + $.param( _.omit( data, [ '_ajax_nonce', 'pagenow' ] ) );
    20462046
    20472047                        // Clear on escape.
    20482048                        if ( 'keyup' === event.type && 27 === event.which ) {
     
    20562056                                wp.updates.searchTerm = data.s;
    20572057                        }
    20582058
    2059                         if ( window.history && window.history.pushState ) {
     2059
     2060                        if ( urlSearchString != event.target.value && window.history && window.history.pushState ) {
    20602061                                window.history.pushState( null, '', searchLocation );
    20612062                        }
    20622063
     
    21062107                 * @since 4.6.0
    21072108                 */
    21082109                $pluginSearch.on( 'keyup input', _.debounce( function( event ) {
     2110                        var currentLocation = location.href, urlSearchString = wp.urlParam( 's', currentLocation );
    21092111                        var data = {
    21102112                                _ajax_nonce: wp.updates.ajaxNonce,
    21112113                                s:           event.target.value,
     
    21232125                                wp.updates.searchTerm = data.s;
    21242126                        }
    21252127
    2126                         if ( window.history && window.history.pushState ) {
     2128                        if ( urlSearchString != event.target.value &&  window.history && window.history.pushState ) {
    21272129                                window.history.pushState( null, '', location.href.split( '?' )[ 0 ] + '?s=' + data.s );
    21282130                        }
    21292131
     
    22982300                 * @since 4.2.0
    22992301                 */
    23002302                $( window ).on( 'beforeunload', wp.updates.beforeunload );
     2303
     2304                /**
     2305                 * Window popstate for search plugins
     2306                 */
     2307                $( window ).on( 'popstate', function ( event ) {
     2308
     2309                        var bodyElem = $( 'body' ), currentLocation = location.href, urlSearchString = wp.urlParam( 's', currentLocation );
     2310
     2311                        if ( bodyElem.hasClass( "plugins-php" ) ) {
     2312                                $pluginSearch.val( urlSearchString );
     2313                                $pluginSearch.trigger( 'input' );
     2314                        }
     2315
     2316                        if ( bodyElem.hasClass( "plugin-install-php" ) ) {
     2317                                $pluginInstallSearch.val( urlSearchString );
     2318                                $pluginInstallSearch.trigger( 'input' );
     2319                        }
     2320                } );
    23012321        } );
    23022322})( jQuery, window.wp, _.extend( window._wpUpdatesSettings, window._wpUpdatesItemCounts || {} ) );
  • src/wp-includes/js/wp-util.js

    diff --git a/src/wp-includes/js/wp-util.js b/src/wp-includes/js/wp-util.js
    index 527441d..d90d615 100644
    a b window.wp = window.wp || {}; 
    3535                };
    3636        });
    3737
     38        /**
     39         * wp.urlParam( name, url )
     40         *
     41         * Fetch parameters from given URL
     42         *
     43         * @param  {string} name   A parameter name to fetch value from url.
     44         *                       For example, "param1" for example.com?param1=value1&param2=value2
     45         *
     46         * @param  {string} url   Url to fetch parameters
     47         *                       For example, example.com?param1=value1&param2=value2
     48         *
     49         * @return {string}    Return value of given parameter name from url
     50         */
     51        wp.urlParam = function ( name, url ) {
     52                var results = new RegExp( '[\?&]' + name + '=([^&#]*)' ).exec( url );
     53                if ( 'undefined' === typeof results || null == results ) {
     54                        return "";
     55                }
     56                return results[1] || "";
     57        };
     58
    3859        // wp.ajax
    3960        // ------
    4061        //