Making WordPress.org

Ticket #542: 542.3.patch

File 542.3.patch, 7.9 KB (added by keesiemeijer, 8 years ago)

Remove duplicate autocomplete items and add focus back to searchfield after clicking autocomplete item

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

     
    6565
    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                }
    8793
     94                $post_types = !empty( $form_data['post_type'] ) ? $form_data['post_type'] : $parser_post_types;
     95
    8896                $args = array(
    8997                        'posts_per_page'       => -1,
    9098                        'post_type'            => $post_types,
     
    96104
    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
    104112                wp_send_json_success ( $form_data );
    105113        }
    106114
    107 
    108115}
    109116
    110 $autocomplete = new DevHub_Search_Form_Autocomplete();
    111  No newline at end of file
     117$autocomplete = new DevHub_Search_Form_Autocomplete();
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/autocomplete.js

     
    1818
    1919        var awesome = new Awesomplete( searchfield.get( 0 ), {
    2020                maxItems: 9999,
     21                minChars: 3,
    2122                filter: function( text, input ) {
    2223                        // Filter autocomplete matches
    2324
     
    5657                        }
    5758
    5859                        return false;
    59                 }
     60                },
    6061        } );
    6162
    6263        // On input event for the search field.
     
    6566                // Update the autocomlete list:
    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();
    7172                }
     
    9293                                        return false;
    9394                                }
    9495
    95                                 if ( typeof response.data.posts === 'undefined' ) {
     96                                if ( typeof response.data === 'undefined' ) {
    9697                                        return false;
    9798                                }
    9899
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/awesomplete.min.js

     
    22(function(){function m(a,b){for(var c in a){var g=a[c],e=this.input.getAttribute("data-"+c.toLowerCase());this[c]="number"===typeof g?parseInt(e):!1===g?null!==e:g instanceof Function?null:e;this[c]||0===this[c]||(this[c]=c in b?b[c]:g)}}function d(a,b){return"string"===typeof a?(b||document).querySelector(a):a||null}function h(a,b){return k.call((b||document).querySelectorAll(a))}function l(){h("input.awesomplete").forEach(function(a){new f(a)})}var f=function(a,b){var c=this;this.input=d(a);this.input.setAttribute("autocomplete",
    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});
    88c||(this.replace(a.textContent),this.close(),d.fire(this.input,"awesomplete-selectcomplete"))}},evaluate:function(){var a=this,b=this.input.value;b.length>=this.minChars&&0<this._list.length?(this.index=-1,this.ul.innerHTML="",this._list.filter(function(c){return a.filter(c,b)}).sort(this.sort).every(function(c,d){a.ul.appendChild(a.item(c,b));return d<a.maxItems-1}),0===this.ul.children.length?this.close():this.open()):this.close()}};f.all=[];f.FILTER_CONTAINS=function(a,b){return RegExp(d.regExpEscape(b.trim()),