1 <!-- 2 -- Copyright 2013 The Chromium Authors. All rights reserved. 3 -- Use of this source code is governed by a BSD-style license that can be 4 -- found in the LICENSE file. 5 --> 6 7 <polymer-element name="kb-altkey-data" attributes="char list"> 8 <script> 9 (function() { 10 var altKeys = {}; 11 var idMap = {}; 12 13 /** 14 * Creates a unique identifier based on the key provided. 15 * This identifier is of the form 'idHASH' where HASH is 16 * the concatenation of the keycodes of every character in the string. 17 * @param {string} key Key for which we want an identifier. 18 * @return {string} The unique id for key. 19 */ 20 function createId(key) { 21 var hash = key.split('').map( 22 // Returns the key code for the character. 23 function(character) { 24 return character.charCodeAt(0); 25 } 26 ).join(''); 27 return 'id' + hash; 28 } 29 30 Polymer('kb-altkey-data', { 31 32 /** 33 * Retrieves a list of alternative keys to display on a long-press. 34 * @param {string} char The base character. 35 * @param {boolean=} opt_force If true, force the creation of a list 36 * even if empty. Used when constructing a set of alternates for keys 37 * with hintTexts. 38 * @return {?Object.{id: string, list: string}} 39 */ 40 getAltkeys: function(char, opt_force) { 41 var id = idMap[char]; 42 if (id) { 43 return { 44 'id': id, 45 'keys': altKeys[id] 46 }; 47 } 48 if (opt_force) { 49 return { 50 'id': createId(char), 51 'keys': [] 52 }; 53 } 54 }, 55 56 /** 57 * Registers lists of alternative keys displayed on a long-press. 58 * @param {Object.<string, Array.<string>>} data Mapping of characters to 59 * lists of alternatives. 60 */ 61 registerAltkeys: function(data) { 62 for (var key in data) { 63 var id = idMap[key]; 64 if (!id) 65 idMap[key] = id = createId(key); 66 altKeys[id] = data[key]; 67 } 68 }, 69 70 /** 71 * Creates a list of alternate candidates to display in a popup on a 72 * long-press. 73 * @param {string} char The base character. 74 * @param {number} maxLeftOffset Limits the number of candidates 75 * displayed to the left of the base character to prevent running 76 * past the left edge of the keyboard. 77 * @param {number} maxRightOffset Limits the number of candidates 78 * displayed to the right of the base character to prvent running 79 * past the right edge of the keyboard. 80 * @param {string=} opt_additionalKeys Optional list of additional keys 81 * to include in the candidates list. 82 */ 83 createAltkeySet: function(char, 84 maxLeftOffset, 85 maxRightOffset, 86 opt_additionalKeys) { 87 var altKeys = this.getAltkeys(char, true /* forced */); 88 if (altKeys) { 89 var list = altKeys.keys; 90 if (opt_additionalKeys) 91 list = opt_additionalKeys.split('').concat(list); 92 list = [char].concat(list); 93 94 var set = document.createElement('kb-altkey-set'); 95 // Candiates are approximately in decreasing order of usage, and are 96 // arranged in a single row in the popup display. To reduce the 97 // expected length of the drag gesture for selecting a candidate, 98 // more likely candidates are placed in the center of the popup, 99 // which is achieved by alternately appending and prepending 100 // candiates in the alternatives popup. 101 var prepend = false; 102 var leftOffset = 0; 103 var rightOffset = 0; 104 for (var i = 0; i < list.length; i++) { 105 var key = document.createElement('kb-altkey'); 106 key.textContent = list[i]; 107 if (prepend) { 108 set.insertBefore(key, set.firstChild); 109 leftOffset++; 110 } else { 111 set.appendChild(key); 112 rightOffset++; 113 } 114 prepend = !prepend; 115 // Verify that there is room remaining for an additional character. 116 if (leftOffset == maxLeftOffset && rightOffset == maxRightOffset) 117 break; 118 if (leftOffset == maxLeftOffset) 119 prepend = false; 120 else if (rightOffset == maxRightOffset) 121 prepend = true; 122 } 123 set.id = altKeys.id; 124 set.offset = leftOffset; 125 return set; 126 } 127 }, 128 129 }); 130 })(); 131 </script> 132 </polymer-element> 133