Skip to content

Commit

Permalink
Merge pull request #25 from dontangg/value-restrict2
Browse files Browse the repository at this point in the history
Restricting values in the dropdown to valid values + tabs are now spaces... attempt 2
  • Loading branch information
Jamie Appleseed committed Dec 12, 2011
2 parents 3ec7a20 + b7f6602 commit b41a3f1
Showing 1 changed file with 67 additions and 61 deletions.
128 changes: 67 additions & 61 deletions jquery.select-to-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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 {
Expand All @@ -160,46 +166,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 ) {
Expand All @@ -218,12 +224,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,
Expand All @@ -238,10 +244,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(){
Expand Down

0 comments on commit b41a3f1

Please sign in to comment.