OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 * Helper that binds the |this| object to a method to create a callback. |
| 7 */ |
| 8 Function.prototype.bind = function(thisObj) { |
| 9 var func = this; |
| 10 var args = Array.prototype.slice.call(arguments, 1); |
| 11 return function() { |
| 12 return func.apply(thisObj, |
| 13 args.concat(Array.prototype.slice.call(arguments, 0))) |
| 14 }; |
| 15 }; |
| 16 |
| 17 /** |
| 18 * Sets the width (in pixels) on a DOM node. |
| 19 */ |
| 20 function setNodeWidth(node, widthPx) { |
| 21 node.style.width = widthPx.toFixed(0) + "px"; |
| 22 } |
| 23 |
| 24 /** |
| 25 * Sets the height (in pixels) on a DOM node. |
| 26 */ |
| 27 function setNodeHeight(node, heightPx) { |
| 28 node.style.height = heightPx.toFixed(0) + "px"; |
| 29 } |
| 30 |
| 31 /** |
| 32 * Sets the position and size of a DOM node (in pixels). |
| 33 */ |
| 34 function setNodePosition(node, leftPx, topPx, widthPx, heightPx) { |
| 35 node.style.left = leftPx.toFixed(0) + "px"; |
| 36 node.style.top = topPx.toFixed(0) + "px"; |
| 37 setNodeWidth(node, widthPx); |
| 38 setNodeHeight(node, heightPx); |
| 39 } |
| 40 |
| 41 /** |
| 42 * Adds a node to |parentNode|, of type |tagName|. |
| 43 */ |
| 44 function addNode(parentNode, tagName) { |
| 45 var elem = parentNode.ownerDocument.createElement(tagName); |
| 46 parentNode.appendChild(elem); |
| 47 return elem; |
| 48 } |
| 49 |
| 50 /** |
| 51 * Adds text to node |parentNode|. |
| 52 */ |
| 53 function addTextNode(parentNode, text) { |
| 54 var textNode = parentNode.ownerDocument.createTextNode(text); |
| 55 parentNode.appendChild(textNode); |
| 56 return textNode; |
| 57 } |
| 58 |
| 59 /** |
| 60 * Adds or removes a CSS class to |node|. |
| 61 */ |
| 62 function changeClassName(node, classNameToAddOrRemove, isAdd) { |
| 63 // Multiple classes can be separated by spaces. |
| 64 var currentNames = node.className.split(" "); |
| 65 |
| 66 if (isAdd) { |
| 67 if (!(classNameToAddOrRemove in currentNames)) { |
| 68 currentNames.push(classNameToAddOrRemove); |
| 69 } |
| 70 } else { |
| 71 for (var i = 0; i < currentNames.length; ++i) { |
| 72 if (currentNames[i] == classNameToAddOrRemove) { |
| 73 currentNames.splice(i, 1); |
| 74 break; |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 node.className = currentNames.join(" "); |
| 80 } |
| 81 |
| 82 function getKeyWithValue(map, value) { |
| 83 for (key in map) { |
| 84 if (map[key] == value) |
| 85 return key; |
| 86 } |
| 87 return '?'; |
| 88 } |
OLD | NEW |