Making WordPress.org

Changeset 2780


Ignore:
Timestamp:
03/22/2016 07:37:41 PM (8 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Remove duplicate autocomplete items and add focus back to searchfield after clicking autocomplete item.

Props keesiemeijer.
Fixes #542.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/autocomplete.php

    r2359 r2780  
    6666        check_ajax_referer( 'autocomplete_nonce', 'nonce' );
    6767
     68        $parser_post_types = DevHub\get_parsed_post_types();
     69        $defaults          = array(
     70            's'         => '',
     71            'post_type' => $parser_post_types,
     72            'posts'     => array(),
     73        );
     74
    6875        if ( !( isset( $_POST['data'] ) && $_POST['data'] ) ) {
    69             wp_send_json_error();
     76            wp_send_json_error( $defaults );
    7077        }
    7178
    7279        // Parse the search form fields.
    7380        wp_parse_str( $_POST['data'], $form_data );
     81        $form_data = array_merge( $defaults, $form_data );
    7482
    75         if ( !isset( $form_data['post_type'] ) || !isset( $form_data['s'] ) ) {
    76             wp_send_json_error();
     83        // No search query.
     84        if ( empty( $form_data['s'] ) ) {
     85            wp_send_json_error( $defaults );
    7786        }
    7887
    79         $post_types        = array();
    80         $parser_post_types = DevHub\get_parsed_post_types();
    81 
    82         foreach ( $form_data['post_type'] as $post_type ) {
    83             if ( in_array( $post_type , $parser_post_types ) ) {
    84                 $post_types[] = $post_type;
     88        foreach ( $form_data['post_type'] as $key => $post_type ) {
     89            if ( !in_array( $post_type , $parser_post_types ) ) {
     90                unset( $form_data['post_type'][ $key ] );
    8591            }
    8692        }
     93
     94        $post_types = !empty( $form_data['post_type'] ) ? $form_data['post_type'] : $parser_post_types;
    8795
    8896        $args = array(
     
    97105        $search = get_posts( $args );
    98106
    99         $form_data['posts'] = array();
    100107        if ( !empty( $search ) ) {
    101             $form_data['posts'] = wp_list_pluck( $search, 'post_title' );
     108            $titles = wp_list_pluck( $search, 'post_title' );
     109            $form_data['posts'] = array_values( array_unique( $titles ) );
    102110        }
    103111
     
    105113    }
    106114
    107 
    108115}
    109116
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/autocomplete.js

    r2359 r2780  
    1919    var awesome = new Awesomplete( searchfield.get( 0 ), {
    2020        maxItems: 9999,
     21        minChars: 3,
    2122        filter: function( text, input ) {
    2223            // Filter autocomplete matches
     
    5758
    5859            return false;
    59         }
     60        },
    6061    } );
    6162
     
    6667        //     if there are more than 2 characters
    6768        //     and it's not already processing an Ajax request
    68         if ( !processing && $( this ).val().length > 2 ) {
     69        if ( !processing && $( this ).val().trim().length > 2 ) {
    6970            search = $( this ).val();
    7071            autocomplete_update();
     
    9394                }
    9495
    95                 if ( typeof response.data.posts === 'undefined' ) {
     96                if ( typeof response.data === 'undefined' ) {
    9697                    return false;
    9798                }
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/awesomplete.min.js

    r2359 r2780  
    33"off");this.input.setAttribute("aria-autocomplete","list");b=b||{};m.call(this,{minChars:2,maxItems:10,autoFirst:!1,filter:f.FILTER_CONTAINS,sort:f.SORT_BYLENGTH,item:function(a,b){var c=""===b?a:a.replace(RegExp(d.regExpEscape(b.trim()),"gi"),"<mark>$&</mark>");return d.create("li",{innerHTML:c,"aria-selected":"false"})},replace:function(a){this.input.value=a}},b);this.index=-1;this.container=d.create("div",{className:"awesomplete",around:a});this.ul=d.create("ul",{hidden:"hidden",inside:this.container});
    44this.status=d.create("span",{className:"visually-hidden",role:"status","aria-live":"assertive","aria-relevant":"additions",inside:this.container});d.bind(this.input,{input:this.evaluate.bind(this),blur:this.close.bind(this),keydown:function(a){var b=a.keyCode;if(c.opened)if(13===b&&c.selected)a.preventDefault(),c.select();else if(27===b)c.close();else if(38===b||40===b)a.preventDefault(),c[38===b?"previous":"next"]()}});d.bind(this.input.form,{submit:this.close.bind(this)});d.bind(this.ul,{mousedown:function(a){var b=
    5 a.target;if(b!==this){for(;b&&!/li/i.test(b.nodeName);)b=b.parentNode;b&&0===a.button&&c.select(b,a)}}});this.input.hasAttribute("list")?(this.list="#"+this.input.getAttribute("list"),this.input.removeAttribute("list")):this.list=this.input.getAttribute("data-list")||b.list||[];f.all.push(this)};f.prototype={set list(a){Array.isArray(a)?this._list=a:"string"===typeof a&&-1<a.indexOf(",")?this._list=a.split(/\s*,\s*/):(a=d(a))&&a.children&&(this._list=k.apply(a.children).map(function(a){return a.textContent.trim()}));
     5a.target;if(b!==this){for(;b&&!/li/i.test(b.nodeName);)b=b.parentNode;b&&0===a.button&&(a.preventDefault(),c.select(b,a))}}});this.input.hasAttribute("list")?(this.list="#"+this.input.getAttribute("list"),this.input.removeAttribute("list")):this.list=this.input.getAttribute("data-list")||b.list||[];f.all.push(this)};f.prototype={set list(a){Array.isArray(a)?this._list=a:"string"===typeof a&&-1<a.indexOf(",")?this._list=a.split(/\s*,\s*/):(a=d(a))&&a.children&&(this._list=k.apply(a.children).map(function(a){return a.textContent.trim()}));
    66document.activeElement===this.input&&this.evaluate()},get selected(){return-1<this.index},get opened(){return this.ul&&null==this.ul.getAttribute("hidden")},close:function(){this.ul.setAttribute("hidden","");this.index=-1;d.fire(this.input,"awesomplete-close")},open:function(){this.ul.removeAttribute("hidden");this.autoFirst&&-1===this.index&&this["goto"](0);d.fire(this.input,"awesomplete-open")},next:function(){this["goto"](this.index<this.ul.children.length-1?this.index+1:-1)},previous:function(){var a=
    77this.ul.children.length;this["goto"](this.selected?this.index-1:a-1)},"goto":function(a){var b=this.ul.children;this.selected&&b[this.index].setAttribute("aria-selected","false");this.index=a;-1<a&&0<b.length&&(b[a].setAttribute("aria-selected","true"),this.status.textContent=b[a].textContent);d.fire(this.input,"awesomplete-highlight")},select:function(a,b){if(a=a||this.ul.children[this.index]){var c;d.fire(this.input,"awesomplete-select",{text:a.textContent,preventDefault:function(){c=!0},originalEvent:b});
Note: See TracChangeset for help on using the changeset viewer.