-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert to previously selected value when the field is left with inval…
…id input
- Loading branch information
Showing
1 changed file
with
18 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = $( "<input></input>" ); | ||
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' ) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dontangg
Author
Contributor
|
||
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 { | ||
|
@dontangg
The "in" statement actually doesn't work? I found this out when I was using twitter bootstrap autocomplete as an adapter for this. I would use (
$.inArray($text_field.val(), valid_values) < 0
)