Chromium Code Reviews| 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 cr.define('options', function() { | |
| 6 const List = cr.ui.List; | |
| 7 const ListItem = cr.ui.ListItem; | |
| 8 | |
| 9 /** | |
| 10 * Wraps a list item to make it deletable, adding a button that will trigger a | |
| 11 * call to deleteItemAtIndex(index) in the list. | |
| 12 * @param {!ListItem} baseItem The list element to wrap. | |
| 13 */ | |
| 14 function DeletableListItem(baseItem) { | |
| 15 var el = cr.doc.createElement('div'); | |
| 16 el.baseItem_ = baseItem; | |
| 17 DeletableListItem.decorate(el); | |
| 18 return el; | |
| 19 } | |
| 20 | |
| 21 /** | |
| 22 * Decorates an element as a deletable list item. | |
| 23 * @param {!HTMLElement} el The element to decorate. | |
| 24 */ | |
| 25 DeletableListItem.decorate = function(el) { | |
| 26 el.__proto__ = DeletableListItem.prototype; | |
| 27 el.decorate(); | |
| 28 }; | |
| 29 | |
| 30 DeletableListItem.prototype = { | |
| 31 __proto__: ListItem.prototype, | |
| 32 | |
| 33 /** | |
| 34 * The list item being wrapped to make in deletable. | |
| 35 * @type {!ListItem} | |
| 36 * @private | |
| 37 */ | |
| 38 baseItem_: null, | |
| 39 | |
| 40 /** @inheritDoc */ | |
| 41 decorate: function() { | |
| 42 ListItem.prototype.decorate.call(this); | |
| 43 | |
| 44 this.className = 'deletable-item'; | |
| 45 var contentEl = this.ownerDocument.createElement('div'); | |
| 46 contentEl.appendChild(this.baseItem_); | |
| 47 var closeButtonEl = this.ownerDocument.createElement('button'); | |
| 48 closeButtonEl.className = 'close-button'; | |
| 49 var self = this; | |
|
arv (Not doing code reviews)
2010/12/14 19:07:41
self is no longer used
stuartmorgan
2010/12/14 22:09:50
Done.
| |
| 50 | |
| 51 this.appendChild(contentEl); | |
| 52 this.appendChild(closeButtonEl); | |
| 53 }, | |
| 54 | |
| 55 /** | |
| 56 * Returns the list item being wrapped to make in deletable. | |
| 57 * @return {!ListItem} The list item being wrapped | |
| 58 */ | |
| 59 get contentItem() { | |
| 60 return this.baseItem_; | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 var DeletableItemList = cr.ui.define('list'); | |
| 65 | |
| 66 DeletableItemList.prototype = { | |
| 67 __proto__: List.prototype, | |
| 68 | |
| 69 /** @inheritDoc */ | |
| 70 decorate: function() { | |
| 71 List.prototype.decorate.call(this); | |
| 72 this.addEventListener('click', this.handleClick_); | |
| 73 }, | |
| 74 | |
| 75 /** @inheritDoc */ | |
| 76 createItem: function(value) { | |
| 77 var baseItem = this.createItemContents(value); | |
| 78 return new DeletableListItem(baseItem); | |
| 79 }, | |
| 80 | |
| 81 /** | |
| 82 * Creates a new list item to use as the main row contents for |value|. | |
| 83 * Subclasses should override this instead of createItem. | |
| 84 * @param {*} value The value to use for the item. | |
| 85 * @return {!ListItem} The newly created list item. | |
| 86 */ | |
| 87 createItemContents: function(value) { | |
| 88 List.prototype.createItem.call(this, value); | |
| 89 }, | |
| 90 | |
| 91 /** | |
| 92 * Callback for onclick events. | |
| 93 * @param {Event} e The click event object. | |
| 94 * @private | |
| 95 */ | |
| 96 handleClick_: function(e) { | |
| 97 if (this.disabled) | |
| 98 return; | |
| 99 | |
| 100 var target = e.target; | |
| 101 if (target.className == 'close-button') { | |
| 102 var listItem = this.getListItemContainingElement_(target); | |
|
arv (Not doing code reviews)
2010/12/14 19:07:41
Where is getListItemContainingElement_ defined?
S
stuartmorgan
2010/12/14 22:09:50
Oops, victim of refactoring. This part of the logi
| |
| 103 if (listItem) | |
| 104 this.deleteItemAtIndex(this.getIndexOfListItem(listItem)); | |
| 105 } | |
| 106 }, | |
| 107 | |
| 108 /** | |
| 109 * Called when an item should be deleted; subclasses are responsible for | |
| 110 * implementing. | |
| 111 * @param {number} index The indexd of the item that is being deleted. | |
| 112 */ | |
| 113 deleteItemAtIndex: function(index) { | |
| 114 }, | |
| 115 }; | |
| 116 | |
| 117 return { | |
| 118 DeletableItemList: DeletableItemList | |
| 119 }; | |
| 120 }); | |
| OLD | NEW |