| 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 // require: listselectionmodel.js | 5 // require: listselectionmodel.js |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * @fileoverview This implements a list control. | 8 * @fileoverview This implements a list control. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 if (this.disabled) | 296 if (this.disabled) |
| 297 return; | 297 return; |
| 298 | 298 |
| 299 var target = e.target; | 299 var target = e.target; |
| 300 | 300 |
| 301 // If the target was this element we need to make sure that the user did | 301 // If the target was this element we need to make sure that the user did |
| 302 // not click on a border or a scrollbar. | 302 // not click on a border or a scrollbar. |
| 303 if (target == this && !inViewport(target, e)) | 303 if (target == this && !inViewport(target, e)) |
| 304 return; | 304 return; |
| 305 | 305 |
| 306 while (target && target.parentNode != this) { | 306 target = this.getListItemAncestor(target); |
| 307 target = target.parentNode; | |
| 308 } | |
| 309 | 307 |
| 310 if (!target) { | 308 var index = target ? this.getIndexOfListItem(target) : -1; |
| 311 this.selectionController_.handleMouseDownUp(e, -1); | 309 this.selectionController_.handleMouseDownUp(e, index); |
| 312 } else { | |
| 313 var cs = getComputedStyle(target); | |
| 314 var top = target.offsetTop - | |
| 315 parseFloat(cs.marginTop); | |
| 316 var index = Math.floor(top / this.getItemHeight_()); | |
| 317 this.selectionController_.handleMouseDownUp(e, index); | |
| 318 } | |
| 319 }, | 310 }, |
| 320 | 311 |
| 321 /** | 312 /** |
| 313 * Returns the list item element containing the given element, or null if |
| 314 * it doesn't belong to any list item element. |
| 315 * @param {HTMLElement} element The element. |
| 316 * @return {ListItem} The list item containing |element|, or null. |
| 317 */ |
| 318 getListItemAncestor: function(element) { |
| 319 var container = element; |
| 320 while (container && container.parentNode != this) { |
| 321 container = container.parentNode; |
| 322 } |
| 323 return container; |
| 324 }, |
| 325 |
| 326 /** |
| 322 * Handle a keydown event. | 327 * Handle a keydown event. |
| 323 * @param {Event} e The keydown event. | 328 * @param {Event} e The keydown event. |
| 324 * @return {boolean} Whether the key event was handled. | 329 * @return {boolean} Whether the key event was handled. |
| 325 */ | 330 */ |
| 326 handleKeyDown: function(e) { | 331 handleKeyDown: function(e) { |
| 327 if (this.disabled) | 332 if (this.disabled) |
| 328 return; | 333 return; |
| 329 | 334 |
| 330 return this.selectionController_.handleKeyDown(e); | 335 return this.selectionController_.handleKeyDown(e); |
| 331 }, | 336 }, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 * @return {ListItem} The found list item or null if not found. | 453 * @return {ListItem} The found list item or null if not found. |
| 449 */ | 454 */ |
| 450 getListItemByIndex: function(index) { | 455 getListItemByIndex: function(index) { |
| 451 if (index < this.firstIndex_ || index >= this.lastIndex_) | 456 if (index < this.firstIndex_ || index >= this.lastIndex_) |
| 452 return null; | 457 return null; |
| 453 | 458 |
| 454 return this.children[index - this.firstIndex_ + 1]; | 459 return this.children[index - this.firstIndex_ + 1]; |
| 455 }, | 460 }, |
| 456 | 461 |
| 457 /** | 462 /** |
| 463 * Find the index of the given list item element. |
| 464 * @param {ListItem} item The list item to get the index of. |
| 465 * @return {number} The index of the list item, or -1 if not found. |
| 466 */ |
| 467 getIndexOfListItem: function(item) { |
| 468 var cs = getComputedStyle(item); |
| 469 var top = item.offsetTop - parseFloat(cs.marginTop); |
| 470 var index = Math.floor(top / this.getItemHeight_()); |
| 471 var childIndex = index - this.firstIndex_ + 1; |
| 472 if (childIndex >= 0 && childIndex < this.children.length && |
| 473 this.children[childIndex] == item) |
| 474 return index; |
| 475 return -1; |
| 476 }, |
| 477 |
| 478 /** |
| 458 * Creates a new list item. | 479 * Creates a new list item. |
| 459 * @param {*} value The value to use for the item. | 480 * @param {*} value The value to use for the item. |
| 460 * @return {!ListItem} The newly created list item. | 481 * @return {!ListItem} The newly created list item. |
| 461 */ | 482 */ |
| 462 createItem: function(value) { | 483 createItem: function(value) { |
| 463 return new cr.ui.ListItem({label: value}); | 484 return new cr.ui.ListItem({label: value}); |
| 464 }, | 485 }, |
| 465 | 486 |
| 466 /** | 487 /** |
| 467 * Creates the selection controller to use internally. | 488 * Creates the selection controller to use internally. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 } | 591 } |
| 571 } | 592 } |
| 572 }; | 593 }; |
| 573 | 594 |
| 574 cr.defineProperty(List, 'disabled', cr.PropertyKind.BOOL_ATTR); | 595 cr.defineProperty(List, 'disabled', cr.PropertyKind.BOOL_ATTR); |
| 575 | 596 |
| 576 return { | 597 return { |
| 577 List: List | 598 List: List |
| 578 } | 599 } |
| 579 }); | 600 }); |
| OLD | NEW |