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.layouts.util'); |
| 15 goog.require('i18n.input.chrome.inputview.Css'); |
| 16 goog.require('i18n.input.chrome.inputview.elements.ElementType'); |
| 17 |
| 18 |
| 19 |
| 20 goog.scope(function() { |
| 21 var ElementType = i18n.input.chrome.inputview.elements.ElementType; |
| 22 |
| 23 |
| 24 /** |
| 25 * The id for the key. |
| 26 * |
| 27 * @type {number} |
| 28 * @private |
| 29 */ |
| 30 i18n.input.chrome.inputview.layouts.util.keyId_ = 0; |
| 31 |
| 32 |
| 33 /** |
| 34 * The prefix of the key id, it's overwritten by layout file. |
| 35 * |
| 36 * @type {string} |
| 37 */ |
| 38 i18n.input.chrome.inputview.layouts.util.keyIdPrefix = ''; |
| 39 |
| 40 /** |
| 41 * Resets id counter for keys in preparation for processing a new layout. |
| 42 * @param {string} prefix The prefix for the key id. |
| 43 */ |
| 44 i18n.input.chrome.inputview.layouts.util.setPrefix = function(prefix) { |
| 45 i18n.input.chrome.inputview.layouts.util.keyIdPrefix = prefix; |
| 46 i18n.input.chrome.inputview.layouts.util.keyId_ = 0; |
| 47 }; |
| 48 |
| 49 /** |
| 50 * Creates a sequence of key with the same specification. |
| 51 * |
| 52 * @param {!Object} spec The specification. |
| 53 * @param {number} num How many keys. |
| 54 * @return {!Array.<Object>} The keys. |
| 55 */ |
| 56 i18n.input.chrome.inputview.layouts.util.createKeySequence = function(spec, |
| 57 num) { |
| 58 var sequence = []; |
| 59 for (var i = 0; i < num; i++) { |
| 60 sequence.push(i18n.input.chrome.inputview.layouts.util.createKey(spec)); |
| 61 } |
| 62 return sequence; |
| 63 }; |
| 64 |
| 65 |
| 66 /** |
| 67 * Creates a soft key view. |
| 68 * |
| 69 * @param {!Object} spec The specification. |
| 70 * @param {string=} opt_id The id. |
| 71 * @return {!Object} The soft key view. |
| 72 */ |
| 73 i18n.input.chrome.inputview.layouts.util.createKey = function(spec, opt_id) { |
| 74 var id = i18n.input.chrome.inputview.layouts.util.keyIdPrefix + |
| 75 i18n.input.chrome.inputview.layouts.util.keyId_++; |
| 76 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 77 ElementType.SOFT_KEY_VIEW, spec, id); |
| 78 }; |
| 79 |
| 80 |
| 81 /** |
| 82 * Creates a linear layout. |
| 83 * |
| 84 * @param {!Object} spec The specification. |
| 85 * @param {string=} opt_id The id. |
| 86 * @return {!Object} The linear layout. |
| 87 */ |
| 88 i18n.input.chrome.inputview.layouts.util.createLinearLayout = function(spec, |
| 89 opt_id) { |
| 90 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 91 ElementType.LINEAR_LAYOUT, spec, opt_id, spec['iconCssClass']); |
| 92 }; |
| 93 |
| 94 |
| 95 /** |
| 96 * Creates an extended layout. |
| 97 * |
| 98 * @param {!Object} spec The specification. |
| 99 * @param {string=} opt_id The id. |
| 100 * @return {!Object} The extended layout. |
| 101 */ |
| 102 i18n.input.chrome.inputview.layouts.util.createExtendedLayout = function(spec, |
| 103 opt_id) { |
| 104 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 105 ElementType.EXTENDED_LAYOUT, spec, opt_id, spec['iconCssClass']); |
| 106 }; |
| 107 |
| 108 |
| 109 /** |
| 110 * Creates a handwriting layout. |
| 111 * |
| 112 * @param {!Object} spec The specification. |
| 113 * @param {string=} opt_id The id. |
| 114 * @return {!Object} The handwriting layout. |
| 115 */ |
| 116 i18n.input.chrome.inputview.layouts.util.createHandwritingLayout = |
| 117 function(spec, opt_id) { |
| 118 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 119 ElementType.HANDWRITING_LAYOUT, spec, opt_id); |
| 120 }; |
| 121 |
| 122 |
| 123 /** |
| 124 * Creates a vertical layout. |
| 125 * |
| 126 * @param {!Object} spec The specification. |
| 127 * @param {string=} opt_id The id. |
| 128 * @return {!Object} The vertical layout. |
| 129 */ |
| 130 i18n.input.chrome.inputview.layouts.util.createVerticalLayout = function(spec, |
| 131 opt_id) { |
| 132 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 133 ElementType.VERTICAL_LAYOUT, spec, opt_id); |
| 134 }; |
| 135 |
| 136 |
| 137 /** |
| 138 * Creates a layout view. |
| 139 * |
| 140 * @param {!Object} spec The specification. |
| 141 * @param {string=} opt_id The id. |
| 142 * @return {!Object} The view. |
| 143 */ |
| 144 i18n.input.chrome.inputview.layouts.util.createLayoutView = function(spec, |
| 145 opt_id) { |
| 146 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 147 ElementType.LAYOUT_VIEW, spec, opt_id); |
| 148 }; |
| 149 |
| 150 |
| 151 /** |
| 152 * Creates a candidate view. |
| 153 * |
| 154 * @param {!Object} spec The specification. |
| 155 * @param {string=} opt_id The id. |
| 156 * @return {!Object} The view. |
| 157 */ |
| 158 i18n.input.chrome.inputview.layouts.util.createCandidateView = function(spec, |
| 159 opt_id) { |
| 160 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 161 ElementType.CANDIDATE_VIEW, spec, opt_id); |
| 162 }; |
| 163 |
| 164 |
| 165 /** |
| 166 * Creates a canvas view. |
| 167 * |
| 168 * @param {!Object} spec The specification. |
| 169 * @param {string=} opt_id The id. |
| 170 * @return {!Object} The view. |
| 171 */ |
| 172 i18n.input.chrome.inputview.layouts.util.createCanvasView = function(spec, |
| 173 opt_id) { |
| 174 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 175 ElementType.CANVAS_VIEW, spec, opt_id); |
| 176 }; |
| 177 |
| 178 |
| 179 /** |
| 180 * Creates the keyboard. |
| 181 * |
| 182 * @param {Object} spec The specification. |
| 183 * @param {string=} opt_id The id. |
| 184 * @return {Object} The keyboard. |
| 185 */ |
| 186 i18n.input.chrome.inputview.layouts.util.createKeyboard = function(spec, |
| 187 opt_id) { |
| 188 return i18n.input.chrome.inputview.layouts.util.createElem( |
| 189 ElementType.KEYBOARD, spec, opt_id); |
| 190 }; |
| 191 |
| 192 |
| 193 /** |
| 194 * Creates an element which could be any type, such as soft key view, layout, |
| 195 * etc. |
| 196 * |
| 197 * @param {!ElementType} type The type. |
| 198 * @param {Object} spec The specification. |
| 199 * @param {string=} opt_id The id. |
| 200 * @param {i18n.input.chrome.inputview.Css=} opt_iconCssClass The Css class. |
| 201 * @return {!Object} The element. |
| 202 */ |
| 203 i18n.input.chrome.inputview.layouts.util.createElem = function(type, spec, |
| 204 opt_id, opt_iconCssClass) { |
| 205 var newSpec = {}; |
| 206 for (var key in spec) { |
| 207 newSpec[key] = spec[key]; |
| 208 } |
| 209 newSpec['type'] = type; |
| 210 if (opt_id) { |
| 211 newSpec['id'] = opt_id; |
| 212 } |
| 213 if (opt_iconCssClass) { |
| 214 newSpec['iconCssClass'] = opt_iconCssClass; |
| 215 } |
| 216 return { |
| 217 'spec': newSpec |
| 218 }; |
| 219 }; |
| 220 |
| 221 }); // goog.scope |
OLD | NEW |