diff --git a/README.txt b/README.txt index 0d767d2..ab638fe 100755 --- a/README.txt +++ b/README.txt @@ -114,6 +114,10 @@ Yep! Just copy over the function from underscore-template.php (without the funct == Changelog == += 0.9 = +* added some CSS classes wpls--empty and wpls--full to the custom target div to aid in custom theming +* added logic to prevent searching on escape or arrow keys + = 0.8 = * added "dropdown" option mode for use in small spaces * added "results_style" option for use in small spaces diff --git a/public/assets/js/wp-live-search.js b/public/assets/js/wp-live-search.js index e31d144..3beb548 100644 --- a/public/assets/js/wp-live-search.js +++ b/public/assets/js/wp-live-search.js @@ -22,6 +22,8 @@ , api = WP_API_Settings.root , timer + $( postList ).addClass('wpls--empty'); + $( input ).on('keyup keypress', function ( e ) { // clear the previous timer @@ -46,27 +48,32 @@ // what if the user only types two characters? if ( val.length == 2 && !$(helper).length ) { - $(input).after( helperSpan ); + $( input ).after( helperSpan ); } // if we have more than 3 characters if ( val.length >= 3 || val.length >= 3 && 13 == key ) { + // dont run on escape or arrow keys + if( blacklistedKeys( key ) ) + return false; + // show loader - $(loader).removeClass('wpls--hide').addClass('wpls--show'); + $( loader ).removeClass('wpls--hide').addClass('wpls--show'); // remove any helpers $( helper ).fadeOut().remove(); // remove the cose - destroyClose(); + destroyClose() // make the search request $.getJSON( url, function( response ) { // remove current list of posts $(postList).children().remove(); + $(postList).removeClass('wpls--full').addClass('wpls--empty') // show results $(results).parent().removeClass('wpls--hide').addClass('wpls--show'); @@ -85,6 +92,10 @@ } else { + // again, dont run on escape or arrow keys + if( blacklistedKeys( key ) ) + return false; + // append close button if ( !$( clearItem ).length ) { @@ -97,7 +108,9 @@ // loop through each object $.each( response, function ( i ) { - $(postList).append( itemTemplate( { post: response[i], settings: WP_API_Settings, excerpt: showExcerpt } ) ); + $(postList).append( itemTemplate( { post: response[i], settings: WP_API_Settings, excerpt: showExcerpt } ) ) + .removeClass('wpls--empty') + .addClass('wpls--full') } ); } @@ -143,9 +156,21 @@ $( postList ).children().remove(); $( input ).val(''); $( results ).parent().removeClass('wpls--show').addClass('wpls--hide'); + $( postList ).removeClass('wpls--full').addClass('wpls--empty') $( helper ).remove(); destroyClose() } + + /** + * Blacklisted keys - dont allow search on escape or arrow keys + * @since 0.9 + */ + function blacklistedKeys( key ){ + + return 27 == key || 37 == key || 38 == key || 39 == key || 40 == key; + + } + }); })( jQuery, Backbone, _, WP_API_Settings ); \ No newline at end of file diff --git a/wp-live-search.php b/wp-live-search.php index 37fb6c2..ef1e269 100755 --- a/wp-live-search.php +++ b/wp-live-search.php @@ -10,7 +10,7 @@ * Plugin Name: WP Live Search * Plugin URI: http://nickhaskins.com * Description: A super light-weight live search plugin that utilizes the WP REST API - * Version: 0.8 + * Version: 0.9 * GitHub Plugin URI: https://github.com/bearded-avenger/wp-live-search */ @@ -20,7 +20,7 @@ } // Set some constants -define('WP_LIVE_SEARCH_VERSION', '0.8'); +define('WP_LIVE_SEARCH_VERSION', '0.9'); define('WP_LIVE_SEARCH_DIR', plugin_dir_path( __FILE__ )); define('WP_LIVE_SEARCH_URL', plugins_url( '', __FILE__ ));