Revert "Merge pull request #2833 from mamhoff/new-bugfix-autocomplete"

This reverts commit 2450d284c5, reversing
changes made to 489078bfcf.
This commit is contained in:
Robin Ward 2014-10-02 16:00:50 -04:00
parent 381814fd5d
commit ad5e4f98d9

View file

@ -6,26 +6,44 @@
export var CANCELLED_STATUS = "__CANCELLED"; export var CANCELLED_STATUS = "__CANCELLED";
var Keys = { var shiftMap = [];
BackSpace: 8, shiftMap[192] = "~";
Tab: 9, shiftMap[49] = "!";
Enter: 13, shiftMap[50] = "@";
Shift: 16, shiftMap[51] = "#";
Ctrl: 17, shiftMap[52] = "$";
Alt: 18, shiftMap[53] = "%";
Esc: 27, shiftMap[54] = "^";
Space: 32, shiftMap[55] = "&";
LeftWindows: 91, shiftMap[56] = "*";
RightWindows: 92, shiftMap[57] = "(";
PageUp: 33, shiftMap[48] = ")";
PageDown: 34, shiftMap[109] = "_";
End: 35, shiftMap[107] = "+";
Home: 36, shiftMap[219] = "{";
LeftArrow: 37, shiftMap[221] = "}";
UpArrow: 38, shiftMap[220] = "|";
RightArrow: 39, shiftMap[59] = ":";
DownArrow: 40, shiftMap[222] = "\"";
}; shiftMap[188] = "<";
shiftMap[190] = ">";
shiftMap[191] = "?";
shiftMap[32] = " ";
function mapKeyPressToActualCharacter(isShiftKey, characterCode) {
if ( characterCode === 27 || characterCode === 8 || characterCode === 9 || characterCode === 20 || characterCode === 16 || characterCode === 17 || characterCode === 91 || characterCode === 13 || characterCode === 92 || characterCode === 18 ) { return false; }
// Lookup non-letter keypress while holding shift
if (isShiftKey && ( characterCode < 65 || characterCode > 90 )) {
return shiftMap[characterCode];
}
var stringValue = String.fromCharCode(characterCode);
if ( !isShiftKey ) {
stringValue = stringValue.toLowerCase();
}
return stringValue;
}
export default function(options) { export default function(options) {
var autocompletePlugin = this; var autocompletePlugin = this;
@ -60,14 +78,9 @@ export default function(options) {
} }
div = null; div = null;
completeStart = null; completeStart = null;
completeEnd = null;
autocompleteOptions = null; autocompleteOptions = null;
}; };
var autoCompleting = function () {
return completeStart !== null;
};
var addInputSelectedItem = function(item) { var addInputSelectedItem = function(item) {
var transformed, var transformed,
transformedItem = item; transformedItem = item;
@ -118,7 +131,7 @@ export default function(options) {
term = options.transformComplete(term); term = options.transformComplete(term);
} }
var text = me.val(); var text = me.val();
text = text.substring(0, completeStart) + term + ' ' + text.substring(completeEnd, text.length); text = text.substring(0, completeStart) + (options.key || "") + term + ' ' + text.substring(completeEnd + 1, text.length);
me.val(text); me.val(text);
Discourse.Utilities.setCaretPosition(me[0], completeStart + 1 + term.length); Discourse.Utilities.setCaretPosition(me[0], completeStart + 1 + term.length);
} }
@ -251,37 +264,24 @@ export default function(options) {
closeAutocomplete(); closeAutocomplete();
}); });
var getTerm = function() {
return me.val().slice(completeStart, completeEnd);
};
$(this).keypress(function(e) { $(this).keypress(function(e) {
var term, key = (e.char || String.fromCharCode(e.charCode));
// if we just started with an options.key, set start and end. if (!options.key) return;
if (key === options.key && !autoCompleting()) {
completeStart = completeEnd = Discourse.Utilities.caretPosition(me[0]) + 1;
}
if (!options.key) { // keep hunting backwards till you hit a
completeStart = 0; if (e.which === options.key.charCodeAt(0)) {
completeEnd = Discourse.Utilities.caretPosition(me[0]); var caretPosition = Discourse.Utilities.caretPosition(me[0]);
} var prevChar = me.val().charAt(caretPosition - 1);
if (!prevChar || /\s/.test(prevChar)) {
if (autoCompleting()) { completeStart = completeEnd = caretPosition;
if ((completeStart === completeEnd) && key === options.key) {
updateAutoComplete(options.dataSource("")); updateAutoComplete(options.dataSource(""));
} else {
term = getTerm() + key;
completeEnd += 1;
updateAutoComplete(options.dataSource(term));
} }
return true;
} }
}); });
$(this).keydown(function(e) { $(this).keydown(function(e) {
var caretPosition, i, term, total, userToComplete; var c, caretPosition, i, initial, next, prev, prevIsGood, stopFound, term, total, userToComplete;
if(options.allowAny){ if(options.allowAny){
// saves us wiring up a change event as well, keypress is while its pressed // saves us wiring up a change event as well, keypress is while its pressed
@ -301,38 +301,57 @@ export default function(options) {
},50); },50);
} }
// Handle Backspacing into stuff if (!options.key) {
if ((!autoCompleting()) && e.which === Keys.BackSpace && options.key) { completeStart = 0;
var c = Discourse.Utilities.caretPosition(me[0]), }
last, first, if (e.which === 16) return;
text = me[0].value; if ((completeStart === null) && e.which === 8 && options.key) {
// search backwards until you find the last letter of the word c = Discourse.Utilities.caretPosition(me[0]);
while (/[\s]/.test(text[c]) && c >= 0) { c--; } next = me[0].value[c];
last = c; c -= 1;
// search further until you find the first letter of the word initial = c;
while (/[\S]/.test(text[c]) && c >= 0) { c--; } prevIsGood = true;
first = c + 1; while (prevIsGood && c >= 0) {
c -= 1;
if (text[first] === options.key) { prev = me[0].value[c];
completeStart = first + 1; stopFound = prev === options.key;
completeEnd = (options.key === ":" ? last - 1 : last); if (stopFound) {
prev = me[0].value[c - 1];
if (completeEnd >= completeStart) { if (!prev || /\s/.test(prev)) {
updateAutoComplete(options.dataSource(getTerm())); completeStart = c;
caretPosition = completeEnd = initial;
term = me[0].value.substring(c + 1, initial);
updateAutoComplete(options.dataSource(term));
return true;
}
} }
return true; prevIsGood = /[a-zA-Z\.]/.test(prev);
} }
} }
if (autoCompleting()) { // ESC
if (e.which === 27) {
if (completeStart !== null) {
closeAutocomplete();
return false;
}
return true;
}
if (completeStart !== null) {
caretPosition = Discourse.Utilities.caretPosition(me[0]);
// If we've backspaced past the beginning, cancel unless no key
if (caretPosition <= completeStart && options.key) {
closeAutocomplete();
return false;
}
// Keyboard codes! So 80's. // Keyboard codes! So 80's.
switch (e.which) { switch (e.which) {
case Keys.Esc: case 13:
closeAutocomplete(); case 39:
return false; case 9:
case Keys.Enter:
case Keys.RightArrow:
case Keys.Tab:
if (!autocompleteOptions) return true; if (!autocompleteOptions) return true;
if (selectedOption >= 0 && (userToComplete = autocompleteOptions[selectedOption])) { if (selectedOption >= 0 && (userToComplete = autocompleteOptions[selectedOption])) {
completeTerm(userToComplete); completeTerm(userToComplete);
@ -342,14 +361,14 @@ export default function(options) {
} }
e.stopImmediatePropagation(); e.stopImmediatePropagation();
return false; return false;
case Keys.UpArrow: case 38:
selectedOption = selectedOption - 1; selectedOption = selectedOption - 1;
if (selectedOption < 0) { if (selectedOption < 0) {
selectedOption = 0; selectedOption = 0;
} }
markSelected(); markSelected();
return false; return false;
case Keys.DownArrow: case 40:
total = autocompleteOptions.length; total = autocompleteOptions.length;
selectedOption = selectedOption + 1; selectedOption = selectedOption + 1;
if (selectedOption >= total) { if (selectedOption >= total) {
@ -360,11 +379,12 @@ export default function(options) {
} }
markSelected(); markSelected();
return false; return false;
case Keys.BackSpace: default:
caretPosition = Discourse.Utilities.caretPosition(me[0]) - 1; // otherwise they're typing - let's search for it!
completeEnd = caretPosition; completeEnd = caretPosition;
if (e.which === 8) {
caretPosition--;
}
if (caretPosition < 0) { if (caretPosition < 0) {
closeAutocomplete(); closeAutocomplete();
if (isInput) { if (isInput) {
@ -375,17 +395,23 @@ export default function(options) {
} }
return false; return false;
} }
term = me.val().substring(completeStart + (options.key ? 1 : 0), caretPosition);
if (completeEnd < completeStart) { if (e.which >= 48 && e.which <= 90) {
closeAutocomplete(); term += mapKeyPressToActualCharacter(e.shiftKey, e.which);
return true; } else if (e.which === 187) {
term += "+";
} else if (e.which === 189) {
term += (e.shiftKey) ? "_" : "-";
} else if (e.which === 220) {
term += (e.shiftKey) ? "|" : "]";
} else if (e.which === 222) {
term += (e.shiftKey) ? "\"" : "'";
} else if (e.which !== 8) {
term += ",";
} }
term = getTerm();
updateAutoComplete(options.dataSource(term)); updateAutoComplete(options.dataSource(term));
return true; return true;
default:
return true;
} }
} }
}); });