Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Commit

Permalink
Use event.key rather than event.which
Browse files Browse the repository at this point in the history
  • Loading branch information
hrobertson committed Jul 25, 2017
1 parent 6468802 commit 52506cb
Showing 1 changed file with 14 additions and 36 deletions.
50 changes: 14 additions & 36 deletions src/bootstrap-tagsinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
addOnBlur: true,
maxTags: undefined,
maxChars: undefined,
confirmKeys: [13, 44],
deleteKeys: [8, 46],
confirmKeys: ['Enter', ','],
deleteKeys: ['Backspace', 'Delete'],
delimiter: ',',
delimiterRegex: null,
cancelConfirmKeysOnEmpty: false,
Expand Down Expand Up @@ -405,10 +405,10 @@
return;
}

switch (event.which) {
switch (event.key) {
// BACKSPACE
case 8:
if(self.options.deleteKeys && jQuery.inArray(event.which, self.options.deleteKeys) > -1){
case 'Backspace':
if(self.options.deleteKeys && keyInList(event, self.options.deleteKeys) > -1){
if (doGetCaretPosition($input[0]) === 0) {
var prev = $inputWrapper.prev();
if (prev.length) {
Expand All @@ -419,8 +419,8 @@
break;

// DELETE
case 46:
if(self.options.deleteKeys && jQuery.inArray(event.which, self.options.deleteKeys) > -1) {
case 'Delete':
if(self.options.deleteKeys && keyInList(event, self.options.deleteKeys) > -1) {
if (doGetCaretPosition($input[0]) === 0) {
var next = $inputWrapper.next();
if (next.length) {
Expand All @@ -431,7 +431,7 @@
break;

// LEFT ARROW
case 37:
case 'ArrowLeft':
// Try to move the input before the previous tag
var $prevTag = $inputWrapper.prev();
if ($input.val().length === 0 && $prevTag[0]) {
Expand All @@ -440,7 +440,7 @@
}
break;
// RIGHT ARROW
case 39:
case 'ArrowRight':
// Try to move the input after the next tag
var $nextTag = $inputWrapper.next();
if ($input.val().length === 0 && $nextTag[0]) {
Expand Down Expand Up @@ -469,7 +469,7 @@

var text = $input.val(),
maxLengthReached = self.options.maxChars && text.length >= self.options.maxChars;
if (self.options.freeInput && (keyCombinationInList(event, self.options.confirmKeys) || maxLengthReached)) {
if (self.options.freeInput && (keyInList(event, self.options.confirmKeys) || maxLengthReached)) {
// Only attempt to add a tag if there is data in the field
if (text.length !== 0) {
self.add(maxLengthReached ? text.substr(0, self.options.maxChars) : text);
Expand Down Expand Up @@ -661,32 +661,10 @@
}

/**
* Returns boolean indicates whether user has pressed an expected key combination.
* @param object keyPressEvent: JavaScript event object, refer
* http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
* @param object lookupList: expected key combinations, as in:
* [13, {which: 188, shiftKey: true}]
*/
function keyCombinationInList(keyPressEvent, lookupList) {
var found = false;
$.each(lookupList, function (index, keyCombination) {
if (typeof (keyCombination) === 'number' && keyPressEvent.which === keyCombination) {
found = true;
return false;
}

if (keyPressEvent.which === keyCombination.which) {
var alt = !keyCombination.hasOwnProperty('altKey') || keyPressEvent.altKey === keyCombination.altKey,
shift = !keyCombination.hasOwnProperty('shiftKey') || keyPressEvent.shiftKey === keyCombination.shiftKey,
ctrl = !keyCombination.hasOwnProperty('ctrlKey') || keyPressEvent.ctrlKey === keyCombination.ctrlKey;
if (alt && shift && ctrl) {
found = true;
return false;
}
}
});

return found;
* Use event.key rather than event.which as which is deprecated.
*/
function keyInList(keyPressEvent, lookupList) {
return $.inArray(keyPressEvent.key, lookupList) !== -1;
}

/**
Expand Down

0 comments on commit 52506cb

Please sign in to comment.