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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 | 100 |
| 101 var DeletableItemList = cr.ui.define('list'); | 101 var DeletableItemList = cr.ui.define('list'); |
| 102 | 102 |
| 103 DeletableItemList.prototype = { | 103 DeletableItemList.prototype = { |
| 104 __proto__: List.prototype, | 104 __proto__: List.prototype, |
| 105 | 105 |
| 106 /** @inheritDoc */ | 106 /** @inheritDoc */ |
| 107 decorate: function() { | 107 decorate: function() { |
| 108 List.prototype.decorate.call(this); | 108 List.prototype.decorate.call(this); |
| 109 this.addEventListener('click', this.handleClick_); | 109 this.addEventListener('click', this.handleClick_); |
| 110 this.addEventListener('keydown', this.handleKeyDown_); | |
| 110 }, | 111 }, |
| 111 | 112 |
| 112 /** | 113 /** |
| 113 * Callback for onclick events. | 114 * Callback for onclick events. |
| 114 * @param {Event} e The click event object. | 115 * @param {Event} e The click event object. |
| 115 * @private | 116 * @private |
| 116 */ | 117 */ |
| 117 handleClick_: function(e) { | 118 handleClick_: function(e) { |
| 118 if (this.disabled) | 119 if (this.disabled) |
| 119 return; | 120 return; |
| 120 | 121 |
| 121 var target = e.target; | 122 var target = e.target; |
| 122 if (target.className == 'close-button') { | 123 if (target.className == 'close-button') { |
| 123 var listItem = this.getListItemAncestor(target); | 124 var listItem = this.getListItemAncestor(target); |
| 124 var selected = this.selectionModel.selectedIndexes; | 125 var selected = this.selectionModel.selectedIndexes; |
| 125 | 126 |
| 126 // Check if the list item that contains the close button being clicked | 127 // Check if the list item that contains the close button being clicked |
| 127 // is not in the list of selected items. Only delete this item in that | 128 // is not in the list of selected items. Only delete this item in that |
| 128 // case. | 129 // case. |
| 129 var idx = this.getIndexOfListItem(listItem); | 130 var idx = this.getIndexOfListItem(listItem); |
| 130 if (selected.indexOf(idx) == -1) { | 131 if (selected.indexOf(idx) == -1) { |
| 131 this.deleteItemAtIndex(idx); | 132 this.deleteItemAtIndex(idx); |
| 132 } else { | 133 } else { |
| 133 // Reverse through the list of selected indexes to maintain the | 134 this.deleteSelectedItems_(); |
| 134 // correct index values after deletion. | |
| 135 for (var j = selected.length - 1; j >= 0; j--) | |
| 136 this.deleteItemAtIndex(selected[j]); | |
| 137 } | 135 } |
| 138 } | 136 } |
| 139 }, | 137 }, |
| 140 | 138 |
| 141 /** | 139 /** |
| 140 * Callback for keydown events. | |
| 141 * @param {Event} e The keydown event object. | |
| 142 * @private | |
| 143 */ | |
| 144 handleKeyDown_: function(e) { | |
| 145 // Map delete (and backspapce on Mac) to item deletion (unless focus is | |
|
arv (Not doing code reviews)
2011/01/18 19:56:24
typo
stuartmorgan
2011/01/18 21:55:54
Oops, I'll fix that next time I'm in the file.
| |
| 146 // in an input field, where it's intended for text editing). | |
| 147 if ((e.keyCode == 46 || (e.keyCode == 8 && cr.isMac)) && | |
|
arv (Not doing code reviews)
2011/01/18 19:56:24
I thought Mac used Command-Backspace (Meta-Backspa
stuartmorgan
2011/01/18 21:55:54
For menu items, yes. For interacting with tables a
| |
| 148 e.target.tagName != 'INPUT') { | |
| 149 this.deleteSelectedItems_(); | |
| 150 // Prevent the browser from going back. | |
| 151 e.preventDefault(); | |
| 152 } | |
| 153 }, | |
| 154 | |
| 155 /** | |
| 156 * Deletes all the currently selected items that are deletable. | |
| 157 * @private | |
| 158 */ | |
| 159 deleteSelectedItems_: function() { | |
| 160 var selected = this.selectionModel.selectedIndexes; | |
| 161 // Reverse through the list of selected indexes to maintain the | |
| 162 // correct index values after deletion. | |
| 163 for (var j = selected.length - 1; j >= 0; j--) { | |
| 164 var index = selected[j]; | |
| 165 if (this.getListItemByIndex(index).deletable) | |
| 166 this.deleteItemAtIndex(index); | |
| 167 } | |
| 168 }, | |
| 169 | |
| 170 /** | |
| 142 * Called when an item should be deleted; subclasses are responsible for | 171 * Called when an item should be deleted; subclasses are responsible for |
| 143 * implementing. | 172 * implementing. |
| 144 * @param {number} index The index of the item that is being deleted. | 173 * @param {number} index The index of the item that is being deleted. |
| 145 */ | 174 */ |
| 146 deleteItemAtIndex: function(index) { | 175 deleteItemAtIndex: function(index) { |
| 147 }, | 176 }, |
| 148 }; | 177 }; |
| 149 | 178 |
| 150 return { | 179 return { |
| 151 DeletableItemList: DeletableItemList, | 180 DeletableItemList: DeletableItemList, |
| 152 DeletableItem: DeletableItem, | 181 DeletableItem: DeletableItem, |
| 153 }; | 182 }; |
| 154 }); | 183 }); |
| OLD | NEW |