OLD | NEW |
1 // Copyright 2014 The ChromeOS IME Authors. All Rights Reserved. | 1 // Copyright 2014 The ChromeOS IME Authors. All Rights Reserved. |
2 // limitations under the License. | 2 // limitations under the License. |
3 // See the License for the specific language governing permissions and | 3 // See the License for the specific language governing permissions and |
4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
5 // distributed under the License is distributed on an "AS-IS" BASIS, | 5 // distributed under the License is distributed on an "AS-IS" BASIS, |
6 // Unless required by applicable law or agreed to in writing, software | 6 // Unless required by applicable law or agreed to in writing, software |
7 // | 7 // |
8 // http://www.apache.org/licenses/LICENSE-2.0 | 8 // http://www.apache.org/licenses/LICENSE-2.0 |
9 // | 9 // |
10 // You may obtain a copy of the License at | 10 // You may obtain a copy of the License at |
11 // you may not use this file except in compliance with the License. | 11 // you may not use this file except in compliance with the License. |
12 // Licensed under the Apache License, Version 2.0 (the "License"); | 12 // Licensed under the Apache License, Version 2.0 (the "License"); |
13 // | 13 // |
14 goog.provide('i18n.input.chrome.inputview.Accents'); | 14 goog.provide('i18n.input.chrome.inputview.Accents'); |
15 | 15 |
16 goog.require('goog.dom'); | 16 goog.require('goog.dom'); |
| 17 goog.require('goog.dom.TagName'); |
| 18 goog.require('goog.dom.classlist'); |
17 goog.require('goog.math.Coordinate'); | 19 goog.require('goog.math.Coordinate'); |
18 goog.require('goog.style'); | 20 goog.require('goog.style'); |
| 21 goog.require('i18n.input.chrome.inputview.Css'); |
19 goog.require('i18n.input.chrome.inputview.util'); | 22 goog.require('i18n.input.chrome.inputview.util'); |
20 | 23 |
21 | 24 |
22 goog.scope(function() { | 25 goog.scope(function() { |
23 var Accents = i18n.input.chrome.inputview.Accents; | 26 var Accents = i18n.input.chrome.inputview.Accents; |
| 27 var Css = i18n.input.chrome.inputview.Css; |
24 | 28 |
25 | 29 |
26 /** | 30 /** |
27 * The highlighted element. | 31 * The highlighted element. |
28 * | 32 * |
29 * @type {Element} | 33 * @type {Element} |
30 * @private | 34 * @private |
31 */ | 35 */ |
32 Accents.highlightedItem_ = null; | 36 Accents.highlightedItem_ = null; |
33 | 37 |
(...skipping 15 matching lines...) Expand all Loading... |
49 * | 53 * |
50 * @param {number} x The x position of finger in screen coordinate system. | 54 * @param {number} x The x position of finger in screen coordinate system. |
51 * @param {number} y The y position of finger in screen coordinate system. | 55 * @param {number} y The y position of finger in screen coordinate system. |
52 * @param {number} offset The offset to cancel highlight. | 56 * @param {number} offset The offset to cancel highlight. |
53 * @private | 57 * @private |
54 */ | 58 */ |
55 Accents.highlightItem_ = function(x, y, offset) { | 59 Accents.highlightItem_ = function(x, y, offset) { |
56 var highlightedItem = Accents.getHighlightedItem_(x, y, offset); | 60 var highlightedItem = Accents.getHighlightedItem_(x, y, offset); |
57 if (Accents.highlightedItem_ != highlightedItem) { | 61 if (Accents.highlightedItem_ != highlightedItem) { |
58 if (Accents.highlightedItem_) { | 62 if (Accents.highlightedItem_) { |
59 Accents.highlightedItem_.classList.remove('highlight'); | 63 goog.dom.classlist.remove(Accents.highlightedItem_, |
| 64 i18n.input.chrome.inputview.Css.ELEMENT_HIGHLIGHT); |
60 } | 65 } |
61 Accents.highlightedItem_ = highlightedItem; | 66 Accents.highlightedItem_ = highlightedItem; |
62 if (Accents.highlightedItem_ && | 67 if (Accents.highlightedItem_ && |
63 Accents.highlightedItem_.textContent.trim()) { | 68 Accents.highlightedItem_.textContent.trim()) { |
64 Accents.highlightedItem_.classList.add('highlight'); | 69 goog.dom.classlist.add(Accents.highlightedItem_, |
| 70 i18n.input.chrome.inputview.Css.ELEMENT_HIGHLIGHT); |
65 } | 71 } |
66 } | 72 } |
67 }; | 73 }; |
68 | 74 |
69 | 75 |
70 /** | 76 /** |
71 * Gets the higlighted item from |x| and |y| position. | 77 * Gets the higlighted item from |x| and |y| position. |
72 * @param {number} x The x position of finger in screen coordinate system. | 78 * @param {number} x The x position of finger in screen coordinate system. |
73 * @param {number} y The y position of finger in screen coordinate system. | 79 * @param {number} y The y position of finger in screen coordinate system. |
74 * @param {number} offset The offset to cancel highlight. | 80 * @param {number} offset The offset to cancel highlight. |
75 * @private | 81 * @private |
76 */ | 82 */ |
77 Accents.getHighlightedItem_ = function(x, y, offset) { | 83 Accents.getHighlightedItem_ = function(x, y, offset) { |
78 var dom = goog.dom.getDomHelper(); | 84 var dom = goog.dom.getDomHelper(); |
79 var row = null; | 85 var row = null; |
80 var rows = dom.getElementsByClass('accent-row'); | 86 var rows = dom.getElementsByClass(i18n.input.chrome.inputview.Css.ACCENT_ROW); |
81 for (var i = 0; i < rows.length; i++) { | 87 for (var i = 0; i < rows.length; i++) { |
82 var coordinate = goog.style.getClientPosition(rows[i]); | 88 var coordinate = goog.style.getClientPosition(rows[i]); |
83 var size = goog.style.getSize(rows[i]); | 89 var size = goog.style.getSize(rows[i]); |
84 var screenYStart = coordinate.y + window.screenY; | 90 var screenYStart = coordinate.y + window.screenY; |
85 screenYStart = i == 0 ? screenYStart - offset : screenYStart; | 91 screenYStart = i == 0 ? screenYStart - offset : screenYStart; |
86 var screenYEnd = coordinate.y + window.screenY + size.height; | 92 var screenYEnd = coordinate.y + window.screenY + size.height; |
87 screenYEnd = i == rows.length - 1 ? screenYEnd + offset : screenYEnd; | 93 screenYEnd = i == rows.length - 1 ? screenYEnd + offset : screenYEnd; |
88 if (screenYStart < y && screenYEnd > y) { | 94 if (screenYStart < y && screenYEnd > y) { |
89 row = rows[i]; | 95 row = rows[i]; |
90 break; | 96 break; |
(...skipping 23 matching lines...) Expand all Loading... |
114 * @param {!Array.<string>} accents The accents to display. | 120 * @param {!Array.<string>} accents The accents to display. |
115 * @param {!number} numOfColumns The number of colums of this accents window. | 121 * @param {!number} numOfColumns The number of colums of this accents window. |
116 * @param {!number} numOfRows The number of rows of this accents window. | 122 * @param {!number} numOfRows The number of rows of this accents window. |
117 * @param {number} width The width of accent key. | 123 * @param {number} width The width of accent key. |
118 * @param {number} height The height of accent key. | 124 * @param {number} height The height of accent key. |
119 * @param {number} startKeyIndex The index of the start key in bottom row. | 125 * @param {number} startKeyIndex The index of the start key in bottom row. |
120 * @private | 126 * @private |
121 */ | 127 */ |
122 Accents.setAccents_ = function(accents, numOfColumns, numOfRows, width, | 128 Accents.setAccents_ = function(accents, numOfColumns, numOfRows, width, |
123 height, startKeyIndex) { | 129 height, startKeyIndex) { |
124 var container = document.createElement('div'); | 130 var TagName = goog.dom.TagName; |
| 131 var dom = goog.dom.getDomHelper(); |
| 132 var container = dom.createDom(TagName.DIV, Css.ACCENT_CONTAINER); |
| 133 goog.dom.setProperties(container, { |
| 134 'id' : 'container' |
| 135 }); |
125 container.id = 'container'; | 136 container.id = 'container'; |
126 container.classList.add('accent-container'); | |
127 | 137 |
128 var orderedAccents = Accents.reorderAccents_(accents, numOfColumns, numOfRows, | 138 var orderedAccents = Accents.reorderAccents_(accents, numOfColumns, numOfRows, |
129 startKeyIndex); | 139 startKeyIndex); |
130 var row = null; | 140 var row = null; |
131 for (var i = 0; i < orderedAccents.length; i++) { | 141 for (var i = 0; i < orderedAccents.length; i++) { |
132 var keyElem = document.createElement('div'); | 142 var keyElem = dom.createDom(TagName.DIV, Css.ACCENT_KEY); |
133 // Even if this is an empty key, we still need to add textDiv. Otherwise, | 143 // Even if this is an empty key, we still need to add textDiv. Otherwise, |
134 // the keys have layout issues. | 144 // the keys have layout issues. |
135 var textDiv = document.createElement('div'); | 145 var textDiv = dom.createElement(TagName.DIV); |
136 textDiv.textContent = | 146 var text = i18n.input.chrome.inputview.util.getVisibleCharacter( |
137 i18n.input.chrome.inputview.util.getVisibleCharacter( | 147 orderedAccents[i]); |
138 orderedAccents[i]); | 148 textDiv.textContent = text; |
139 textDiv.style.lineHeight = height + 'px'; | 149 // If accent is a word use a smaller font size. |
140 keyElem.appendChild(textDiv); | 150 goog.dom.classlist.add(textDiv, text.length > 1 ? Css.FONT_SMALL : |
| 151 Css.FONT); |
| 152 |
| 153 goog.style.setStyle(textDiv, 'lineHeight', height + 'px'); |
| 154 dom.appendChild(keyElem, textDiv); |
141 if (!orderedAccents[i]) { | 155 if (!orderedAccents[i]) { |
142 keyElem.classList.add('empty-key'); | 156 goog.dom.classlist.add(keyElem, Css.ACCENT_EMPTY_KEY); |
143 } | 157 } |
144 keyElem.style.width = width + 'px'; | 158 goog.style.setSize(keyElem, width, height); |
145 keyElem.style.height = height + 'px'; | |
146 if (i % numOfColumns == 0) { | 159 if (i % numOfColumns == 0) { |
147 if (row) { | 160 if (row) { |
148 container.appendChild(row); | 161 container.appendChild(row); |
149 } | 162 } |
150 row = document.createElement('div'); | 163 row = dom.createDom(TagName.DIV, Css.ACCENT_ROW); |
151 row.classList.add('accent-row'); | |
152 } | 164 } |
153 row.appendChild(keyElem); | 165 dom.appendChild(row, keyElem); |
154 } | 166 } |
155 container.appendChild(row); | 167 dom.appendChild(container, row); |
156 document.body.appendChild(container); | 168 dom.appendChild(document.body, container); |
157 }; | 169 }; |
158 | 170 |
159 | 171 |
160 /** | 172 /** |
161 * Generates the reordered accents which is optimized for creating accent key | 173 * Generates the reordered accents which is optimized for creating accent key |
162 * elements sequentially(from top to bottom, left to right). | 174 * elements sequentially(from top to bottom, left to right). |
163 * Accent in |accents| is ordered according to its frequency(more frequently | 175 * Accent in |accents| is ordered according to its frequency(more frequently |
164 * used appears first). Once reordered, the more frequently used accents will be | 176 * used appears first). Once reordered, the more frequently used accents will be |
165 * positioned closer to the parent key. See tests for example. | 177 * positioned closer to the parent key. See tests for example. |
166 * @param {!Array.<string>} accents The accents to display. | 178 * @param {!Array.<string>} accents The accents to display. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 } | 213 } |
202 | 214 |
203 return orderedAccents; | 215 return orderedAccents; |
204 }; | 216 }; |
205 | 217 |
206 goog.exportSymbol('accents.highlightedAccent', Accents.getHighlightedAccent_); | 218 goog.exportSymbol('accents.highlightedAccent', Accents.getHighlightedAccent_); |
207 goog.exportSymbol('accents.highlightItem', Accents.highlightItem_); | 219 goog.exportSymbol('accents.highlightItem', Accents.highlightItem_); |
208 goog.exportSymbol('accents.setAccents', Accents.setAccents_); | 220 goog.exportSymbol('accents.setAccents', Accents.setAccents_); |
209 | 221 |
210 }); // goog.scope | 222 }); // goog.scope |
OLD | NEW |