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.M17nModel');
16 goog.require('goog.events.EventHandler');
17 goog.require('goog.events.EventTarget');
18 goog.require('i18n.input.chrome.inputview.SpecNodeName');
19 goog.require('i18n.input.chrome.inputview.content.util');
20 goog.require('i18n.input.chrome.inputview.events.ConfigLoadedEvent');
21 goog.require('i18n.input.chrome.vk.KeyCode');
22 goog.require('i18n.input.chrome.vk.Model');
27 * The model to legacy cloud vk configuration.
30 * @extends {goog.events.EventTarget}
32 i18n.input.chrome.inputview.M17nModel = function() {
38 * @type {!goog.events.EventHandler}
41 this.handler_ = new goog.events.EventHandler(this);
44 * The model for cloud vk.
46 * @type {!i18n.input.chrome.vk.Model}
49 this.model_ = new i18n.input.chrome.vk.Model();
50 this.handler_.listen(this.model_,
51 i18n.input.chrome.vk.EventType.LAYOUT_LOADED,
52 this.onLayoutLoaded_);
54 goog.inherits(i18n.input.chrome.inputview.M17nModel,
55 goog.events.EventTarget);
59 * The active layout view.
61 * @type {!i18n.input.chrome.vk.ParsedLayout}
64 i18n.input.chrome.inputview.M17nModel.prototype.layoutView_;
68 * Loads the configuration.
70 * @param {string} lang The m17n keyboard layout code (with 'm17n:' prefix).
72 i18n.input.chrome.inputview.M17nModel.prototype.loadConfig = function(lang) {
73 var m17nMatches = lang.match(/^m17n:(.*)/);
74 if (m17nMatches && m17nMatches[1]) {
75 this.model_.loadLayout(m17nMatches[1]);
81 * Callback when legacy model is loaded.
83 * @param {!i18n.input.chrome.vk.LayoutEvent} e The event.
86 i18n.input.chrome.inputview.M17nModel.prototype.onLayoutLoaded_ = function(
88 var layoutView = /** @type {!i18n.input.chrome.vk.ParsedLayout} */
90 this.layoutView_ = layoutView;
91 var is102 = layoutView.view.is102;
92 var codes = is102 ? i18n.input.chrome.vk.KeyCode.CODES102 :
93 i18n.input.chrome.vk.KeyCode.CODES101;
94 var keyCount = is102 ? 48 : 47;
95 var keyCharacters = [];
96 for (var i = 0; i < keyCount; i++) {
97 var characters = this.findCharacters_(layoutView.view.mappings,
99 keyCharacters.push(characters);
101 keyCharacters.push(['\u0020', '\u0020']);
102 var hasAltGrKey = !!layoutView.view.mappings['c'] &&
103 layoutView.view.mappings['c'] != layoutView.view.mappings[''];
104 var skvPrefix = is102 ? '102kbd-k-' : '101kbd-k-';
105 var skPrefix = layoutView.view.id + '-k-';
106 var data = i18n.input.chrome.inputview.content.util.createData(keyCharacters,
107 skvPrefix, is102, hasAltGrKey);
109 data[i18n.input.chrome.inputview.SpecNodeName.TITLE] =
110 layoutView.view.title;
111 data[i18n.input.chrome.inputview.SpecNodeName.ID] =
112 'm17n:' + e.layoutCode;
113 this.dispatchEvent(new i18n.input.chrome.inputview.events.
114 ConfigLoadedEvent(data));
120 * Finds out the characters for the key.
122 * @param {!Object} mappings The mappings.
123 * @param {string} code The code.
124 * @return {!Array.<string>} The characters for the code.
127 i18n.input.chrome.inputview.M17nModel.prototype.findCharacters_ = function(
140 for (var i = 0; i < states.length; i++) {
141 if (mappings[states[i]] && mappings[states[i]][code]) {
142 characters[i] = mappings[states[i]][code][1];
143 } else if (code == '\u0020') {
144 characters[i] = '\u0020';
154 i18n.input.chrome.inputview.M17nModel.prototype.disposeInternal = function() {
155 goog.dispose(this.handler_);
157 goog.base(this, 'disposeInternal');