Skip to content
Browse files

Added autocomplete_delay option

  • Loading branch information...
1 parent 13afd62 commit 45064908625fd2301ecd6df6d40a9a9eff425c23 @rfletcher committed Mar 19, 2009
Showing with 27 additions and 5 deletions.
  1. +21 −3 src/lib/Tagbox.Autocomplete.js
  2. +6 −2 src/lib/Tagbox.js
View
24 src/lib/Tagbox.Autocomplete.js
@@ -36,6 +36,7 @@ Tagbox.Autocomplete = Class.create( {
query: null,
regexp: null,
results: [],
+ timer: null,
/**
* new Tagbox.Autocomplete( tagbox, options )
@@ -198,19 +199,19 @@ Tagbox.Autocomplete = Class.create( {
return;
}
- var query = this.tagbox.current.down( 'input[type=text]' ).value .replace( /(^\s+|\s+$)/g, '' ).toLowerCase();
+ var query = this.tagbox.current.down( 'input[type=text]' ).value.replace( /(^\s+|\s+$)/g, '' ).toLowerCase();
if( query.length && query.length >= this.tagbox.options.get( 'minimum_chars_for_autocomplete' ) ) {
if( query != this.query ) {
this.query = query;
+ // build the match regexp
this.regexp = new RegExp( '(' + this.query.split( '' ).collect( function( c ) {
var hex = c.charCodeAt( 0 ).toString( 16 );
return "\\x" + ( hex.length == 1 ? "0" : "" ) + hex;
} ).join( '' ) + ')', 'gi' );
- this.show();
- this.next();
+ this.showAfterDelay();
}
} else if( this.element.visible() ) {
this.hide();
@@ -256,6 +257,7 @@ Tagbox.Autocomplete = Class.create( {
var updateAndShow = function() {
this.update();
this.results.length && this.element.show();
+ this.next();
}.bind( this );
// let the server do the filtering
@@ -310,6 +312,22 @@ Tagbox.Autocomplete = Class.create( {
this.element.insert( li );
}.bind( this ) );
+ },
+
+ /**
+ * Tagbox.Autcomplete#showAfterDelay() -> undefined
+ *
+ * Wait for any user-defined autocomplete delay to pass, then show the results.
+ **/
+ showAfterDelay: function() {
+ var delay = parseInt( this.tagbox.options.get( 'autocomplete_delay' ) );
+ clearTimeout( this.timer );
+
+ if( delay ) {
+ this.timer = setTimeout( this.show.bind( this ), delay );
+ } else {
+ this.show();
+ }
}
} );
View
8 src/lib/Tagbox.js
@@ -23,14 +23,17 @@ var Tagbox = Class.create( {
* format acceptable to the Tagbox.Tags constructor.
* allowed_url (String) = null:
* A URL to which will be passed user input via a `query` param, and
- * which should return a JSON array of autocomplete results. This is
+ * which should return a JSON array of autocompaadlete results. This is
* the ajax version of the ``allowed`` option.
* arbitrary_value_field_name = null:
* An alternate form field name to use for values not contained in
* the ``allowed`` array.
* autocomplete (Boolean) = true:
* Display a drop-down list of allowed values, filtered as the user
* types. This option has no effect when the ``allowed`` array is empty.
+ * autocomplete_delay (Number) = 0:
+ * Number of milliseconds to wait after the user stops typing before
+ * fetching autocomplete results.
* autocomplete_tag_renderer (Function) = Tagbox.Autocomplete.renderTag:
* Function which generates HTML representation of a tag when it's
* displayed as part of the autocomplete results list.
@@ -63,6 +66,7 @@ var Tagbox = Class.create( {
allowed_url: null,
arbitrary_value_field_name: null,
autocomplete: true,
+ autocomplete_delay: 100,
autocomplete_tag_renderer: null,
case_sensitive: false,
delimiters: [ Event.KEY_COMMA, Event.KEY_RETURN ],
@@ -399,7 +403,7 @@ var Tagbox = Class.create( {
**/
initializeAllowedTags: function() {
this.options.set( 'allowed',
- ( this.options.get( 'allowed' ) ? this.options.get( 'allowed' ) : [] ) .collect( this.objectToTag.bind( this ) )
+ ( this.options.get( 'allowed' ) ? this.options.get( 'allowed' ) : [] ).collect( this.objectToTag.bind( this ) )
);
},

0 comments on commit 4506490

Please sign in to comment.
Something went wrong with that request. Please try again.