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. | |
146 if ((e.keyCode == 46 || (e.keyCode == 8 && cr.isMac)) && | |
147 e.target.tagName != 'INPUT') { | |
James Hawkins
2011/01/18 19:32:23
Can you explain this check for 'INPUT'?
stuartmorgan
2011/01/18 19:34:51
Done.
| |
148 this.deleteSelectedItems_(); | |
149 // Prevent the browser from going back. | |
150 e.preventDefault(); | |
151 } | |
152 }, | |
153 | |
154 /** | |
155 * Deletes all the currently selected items that are deletable. | |
156 * @private | |
157 */ | |
158 deleteSelectedItems_: function() { | |
159 var selected = this.selectionModel.selectedIndexes; | |
160 // Reverse through the list of selected indexes to maintain the | |
161 // correct index values after deletion. | |
162 for (var j = selected.length - 1; j >= 0; j--) { | |
163 var index = selected[j]; | |
164 if (this.getListItemByIndex(index).deletable) | |
165 this.deleteItemAtIndex(index); | |
166 } | |
167 }, | |
168 | |
169 /** | |
142 * Called when an item should be deleted; subclasses are responsible for | 170 * Called when an item should be deleted; subclasses are responsible for |
143 * implementing. | 171 * implementing. |
144 * @param {number} index The index of the item that is being deleted. | 172 * @param {number} index The index of the item that is being deleted. |
145 */ | 173 */ |
146 deleteItemAtIndex: function(index) { | 174 deleteItemAtIndex: function(index) { |
147 }, | 175 }, |
148 }; | 176 }; |
149 | 177 |
150 return { | 178 return { |
151 DeletableItemList: DeletableItemList, | 179 DeletableItemList: DeletableItemList, |
152 DeletableItem: DeletableItem, | 180 DeletableItem: DeletableItem, |
153 }; | 181 }; |
154 }); | 182 }); |
OLD | NEW |