fixed auto complete for insertion in the middle, 6am commits FTW

This commit is contained in:
Baris Soner Usakli
2014-01-08 06:15:48 -05:00
parent 0d26b21a2c
commit d2c0aa9949
2 changed files with 54 additions and 12 deletions

View File

@@ -149,9 +149,13 @@ define(['taskbar'], function(taskbar) {
$(bodyEl).autocomplete({
source: function(request, response) {
var term = request.term;
var lastMention = request.term.lastIndexOf('@');
var cursorPosition = $(bodyEl).getCursorPosition();
term = term.substr(0, cursorPosition);
var lastMention = term.lastIndexOf('@');
if(lastMention !== -1) {
term = request.term.substr(lastMention);
term = term.substr(lastMention);
}
var userslugs = getUniqueUserslugs();
@@ -162,18 +166,22 @@ define(['taskbar'], function(taskbar) {
response(userslugs);
$('.ui-autocomplete a').attr('href', '#');
},
response: function(event, ui) {
var content = $(bodyEl).val();
var lastIndex = content.lastIndexOf('@');
if(content === '@') {
content = '';
} else if(lastIndex !== -1) {
content = content.substr(0, lastIndex);
}
focus: function(event, ui) {
return false;
},
select: function(event, ui) {
var cursorPosition = $(bodyEl).getCursorPosition();
var upToCursor = $(bodyEl).val().substr(0, cursorPosition);
var index = upToCursor.lastIndexOf('@');
for(var i=0; i<ui.content.length; ++i) {
ui.content[i].value = content + ui.content[i].value;
if(index !== -1) {
var firstPart = $(bodyEl).val().substr(0, index);
var lastPart = $(bodyEl).val().substr(cursorPosition);
$(bodyEl).val(firstPart + ui.item.value + lastPart);
$(bodyEl).selectRange(index + ui.item.value.length);
}
return false;
},
position: { my : "left bottom", at: "left bottom" }
});