Chromium Code Reviews| 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 cr.define('options', function() { | 5 cr.define('options', function() { |
| 6 const List = cr.ui.List; | 6 const List = cr.ui.List; |
| 7 const ListItem = cr.ui.ListItem; | 7 const ListItem = cr.ui.ListItem; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Creates a deletable list item, which has a button that will trigger a call | 10 * Creates a deletable list item, which has a button that will trigger a call |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 ListItem.prototype.decorate.call(this); | 54 ListItem.prototype.decorate.call(this); |
| 55 | 55 |
| 56 this.classList.add('deletable-item'); | 56 this.classList.add('deletable-item'); |
| 57 | 57 |
| 58 this.contentElement_ = this.ownerDocument.createElement('div'); | 58 this.contentElement_ = this.ownerDocument.createElement('div'); |
| 59 this.appendChild(this.contentElement_); | 59 this.appendChild(this.contentElement_); |
| 60 | 60 |
| 61 this.closeButtonElement_ = this.ownerDocument.createElement('button'); | 61 this.closeButtonElement_ = this.ownerDocument.createElement('button'); |
| 62 this.closeButtonElement_.className = 'close-button'; | 62 this.closeButtonElement_.className = 'close-button'; |
| 63 this.closeButtonElement_.addEventListener('mousedown', | 63 this.closeButtonElement_.addEventListener('mousedown', |
| 64 this.handleMouseDownOnClose_); | 64 this.handleMouseDownUpOnClose_); |
| 65 this.closeButtonElement_.addEventListener('mouseup', | |
| 66 this.handleMouseDownUpOnClose_); | |
| 65 this.appendChild(this.closeButtonElement_); | 67 this.appendChild(this.closeButtonElement_); |
| 66 }, | 68 }, |
| 67 | 69 |
| 68 /** | 70 /** |
| 69 * Returns the element subclasses should add content to. | 71 * Returns the element subclasses should add content to. |
| 70 * @return {HTMLElement} The element subclasses should popuplate. | 72 * @return {HTMLElement} The element subclasses should popuplate. |
| 71 */ | 73 */ |
| 72 get contentElement() { | 74 get contentElement() { |
| 73 return this.contentElement_; | 75 return this.contentElement_; |
| 74 }, | 76 }, |
| 75 | 77 |
| 76 /* Gets/sets the deletable property. An item that is not deletable doesn't | 78 /* Gets/sets the deletable property. An item that is not deletable doesn't |
| 77 * show the delete button (although space is still reserved for it). | 79 * show the delete button (although space is still reserved for it). |
| 78 */ | 80 */ |
| 79 get deletable() { | 81 get deletable() { |
| 80 return this.deletable_; | 82 return this.deletable_; |
| 81 }, | 83 }, |
| 82 set deletable(value) { | 84 set deletable(value) { |
| 83 this.deletable_ = value; | 85 this.deletable_ = value; |
| 84 this.closeButtonElement_.disabled = !value; | 86 this.closeButtonElement_.disabled = !value; |
| 85 }, | 87 }, |
| 86 | 88 |
| 87 /** | 89 /** |
| 88 * Don't let the list have a crack at the event. We don't want clicking the | 90 * Don't let the list have a crack at the event. We don't want clicking the |
| 89 * close button to select the list. | 91 * close button to change the selection of the list. |
| 90 * @param {Event} e The mouse down event object. | 92 * @param {Event} e The mouse down/up event object. |
| 91 * @private | 93 * @private |
| 92 */ | 94 */ |
| 93 handleMouseDownOnClose_: function(e) { | 95 handleMouseDownUpOnClose_: function(e) { |
| 94 if (!e.target.disabled) | 96 if (!e.target.disabled) |
| 95 e.stopPropagation(); | 97 e.stopPropagation(); |
| 96 }, | 98 }, |
| 97 }; | 99 }; |
| 98 | 100 |
| 99 var DeletableItemList = cr.ui.define('list'); | 101 var DeletableItemList = cr.ui.define('list'); |
| 100 | 102 |
| 101 DeletableItemList.prototype = { | 103 DeletableItemList.prototype = { |
| 102 __proto__: List.prototype, | 104 __proto__: List.prototype, |
| 103 | 105 |
| 104 /** @inheritDoc */ | 106 /** @inheritDoc */ |
| 105 decorate: function() { | 107 decorate: function() { |
| 106 List.prototype.decorate.call(this); | 108 List.prototype.decorate.call(this); |
| 107 this.addEventListener('click', this.handleClick_); | 109 this.addEventListener('click', this.handleClick_); |
| 108 }, | 110 }, |
| 109 | 111 |
| 110 /** | 112 /** |
| 111 * Callback for onclick events. | 113 * Callback for onclick events. |
| 112 * @param {Event} e The click event object. | 114 * @param {Event} e The click event object. |
| 113 * @private | 115 * @private |
| 114 */ | 116 */ |
| 115 handleClick_: function(e) { | 117 handleClick_: function(e) { |
| 116 if (this.disabled) | 118 if (this.disabled) |
| 117 return; | 119 return; |
| 118 | 120 |
| 119 var target = e.target; | 121 var target = e.target; |
| 120 if (target.className == 'close-button') { | 122 if (target.className == 'close-button') { |
| 121 var listItem = this.getListItemAncestor(target); | 123 var listItem = this.getListItemAncestor(target); |
| 122 if (listItem) | 124 var selected = this.selectionModel.selectedIndexes; |
| 123 this.deleteItemAtIndex(this.getIndexOfListItem(listItem)); | 125 |
| 126 // Check if the list item that contains the close button being clicked | |
| 127 // is in the list of selected items. Only delete this item in that case. | |
|
stuartmorgan
2011/01/06 20:59:34
The comment (and the logic) are backward here. If
James Hawkins
2011/01/06 21:01:54
Done.
James Hawkins
2011/01/06 21:03:40
Actually fixed now.
| |
| 128 var idx = this.getIndexOfListItem(listItem); | |
| 129 if (selected.indexOf(idx)) { | |
| 130 this.deleteItemAtIndex(idx); | |
| 131 } else { | |
| 132 // Reverse through the list of selected indexes to maintain the | |
| 133 // correct index values after deletion. | |
| 134 for (var j = selected.length - 1; j >= 0; j--) | |
| 135 this.deleteItemAtIndex(selected[j]); | |
| 136 } | |
| 124 } | 137 } |
| 125 }, | 138 }, |
| 126 | 139 |
| 127 /** | 140 /** |
| 128 * Called when an item should be deleted; subclasses are responsible for | 141 * Called when an item should be deleted; subclasses are responsible for |
| 129 * implementing. | 142 * implementing. |
| 130 * @param {number} index The index of the item that is being deleted. | 143 * @param {number} index The index of the item that is being deleted. |
| 131 */ | 144 */ |
| 132 deleteItemAtIndex: function(index) { | 145 deleteItemAtIndex: function(index) { |
| 133 }, | 146 }, |
| 134 }; | 147 }; |
| 135 | 148 |
| 136 return { | 149 return { |
| 137 DeletableItemList: DeletableItemList, | 150 DeletableItemList: DeletableItemList, |
| 138 DeletableItem: DeletableItem, | 151 DeletableItem: DeletableItem, |
| 139 }; | 152 }; |
| 140 }); | 153 }); |
| OLD | NEW |