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 |
| 50 this.appendChild(contentEl); |
| 51 this.appendChild(closeButtonEl); |
| 52 }, |
| 53 |
| 54 /** |
| 55 * Returns the list item being wrapped to make in deletable. |
| 56 * @return {!ListItem} The list item being wrapped |
| 57 */ |
| 58 get contentItem() { |
| 59 return this.baseItem_; |
| 60 } |
| 61 }; |
| 62 |
| 63 var DeletableItemList = cr.ui.define('list'); |
| 64 |
| 65 DeletableItemList.prototype = { |
| 66 __proto__: List.prototype, |
| 67 |
| 68 /** @inheritDoc */ |
| 69 decorate: function() { |
| 70 List.prototype.decorate.call(this); |
| 71 this.addEventListener('click', this.handleClick_); |
| 72 }, |
| 73 |
| 74 /** @inheritDoc */ |
| 75 createItem: function(value) { |
| 76 var baseItem = this.createItemContents(value); |
| 77 return new DeletableListItem(baseItem); |
| 78 }, |
| 79 |
| 80 /** |
| 81 * Creates a new list item to use as the main row contents for |value|. |
| 82 * Subclasses should override this instead of createItem. |
| 83 * @param {*} value The value to use for the item. |
| 84 * @return {!ListItem} The newly created list item. |
| 85 */ |
| 86 createItemContents: function(value) { |
| 87 List.prototype.createItem.call(this, value); |
| 88 }, |
| 89 |
| 90 /** |
| 91 * Callback for onclick events. |
| 92 * @param {Event} e The click event object. |
| 93 * @private |
| 94 */ |
| 95 handleClick_: function(e) { |
| 96 if (this.disabled) |
| 97 return; |
| 98 |
| 99 var target = e.target; |
| 100 if (target.className == 'close-button') { |
| 101 var listItem = this.getListItemAncestor(target); |
| 102 if (listItem) |
| 103 this.deleteItemAtIndex(this.getIndexOfListItem(listItem)); |
| 104 } |
| 105 }, |
| 106 |
| 107 /** |
| 108 * Called when an item should be deleted; subclasses are responsible for |
| 109 * implementing. |
| 110 * @param {number} index The indexd of the item that is being deleted. |
| 111 */ |
| 112 deleteItemAtIndex: function(index) { |
| 113 }, |
| 114 }; |
| 115 |
| 116 return { |
| 117 DeletableItemList: DeletableItemList |
| 118 }; |
| 119 }); |
OLD | NEW |