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.
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.
10 function setNodeWidth(node, widthPx) {
11 node.style.width = widthPx.toFixed(0) + 'px';
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.
19 function setNodeHeight(node, heightPx) {
20 node.style.height = heightPx.toFixed(0) + 'px';
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.
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);
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.
43 function setNodeDisplay(node, isVisible) {
44 node.style.display = isVisible ? '' : 'none';
48 * Adds a node to |parentNode|, of type |tagName|.
49 * @param {!HtmlNode} parentNode The node that will be the parent of the new
51 * @param {string} tagName the tag name of the new element.
52 * @return {!HtmlElement} The newly created element.
54 function addNode(parentNode, tagName) {
55 var elem = parentNode.ownerDocument.createElement(tagName);
56 parentNode.appendChild(elem);
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.
66 function addTextNode(parentNode, text) {
67 var textNode = parentNode.ownerDocument.createTextNode(text);
68 parentNode.appendChild(textNode);
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
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.
81 function addNodeWithText(parentNode, tagName, text) {
82 var elem = parentNode.ownerDocument.createElement(tagName);
83 parentNode.appendChild(elem);
84 addTextNode(elem, text);
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.
95 function getKeyWithValue(map, value) {
96 for (var key in map) {
97 if (map[key] == value)
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.
109 function makeInverseMap(map) {
112 reversed[map[key]] = key;
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.
125 function tryGetValueWithKey(map, key) {
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
137 function makeRepeatedString(str, count) {
139 for (var i = 0; i < count; ++i)
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|.
150 function shallowCloneObject(object) {
151 if (!(object instanceof Object))
154 for (var key in object) {
155 copy[key] = object[key];
161 * Helper to make sure singleton classes are not instantiated more than once.
162 * @param {Function} ctor The constructor function being checked.
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().');
170 ctor.hasCreateFirstInstance_ = true;
173 function hasTouchScreen() {
174 return 'ontouchstart' in window;