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.
10 * Creates a unique identifier based on the key provided.
11 * This identifier is of the form 'idHASH' where HASH is
12 * the concatenation of the keycodes of every character in the string.
13 * @param {string} key Key for which we want an identifier.
14 * @return {string} The unique id for key.
16 function createId(key) {
17 var hash = key.split('').map(
18 // Returns the key code for the character.
20 return character.charCodeAt(0);
26 Polymer('kb-altkey-data', {
29 * Retrieves a list of alternative keys to display on a long-press.
30 * @param {string} char The base character.
31 * @param {boolean=} opt_force If true, force the creation of a list
32 * even if empty. Used when constructing a set of alternates for keys
34 * @return {?Object.{id: string, list: string}}
36 getAltkeys: function(char, opt_force) {
53 * Registers lists of alternative keys displayed on a long-press.
54 * @param {Object.<string, Array.<string>>} data Mapping of characters to
55 * lists of alternatives.
57 registerAltkeys: function(data) {
58 for (var key in data) {
61 idMap[key] = id = createId(key);
62 altKeys[id] = data[key];
67 * Creates a list of alternate candidates to display in a popup on a
69 * @param {string} char The base character.
70 * @param {number} maxLeftOffset Limits the number of candidates
71 * displayed to the left of the base character to prevent running
72 * past the left edge of the keyboard.
73 * @param {number} maxRightOffset Limits the number of candidates
74 * displayed to the right of the base character to prvent running
75 * past the right edge of the keyboard.
76 * @param {string=} opt_additionalKeys Optional list of additional keys
77 * to include in the candidates list.
79 createAltkeySet: function(char,
83 var altKeys = this.getAltkeys(char, true /* forced */);
85 var list = altKeys.keys;
86 if (opt_additionalKeys)
87 list = opt_additionalKeys.split('').concat(list);
88 list = [char].concat(list);
90 var set = document.createElement('kb-altkey-set');
91 // Candiates are approximately in decreasing order of usage, and are
92 // arranged in a single row in the popup display. To reduce the
93 // expected length of the drag gesture for selecting a candidate,
94 // more likely candidates are placed in the center of the popup,
95 // which is achieved by alternately appending and prepending
96 // candiates in the alternatives popup.
100 for (var i = 0; i < list.length; i++) {
101 var key = document.createElement('kb-altkey');
102 key.textContent = list[i];
104 set.insertBefore(key, set.firstChild);
107 set.appendChild(key);
111 // Verify that there is room remaining for an additional character.
112 if (leftOffset == maxLeftOffset && rightOffset == maxRightOffset)
114 if (leftOffset == maxLeftOffset)
116 else if (rightOffset == maxRightOffset)
120 set.offset = leftOffset;