From 7832f939f972a842739cc92eff22780dfffcf642 Mon Sep 17 00:00:00 2001 From: Bearded Avenger Date: Sun, 21 Jun 2015 15:19:27 -0500 Subject: [PATCH 1/4] dont search again if we already have ar esults and user presses escape key --- public/assets/js/wp-live-search.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/public/assets/js/wp-live-search.js b/public/assets/js/wp-live-search.js index e31d144..964f68a 100644 --- a/public/assets/js/wp-live-search.js +++ b/public/assets/js/wp-live-search.js @@ -60,7 +60,7 @@ $( helper ).fadeOut().remove(); // remove the cose - destroyClose(); + destroyClose() // make the search request $.getJSON( url, function( response ) { @@ -85,6 +85,10 @@ } else { + // dont run again on escape + if( 27 == key ) + return false; + // append close button if ( !$( clearItem ).length ) { From 772c867b5daa2eb589af5be2fb6885153c98badb Mon Sep 17 00:00:00 2001 From: Bearded Avenger Date: Mon, 22 Jun 2015 08:24:28 -0500 Subject: [PATCH 2/4] add some classes wpls--full wpls--empty to custom list targets for styling, also dont search on any arrow key along with escape keys --- public/assets/js/wp-live-search.js | 31 +++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/public/assets/js/wp-live-search.js b/public/assets/js/wp-live-search.js index 964f68a..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,15 +48,19 @@ // 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(); @@ -67,6 +73,7 @@ // 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,8 +92,8 @@ } else { - // dont run again on escape - if( 27 == key ) + // again, dont run on escape or arrow keys + if( blacklistedKeys( key ) ) return false; // append close button @@ -101,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') } ); } @@ -147,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 From 85753153d9d462d95f18517898daf8fbe9e9835c Mon Sep 17 00:00:00 2001 From: Bearded Avenger Date: Mon, 22 Jun 2015 08:25:55 -0500 Subject: [PATCH 3/4] changelog before i forget --- README.txt | 4 ++++ 1 file changed, 4 insertions(+) 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 From e10012159bda1bf4b3ed16bf3b1cbd5c51eb6fc2 Mon Sep 17 00:00:00 2001 From: Bearded Avenger Date: Wed, 24 Jun 2015 16:23:30 -0500 Subject: [PATCH 4/4] 0.9 --- wp-live-search.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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__ ));