diff --git a/src/bootstrap-tagsinput.js b/src/bootstrap-tagsinput.js index c1bbd505..8ee8ed23 100644 --- a/src/bootstrap-tagsinput.js +++ b/src/bootstrap-tagsinput.js @@ -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, @@ -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) { @@ -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) { @@ -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]) { @@ -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]) { @@ -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); @@ -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; } /**