1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 (function() { 6 7 var metadata = null; 8 9 function getKeyCodeAndModifiers(keyCodeIndex) { 10 if (!metadata) 11 metadata = document.createElement('kb-key-codes'); 12 return metadata.GetKeyCodeAndModifiers(keyCodeIndex); 13 } 14 15 Polymer('kb-key-sequence', { 16 /** 17 * Generates the DOM structure to replace (expand) this kb-key-sequence. 18 */ 19 generateDom: function() { 20 var replacement = document.createDocumentFragment(); 21 var newKeys = this.getAttribute('keys'); 22 var newHintTexts = this.getAttribute('hintTexts'); 23 var keyCodes = this.getAttribute('hintCodes') || newKeys; 24 var invert = this.getAttribute('invert'); 25 if (newKeys) { 26 if (newHintTexts && newKeys.length != newHintTexts.length) { 27 console.error('keys and hintTexts do not match'); 28 return; 29 } 30 if (keyCodes && newKeys.length != keyCodes.length) { 31 console.error('keys and keyCodes do not match'); 32 return; 33 } 34 35 for (var i = 0; i < newKeys.length; i++) { 36 var key = document.createElement('kb-key'); 37 key.innerText = newKeys[i]; 38 key.accents = newKeys[i]; 39 if (newHintTexts) 40 key.hintText = newHintTexts[i]; 41 var keyCodeIndex = keyCodes[i]; 42 if (invert) { 43 key.invert = true; 44 key.char = newKeys[i]; 45 keyCodeIndex = key.hintText; 46 } 47 var state = getKeyCodeAndModifiers(keyCodeIndex); 48 if (state) { 49 key.keyCode = state.keyCode; 50 key.keyName = state.keyName; 51 key.shiftModifier = state.shiftModifier; 52 } 53 replacement.appendChild(key); 54 } 55 } 56 return replacement; 57 } 58 }); 59 })(); 60 61