update components
This commit is contained in:
parent
fe7276b4e4
commit
301033336a
18 changed files with 306 additions and 216 deletions
|
@ -21,46 +21,10 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
* Values taken from: http://www.w3.org/TR/2007/WD-DOM-Level-3-Events-20071221/keyset.html#KeySet-Set
|
||||
*/
|
||||
var KEY_IDENTIFIER = {
|
||||
'U+0008': 'backspace',
|
||||
'U+0009': 'tab',
|
||||
'U+001B': 'esc',
|
||||
'U+0020': 'space',
|
||||
'U+002A': '*',
|
||||
'U+0030': '0',
|
||||
'U+0031': '1',
|
||||
'U+0032': '2',
|
||||
'U+0033': '3',
|
||||
'U+0034': '4',
|
||||
'U+0035': '5',
|
||||
'U+0036': '6',
|
||||
'U+0037': '7',
|
||||
'U+0038': '8',
|
||||
'U+0039': '9',
|
||||
'U+0041': 'a',
|
||||
'U+0042': 'b',
|
||||
'U+0043': 'c',
|
||||
'U+0044': 'd',
|
||||
'U+0045': 'e',
|
||||
'U+0046': 'f',
|
||||
'U+0047': 'g',
|
||||
'U+0048': 'h',
|
||||
'U+0049': 'i',
|
||||
'U+004A': 'j',
|
||||
'U+004B': 'k',
|
||||
'U+004C': 'l',
|
||||
'U+004D': 'm',
|
||||
'U+004E': 'n',
|
||||
'U+004F': 'o',
|
||||
'U+0050': 'p',
|
||||
'U+0051': 'q',
|
||||
'U+0052': 'r',
|
||||
'U+0053': 's',
|
||||
'U+0054': 't',
|
||||
'U+0055': 'u',
|
||||
'U+0056': 'v',
|
||||
'U+0057': 'w',
|
||||
'U+0058': 'x',
|
||||
'U+0059': 'y',
|
||||
'U+005A': 'z',
|
||||
'U+007F': 'del'
|
||||
};
|
||||
|
||||
|
@ -72,6 +36,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
* Values from: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode#Value_of_keyCode
|
||||
*/
|
||||
var KEY_CODE = {
|
||||
8: 'backspace',
|
||||
9: 'tab',
|
||||
13: 'enter',
|
||||
27: 'esc',
|
||||
|
@ -100,16 +65,6 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
'meta': 'metaKey'
|
||||
};
|
||||
|
||||
/**
|
||||
* KeyboardEvent.key is mostly represented by printable character made by
|
||||
* the keyboard, with unprintable keys labeled nicely.
|
||||
*
|
||||
* However, on OS X, Alt+char can make a Unicode character that follows an
|
||||
* Apple-specific mapping. In this case, we
|
||||
* fall back to .keyCode.
|
||||
*/
|
||||
var KEY_CHAR = /[a-z0-9*]/;
|
||||
|
||||
/**
|
||||
* Matches a keyIdentifier string.
|
||||
*/
|
||||
|
@ -130,14 +85,12 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
var validKey = '';
|
||||
if (key) {
|
||||
var lKey = key.toLowerCase();
|
||||
if (lKey.length == 1) {
|
||||
if (KEY_CHAR.test(lKey)) {
|
||||
validKey = lKey;
|
||||
}
|
||||
if (lKey === ' ' || SPACE_KEY.test(lKey)) {
|
||||
validKey = 'space';
|
||||
} else if (lKey.length == 1) {
|
||||
validKey = lKey;
|
||||
} else if (ARROW_KEY.test(lKey)) {
|
||||
validKey = lKey.replace('arrow', '');
|
||||
} else if (SPACE_KEY.test(lKey)) {
|
||||
validKey = 'space';
|
||||
} else if (lKey == 'multiply') {
|
||||
// numpad '*' can map to Multiply on IE/Windows
|
||||
validKey = '*';
|
||||
|
@ -151,8 +104,11 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
function transformKeyIdentifier(keyIdent) {
|
||||
var validKey = '';
|
||||
if (keyIdent) {
|
||||
if (IDENT_CHAR.test(keyIdent)) {
|
||||
if (keyIdent in KEY_IDENTIFIER) {
|
||||
validKey = KEY_IDENTIFIER[keyIdent];
|
||||
} else if (IDENT_CHAR.test(keyIdent)) {
|
||||
keyIdent = parseInt(keyIdent.replace('U+', '0x'), 16);
|
||||
validKey = String.fromCharCode(keyIdent).toLowerCase();
|
||||
} else {
|
||||
validKey = keyIdent.toLowerCase();
|
||||
}
|
||||
|
@ -192,15 +148,24 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
transformKey(keyEvent.detail.key) || '';
|
||||
}
|
||||
|
||||
function keyComboMatchesEvent(keyCombo, keyEvent) {
|
||||
return normalizedKeyForEvent(keyEvent) === keyCombo.key &&
|
||||
!!keyEvent.shiftKey === !!keyCombo.shiftKey &&
|
||||
!!keyEvent.ctrlKey === !!keyCombo.ctrlKey &&
|
||||
!!keyEvent.altKey === !!keyCombo.altKey &&
|
||||
!!keyEvent.metaKey === !!keyCombo.metaKey;
|
||||
function keyComboMatchesEvent(keyCombo, event, eventKey) {
|
||||
return eventKey === keyCombo.key &&
|
||||
(!keyCombo.hasModifiers || (
|
||||
!!event.shiftKey === !!keyCombo.shiftKey &&
|
||||
!!event.ctrlKey === !!keyCombo.ctrlKey &&
|
||||
!!event.altKey === !!keyCombo.altKey &&
|
||||
!!event.metaKey === !!keyCombo.metaKey)
|
||||
);
|
||||
}
|
||||
|
||||
function parseKeyComboString(keyComboString) {
|
||||
if (keyComboString.length === 1) {
|
||||
return {
|
||||
combo: keyComboString,
|
||||
key: keyComboString,
|
||||
event: 'keydown'
|
||||
};
|
||||
}
|
||||
return keyComboString.split('+').reduce(function(parsedKeyCombo, keyComboPart) {
|
||||
var eventParts = keyComboPart.split(':');
|
||||
var keyName = eventParts[0];
|
||||
|
@ -208,6 +173,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
|
||||
if (keyName in MODIFIER_KEYS) {
|
||||
parsedKeyCombo[MODIFIER_KEYS[keyName]] = true;
|
||||
parsedKeyCombo.hasModifiers = true;
|
||||
} else {
|
||||
parsedKeyCombo.key = keyName;
|
||||
parsedKeyCombo.event = event || 'keydown';
|
||||
|
@ -220,12 +186,11 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
}
|
||||
|
||||
function parseEventString(eventString) {
|
||||
return eventString.split(' ').map(function(keyComboString) {
|
||||
return eventString.trim().split(' ').map(function(keyComboString) {
|
||||
return parseKeyComboString(keyComboString);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `Polymer.IronA11yKeysBehavior` provides a normalized interface for processing
|
||||
* keyboard commands that pertain to [WAI-ARIA best practices](http://www.w3.org/TR/wai-aria-practices/#kbd_general_binding).
|
||||
|
@ -321,14 +286,12 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
|
||||
keyboardEventMatchesKeys: function(event, eventString) {
|
||||
var keyCombos = parseEventString(eventString);
|
||||
var index;
|
||||
|
||||
for (index = 0; index < keyCombos.length; ++index) {
|
||||
if (keyComboMatchesEvent(keyCombos[index], event)) {
|
||||
var eventKey = normalizedKeyForEvent(event);
|
||||
for (var i = 0; i < keyCombos.length; ++i) {
|
||||
if (keyComboMatchesEvent(keyCombos[i], event, eventKey)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
|
@ -356,6 +319,15 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
for (var eventString in this._imperativeKeyBindings) {
|
||||
this._addKeyBinding(eventString, this._imperativeKeyBindings[eventString]);
|
||||
}
|
||||
|
||||
// Give precedence to combos with modifiers to be checked first.
|
||||
for (var eventName in this._keyBindings) {
|
||||
this._keyBindings[eventName].sort(function (kb1, kb2) {
|
||||
var b1 = kb1[0].hasModifiers;
|
||||
var b2 = kb2[0].hasModifiers;
|
||||
return (b1 === b2) ? 0 : b1 ? -1 : 1;
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
_addKeyBinding: function(eventString, handlerName) {
|
||||
|
@ -411,14 +383,23 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
event.stopPropagation();
|
||||
}
|
||||
|
||||
keyBindings.forEach(function(keyBinding) {
|
||||
var keyCombo = keyBinding[0];
|
||||
var handlerName = keyBinding[1];
|
||||
// if event has been already prevented, don't do anything
|
||||
if (event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!event.defaultPrevented && keyComboMatchesEvent(keyCombo, event)) {
|
||||
var eventKey = normalizedKeyForEvent(event);
|
||||
for (var i = 0; i < keyBindings.length; i++) {
|
||||
var keyCombo = keyBindings[i][0];
|
||||
var handlerName = keyBindings[i][1];
|
||||
if (keyComboMatchesEvent(keyCombo, event, eventKey)) {
|
||||
this._triggerKeyHandler(keyCombo, handlerName, event);
|
||||
// exit the loop if eventDefault was prevented
|
||||
if (event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}, this);
|
||||
}
|
||||
},
|
||||
|
||||
_triggerKeyHandler: function(keyCombo, handlerName, keyboardEvent) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue