Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / google_input_tools / src / chrome / os / inputview / elements / content / charactermodel.js
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
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
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");
13 //
14 goog.provide('i18n.input.chrome.inputview.elements.content.CharacterModel');
15
16 goog.require('i18n.input.chrome.inputview.StateType');
17
18
19
20 goog.scope(function() {
21
22
23
24 /**
25  * The character model.
26  *
27  * @param {string} character The character.
28  * @param {boolean} belongToLetterKey True if this characters belongs to a
29  *     letter key.
30  * @param {boolean} hasAltGrCharacterInTheKeyset True if this kind of key has
31  *     altgr character.
32  * @param {boolean} alwaysRenderAltGrCharacter True if always renders the altgr
33  *     character.
34  * @param {number} stateType The state type for this character.
35  * @param {!i18n.input.chrome.inputview.StateManager} stateManager The state
36  *     manager.
37  * @param {string=} opt_capslockCharacter .
38  * @constructor
39  */
40 i18n.input.chrome.inputview.elements.content.CharacterModel = function(
41     character, belongToLetterKey, hasAltGrCharacterInTheKeyset,
42     alwaysRenderAltGrCharacter, stateType, stateManager,
43     opt_capslockCharacter) {
44
45   /**
46    * The character.
47    *
48    * @type {string}
49    * @private
50    */
51   this.character_ = character;
52
53   /**
54    * The character for the capslock state.
55    *
56    * @private {string}
57    */
58   this.capslockCharacter_ = opt_capslockCharacter || '';
59
60   /**
61    * Whether this character is belong to a letter key.
62    *
63    * @type {boolean}
64    * @private
65    */
66   this.belongToLetterKey_ = belongToLetterKey;
67
68   /**
69    * The state.
70    *
71    * @type {number}
72    * @private
73    */
74   this.stateType_ = stateType;
75
76   /**
77    * The state manager.
78    *
79    * @type {!i18n.input.chrome.inputview.StateManager}
80    * @private
81    */
82   this.stateManager_ = stateManager;
83
84   /**
85    * Whether to always renders the altgr character..
86    *
87    * @type {boolean}
88    * @private
89    */
90   this.alwaysRenderAltGrCharacter_ = alwaysRenderAltGrCharacter;
91
92   /**
93    * True if this key set has altgr character.
94    *
95    * @type {boolean}
96    * @private
97    */
98   this.hasAltGrCharacterInTheKeyset_ = hasAltGrCharacterInTheKeyset;
99 };
100 var CharacterModel = i18n.input.chrome.inputview.elements.content.
101     CharacterModel;
102
103
104 /**
105  * The alignment type.
106  *
107  * @enum {number}
108  */
109 CharacterModel.AlignType = {
110   CENTER: 0,
111   CORNER: 1
112 };
113
114
115 /**
116  * The position attributes.
117  *
118  * @type {!Array.<!Array.<string>>}
119  * @private
120  */
121 CharacterModel.CORNERS_ = [
122   ['bottom', 'left'],
123   ['top', 'left'],
124   ['bottom', 'right'],
125   ['top', 'right']
126 ];
127
128
129 /**
130  * True if this character is highlighed.
131  *
132  * @return {boolean} True if the character is highlighted.
133  */
134 CharacterModel.prototype.isHighlighted = function() {
135   var state = this.stateManager_.getState();
136   state = state & (i18n.input.chrome.inputview.StateType.SHIFT |
137       i18n.input.chrome.inputview.StateType.ALTGR);
138   return this.stateType_ == state;
139 };
140
141
142 /**
143  * True if this character is visible.
144  *
145  * @return {boolean} True if the character is visible.
146  */
147 CharacterModel.prototype.isVisible = function() {
148   if (this.stateType_ == i18n.input.chrome.inputview.StateType.DEFAULT) {
149     return !this.stateManager_.hasState(
150         i18n.input.chrome.inputview.StateType.ALTGR) && (
151         !this.belongToLetterKey_ || !this.stateManager_.hasState(
152         i18n.input.chrome.inputview.StateType.SHIFT));
153   }
154   if (this.stateType_ == i18n.input.chrome.inputview.StateType.SHIFT) {
155     return !this.stateManager_.hasState(
156         i18n.input.chrome.inputview.StateType.ALTGR) && (
157         !this.belongToLetterKey_ || this.stateManager_.hasState(
158         i18n.input.chrome.inputview.StateType.SHIFT));
159   }
160   if ((this.stateType_ & i18n.input.chrome.inputview.StateType.ALTGR) != 0) {
161     // AltGr or AltGr+Shift character.
162     return this.alwaysRenderAltGrCharacter_ || this.stateManager_.
163         hasState(i18n.input.chrome.inputview.StateType.ALTGR);
164   }
165   return false;
166 };
167
168
169 /**
170  * Gets the reversed case character.
171  *
172  * @return {string} The reversed character
173  * @private
174  */
175 CharacterModel.prototype.toReversedCase_ = function() {
176   var reversed;
177   if (this.character_.toUpperCase() == this.character_) {
178     reversed = this.character_.toLowerCase();
179   } else {
180     reversed = this.character_.toUpperCase();
181   }
182   return reversed;
183 };
184
185
186 /**
187  * Gets the content of this character..
188  *
189  * @return {string} The content.
190  */
191 CharacterModel.prototype.getContent = function() {
192   if (this.stateManager_.hasState(
193       i18n.input.chrome.inputview.StateType.CAPSLOCK)) {
194     return this.capslockCharacter_ ? this.capslockCharacter_ :
195         this.toReversedCase_();
196   }
197
198   return this.character_;
199 };
200
201
202 /**
203  * True if align the character in the center horizontally.
204  *
205  * @return {boolean} True to align in the center.
206  */
207 CharacterModel.prototype.isHorizontalAlignCenter = function() {
208   if (this.stateType_ == i18n.input.chrome.inputview.StateType.DEFAULT ||
209       this.stateType_ == i18n.input.chrome.inputview.StateType.SHIFT) {
210     return !this.alwaysRenderAltGrCharacter_ ||
211         !this.hasAltGrCharacterInTheKeyset_;
212   }
213
214   return false;
215 };
216
217
218 /**
219  * True to align the character in the center vertically.
220  *
221  * @return {boolean} True to be in the center.
222  */
223 CharacterModel.prototype.isVerticalAlignCenter = function() {
224   if (this.stateType_ == i18n.input.chrome.inputview.StateType.DEFAULT ||
225       this.stateType_ == i18n.input.chrome.inputview.StateType.SHIFT) {
226     return this.belongToLetterKey_;
227   }
228
229   return false;
230 };
231
232
233 /**
234  * Gets the attribute for position.
235  *
236  * @return {!Array.<string>} The attributes.
237  */
238 CharacterModel.prototype.getPositionAttribute = function() {
239   var index;
240   switch (this.stateType_) {
241     case i18n.input.chrome.inputview.StateType.DEFAULT:
242       return CharacterModel.CORNERS_[0];
243     case i18n.input.chrome.inputview.StateType.SHIFT:
244       return CharacterModel.CORNERS_[1];
245     case i18n.input.chrome.inputview.StateType.ALTGR:
246       return CharacterModel.CORNERS_[2];
247     default:
248       return CharacterModel.CORNERS_[3];
249   }
250 };
251
252
253 });  // goog.scope
254