Index: chrome/browser/resources/shared/js/cr/ui/list.js |
diff --git a/chrome/browser/resources/shared/js/cr/ui/list.js b/chrome/browser/resources/shared/js/cr/ui/list.js |
index 33a984ebf5e87e3565666798f8bf9ca90a2343d6..9643403aaf8152e5cef9dc78e4a85c2ce1b24041 100644 |
--- a/chrome/browser/resources/shared/js/cr/ui/list.js |
+++ b/chrome/browser/resources/shared/js/cr/ui/list.js |
@@ -303,19 +303,25 @@ cr.define('cr.ui', function() { |
if (target == this && !inViewport(target, e)) |
return; |
- while (target && target.parentNode != this) { |
- target = target.parentNode; |
- } |
+ target = this.getListItemContainingElement_(target); |
- if (!target) { |
- this.selectionController_.handleMouseDownUp(e, -1); |
- } else { |
- var cs = getComputedStyle(target); |
- var top = target.offsetTop - |
- parseFloat(cs.marginTop); |
- var index = Math.floor(top / this.getItemHeight_()); |
- this.selectionController_.handleMouseDownUp(e, index); |
+ var index = target ? this.getIndexOfListItem(target) : -1; |
+ this.selectionController_.handleMouseDownUp(e, index); |
+ }, |
+ |
+ /** |
+ * Returns the list item element containing the given element, or null if |
+ * it doesn't belong to any list item element. |
+ * @param {HTMLElement} element The element. |
+ * @return {ListItem} The list item containing |element|, or null. |
+ * @private |
+ */ |
+ getListItemContainingElement_: function(element) { |
arv (Not doing code reviews)
2010/12/14 19:07:41
This should be public. Maybe rename it getListItem
stuartmorgan
2010/12/14 22:09:50
Done.
|
+ var container = element; |
+ while (container && container.parentNode != this) { |
+ container = container.parentNode; |
} |
+ return container; |
}, |
/** |
@@ -455,6 +461,22 @@ cr.define('cr.ui', function() { |
}, |
/** |
+ * Find the index of the given list item element. |
+ * @param {ListItem} item The list item to get the index of. |
+ * @return {number} The index of the list item, or -1 if not found. |
+ */ |
+ getIndexOfListItem: function(item) { |
+ var cs = getComputedStyle(item); |
+ var top = item.offsetTop - parseFloat(cs.marginTop); |
+ var index = Math.floor(top / this.getItemHeight_()); |
+ var childIndex = index - this.firstIndex_ + 1; |
+ if (childIndex >= 0 && childIndex < this.children.length && |
+ this.children[childIndex] == item) |
+ return index; |
+ return -1; |
+ }, |
+ |
+ /** |
* Creates a new list item. |
* @param {*} value The value to use for the item. |
* @return {!ListItem} The newly created list item. |