1 // Copyright 2014 The ChromeOS IME Authors. All Rights Reserved.
2 // limitations under the License.
3 // See the License for the specific language governing permissions and
4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5 // distributed under the License is distributed on an "AS-IS" BASIS,
6 // Unless required by applicable law or agreed to in writing, software
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // You may obtain a copy of the License at
11 // you may not use this file except in compliance with the License.
12 // Licensed under the Apache License, Version 2.0 (the "License");
14 goog.provide('i18n.input.chrome.inputview.elements.content.CompactKey');
16 goog.require('goog.array');
17 goog.require('goog.dom');
18 goog.require('goog.dom.TagName');
19 goog.require('goog.dom.classlist');
20 goog.require('goog.math.Coordinate');
21 goog.require('goog.style');
22 goog.require('i18n.input.chrome.inputview.Css');
23 goog.require('i18n.input.chrome.inputview.MoreKeysShiftOperation');
24 goog.require('i18n.input.chrome.inputview.SpecNodeName');
25 goog.require('i18n.input.chrome.inputview.StateType');
26 goog.require('i18n.input.chrome.inputview.SwipeDirection');
27 goog.require('i18n.input.chrome.inputview.elements.ElementType');
28 goog.require('i18n.input.chrome.inputview.elements.content.CompactKeyModel');
29 goog.require('i18n.input.chrome.inputview.elements.content.FunctionalKey');
30 goog.require('i18n.input.chrome.inputview.elements.content.GaussianEstimator');
31 goog.require('i18n.input.chrome.message.ContextType');
35 goog.scope(function() {
37 i18n.input.chrome.inputview.elements.content.CompactKeyModel;
38 var ContextType = i18n.input.chrome.message.ContextType;
39 var MoreKeysShiftOperation = i18n.input.chrome.inputview.MoreKeysShiftOperation;
40 var SpecNodeName = i18n.input.chrome.inputview.SpecNodeName;
44 * The key to switch between different key set.
46 * @param {string} id The id.
47 * @param {string} text The text.
48 * @param {string} hintText The hint text.
49 * @param {!i18n.input.chrome.inputview.StateManager} stateManager The state
51 * @param {boolean} hasShift True if the compact key has shift.}
52 * @param {!CompactKeyModel} compactKeyModel The attributes of compact key.
53 * @param {goog.events.EventTarget=} opt_eventTarget The event target.
55 * @extends {i18n.input.chrome.inputview.elements.content.FunctionalKey}
57 i18n.input.chrome.inputview.elements.content.CompactKey = function(id, text,
58 hintText, stateManager, hasShift, compactKeyModel, opt_eventTarget) {
59 var textCssClass = compactKeyModel.textCssClass;
60 goog.base(this, id, i18n.input.chrome.inputview.elements.ElementType.
61 COMPACT_KEY, text, '', opt_eventTarget, textCssClass);
68 this.hintText = hintText;
70 /** @private {boolean} */
71 this.hasShift_ = hasShift;
76 * @type {!i18n.input.chrome.inputview.StateManager}
79 this.stateManager_ = stateManager;
82 * The attributes of compact key.
84 * @private {CompactKeyModel}
86 this.compactKeyModel_ = compactKeyModel;
88 this.pointerConfig.longPressWithPointerUp = true;
89 this.pointerConfig.flickerDirection =
90 i18n.input.chrome.inputview.SwipeDirection.UP;
91 this.pointerConfig.longPressDelay = 500;
93 goog.inherits(i18n.input.chrome.inputview.elements.content.CompactKey,
94 i18n.input.chrome.inputview.elements.content.FunctionalKey);
95 var CompactKey = i18n.input.chrome.inputview.elements.content.CompactKey;
99 * The flickerred character.
103 CompactKey.prototype.flickerredCharacter = '';
107 CompactKey.prototype.createDom = function() {
108 goog.base(this, 'createDom');
110 goog.dom.classlist.add(this.tableCell,
111 i18n.input.chrome.inputview.Css.COMPACT_KEY);
112 if (!this.compactKeyModel_.isGrey) {
113 goog.dom.classlist.remove(this.bgElem,
114 i18n.input.chrome.inputview.Css.SPECIAL_KEY_BG);
118 var dom = this.getDomHelper();
119 dom.removeChildren(this.tableCell);
120 this.inlineWrap = dom.createDom(goog.dom.TagName.DIV,
121 i18n.input.chrome.inputview.Css.INLINE_DIV);
122 dom.appendChild(this.tableCell, this.inlineWrap);
123 this.hintTextElem = dom.createDom(goog.dom.TagName.DIV,
124 i18n.input.chrome.inputview.Css.HINT_TEXT, this.hintText);
125 dom.appendChild(this.inlineWrap, this.hintTextElem);
126 dom.appendChild(this.inlineWrap, this.textElem);
132 CompactKey.prototype.resize = function(width, height) {
133 var elem = this.getElement();
134 var marginLeft = Math.floor(width * this.compactKeyModel_.marginLeftPercent);
135 if (marginLeft > 0) {
137 elem.style.marginLeft = marginLeft + 'px';
140 Math.floor(width * this.compactKeyModel_.marginRightPercent);
141 // TODO: Remove this ugly hack. The default margin right is 10px, we
142 // need to add the default margin here to make all the keys have the same
144 if (marginRight > 0) {
146 elem.style.marginRight = marginRight + 'px';
149 goog.base(this, 'resize', width, height);
151 this.topLeftCoordinate = goog.style.getClientPosition(elem);
152 this.centerCoordinate = new goog.math.Coordinate(
153 this.topLeftCoordinate.x + this.availableWidth / 2,
154 this.topLeftCoordinate.y + this.availableHeight / 2);
155 this.estimator = new i18n.input.chrome.inputview.elements.content.
156 GaussianEstimator(this.centerCoordinate,
157 this.stateManager_.covariance.getValue(this.type),
158 this.availableHeight / this.availableWidth);
163 * Gets the active character factoring in the current input type context.
167 CompactKey.prototype.getContextOptimizedText_ = function() {
168 var context = this.stateManager_.contextType;
169 // Remove all old css rules.
170 for (var contextType in this.compactKeyModel_.textOnContext) {
172 this.compactKeyModel_.
173 textOnContext[contextType][SpecNodeName.TEXT_CSS_CLASS];
174 goog.dom.classlist.remove(this.tableCell, oldCss);
177 if (context && this.compactKeyModel_.textOnContext[context]) {
178 text = this.compactKeyModel_.textOnContext[context][SpecNodeName.TEXT];
180 this.compactKeyModel_.
181 textOnContext[context][SpecNodeName.TEXT_CSS_CLASS];
182 goog.dom.classlist.add(this.tableCell, newCss);
183 } else if (this.hasShift_ && this.stateManager_.hasState(
184 i18n.input.chrome.inputview.StateType.SHIFT)) {
185 // When there is specific text to display when shift is pressed down,
186 // the text should be set accordingly.
187 text = this.compactKeyModel_.textOnShift ?
188 this.compactKeyModel_.textOnShift : this.text.toUpperCase();
197 * Get the active character. It may be upper case |text| when shift is pressed
198 * or flickerred character when swipe. Note this should replace Compactkey.text
201 CompactKey.prototype.getActiveCharacter = function() {
202 if (this.flickerredCharacter) {
203 return this.flickerredCharacter;
205 return this.getContextOptimizedText_();
211 CompactKey.prototype.update = function() {
212 goog.base(this, 'update');
214 var text = this.getContextOptimizedText_();
215 var displayHintText = this.stateManager_.contextType != ContextType.PASSWORD;
216 if (this.compactKeyModel_.textOnShift) {
217 if (this.hasShift_ && this.stateManager_.hasState(
218 i18n.input.chrome.inputview.StateType.SHIFT)) {
219 // Deal with the case that when shift is pressed down,
220 // only one character should show in the compact key.
221 displayHintText = false;
222 text = this.compactKeyModel_.textOnShift;
226 goog.style.setElementShown(this.hintTextElem, displayHintText);
227 text = this.compactKeyModel_.title ?
228 chrome.i18n.getMessage(this.compactKeyModel_.title) : text;
229 goog.dom.setTextContent(this.textElem, text);
234 * Gets the more characters.
236 * @return {!Array.<string>} The characters.
238 CompactKey.prototype.getMoreCharacters = function() {
239 var moreCharacters = goog.array.clone(this.compactKeyModel_.moreKeys);
240 switch (this.compactKeyModel_.moreKeysShiftOperation) {
241 case MoreKeysShiftOperation.TO_UPPER_CASE:
242 if (this.getActiveCharacter().toLowerCase() !=
243 this.getActiveCharacter()) {
244 for (var i = 0; i < this.compactKeyModel_.moreKeys.length; i++) {
245 moreCharacters[i] = this.compactKeyModel_.moreKeys[i].toUpperCase();
247 goog.array.removeDuplicates(moreCharacters);
249 return moreCharacters;
250 case MoreKeysShiftOperation.TO_LOWER_CASE:
251 if (this.hasShift_ && this.stateManager_.hasState(
252 i18n.input.chrome.inputview.StateType.SHIFT)) {
253 for (var i = 0; i < this.compactKeyModel_.moreKeys.length; i++) {
254 moreCharacters[i] = this.compactKeyModel_.moreKeys[i].toLowerCase();
256 goog.array.removeDuplicates(moreCharacters);
258 return moreCharacters;
259 case MoreKeysShiftOperation.STABLE:
262 return moreCharacters;