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 function createId(char) { 14 return 'id' + char.charCodeAt(0); 15 } 16 17 Polymer('kb-altkey-data', { 18 19 /** 20 * Retrieves a list of alternative keys to display on a long-press. 21 * @param {string} char The base character. 22 * @param {boolean=} opt_force If true, force the creation of a list 23 * even if empty. Used when constructing a set of alternates for keys 24 * with superscripts. 25 * @return {?Object.{id: string, list: string}} 26 */ 27 getAltkeys: function(char, opt_force) { 28 var id = idMap[char]; 29 if (id) { 30 return { 31 'id': id, 32 'keys': altKeys[id] 33 }; 34 } 35 if (opt_force) { 36 return { 37 'id': createId(char), 38 'keys': [] 39 }; 40 } 41 }, 42 43 /** 44 * Registers lists of alternative keys displayed on a long-press. 45 * @param {Object.<string, Array.<string>>} data Mapping of characters to 46 * lists of alternatives. 47 */ 48 registerAltkeys: function(data) { 49 for (var key in data) { 50 var id = idMap[key]; 51 if (!id) 52 idMap[key] = id = createId(key); 53 altKeys[id] = data[key]; 54 } 55 }, 56 57 /** 58 * Creates a list of alternate candidates to display in a popup on a 59 * long-press. 60 * @param {string} char The base character. 61 * @param {number} maxLeftOffset Limits the number of candidates 62 * displayed to the left of the base character to prevent running 63 * past the left edge of the keyboard. 64 * @param {number} maxRightOffset Limits the number of candidates 65 * displayed to the right of the base character to prvent running 66 * past the right edge of the keyboard. 67 * @param {string=} opt_additionalKeys Optional list of additional keys 68 * to include in the candidates list. 69 */ 70 createAltkeySet: function(char, 71 maxLeftOffset, 72 maxRightOffset, 73 opt_additionalKeys) { 74 var altKeys = this.getAltkeys(char, true /* forced */); 75 if (altKeys) { 76 var list = altKeys.keys; 77 if (opt_additionalKeys) 78 list = opt_additionalKeys.split('').concat(list); 79 list = [char].concat(list); 80 81 var set = document.createElement('kb-altkey-set'); 82 // Candiates are approximately in decreasing order of usage, and are 83 // arranged in a single row in the popup display. To reduce the 84 // expected length of the drag gesture for selecting a candidate, 85 // more likely candidates are placed in the center of the popup, 86 // which is achieved by alternately appending and prepending 87 // candiates in the alternatives popup. 88 var prepend = false; 89 var leftOffset = 0; 90 var rightOffset = 0; 91 for (var i = 0; i < list.length; i++) { 92 var key = document.createElement('kb-altkey'); 93 key.textContent = list[i]; 94 if (prepend) { 95 set.insertBefore(key, set.firstChild); 96 leftOffset++; 97 } else { 98 set.appendChild(key); 99 rightOffset++; 100 } 101 prepend = !prepend; 102 // Verify that there is room remaining for an additional character. 103 if (leftOffset == maxLeftOffset && rightOffset == maxRightOffset) 104 break; 105 if (leftOffset == maxLeftOffset) 106 prepend = false; 107 else if (rightOffset == maxRightOffset) 108 prepend = true; 109 } 110 set.id = altKeys.id; 111 set.offset = leftOffset; 112 return set; 113 } 114 }, 115 116 }); 117 })(); 118 </script> 119 </polymer-element> 120