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.CharacterModel');
16 goog.require('i18n.input.chrome.inputview.StateType');
20 goog.scope(function() {
25 * The character model.
27 * @param {string} character The character.
28 * @param {boolean} belongToLetterKey True if this characters belongs to a
30 * @param {boolean} hasAltGrCharacterInTheKeyset True if this kind of key has
32 * @param {boolean} alwaysRenderAltGrCharacter True if always renders the altgr
34 * @param {number} stateType The state type for this character.
35 * @param {!i18n.input.chrome.inputview.StateManager} stateManager The state
37 * @param {string=} opt_capslockCharacter .
40 i18n.input.chrome.inputview.elements.content.CharacterModel = function(
41 character, belongToLetterKey, hasAltGrCharacterInTheKeyset,
42 alwaysRenderAltGrCharacter, stateType, stateManager,
43 opt_capslockCharacter) {
51 this.character_ = character;
54 * The character for the capslock state.
58 this.capslockCharacter_ = opt_capslockCharacter || '';
61 * Whether this character is belong to a letter key.
66 this.belongToLetterKey_ = belongToLetterKey;
74 this.stateType_ = stateType;
79 * @type {!i18n.input.chrome.inputview.StateManager}
82 this.stateManager_ = stateManager;
85 * Whether to always renders the altgr character..
90 this.alwaysRenderAltGrCharacter_ = alwaysRenderAltGrCharacter;
93 * True if this key set has altgr character.
98 this.hasAltGrCharacterInTheKeyset_ = hasAltGrCharacterInTheKeyset;
100 var CharacterModel = i18n.input.chrome.inputview.elements.content.
105 * The alignment type.
109 CharacterModel.AlignType = {
116 * The position attributes.
118 * @type {!Array.<!Array.<string>>}
121 CharacterModel.CORNERS_ = [
130 * True if this character is highlighed.
132 * @return {boolean} True if the character is highlighted.
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;
143 * True if this character is visible.
145 * @return {boolean} True if the character is visible.
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));
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));
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);
170 * Gets the reversed case character.
172 * @return {string} The reversed character
175 CharacterModel.prototype.toReversedCase_ = function() {
177 if (this.character_.toUpperCase() == this.character_) {
178 reversed = this.character_.toLowerCase();
180 reversed = this.character_.toUpperCase();
187 * Gets the content of this character..
189 * @return {string} The content.
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_();
198 return this.character_;
203 * True if align the character in the center horizontally.
205 * @return {boolean} True to align in the center.
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_;
219 * True to align the character in the center vertically.
221 * @return {boolean} True to be in the center.
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_;
234 * Gets the attribute for position.
236 * @return {!Array.<string>} The attributes.
238 CharacterModel.prototype.getPositionAttribute = function() {
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];
248 return CharacterModel.CORNERS_[3];