diff --git a/src/util/util.js b/src/util/util.js index 74fb6e2f49fc15a51aa35e54f1868da31f0da7e7..8242a16f76b228c4e3503fb4b2f604e5bd03eeab 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -420,68 +420,109 @@ morpheus.Util.autosuggest = function(options) { suggestWhenEmpty : true, }, options); options.$el - // don't navigate away from the field on tab when selecting an item - .on( - 'keydown', - function(event) { - if ((event.keyCode === $.ui.keyCode.TAB) - && $(this).data('ui-autocomplete').menu.active) { - event.preventDefault(); - } - }).autocomplete({ - minLength : 1, - delay : options.delay, - source : function(request, response) { - // delegate back to autocomplete, but extract the last term - var terms = morpheus.Util.getAutocompleteTokens(request.term); - if (options.suggestWhenEmpty || terms.length > 0) { - options.filter(terms, response); - } - }, - focus : function() { - // prevent value inserted on focus - return false; - }, - select : function(event, ui) { - if (options.multi) { - var terms = morpheus.Util.getAutocompleteTokens(this.value); - // remove the current input - terms.pop(); - // add the selected item - var val = ui.item.value; - // if ((val.indexOf(' ') > 0 && val[0] !== '"')) { - // val = '"' + val + '"'; // quote - // } - // val = val.replace(fieldRegExp, '\\:'); // TODO escape field - // separators - terms.push(val); + // don't navigate away from the field on tab when selecting an item + .on( + 'keydown', + function(event) { + if ((event.keyCode === $.ui.keyCode.TAB) + && $(this).data('ui-autocomplete').menu.active) { + event.preventDefault(); + } + }) + .autocomplete( + { + minLength : 0, + delay : options.delay, + source : function(request, response) { + // delegate back to autocomplete, but extract the + // autocomplete term + var terms = morpheus.Util + .getAutocompleteTokens( + request.term, + { + trim : false, + selectionStart : options.$el[0].selectionStart + }); - this.value = terms.join(' '); - if (options.select) { - options.select(); - } - $(this).autocomplete('close'); - return false; - } else if (options.select) { - options.select(); - $(this).autocomplete('close'); - } - if (event.which === 13) { - event.stopImmediatePropagation(); - } - } - }); + if (terms.selectionStartIndex === undefined + || terms.selectionStartIndex === -1) { + terms.selectionStartIndex = terms.length - 1; + } + if (options.suggestWhenEmpty || terms.length > 0) { + options.filter(terms, response); + } + }, + focus : function() { + // prevent value inserted on focus + return false; + }, + select : function(event, ui) { + if (ui.item.skip) { + return false; + } + if (options.multi) { + var terms = morpheus.Util + .getAutocompleteTokens( + this.value, + { + trim : false, + selectionStart : options.$el[0].selectionStart + }); + // quote value if needed + var value = (ui.item.value[0] !== '"' + && ui.item.value.indexOf(' ') > 0 ? ('"' + + ui.item.value + '"') + : ui.item.value); + + var show = ui.item.show; // || (ui.item.space + // && + // options.suggestWhenEmpty); + + // replace the current input + if (terms.length === 0) { + terms.push(value); + } else { + terms[terms.selectionStartIndex === -1 + || terms.selectionStartIndex === undefined ? terms.length - 1 + : terms.selectionStartIndex] = value; + } + // add the selected item + this.value = terms.join(' '); + if (show) { // did + // we + // select + // just a + // field name? + setTimeout(function() { + options.$el.autocomplete('search', + options.$el.val()); + }, 20); + + } + if (options.select) { + options.select(); + } + return false; + } + if (options.select) { + options.select(); + } + if (event.which === 13) { + event.stopImmediatePropagation(); + } + } + }); // use html for label instead of default text - options.$el.autocomplete('instance')._renderItem = function(ul, item) { - return $('