Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configurable submitKeys #17058

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var _ = function (input, o) {
minChars: 2,
maxItems: 10,
autoFirst: false,
submitKeys: [ _.KEY_ENTER ],
data: _.DATA,
filter: _.FILTER_CONTAINS,
sort: o.sort === false ? false : _.SORT_BYLENGTH,
Expand Down Expand Up @@ -64,16 +65,16 @@ var _ = function (input, o) {
// If the dropdown `ul` is in view, then act on keydown for the following keys:
// Enter / Esc / Up / Down
if(me.opened) {
if (c === 13 && me.selected) { // Enter
if (me.submitKeys.indexOf(c) > -1 && me.selected) { // Enter
evt.preventDefault();
me.select();
}
else if (c === 27) { // Esc
else if (c === _.KEY_ESCAPE) { // Esc
me.close({ reason: "esc" });
}
else if (c === 38 || c === 40) { // Down/Up arrow
else if (c === _.KEY_UP || c === _.KEY_DOWN) { // Down/Up arrow
evt.preventDefault();
me[c === 38? "previous" : "next"]();
me[c === _.KEY_UP ? "previous" : "next"]();
}
}
}
Expand Down Expand Up @@ -267,13 +268,51 @@ _.prototype = {
else {
this.close({ reason: "nomatches" });
}
},

// set keyCodes for tag submitting (can be a single keyCode or an array of keyCodes)
setSubmitKeys: function( keys ) {
var newSubmitKeys = [];
if (!keys.length) keys = [ keys ];
for (var i=0; i < keys.length; i++) {
var tok = typeof keys[i];
switch (tok) {
case "object":
case "boolean":
case "function":
continue;
case "string":
var s = keys[i].toUpperCase();
for (var j=0; j < s.length; j++) {
newSubmitKeys.push( s.charCodeAt(j) );
}
continue;
}
newSubmitKeys.push( keys[i] );
}

if (!newSubmitKeys.length) return false;

this.submitKeys = newSubmitKeys;

return true;
}
};

// Static methods/properties

_.all = [];

// Keycodes
_.KEY_BACKSPACE = 8;
_.KEY_COMMA = 188;
_.KEY_TAB = 9;
_.KEY_ENTER = 13;
_.KEY_ESCAPE = 27;
_.KEY_UP = 38;
_.KEY_DOWN = 40;


_.FILTER_CONTAINS = function (text, input) {
return RegExp($.regExpEscape(input.trim()), "i").test(text);
};
Expand Down