Skip to content
Browse files

Allow options to pass to jQuery autocomplete.

  • Loading branch information...
1 parent 367af67 commit 411d7080f9f71f8a008b62c98dd532bf2894b524 @ordinaryzelig committed Apr 17, 2012
View
55 CHANGELOG.md
@@ -1,51 +1,48 @@
# CHANGELOG
-## v0.2.0 ???
+## v0.3.0 2012-04-17
-### Minor features
+ * Minor features
+ * Allow extra options to pass to jQuery autocomplete.
-* Follow good jQuery plugin practice:
- * chainable.
+## v0.2.0 2012-04-17
-## v0.1.1 2012-04-10
+ * Minor features
+ * Follow good jQuery plugin practice:
+ * chainable.
-### Minor features
+## v0.1.1 2012-04-10
-* Prevent form submission when 'enter' key pressed in text field.
-* Hide floater on blur.
-* Focus on select tag after selection made.
+ * Minor features
+ * Prevent form submission when 'enter' key pressed in text field.
+ * Hide floater on blur.
+ * Focus on select tag after selection made.
## v0.1.0 2012-04-10
-### Minor features
-
-* Add placeholder to text field.
-* Add some styling to floater.
+ * Minor features
+ * Add placeholder to text field.
+ * Add some styling to floater.
## v0.0.4 2012-04-10
-### Bug fixes
-
-* Use more specific selectors than `siblings()` like `nextAll()`.
+ * Bug fixes
+ * Use more specific selectors than `siblings()` like `nextAll()`.
## v0.0.3 2012-04-10
-### Bug fixes
-
-* Set `$` to `jquery`.
+ * Bug fixes
+ * Set `$` to `jquery`.
## v0.0.2 2012-04-10
-### Minor features
-
-* focus on text field when floater link clicked.
-* clear text field when floater link clicked.
-* add CSS.
-
-### Bug fixes
-
-* iterate through select tags individually and add floaters.
+ * Minor features
+ * focus on text field when floater link clicked.
+ * clear text field when floater link clicked.
+ * add CSS.
+ * Bug fixes
+ * iterate through select tags individually and add floaters.
## v0.0.1 2012-04-10
-First version
+ * First version
View
16 README.md
@@ -57,9 +57,23 @@ What you need:
```javascript
$('.selectTagSelector').autocompleteFloater()
```
+### Options
+
+#### jQueryAutocompleteOptions
+
+Options you would normally pass to jQuery's autocomplete.
+
+```javascript
+// Require at least 3 characters to activate autocomplete.
+$('select').autocompleteFloater({jQueryAutocompleteOptions: {minLength: 3}})
+```
+
+### Gotchas
+
+* Be sure your select tag doesn't have duplicate entries. The first matching option will be selected.
## TODO
-* Allow passing of jQuery autocomplete options.
* Fix failing test for selecting option. The click() is not causing jasmine to behave correctly.
* Wrap in ruby gem.
+* Handle duplicate options (not tested, but currently, it will probably choose the first one).
View
12 spec/javascripts/jquery-selectTagAutcompleteFloater.spec.coffee
@@ -44,6 +44,18 @@ describe 'asdf', ->
link.click()
expect(@selectTag.val()).toEqual(2)
+ describe 'options', ->
+
+ it 'allows passing of extra options to send to jQuery autocomplete', ->
+ @selectTag.autocompleteFloater({jQueryAutocompleteOptions: {disabled: true}})
+ textField = $('.autocompleteFloater').find('input:text')
+ expect(textField.autocomplete('option', 'disabled')).toEqual(true)
+
+ it 'ignores certain jQuery autocomplete options that are necessary for this plugin', ->
+ @selectTag.autocompleteFloater({jQueryAutocompleteOptions: {source: []}})
+ textField = $('.autocompleteFloater').find('input:text')
+ expect(textField.autocomplete('option', 'source')).toEqual(['Volvo', 'Saab', 'Mercedes', 'Audi'])
+
describe 'jQuery plugin', ->
it 'is chainable', ->
View
22 src/jquery-selectTagAutocompleteFloater.coffee
@@ -1,15 +1,15 @@
'========================='
'autocompleteFloater'
''
-'v0.2.0'
+'v0.3.0'
''
'https://github.com/ordinaryzelig/jquery-autocompleteFloater'
'This software is offered as is without warranty, yada yada.'
'========================='
$ = jQuery
-$.fn.autocompleteFloater = ->
+$.fn.autocompleteFloater = (options = {}) ->
@.each ->
selectTag = $(@)
@@ -23,7 +23,7 @@ $.fn.autocompleteFloater = ->
linkToShowFloater.after(floater)
# Add autocomplete.
- autocompleteFloaterFunctions.autocomplete(selectTag)
+ autocompleteFloaterFunctions.autocomplete(selectTag, options.jQueryAutocompleteOptions)
autocompleteFloaterFunctions = {
@@ -69,14 +69,18 @@ autocompleteFloaterFunctions = {
# Generate jQuery autocomplete to floater text fields.
# When an option is selected, the select tag will automatically select the matching option.
# The floater is then hidden.
- autocomplete: (selectTag) ->
+ autocomplete: (selectTag, jQueryAutocompleteOptions) ->
+
# Get options to populate autocomplete.
optionStrings = selectTag.children('option').map(->
$(@).html()
).get()
- # Add jQuery autocomplete to text field.
+
textField = selectTag.nextAll('.autocompleteFloater:first').children('input:text')
- textField.autocomplete
+
+ # Construct options for jQuery autocomplete.
+ # standardOptions should take precedence.
+ standardOptions =
source: optionStrings
select: (event, ui) ->
# Find the matching option in the select tag and change it to selected.
@@ -92,7 +96,9 @@ autocompleteFloaterFunctions = {
return false
# Hide the floater.
floater.hide()
- # Focus on select tag.
- selectTag.focus()
+ autocompleteOptions = $.extend jQueryAutocompleteOptions, standardOptions
+
+ # Add jQuery autocomplete to text field.
+ textField.autocomplete autocompleteOptions
}
View
20 src/jquery-selectTagAutocompleteFloater.js
@@ -2,7 +2,7 @@
'=========================';
'autocompleteFloater';
'';
- 'v0.2.0';
+ 'v0.3.0';
'';
'https://github.com/ordinaryzelig/jquery-autocompleteFloater';
'This software is offered as is without warranty, yada yada.';
@@ -11,15 +11,16 @@
$ = jQuery;
- $.fn.autocompleteFloater = function() {
+ $.fn.autocompleteFloater = function(options) {
+ if (options == null) options = {};
return this.each(function() {
var floater, linkToShowFloater, selectTag;
selectTag = $(this);
linkToShowFloater = autocompleteFloaterFunctions.createLinktoShowFloater();
selectTag.after(linkToShowFloater);
floater = autocompleteFloaterFunctions.createFloater();
linkToShowFloater.after(floater);
- return autocompleteFloaterFunctions.autocomplete(selectTag);
+ return autocompleteFloaterFunctions.autocomplete(selectTag, options.jQueryAutocompleteOptions);
});
};
@@ -63,13 +64,13 @@
});
return link;
},
- autocomplete: function(selectTag) {
- var optionStrings, textField;
+ autocomplete: function(selectTag, jQueryAutocompleteOptions) {
+ var autocompleteOptions, optionStrings, standardOptions, textField;
optionStrings = selectTag.children('option').map(function() {
return $(this).html();
}).get();
textField = selectTag.nextAll('.autocompleteFloater:first').children('input:text');
- return textField.autocomplete({
+ standardOptions = {
source: optionStrings,
select: function(event, ui) {
var floater, selected;
@@ -84,10 +85,11 @@
return false;
}
});
- floater.hide();
- return selectTag.focus();
+ return floater.hide();
}
- });
+ };
+ autocompleteOptions = $.extend(jQueryAutocompleteOptions, standardOptions);
+ return textField.autocomplete(autocompleteOptions);
}
};

0 comments on commit 411d708

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