OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Sets the width (in pixels) on a DOM node. |
| 7 * @param {!HtmlNode} node The node whose width is being set. |
| 8 * @param {number} widthPx The width in pixels. |
| 9 */ |
| 10 function setNodeWidth(node, widthPx) { |
| 11 node.style.width = widthPx.toFixed(0) + 'px'; |
| 12 } |
| 13 |
| 14 /** |
| 15 * Sets the height (in pixels) on a DOM node. |
| 16 * @param {!HtmlNode} node The node whose height is being set. |
| 17 * @param {number} heightPx The height in pixels. |
| 18 */ |
| 19 function setNodeHeight(node, heightPx) { |
| 20 node.style.height = heightPx.toFixed(0) + 'px'; |
| 21 } |
| 22 |
| 23 /** |
| 24 * Sets the position and size of a DOM node (in pixels). |
| 25 * @param {!HtmlNode} node The node being positioned. |
| 26 * @param {number} leftPx The left position in pixels. |
| 27 * @param {number} topPx The top position in pixels. |
| 28 * @param {number} widthPx The width in pixels. |
| 29 * @param {number} heightPx The height in pixels. |
| 30 */ |
| 31 function setNodePosition(node, leftPx, topPx, widthPx, heightPx) { |
| 32 node.style.left = leftPx.toFixed(0) + 'px'; |
| 33 node.style.top = topPx.toFixed(0) + 'px'; |
| 34 setNodeWidth(node, widthPx); |
| 35 setNodeHeight(node, heightPx); |
| 36 } |
| 37 |
| 38 /** |
| 39 * Sets the visibility for a DOM node. |
| 40 * @param {!HtmlNode} node The node being positioned. |
| 41 * @param {boolean} isVisible Whether to show the node or not. |
| 42 */ |
| 43 function setNodeDisplay(node, isVisible) { |
| 44 node.style.display = isVisible ? '' : 'none'; |
| 45 } |
| 46 |
| 47 /** |
| 48 * Adds a node to |parentNode|, of type |tagName|. |
| 49 * @param {!HtmlNode} parentNode The node that will be the parent of the new |
| 50 * element. |
| 51 * @param {string} tagName the tag name of the new element. |
| 52 * @return {!HtmlElement} The newly created element. |
| 53 */ |
| 54 function addNode(parentNode, tagName) { |
| 55 var elem = parentNode.ownerDocument.createElement(tagName); |
| 56 parentNode.appendChild(elem); |
| 57 return elem; |
| 58 } |
| 59 |
| 60 /** |
| 61 * Adds |text| to node |parentNode|. |
| 62 * @param {!HtmlNode} parentNode The node to add text to. |
| 63 * @param {string} text The text to be added. |
| 64 * @return {!Object} The newly created text node. |
| 65 */ |
| 66 function addTextNode(parentNode, text) { |
| 67 var textNode = parentNode.ownerDocument.createTextNode(text); |
| 68 parentNode.appendChild(textNode); |
| 69 return textNode; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Adds a node to |parentNode|, of type |tagName|. Then adds |
| 74 * |text| to the new node. |
| 75 * @param {!HtmlNode} parentNode The node that will be the parent of the new |
| 76 * element. |
| 77 * @param {string} tagName the tag name of the new element. |
| 78 * @param {string} text The text to be added. |
| 79 * @return {!HtmlElement} The newly created element. |
| 80 */ |
| 81 function addNodeWithText(parentNode, tagName, text) { |
| 82 var elem = parentNode.ownerDocument.createElement(tagName); |
| 83 parentNode.appendChild(elem); |
| 84 addTextNode(elem, text); |
| 85 return elem; |
| 86 } |
| 87 |
| 88 /** |
| 89 * Returns the key such that map[key] == value, or the string '?' if |
| 90 * there is no such key. |
| 91 * @param {!Object} map The object being used as a lookup table. |
| 92 * @param {Object} value The value to be found in |map|. |
| 93 * @return {string} The key for |value|, or '?' if there is no such key. |
| 94 */ |
| 95 function getKeyWithValue(map, value) { |
| 96 for (var key in map) { |
| 97 if (map[key] == value) |
| 98 return key; |
| 99 } |
| 100 return '?'; |
| 101 } |
| 102 |
| 103 /** |
| 104 * Returns a new map with the keys and values of the input map inverted. |
| 105 * @param {!Object} map The object to have its keys and values reversed. Not |
| 106 * modified by the function call. |
| 107 * @return {Object} The new map with the reversed keys and values. |
| 108 */ |
| 109 function makeInverseMap(map) { |
| 110 var reversed = {}; |
| 111 for (var key in map) |
| 112 reversed[map[key]] = key; |
| 113 return reversed; |
| 114 } |
| 115 |
| 116 /** |
| 117 * Looks up |key| in |map|, and returns the resulting entry, if there is one. |
| 118 * Otherwise, returns |key|. Intended primarily for use with incomplete |
| 119 * tables, and for reasonable behavior with system enumerations that may be |
| 120 * extended in the future. |
| 121 * @param {!Object} map The table in which |key| is looked up. |
| 122 * @param {string} key The key to look up. |
| 123 * @return {Object|string} map[key], if it exists, or |key| if it doesn't. |
| 124 */ |
| 125 function tryGetValueWithKey(map, key) { |
| 126 if (key in map) |
| 127 return map[key]; |
| 128 return key; |
| 129 } |
| 130 |
| 131 /** |
| 132 * Builds a string by repeating |str| |count| times. |
| 133 * @param {string} str The string to be repeated. |
| 134 * @param {number} count The number of times to repeat |str|. |
| 135 * @return {string} The constructed string |
| 136 */ |
| 137 function makeRepeatedString(str, count) { |
| 138 var out = []; |
| 139 for (var i = 0; i < count; ++i) |
| 140 out.push(str); |
| 141 return out.join(''); |
| 142 } |
| 143 |
| 144 /** |
| 145 * Clones a basic POD object. Only a new top level object will be cloned. It |
| 146 * will continue to reference the same values as the original object. |
| 147 * @param {Object} object The object to be cloned. |
| 148 * @return {Object} A copy of |object|. |
| 149 */ |
| 150 function shallowCloneObject(object) { |
| 151 if (!(object instanceof Object)) |
| 152 return object; |
| 153 var copy = {}; |
| 154 for (var key in object) { |
| 155 copy[key] = object[key]; |
| 156 } |
| 157 return copy; |
| 158 } |
| 159 |
| 160 /** |
| 161 * Helper to make sure singleton classes are not instantiated more than once. |
| 162 * @param {Function} ctor The constructor function being checked. |
| 163 */ |
| 164 function assertFirstConstructorCall(ctor) { |
| 165 // This is the variable which is set by cr.addSingletonGetter(). |
| 166 if (ctor.hasCreateFirstInstance_) { |
| 167 throw Error('The class ' + ctor.name + ' is a singleton, and should ' + |
| 168 'only be accessed using ' + ctor.name + '.getInstance().'); |
| 169 } |
| 170 ctor.hasCreateFirstInstance_ = true; |
| 171 } |
| 172 |
| 173 function hasTouchScreen() { |
| 174 return 'ontouchstart' in window; |
| 175 } |
| 176 |
OLD | NEW |