Properly auto-complete usernames whenever they appear in a message box

(imported from commit 82ef65125731eb48fa029d16acc28add401ac633)
This commit is contained in:
Leo Franchi 2013-02-20 15:39:27 -05:00
parent 48e7fed00f
commit 771eea22b8
2 changed files with 21 additions and 4 deletions

View File

@ -118,6 +118,11 @@ function select_on_focus(field_id) {
});
}
exports.split_at_cursor = function(query) {
var cursor = $('#new_message_content').caret().start;
return [query.slice(0, cursor), query.slice(cursor)];
};
exports.initialize = function () {
select_on_focus("stream");
select_on_focus("subject");
@ -187,7 +192,9 @@ exports.initialize = function () {
items: 2,
highlighter: composebox_typeahead_highlighter,
matcher: function (item) {
var strings = this.query.split(/[\s*(){}\[\]]/);
var query = exports.split_at_cursor(this.query)[0];
var strings = query.split(/[\s*(){}\[\]]/);
if (strings.length < 1) {
return false;
}
@ -202,7 +209,17 @@ exports.initialize = function () {
},
sorter: typeahead_helper.sort_textbox_typeahead,
updater: function (item) {
return this.query.replace(/@\S+$/, "") + "@**" + typeahead_helper.private_message_mapped[item].full_name + "**";
var pieces = exports.split_at_cursor(this.query);
var beginning = pieces[0];
var rest = pieces[1];
beginning = beginning.replace(/@\S+$/, "") + "@**" + typeahead_helper.private_message_mapped[item].full_name + "**";
// Keep the cursor after the newly inserted name, as Bootstrap will call textbox.change() to overwrite the text
// in the textbox.
setTimeout(function () {
$('#new_message_content').caret(beginning.length, beginning.length);
}, 0);
return beginning + rest;
},
stopAdvance: true // Do not advance to the next field on a tab or enter
});

View File

@ -159,9 +159,9 @@ exports.sort_recipients = function (matches, query) {
exports.sort_textbox_typeahead = function(matches) {
// input may be free text ending in @ for autocomplete
var query = this.query;
var query = composebox_typeahead.split_at_cursor(this.query)[0];
if (query.indexOf('@') > -1) {
var parts = this.query.split('@');
var parts = query.split('@');
query = parts[parts.length - 1];
}
return exports.sort_recipients(matches, query);