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.layouts.util');
15 goog.require('i18n.input.chrome.inputview.elements.ElementType');
19 goog.scope(function() {
20 var ElementType = i18n.input.chrome.inputview.elements.ElementType;
21 var util = i18n.input.chrome.inputview.layouts.util;
34 * The prefix of the key id, it's overwritten by layout file.
38 util.keyIdPrefix = '';
42 * Resets id counter for keys in preparation for processing a new layout.
43 * @param {string} prefix The prefix for the key id.
45 util.setPrefix = function(prefix) {
46 util.keyIdPrefix = prefix;
52 * Creates a sequence of key with the same specification.
54 * @param {!Object} spec The specification.
55 * @param {number} num How many keys.
56 * @return {!Array.<Object>} The keys.
58 util.createKeySequence = function(spec,
61 for (var i = 0; i < num; i++) {
62 sequence.push(util.createKey(spec));
69 * Creates a soft key view.
71 * @param {!Object} spec The specification.
72 * @param {string=} opt_id The id.
73 * @return {!Object} The soft key view.
75 util.createKey = function(spec, opt_id) {
76 var id = util.keyIdPrefix +
78 return util.createElem(
79 ElementType.SOFT_KEY_VIEW, spec, id);
84 * Creates a linear layout.
86 * @param {!Object} spec The specification.
87 * @param {string=} opt_id The id.
88 * @return {!Object} The linear layout.
90 util.createLinearLayout = function(spec,
92 return util.createElem(
93 ElementType.LINEAR_LAYOUT, spec, opt_id, spec['iconCssClass']);
98 * Creates an extended layout.
100 * @param {!Object} spec The specification.
101 * @param {string=} opt_id The id.
102 * @return {!Object} The extended layout.
104 util.createExtendedLayout = function(spec,
106 return util.createElem(
107 ElementType.EXTENDED_LAYOUT, spec, opt_id, spec['iconCssClass']);
112 * Creates a handwriting layout.
114 * @param {!Object} spec The specification.
115 * @param {string=} opt_id The id.
116 * @return {!Object} The handwriting layout.
118 util.createHandwritingLayout =
119 function(spec, opt_id) {
120 return util.createElem(
121 ElementType.HANDWRITING_LAYOUT, spec, opt_id);
126 * Creates a vertical layout.
128 * @param {!Object} spec The specification.
129 * @param {string=} opt_id The id.
130 * @return {!Object} The vertical layout.
132 util.createVerticalLayout = function(spec,
134 return util.createElem(
135 ElementType.VERTICAL_LAYOUT, spec, opt_id);
140 * Creates a layout view.
142 * @param {!Object} spec The specification.
143 * @param {string=} opt_id The id.
144 * @return {!Object} The view.
146 util.createLayoutView = function(spec,
148 return util.createElem(
149 ElementType.LAYOUT_VIEW, spec, opt_id);
154 * Creates a candidate view.
156 * @param {!Object} spec The specification.
157 * @param {string=} opt_id The id.
158 * @return {!Object} The view.
160 util.createCandidateView = function(spec,
162 return util.createElem(
163 ElementType.CANDIDATE_VIEW, spec, opt_id);
168 * Creates a canvas view.
170 * @param {!Object} spec The specification.
171 * @param {string=} opt_id The id.
172 * @return {!Object} The view.
174 util.createCanvasView = function(spec,
176 return util.createElem(
177 ElementType.CANVAS_VIEW, spec, opt_id);
182 * Creates the keyboard.
184 * @param {Object} spec The specification.
185 * @param {string=} opt_id The id.
186 * @return {Object} The keyboard.
188 util.createKeyboard = function(spec,
190 return util.createElem(
191 ElementType.KEYBOARD, spec, opt_id);
196 * Creates an element which could be any type, such as soft key view, layout,
199 * @param {!ElementType} type The type.
200 * @param {Object} spec The specification.
201 * @param {string=} opt_id The id.
202 * @param {string=} opt_iconCssClass The Css class.
203 * @return {!Object} The element.
205 util.createElem = function(type, spec,
206 opt_id, opt_iconCssClass) {
208 for (var key in spec) {
209 newSpec[key] = spec[key];
211 newSpec['type'] = type;
213 newSpec['id'] = opt_id;
215 if (opt_iconCssClass) {
216 newSpec['iconCssClass'] = opt_iconCssClass;