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