OLD | NEW |
(Empty) | |
| 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.KeyboardContainer'); |
| 15 |
| 16 goog.require('goog.dom.TagName'); |
| 17 goog.require('goog.dom.classlist'); |
| 18 goog.require('goog.ui.Container'); |
| 19 goog.require('i18n.input.chrome.inputview.Css'); |
| 20 goog.require('i18n.input.chrome.inputview.elements.content.AltDataView'); |
| 21 goog.require('i18n.input.chrome.inputview.elements.content.CandidateView'); |
| 22 goog.require('i18n.input.chrome.inputview.elements.content.EmojiView'); |
| 23 goog.require('i18n.input.chrome.inputview.elements.content.ExpandedCandidateView
'); |
| 24 goog.require('i18n.input.chrome.inputview.elements.content.HandwritingView'); |
| 25 goog.require('i18n.input.chrome.inputview.elements.content.KeysetView'); |
| 26 goog.require('i18n.input.chrome.inputview.elements.content.MenuView'); |
| 27 |
| 28 |
| 29 |
| 30 goog.scope(function() { |
| 31 var Css = i18n.input.chrome.inputview.Css; |
| 32 var EmojiView = i18n.input.chrome.inputview.elements.content.EmojiView; |
| 33 var HandwritingView = i18n.input.chrome.inputview.elements.content. |
| 34 HandwritingView; |
| 35 var KeysetView = i18n.input.chrome.inputview.elements.content.KeysetView; |
| 36 var SpecNodeName = i18n.input.chrome.inputview.SpecNodeName; |
| 37 var content = i18n.input.chrome.inputview.elements.content; |
| 38 |
| 39 |
| 40 |
| 41 /** |
| 42 * The keyboard container. |
| 43 * |
| 44 * @param {i18n.input.chrome.inputview.Adapter=} opt_adapter . |
| 45 * @constructor |
| 46 * @extends {goog.ui.Container} |
| 47 */ |
| 48 i18n.input.chrome.inputview.KeyboardContainer = function(opt_adapter) { |
| 49 goog.base(this); |
| 50 |
| 51 /** @type {!content.CandidateView} */ |
| 52 this.candidateView = new content.CandidateView('candidateView', this); |
| 53 |
| 54 /** @type {!content.AltDataView} */ |
| 55 this.altDataView = new content.AltDataView(this); |
| 56 |
| 57 /** @type {!content.MenuView} */ |
| 58 this.menuView = new content.MenuView(this); |
| 59 |
| 60 /** @type {!content.ExpandedCandidateView} */ |
| 61 this.expandedCandidateView = new content.ExpandedCandidateView(this); |
| 62 |
| 63 /** |
| 64 * The map of the KeysetViews. |
| 65 * Key: keyboard code. |
| 66 * Value: The view object. |
| 67 * |
| 68 * @type {!Object.<string, !KeysetView>} |
| 69 */ |
| 70 this.keysetViewMap = {}; |
| 71 |
| 72 /** |
| 73 * The bus channel to communicate with background. |
| 74 * |
| 75 * @private {i18n.input.chrome.inputview.Adapter} |
| 76 */ |
| 77 this.adapter_ = opt_adapter || null; |
| 78 }; |
| 79 goog.inherits(i18n.input.chrome.inputview.KeyboardContainer, |
| 80 goog.ui.Container); |
| 81 var KeyboardContainer = i18n.input.chrome.inputview.KeyboardContainer; |
| 82 |
| 83 |
| 84 /** @type {!KeysetView} */ |
| 85 KeyboardContainer.prototype.currentKeysetView; |
| 86 |
| 87 |
| 88 /** |
| 89 * The padding bottom of the whole keyboard. |
| 90 * |
| 91 * @private {number} |
| 92 */ |
| 93 KeyboardContainer.PADDING_BOTTOM_ = 7; |
| 94 |
| 95 |
| 96 /** |
| 97 * The padding value of handwriting panel. |
| 98 * |
| 99 * @type {number} |
| 100 * @private |
| 101 */ |
| 102 KeyboardContainer.HANDWRITING_PADDING_ = 22; |
| 103 |
| 104 |
| 105 /** |
| 106 * The padding of emoji keyset. |
| 107 * |
| 108 * @type {number} |
| 109 * @private |
| 110 */ |
| 111 KeyboardContainer.EMOJI_PADDING_ = 22; |
| 112 |
| 113 |
| 114 /** |
| 115 * An div to wrapper candidate view and keyboard set view. |
| 116 * |
| 117 * @private {Element} |
| 118 */ |
| 119 KeyboardContainer.prototype.wrapperDiv_ = null; |
| 120 |
| 121 |
| 122 /** @override */ |
| 123 KeyboardContainer.prototype.createDom = function() { |
| 124 goog.base(this, 'createDom'); |
| 125 |
| 126 var elem = this.getElement(); |
| 127 this.wrapperDiv_ = this.getDomHelper().createDom( |
| 128 goog.dom.TagName.DIV, Css.WRAPPER); |
| 129 this.candidateView.render(this.wrapperDiv_); |
| 130 this.getDomHelper().appendChild(elem, this.wrapperDiv_); |
| 131 this.altDataView.render(); |
| 132 this.menuView.render(); |
| 133 this.expandedCandidateView.render(this.wrapperDiv_); |
| 134 this.expandedCandidateView.setVisible(false); |
| 135 goog.dom.classlist.add(elem, Css.CONTAINER); |
| 136 }; |
| 137 |
| 138 |
| 139 /** @override */ |
| 140 KeyboardContainer.prototype.enterDocument = function() { |
| 141 goog.base(this, 'enterDocument'); |
| 142 |
| 143 this.setFocusable(false); |
| 144 this.setFocusableChildrenAllowed(false); |
| 145 }; |
| 146 |
| 147 |
| 148 /** |
| 149 * Updates the whole keyboard. |
| 150 */ |
| 151 KeyboardContainer.prototype.update = function() { |
| 152 this.currentKeysetView && this.currentKeysetView.update(); |
| 153 }; |
| 154 |
| 155 |
| 156 /** |
| 157 * Adds a keyset view. |
| 158 * |
| 159 * @param {!Object} keysetData . |
| 160 * @param {!Object} layoutData . |
| 161 * @param {string} keyset . |
| 162 * @param {string} languageCode . |
| 163 * @param {!i18n.input.chrome.inputview.Model} model . |
| 164 * @param {string} inputToolName . |
| 165 * @param {!Object.<string, boolean>} conditions . |
| 166 */ |
| 167 KeyboardContainer.prototype.addKeysetView = function(keysetData, layoutData, |
| 168 keyset, languageCode, model, inputToolName, conditions) { |
| 169 var view; |
| 170 if (keyset == 'emoji') { |
| 171 view = new EmojiView(keysetData, layoutData, keyset, languageCode, model, |
| 172 inputToolName, this, this.adapter_); |
| 173 } else if (keyset == 'hwt') { |
| 174 view = new HandwritingView(keysetData, layoutData, keyset, languageCode, |
| 175 model, inputToolName, this, this.adapter_); |
| 176 } else { |
| 177 view = new KeysetView(keysetData, layoutData, keyset, languageCode, model, |
| 178 inputToolName, this, this.adapter_); |
| 179 } |
| 180 view.render(this.wrapperDiv_); |
| 181 view.applyConditions(conditions); |
| 182 view.setVisible(false); |
| 183 this.keysetViewMap[keyset] = view; |
| 184 }; |
| 185 |
| 186 |
| 187 /** |
| 188 * Switches to a keyset. |
| 189 * |
| 190 * @param {string} keyset . |
| 191 * @param {string} title . |
| 192 * @param {boolean} isPasswordBox . |
| 193 * @param {boolean} isA11yMode . |
| 194 * @param {string} rawKeyset The raw keyset id will switch to. |
| 195 * @param {string} lastRawkeyset . |
| 196 * @param {string} languageCode . |
| 197 * @return {boolean} True if switched successfully. |
| 198 */ |
| 199 KeyboardContainer.prototype.switchToKeyset = function(keyset, title, |
| 200 isPasswordBox, isA11yMode, rawKeyset, lastRawkeyset, languageCode) { |
| 201 if (!this.keysetViewMap[keyset]) { |
| 202 return false; |
| 203 } |
| 204 |
| 205 for (var name in this.keysetViewMap) { |
| 206 var view = this.keysetViewMap[name]; |
| 207 if (name == keyset) { |
| 208 this.candidateView.setVisible(!view.disableCandidateView); |
| 209 view.setVisible(true); |
| 210 view.update(); |
| 211 if (view.spaceKey) { |
| 212 view.spaceKey.updateTitle(title, !isPasswordBox); |
| 213 } |
| 214 if (isA11yMode) { |
| 215 goog.dom.classlist.add(this.getElement(), Css.A11Y); |
| 216 } |
| 217 // If current raw keyset is changed, record it. |
| 218 if (lastRawkeyset != rawKeyset) { |
| 219 view.fromKeyset = lastRawkeyset; |
| 220 } |
| 221 if (view instanceof HandwritingView) { |
| 222 view.setLanguagecode(languageCode); |
| 223 } |
| 224 this.currentKeysetView = view; |
| 225 this.candidateView.updateByKeyset(rawKeyset, isPasswordBox, |
| 226 goog.i18n.bidi.isRtlLanguage(languageCode)); |
| 227 } else { |
| 228 view.setVisible(false); |
| 229 } |
| 230 } |
| 231 return true; |
| 232 }; |
| 233 |
| 234 |
| 235 /** |
| 236 * Resizes the whole keyboard. |
| 237 * |
| 238 * @param {number} width . |
| 239 * @param {number} height . |
| 240 * @param {number} widthPercent . |
| 241 * @param {number} candidateViewHeight . |
| 242 */ |
| 243 KeyboardContainer.prototype.resize = function(width, height, widthPercent, |
| 244 candidateViewHeight) { |
| 245 if (!this.currentKeysetView) { |
| 246 return; |
| 247 } |
| 248 var elem = this.getElement(); |
| 249 |
| 250 var h; |
| 251 if (this.currentKeysetView.isHandwriting()) { |
| 252 h = height - KeyboardContainer.HANDWRITING_PADDING_; |
| 253 elem.style.paddingBottom = ''; |
| 254 } else { |
| 255 h = height - KeyboardContainer.PADDING_BOTTOM_; |
| 256 elem.style.paddingBottom = KeyboardContainer.PADDING_BOTTOM_ + 'px'; |
| 257 } |
| 258 |
| 259 var padding = Math.round((width - width * widthPercent) / 2); |
| 260 elem.style.paddingLeft = elem.style.paddingRight = padding + 'px'; |
| 261 |
| 262 var w = width - 2 * padding; |
| 263 h = this.currentKeysetView.disableCandidateView ? |
| 264 h - KeyboardContainer.EMOJI_PADDING_ : h - candidateViewHeight; |
| 265 |
| 266 this.candidateView.setWidthInWeight( |
| 267 this.currentKeysetView.getWidthInWeight()); |
| 268 this.candidateView.resize(w, candidateViewHeight); |
| 269 this.currentKeysetView.resize(w, h); |
| 270 this.expandedCandidateView.resize(w, h); |
| 271 this.altDataView.resize(screen.width, height); |
| 272 this.menuView.resize(screen.width, height); |
| 273 }; |
| 274 |
| 275 |
| 276 /** @override */ |
| 277 KeyboardContainer.prototype.disposeInternal = function() { |
| 278 goog.dispose(this.candidateView); |
| 279 goog.dispose(this.altDataView); |
| 280 goog.dispose(this.menuView); |
| 281 for (var key in this.keysetViewMap) { |
| 282 goog.dispose(this.keysetViewMap[key]); |
| 283 } |
| 284 |
| 285 goog.base(this, 'disposeInternal'); |
| 286 }; |
| 287 |
| 288 |
| 289 /** |
| 290 * Whether there are strokes on canvas. |
| 291 * |
| 292 * @return {boolean} Whether there are strokes on canvas. |
| 293 */ |
| 294 KeyboardContainer.prototype.hasStrokesOnCanvas = function() { |
| 295 if (this.currentKeysetView) { |
| 296 return this.currentKeysetView.hasStrokesOnCanvas(); |
| 297 } else { |
| 298 return false; |
| 299 } |
| 300 }; |
| 301 |
| 302 |
| 303 /** |
| 304 * Cleans the stokes. |
| 305 */ |
| 306 KeyboardContainer.prototype.cleanStroke = function() { |
| 307 if (this.currentKeysetView) { |
| 308 this.currentKeysetView.cleanStroke(); |
| 309 } |
| 310 }; |
| 311 }); // goog.scope |
OLD | NEW |