From df7a12b88461d4f0e4898ad6a2e715e3339d6a20 Mon Sep 17 00:00:00 2001 From: Augusto Destrero Date: Tue, 10 Sep 2013 16:18:53 +0200 Subject: [PATCH 1/6] possibility to have id_map assigned to element.data() and not necessarily in a hidden field --- jquery-ui.triggeredAutocomplete.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/jquery-ui.triggeredAutocomplete.js b/jquery-ui.triggeredAutocomplete.js index 5628762..94186d3 100644 --- a/jquery-ui.triggeredAutocomplete.js +++ b/jquery-ui.triggeredAutocomplete.js @@ -258,7 +258,12 @@ contents = contents.replace(regex, trigger+'['+this.id_map[key]+']'); if(old_contents == contents) delete this.id_map[key]; } - $(this.options.hidden).val(contents); + if (typeof this.options.hidden != 'undefined') { + $(this.options.hidden).val(contents); + } + if (typeof this.options.data != 'undefined') { + this.element.data(this.options.data, this.id_map); + } this.element.scrollTop(top); } From aeab83dba0fe7b81aded86a750780e612ea3c8fa Mon Sep 17 00:00:00 2001 From: Augusto Destrero Date: Tue, 10 Sep 2013 16:26:01 +0200 Subject: [PATCH 2/6] added copyright --- jquery-ui.triggeredAutocomplete.js | 1 + 1 file changed, 1 insertion(+) diff --git a/jquery-ui.triggeredAutocomplete.js b/jquery-ui.triggeredAutocomplete.js index 94186d3..596bd5f 100644 --- a/jquery-ui.triggeredAutocomplete.js +++ b/jquery-ui.triggeredAutocomplete.js @@ -2,6 +2,7 @@ /* * triggeredAutocomplete (jQuery UI autocomplete widget) * 2012 by Hawkee.com (hawkee@gmail.com) + * 2013 by Augusto Destrero (a.destrero@gmail.com) * * Version 1.4.5 * From 4e8829135011fa321d58c5bab8156b8867165b79 Mon Sep 17 00:00:00 2001 From: Augusto Destrero Date: Tue, 10 Sep 2013 16:26:44 +0200 Subject: [PATCH 3/6] cut contents at cursor position, otherwise if you have multiple triggers only the last will work --- jquery-ui.triggeredAutocomplete.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jquery-ui.triggeredAutocomplete.js b/jquery-ui.triggeredAutocomplete.js index 596bd5f..1eb586d 100644 --- a/jquery-ui.triggeredAutocomplete.js +++ b/jquery-ui.triggeredAutocomplete.js @@ -141,6 +141,9 @@ this.contents = contents; this.cursorPos = cursorPos; + // cut contents at cursor position + contents = contents.substring(0, 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 @@ -152,7 +155,6 @@ // Get the characters following the trigger and before the cursor position. // Get the contents up to the cursortPos first then get the lastIndexOf the trigger to find the search term. - contents = contents.substring(0, cursorPos); var term = contents.substring(contents.lastIndexOf(this.options.trigger) + 1, contents.length); // Only query the server if we have a term and we haven't received a null response. From f9e666ce80445974f5b828b9892c1c20b93b47fb Mon Sep 17 00:00:00 2001 From: Augusto Destrero Date: Tue, 10 Sep 2013 16:32:34 +0200 Subject: [PATCH 4/6] insert space after selection only if needed and compute the new caret position accordingly --- jquery-ui.triggeredAutocomplete.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/jquery-ui.triggeredAutocomplete.js b/jquery-ui.triggeredAutocomplete.js index 1eb586d..be359d6 100644 --- a/jquery-ui.triggeredAutocomplete.js +++ b/jquery-ui.triggeredAutocomplete.js @@ -72,7 +72,13 @@ start = start.substring(0, start.lastIndexOf(self.options.trigger)); var top = self.element.scrollTop(); - this.value = start + self.options.trigger+ui.item.label+' ' + end; + var newvalue = start + self.options.trigger + ui.item.label; + if (end.charAt(0) != ' ') { + newvalue += ' '; + } + var newcaretposition = newvalue.length + 1; + newvalue += end; + this.value = newvalue; self.element.scrollTop(top); // Create an id map so we can create a hidden version of this string with id's instead of labels. @@ -81,13 +87,12 @@ self.updateHidden(); /** 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.move('character', newcaretposition); range.select(); } else if (this.setSelectionRange) { - this.setSelectionRange(index, index); + this.setSelectionRange(newcaretposition, newcaretposition); } return false; From 9ad0a62833fc11b80dd6b5f7602719f38d129adb Mon Sep 17 00:00:00 2001 From: Augusto Destrero Date: Wed, 11 Sep 2013 17:01:58 +0200 Subject: [PATCH 5/6] fix menu keyboard navigation and preload id_map from data() and not from attr() --- jquery-ui.triggeredAutocomplete.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/jquery-ui.triggeredAutocomplete.js b/jquery-ui.triggeredAutocomplete.js index be359d6..f48d290 100644 --- a/jquery-ui.triggeredAutocomplete.js +++ b/jquery-ui.triggeredAutocomplete.js @@ -47,10 +47,10 @@ } }); - // Check for the id_map as an attribute. This is for editing. + // Check for the id_map as a data attribute. This is for editing. - var id_map_string = this.element.attr('id_map'); - if(id_map_string) this.id_map = jQuery.parseJSON(id_map_string); + var id_map_string = this.element.data(self.options.data); + if (id_map_string) this.id_map = jQuery.parseJSON(id_map_string); this.ac = $.ui.autocomplete.prototype; this.ac._create.apply(this, arguments); @@ -131,9 +131,9 @@ this.search( null, event ); return; } - if ( this.menu.first() && /^previous/.test(direction) || - this.menu.last() && /^next/.test(direction) ) { - this.menu.deactivate(); + if ( this.menu.isFirstItem() && /^previous/.test(direction) || + this.menu.isLastItem() && /^next/.test(direction) ) { + this.menu.disable(); return; } this.menu[ direction ]( event ); @@ -154,6 +154,12 @@ var check_contents = contents.substring(contents.lastIndexOf(this.options.trigger) - 1, cursorPos); var regex = new RegExp('\\B\\'+this.options.trigger+'([\\w\\-]+)'); + var only_trigger_regex = new RegExp('\\B\\' + this.options.trigger); + + // Close the suggestion list if there's only the trigger + if (check_contents.match(only_trigger_regex)) { + this.close(); + } if (contents.indexOf(this.options.trigger) >= 0 && check_contents.match(regex)) { From 731c6acc13fe472d6ce9d17ae632179c5e59948e Mon Sep 17 00:00:00 2001 From: Augusto Destrero Date: Wed, 11 Sep 2013 17:19:04 +0200 Subject: [PATCH 6/6] error check --- jquery-ui.triggeredAutocomplete.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/jquery-ui.triggeredAutocomplete.js b/jquery-ui.triggeredAutocomplete.js index f48d290..dfd886e 100644 --- a/jquery-ui.triggeredAutocomplete.js +++ b/jquery-ui.triggeredAutocomplete.js @@ -48,9 +48,14 @@ }); // Check for the id_map as a data attribute. This is for editing. - - var id_map_string = this.element.data(self.options.data); - if (id_map_string) this.id_map = jQuery.parseJSON(id_map_string); + try { + var id_map_string = this.element.data(self.options.data); + if (id_map_string && id_map_string != '') { + this.id_map = jQuery.parseJSON(id_map_string); + } + } catch(err) { + this.id_map = new Object(); + } this.ac = $.ui.autocomplete.prototype; this.ac._create.apply(this, arguments);