summaryrefslogtreecommitdiffstats
path: root/third_party/google_input_tools/src/chrome/os/inputview/elements/content/charactermodel.js
blob: cc1aff019356ac1ab9d7627731ac7cc4f505bd1b (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2014 The ChromeOS IME Authors. All Rights Reserved.
// limitations under the License.
// See the License for the specific language governing permissions and
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// distributed under the License is distributed on an "AS-IS" BASIS,
// Unless required by applicable law or agreed to in writing, software
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// You may obtain a copy of the License at
// you may not use this file except in compliance with the License.
// Licensed under the Apache License, Version 2.0 (the "License");
//
goog.provide('i18n.input.chrome.inputview.elements.content.CharacterModel');

goog.require('i18n.input.chrome.inputview.StateType');



goog.scope(function() {
var StateType = i18n.input.chrome.inputview.StateType;



/**
 * The character model.
 *
 * @param {string} character The character.
 * @param {boolean} belongToLetterKey True if this characters belongs to a
 *     letter key.
 * @param {boolean} hasAltGrCharacterInTheKeyset True if this kind of key has
 *     altgr character.
 * @param {boolean} alwaysRenderAltGrCharacter True if always renders the altgr
 *     character.
 * @param {number} stateType The state type for this character.
 * @param {!i18n.input.chrome.inputview.StateManager} stateManager The state
 *     manager.
 * @param {boolean} enableShiftRendering .
 * @param {string=} opt_capslockCharacter .
 * @constructor
 */
i18n.input.chrome.inputview.elements.content.CharacterModel = function(
    character, belongToLetterKey, hasAltGrCharacterInTheKeyset,
    alwaysRenderAltGrCharacter, stateType, stateManager, enableShiftRendering,
    opt_capslockCharacter) {

  /**
   * The character.
   *
   * @type {string}
   * @private
   */
  this.character_ = character;

  /**
   * The character for the capslock state.
   *
   * @private {string}
   */
  this.capslockCharacter_ = opt_capslockCharacter || '';

  /**
   * Whether this character is belong to a letter key.
   *
   * @type {boolean}
   * @private
   */
  this.belongToLetterKey_ = belongToLetterKey;

  /**
   * The state.
   *
   * @type {number}
   * @private
   */
  this.stateType_ = stateType;

  /**
   * The state manager.
   *
   * @type {!i18n.input.chrome.inputview.StateManager}
   * @private
   */
  this.stateManager_ = stateManager;

  /**
   * Whether to always renders the altgr character..
   *
   * @type {boolean}
   * @private
   */
  this.alwaysRenderAltGrCharacter_ = alwaysRenderAltGrCharacter;

  /**
   * True if this key set has altgr character.
   *
   * @type {boolean}
   * @private
   */
  this.hasAltGrCharacterInTheKeyset_ = hasAltGrCharacterInTheKeyset;

  /** @private {boolean} */
  this.enableShiftRendering_ = enableShiftRendering;
};
var CharacterModel = i18n.input.chrome.inputview.elements.content.
    CharacterModel;


/**
 * The alignment type.
 *
 * @enum {number}
 */
CharacterModel.AlignType = {
  CENTER: 0,
  CORNER: 1
};


/**
 * The position attributes.
 *
 * @type {!Array.<!Array.<string>>}
 * @private
 */
CharacterModel.CORNERS_ = [
  ['bottom', 'left'],
  ['top', 'left'],
  ['bottom', 'right'],
  ['top', 'right']
];


/**
 * True if this character is highlighed.
 *
 * @return {boolean} True if the character is highlighted.
 */
CharacterModel.prototype.isHighlighted = function() {
  var state = this.stateManager_.getState();
  state = state & (StateType.SHIFT | StateType.ALTGR);
  return this.stateType_ == state;
};


/**
 * True if this character is visible.
 *
 * @return {boolean} True if the character is visible.
 */
CharacterModel.prototype.isVisible = function() {
  var enableShiftLetter = this.enableShiftRendering_ ||
      this.stateManager_.hasState(StateType.SHIFT);
  var enableDefaultLetter = this.enableShiftRendering_ || !this.stateManager_.
      hasState(StateType.SHIFT);
  if (this.stateType_ == StateType.DEFAULT) {
    return !this.stateManager_.hasState(StateType.ALTGR) && enableDefaultLetter;
  }
  if (this.stateType_ == StateType.SHIFT) {
    return !this.stateManager_.hasState(StateType.ALTGR) && enableShiftLetter;
  }
  if (this.stateType_ == StateType.ALTGR) {
    // AltGr character.
    return this.stateManager_.hasState(StateType.ALTGR) && enableDefaultLetter;
  }
  if (this.stateType_ == (StateType.ALTGR | StateType.SHIFT)) {
    // Shift + AltGr character.
    return this.stateManager_.hasState(StateType.ALTGR) && enableShiftLetter;
  }
  return false;
};


/**
 * Gets the reversed case character.
 *
 * @return {string} The reversed character
 * @private
 */
CharacterModel.prototype.toReversedCase_ = function() {
  var reversed;
  if (this.character_.toUpperCase() == this.character_) {
    reversed = this.character_.toLowerCase();
  } else {
    reversed = this.character_.toUpperCase();
  }
  return reversed;
};


/**
 * Gets the content of this character..
 *
 * @return {string} The content.
 */
CharacterModel.prototype.getContent = function() {
  if (this.stateManager_.hasState(StateType.CAPSLOCK)) {
    return this.capslockCharacter_ ? this.capslockCharacter_ :
        this.toReversedCase_();
  }

  return this.character_;
};


/**
 * True if align the character in the center horizontally.
 *
 * @return {boolean} True to align in the center.
 */
CharacterModel.prototype.isHorizontalAlignCenter = function() {
  if (this.stateType_ == StateType.DEFAULT ||
      this.stateType_ == StateType.SHIFT) {
    return !this.alwaysRenderAltGrCharacter_ ||
        !this.hasAltGrCharacterInTheKeyset_;
  }

  return false;
};


/**
 * True to align the character in the center vertically.
 *
 * @return {boolean} True to be in the center.
 */
CharacterModel.prototype.isVerticalAlignCenter = function() {
  if (this.stateType_ == StateType.DEFAULT ||
      this.stateType_ == StateType.SHIFT) {
    return this.belongToLetterKey_;
  }

  return false;
};


/**
 * Gets the attribute for position.
 *
 * @return {!Array.<string>} The attributes.
 */
CharacterModel.prototype.getPositionAttribute = function() {
  var index;
  switch (this.stateType_) {
    case StateType.DEFAULT:
      return CharacterModel.CORNERS_[0];
    case StateType.SHIFT:
      return CharacterModel.CORNERS_[1];
    case StateType.ALTGR:
      return CharacterModel.CORNERS_[2];
    default:
      return CharacterModel.CORNERS_[3];
  }
};


});  // goog.scope