Skip to content

Commit

Permalink
Update to latest version of triggeredAutocomplete, modify request to …
Browse files Browse the repository at this point in the history
…close properly when no results are returned.
  • Loading branch information
rjsamson committed Sep 20, 2012
1 parent a2881b2 commit 791d096
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* triggeredAutocomplete (jQuery UI autocomplete widget)
* 2012 by Hawkee.com ([email protected])
*
* Version 1.4.3
* Version 1.4.5
*
* Requires jQuery 1.7 and jQuery UI 1.8
*
Expand All @@ -18,7 +18,8 @@

options: {
trigger: "@",
allowDuplicates: true
allowDuplicates: true,
maxLength: 0
},

_create:function() {
Expand Down Expand Up @@ -81,12 +82,12 @@
/** Places the caret right after the inserted item. */
var index = start.length + self.options.trigger.length + ui.item.label.length + 2;
if (this.createTextRange) {
var range = this.createTextRange();
range.move('character', index);
range.select();
} else if (this.setSelectionRange) {
this.setSelectionRange(index, index);
}
var range = this.createTextRange();
range.move('character', index);
range.select();
} else if (this.setSelectionRange) {
this.setSelectionRange(index, index);
}

return false;
};
Expand Down Expand Up @@ -138,6 +139,10 @@
var cursorPos = this.getCursor();
this.contents = contents;
this.cursorPos = cursorPos;

// Include the character before the trigger and check that the trigger is not in the middle of a word
// This avoids trying to match in the middle of email addresses when '@' is used as the trigger

var check_contents = contents.substring(contents.lastIndexOf(this.options.trigger) - 1, cursorPos);
var regex = new RegExp('\\B\\'+this.options.trigger+'([\\w\\-]+)');

Expand All @@ -154,7 +159,7 @@

if(this.stopIndex == contents.lastIndexOf(this.options.trigger) && term.length > this.stopLength) { term = ''; }

if(term.length > 0) {
if (term.length > 0 && (!this.options.maxLength || term.length <= this.options.maxLength)) {
// Updates the hidden field to check if a name was removed so that we can put them back in the list.
this.updateHidden();
return this._search(term);
Expand Down Expand Up @@ -184,7 +189,7 @@
data: request,
dataType: 'json',
success: function(data) {
if(data != null) {
if(data != null && data.length > 0) {
response($.map(data, function(item) {
if (typeof item === "string") {
label = item;
Expand Down Expand Up @@ -258,4 +263,4 @@
}

}));
})( jQuery, window , document );
})( jQuery, window , document );
1 change: 1 addition & 0 deletions app/assets/javascripts/update.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jQuery $ ->
textarea.triggeredAutocomplete
trigger: "@"
source: "/autocomplete"
maxLength: 10

updateTickyboxes = ->
if userTickiedBox
Expand Down

0 comments on commit 791d096

Please sign in to comment.