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.getListItemContainingElement_(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 * @private | |
318 */ | |
319 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.
| |
320 var container = element; | |
321 while (container && container.parentNode != this) { | |
322 container = container.parentNode; | |
323 } | |
324 return container; | |
325 }, | |
326 | |
327 /** | |
322 * Handle a keydown event. | 328 * Handle a keydown event. |
323 * @param {Event} e The keydown event. | 329 * @param {Event} e The keydown event. |
324 * @return {boolean} Whether the key event was handled. | 330 * @return {boolean} Whether the key event was handled. |
325 */ | 331 */ |
326 handleKeyDown: function(e) { | 332 handleKeyDown: function(e) { |
327 if (this.disabled) | 333 if (this.disabled) |
328 return; | 334 return; |
329 | 335 |
330 return this.selectionController_.handleKeyDown(e); | 336 return this.selectionController_.handleKeyDown(e); |
331 }, | 337 }, |
(...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. | 454 * @return {ListItem} The found list item or null if not found. |
449 */ | 455 */ |
450 getListItemByIndex: function(index) { | 456 getListItemByIndex: function(index) { |
451 if (index < this.firstIndex_ || index >= this.lastIndex_) | 457 if (index < this.firstIndex_ || index >= this.lastIndex_) |
452 return null; | 458 return null; |
453 | 459 |
454 return this.children[index - this.firstIndex_ + 1]; | 460 return this.children[index - this.firstIndex_ + 1]; |
455 }, | 461 }, |
456 | 462 |
457 /** | 463 /** |
464 * Find the index of the given list item element. | |
465 * @param {ListItem} item The list item to get the index of. | |
466 * @return {number} The index of the list item, or -1 if not found. | |
467 */ | |
468 getIndexOfListItem: function(item) { | |
469 var cs = getComputedStyle(item); | |
470 var top = item.offsetTop - parseFloat(cs.marginTop); | |
471 var index = Math.floor(top / this.getItemHeight_()); | |
472 var childIndex = index - this.firstIndex_ + 1; | |
473 if (childIndex >= 0 && childIndex < this.children.length && | |
474 this.children[childIndex] == item) | |
475 return index; | |
476 return -1; | |
477 }, | |
478 | |
479 /** | |
458 * Creates a new list item. | 480 * Creates a new list item. |
459 * @param {*} value The value to use for the item. | 481 * @param {*} value The value to use for the item. |
460 * @return {!ListItem} The newly created list item. | 482 * @return {!ListItem} The newly created list item. |
461 */ | 483 */ |
462 createItem: function(value) { | 484 createItem: function(value) { |
463 return new cr.ui.ListItem({label: value}); | 485 return new cr.ui.ListItem({label: value}); |
464 }, | 486 }, |
465 | 487 |
466 /** | 488 /** |
467 * Creates the selection controller to use internally. | 489 * Creates the selection controller to use internally. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 } | 592 } |
571 } | 593 } |
572 }; | 594 }; |
573 | 595 |
574 cr.defineProperty(List, 'disabled', cr.PropertyKind.BOOL_ATTR); | 596 cr.defineProperty(List, 'disabled', cr.PropertyKind.BOOL_ATTR); |
575 | 597 |
576 return { | 598 return { |
577 List: List | 599 List: List |
578 } | 600 } |
579 }); | 601 }); |
OLD | NEW |