From 783fabb71de2456b66ace6474f9e54246bcc9bcc Mon Sep 17 00:00:00 2001 From: Don Wilson Date: Sun, 11 Dec 2011 20:33:16 -0700 Subject: [PATCH 1/2] Changed all tabs to 2 spaces --- jquery.select-to-autocomplete.js | 98 ++++++++++++++++---------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/jquery.select-to-autocomplete.js b/jquery.select-to-autocomplete.js index 7c67b59..2c89b62 100644 --- a/jquery.select-to-autocomplete.js +++ b/jquery.select-to-autocomplete.js @@ -160,46 +160,46 @@ THE SOFTWARE. var split_term = term.split(' '); var matchers = []; for (var i=0; i < split_term.length; i++) { - if ( split_term[i].length > 0 ) { - var matcher = {}; - matcher['partial'] = new RegExp( $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); - if ( context.settings['relevancy-sorting'] ) { - matcher['strict'] = new RegExp( "^" + $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); - } - matchers.push( matcher ); - } - }; - - return $.grep( context.options, function( option ) { - var partial_matches = 0; - if ( context.settings['relevancy-sorting'] ) { - var strict_match = false; - var split_option_matches = option.matches.split(' '); - } - for ( var i=0; i < matchers.length; i++ ) { - if ( matchers[i]['partial'].test( option.matches ) ) { - partial_matches++; - } - if ( context.settings['relevancy-sorting'] ) { - for (var q=0; q < split_option_matches.length; q++) { - if ( matchers[i]['strict'].test( split_option_matches[q] ) ) { - strict_match = true; - break; - } - }; - } - }; - if ( context.settings['relevancy-sorting'] ) { - var option_score = 0; - option_score += partial_matches * context.settings['relevancy-sorting-partial-match-value']; - if ( strict_match ) { - option_score += context.settings['relevancy-sorting-strict-match-value']; - } - option_score = option_score * option['relevancy-score-booster']; - option['relevancy-score'] = option_score; - } - return (!term || matchers.length === partial_matches ); - }); + if ( split_term[i].length > 0 ) { + var matcher = {}; + matcher['partial'] = new RegExp( $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); + if ( context.settings['relevancy-sorting'] ) { + matcher['strict'] = new RegExp( "^" + $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); + } + matchers.push( matcher ); + } + }; + + return $.grep( context.options, function( option ) { + var partial_matches = 0; + if ( context.settings['relevancy-sorting'] ) { + var strict_match = false; + var split_option_matches = option.matches.split(' '); + } + for ( var i=0; i < matchers.length; i++ ) { + if ( matchers[i]['partial'].test( option.matches ) ) { + partial_matches++; + } + if ( context.settings['relevancy-sorting'] ) { + for (var q=0; q < split_option_matches.length; q++) { + if ( matchers[i]['strict'].test( split_option_matches[q] ) ) { + strict_match = true; + break; + } + }; + } + }; + if ( context.settings['relevancy-sorting'] ) { + var option_score = 0; + option_score += partial_matches * context.settings['relevancy-sorting-partial-match-value']; + if ( strict_match ) { + option_score += context.settings['relevancy-sorting-strict-match-value']; + } + option_score = option_score * option['relevancy-score-booster']; + option['relevancy-score'] = option_score; + } + return (!term || matchers.length === partial_matches ); + }); } // update the select field value using either selected option or current input in the text field var update_select_value = function( option ) { @@ -218,12 +218,12 @@ THE SOFTWARE. if ( matching_option['real-value'] ) { context.$text_field.val( matching_option['label'] ); } - if ( typeof context.settings['handle_invalid_input'] === 'function' && context.$select_field.val() === '' ) { - context.settings['handle_invalid_input']( context ); - } + if ( typeof context.settings['handle_invalid_input'] === 'function' && context.$select_field.val() === '' ) { + context.settings['handle_invalid_input']( context ); + } } - context.$select_field.change(); - } + context.$select_field.change(); + } // jQuery UI autocomplete settings & behavior context.$text_field.autocomplete({ 'minLength': 0, @@ -238,10 +238,10 @@ THE SOFTWARE. }, select: function( event, ui ) { update_select_value( ui.item ); - }, - change: function( event, ui ) { - update_select_value( ui.item ); - } + }, + change: function( event, ui ) { + update_select_value( ui.item ); + } }); // force refresh value of select field when form is submitted context.$text_field.parents('form:first').submit(function(){ From b7f6602f63bf85382ed287b6303fb04836c3ae97 Mon Sep 17 00:00:00 2001 From: Don Wilson Date: Sun, 11 Dec 2011 22:05:40 -0700 Subject: [PATCH 2/2] Revert to previously selected value when the field is left with invalid input --- jquery.select-to-autocomplete.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/jquery.select-to-autocomplete.js b/jquery.select-to-autocomplete.js index 2c89b62..fc44463 100644 --- a/jquery.select-to-autocomplete.js +++ b/jquery.select-to-autocomplete.js @@ -38,16 +38,16 @@ THE SOFTWARE. 'relevancy-sorting-strict-match-value': 5, 'relevancy-sorting-booster-attr': 'data-relevancy-booster', handle_invalid_input: function( context ) { - context.$text_field.val( '' ); + context.$text_field.val( context.$select_field.find('option:selected:first').text() ); }, handle_select_field: function( $select_field ) { return $select_field.hide(); }, - insert_text_field: function( $select_field ) { + insert_text_field: function( context ) { var $text_field = $( "" ); if ( settings['copy-attributes-to-text-field'] ) { var attrs = {}; - var raw_attrs = $select_field[0].attributes; + var raw_attrs = context.$select_field[0].attributes; for (var i=0; i < raw_attrs.length; i++) { var key = raw_attrs[i].nodeName; var value = raw_attrs[i].nodeValue; @@ -57,8 +57,14 @@ THE SOFTWARE. }; $text_field.attr( attrs ); } - return $text_field.val( $select_field.find('option:selected:first').text() ) - .insertAfter( $select_field ); + $text_field.blur(function() { + var valid_values = context.$select_field.find('option').map(function(i, option) { return $(option).text(); }); + if ( !($text_field.val() in valid_values) && typeof settings['handle_invalid_input'] === 'function' ) { + settings['handle_invalid_input'](context); + } + }); + return $text_field.val( context.$select_field.find('option:selected:first').text() ) + .insertAfter( context.$select_field ); }, extract_options: function( $select_field ) { var options = []; @@ -130,17 +136,17 @@ THE SOFTWARE. return this.each(function(){ var $select_field = $(this); - - var options = settings['extract_options']( $select_field ); - var $text_field = settings['insert_text_field']( $select_field ); - settings['handle_select_field']( $select_field ); - + var context = { '$select_field': $select_field, - '$text_field': $text_field, - 'options': options, + 'options': settings['extract_options']( $select_field ), 'settings': settings }; + + context['$text_field'] = settings['insert_text_field']( context ); + + settings['handle_select_field']( $select_field ); + if ( typeof settings['autocomplete-plugin'] === 'string' ) { adapters[settings['autocomplete-plugin']]( context ); } else {