Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('cr.ui', function() { | 5 cr.define('cr.ui', function() { |
| 6 // require cr.ui.define | 6 // require cr.ui.define |
| 7 // require cr.ui.limitInputWidth | 7 // require cr.ui.limitInputWidth |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * The number of pixels to indent per level. | |
| 11 * @type {number} | |
| 12 */ | |
| 13 const INDENT = 20; | |
| 14 | |
| 15 /** | |
| 16 * Returns the computed style for an element. | |
| 17 * @param {!Element} el The element to get the computed style for. | |
| 18 * @return {!CSSStyleDeclaration} The computed style. | |
| 19 */ | |
| 20 function getComputedStyle(el) { | |
| 21 return el.ownerDocument.defaultView.getComputedStyle(el); | |
| 22 } | |
| 23 | |
| 24 /** | |
| 10 * Helper function that finds the first ancestor tree item. | 25 * Helper function that finds the first ancestor tree item. |
| 11 * @param {!Element} el The element to start searching from. | 26 * @param {!Element} el The element to start searching from. |
| 12 * @return {cr.ui.TreeItem} The found tree item or null if not found. | 27 * @return {cr.ui.TreeItem} The found tree item or null if not found. |
| 13 */ | 28 */ |
| 14 function findTreeItem(el) { | 29 function findTreeItem(el) { |
| 15 while (el && !(el instanceof TreeItem)) { | 30 while (el && !(el instanceof TreeItem)) { |
| 16 el = el.parentNode; | 31 el = el.parentNode; |
| 17 } | 32 } |
| 18 return el; | 33 return el; |
| 19 } | 34 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 48 */ | 63 */ |
| 49 get items() { | 64 get items() { |
| 50 return this.children; | 65 return this.children; |
| 51 }, | 66 }, |
| 52 | 67 |
| 53 /** | 68 /** |
| 54 * Adds a tree item to the tree. | 69 * Adds a tree item to the tree. |
| 55 * @param {!cr.ui.TreeItem} treeItem The item to add. | 70 * @param {!cr.ui.TreeItem} treeItem The item to add. |
| 56 */ | 71 */ |
| 57 add: function(treeItem) { | 72 add: function(treeItem) { |
| 58 this.appendChild(treeItem); | 73 this.addAt(treeItem, 0xffffffff); |
| 59 }, | 74 }, |
| 60 | 75 |
| 61 /** | 76 /** |
| 62 * Adds a tree item at the given index. | 77 * Adds a tree item at the given index. |
| 63 * @param {!cr.ui.TreeItem} treeItem The item to add. | 78 * @param {!cr.ui.TreeItem} treeItem The item to add. |
| 64 * @param {number} index The index where we want to add the item. | 79 * @param {number} index The index where we want to add the item. |
| 65 */ | 80 */ |
| 66 addAt: function(treeItem, index) { | 81 addAt: function(treeItem, index) { |
| 67 this.insertBefore(treeItem, this.children[index]); | 82 this.insertBefore(treeItem, this.children[index]); |
| 83 treeItem.setDepth_(this.depth + 1); | |
| 68 }, | 84 }, |
| 69 | 85 |
| 70 /** | 86 /** |
| 71 * Removes a tree item child. | 87 * Removes a tree item child. |
| 72 * @param {!cr.ui.TreeItem} treeItem The tree item to remove. | 88 * @param {!cr.ui.TreeItem} treeItem The tree item to remove. |
| 73 */ | 89 */ |
| 74 remove: function(treeItem) { | 90 remove: function(treeItem) { |
| 75 this.removeChild(treeItem); | 91 this.removeChild(treeItem); |
| 76 }, | 92 }, |
| 77 | 93 |
| 78 /** | 94 /** |
| 95 * The depth of the node. This is 0 for the tree itself. | |
| 96 * @type {number} | |
| 97 */ | |
| 98 get depth() { | |
| 99 return 0 | |
|
feldstein
2010/04/06 00:56:57
semicolon
| |
| 100 }, | |
| 101 | |
| 102 /** | |
| 79 * Handles click events on the tree and forwards the event to the relevant | 103 * Handles click events on the tree and forwards the event to the relevant |
| 80 * tree items as necesary. | 104 * tree items as necesary. |
| 81 * @param {Event} e The click event object. | 105 * @param {Event} e The click event object. |
| 82 */ | 106 */ |
| 83 handleClick: function(e) { | 107 handleClick: function(e) { |
| 84 var treeItem = findTreeItem(e.target); | 108 var treeItem = findTreeItem(e.target); |
| 85 if (treeItem) | 109 if (treeItem) |
| 86 treeItem.handleClick(e); | 110 treeItem.handleClick(e); |
| 87 }, | 111 }, |
| 88 | 112 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 106 * of tree items. | 130 * of tree items. |
| 107 * @param {Event} e The click event object. | 131 * @param {Event} e The click event object. |
| 108 */ | 132 */ |
| 109 handleKeyDown: function(e) { | 133 handleKeyDown: function(e) { |
| 110 var itemToSelect; | 134 var itemToSelect; |
| 111 if (e.ctrlKey) | 135 if (e.ctrlKey) |
| 112 return; | 136 return; |
| 113 | 137 |
| 114 var item = this.selectedItem; | 138 var item = this.selectedItem; |
| 115 | 139 |
| 116 var rtl = window.getComputedStyle(item).direction == 'rtl'; | 140 var rtl = getComputedStyle(item).direction == 'rtl'; |
| 117 | 141 |
| 118 switch (e.keyIdentifier) { | 142 switch (e.keyIdentifier) { |
| 119 case 'Up': | 143 case 'Up': |
| 120 itemToSelect = item ? getPrevious(item) : | 144 itemToSelect = item ? getPrevious(item) : |
| 121 this.items[this.items.length - 1]; | 145 this.items[this.items.length - 1]; |
| 122 break; | 146 break; |
| 123 case 'Down': | 147 case 'Down': |
| 124 itemToSelect = item ? getNext(item) : | 148 itemToSelect = item ? getNext(item) : |
| 125 this.items[0]; | 149 this.items[0]; |
| 126 break; | 150 break; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 }, | 242 }, |
| 219 | 243 |
| 220 /** | 244 /** |
| 221 * The tree items children. | 245 * The tree items children. |
| 222 */ | 246 */ |
| 223 get items() { | 247 get items() { |
| 224 return this.lastElementChild.children; | 248 return this.lastElementChild.children; |
| 225 }, | 249 }, |
| 226 | 250 |
| 227 /** | 251 /** |
| 252 * The depth of the tree item. | |
| 253 * @type {number} | |
| 254 */ | |
| 255 depth_: 0, | |
| 256 get depth() { | |
| 257 return this.depth_; | |
| 258 }, | |
| 259 | |
| 260 /** | |
| 261 * Sets the depth. | |
| 262 * @param {number} depth The new depth. | |
| 263 * @private | |
| 264 */ | |
| 265 setDepth_: function(depth) { | |
| 266 if (depth != this.depth_) { | |
| 267 this.rowElement.style.WebkitPaddingStart = depth * INDENT + 'px'; | |
| 268 this.depth_ = depth; | |
| 269 var items = this.items; | |
| 270 for (var i = 0, item; item = items[i]; i++) { | |
| 271 item.setDepth_(depth + 1); | |
| 272 } | |
| 273 } | |
| 274 }, | |
| 275 | |
| 276 /** | |
| 228 * Adds a tree item as a child. | 277 * Adds a tree item as a child. |
| 229 * @param {!cr.ui.TreeItem} child The child to add. | 278 * @param {!cr.ui.TreeItem} child The child to add. |
| 230 */ | 279 */ |
| 231 add: function(child) { | 280 add: function(child) { |
| 232 this.addAt(child, 0xffffffff); | 281 this.addAt(child, 0xffffffff); |
| 233 }, | 282 }, |
| 234 | 283 |
| 235 /** | 284 /** |
| 236 * Adds a tree item as a child at a given index. | 285 * Adds a tree item as a child at a given index. |
| 237 * @param {!cr.ui.TreeItem} child The child to add. | 286 * @param {!cr.ui.TreeItem} child The child to add. |
| 238 * @param {number} index The index where to add the child. | 287 * @param {number} index The index where to add the child. |
| 239 */ | 288 */ |
| 240 addAt: function(child, index) { | 289 addAt: function(child, index) { |
| 241 this.lastElementChild.insertBefore(child, this.items[index]); | 290 this.lastElementChild.insertBefore(child, this.items[index]); |
| 242 if (this.items.length == 1) | 291 if (this.items.length == 1) |
| 243 this.hasChildren_ = true; | 292 this.hasChildren_ = true; |
| 293 child.setDepth_(this.depth + 1); | |
| 244 }, | 294 }, |
| 245 | 295 |
| 246 /** | 296 /** |
| 247 * Removes a child. | 297 * Removes a child. |
| 248 * @param {!cr.ui.TreeItem} child The tree item child to remove. | 298 * @param {!cr.ui.TreeItem} child The tree item child to remove. |
| 249 */ | 299 */ |
| 250 remove: function(child) { | 300 remove: function(child) { |
| 251 // If we removed the selected item we should become selected. | 301 // If we removed the selected item we should become selected. |
| 252 var tree = this.tree; | 302 var tree = this.tree; |
| 253 var selectedItem = tree.selectedItem; | 303 var selectedItem = tree.selectedItem; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 }, | 402 }, |
| 353 set label(s) { | 403 set label(s) { |
| 354 this.labelElement.textContent = s; | 404 this.labelElement.textContent = s; |
| 355 }, | 405 }, |
| 356 | 406 |
| 357 /** | 407 /** |
| 358 * The URL for the icon. | 408 * The URL for the icon. |
| 359 * @type {string} | 409 * @type {string} |
| 360 */ | 410 */ |
| 361 get icon() { | 411 get icon() { |
| 362 return window.getComputedStyle(this.labelElement). | 412 return getComputedStyle(this.labelElement).backgroundImage.slice(4, -1); |
| 363 backgroundImage.slice(4, -1); | |
| 364 }, | 413 }, |
| 365 set icon(icon) { | 414 set icon(icon) { |
| 366 return this.labelElement.style.backgroundImage = url(icon); | 415 return this.labelElement.style.backgroundImage = url(icon); |
| 367 }, | 416 }, |
| 368 | 417 |
| 369 /** | 418 /** |
| 370 * Whether the tree item is selected or not. | 419 * Whether the tree item is selected or not. |
| 371 * @type {boolean} | 420 * @type {boolean} |
| 372 */ | 421 */ |
| 373 get selected() { | 422 get selected() { |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 594 } | 643 } |
| 595 return item; | 644 return item; |
| 596 } | 645 } |
| 597 | 646 |
| 598 // Export | 647 // Export |
| 599 return { | 648 return { |
| 600 Tree: Tree, | 649 Tree: Tree, |
| 601 TreeItem: TreeItem | 650 TreeItem: TreeItem |
| 602 }; | 651 }; |
| 603 }); | 652 }); |
| OLD | NEW |