OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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('cr.ui', function() { | 5 cr.define('cr.ui', function() { |
6 /** @const */ var EventTarget = cr.EventTarget; | 6 /** @const */ var EventTarget = cr.EventTarget; |
7 | 7 |
8 /** | 8 /** |
9 * Creates a new selection model that is to be used with lists. This only | 9 * Creates a new selection model that is to be used with lists. This only |
10 * allows a single index to be selected. | 10 * allows a single index to be selected. |
(...skipping 11 matching lines...) Expand all Loading... |
22 this.independentLeadItem_ = !cr.isMac && !cr.isChromeOS; | 22 this.independentLeadItem_ = !cr.isMac && !cr.isChromeOS; |
23 } | 23 } |
24 | 24 |
25 ListSingleSelectionModel.prototype = { | 25 ListSingleSelectionModel.prototype = { |
26 __proto__: EventTarget.prototype, | 26 __proto__: EventTarget.prototype, |
27 | 27 |
28 /** | 28 /** |
29 * The number of items in the model. | 29 * The number of items in the model. |
30 * @type {number} | 30 * @type {number} |
31 */ | 31 */ |
32 get length() { | 32 get length() { return this.length_; }, |
33 return this.length_; | |
34 }, | |
35 | 33 |
36 /** | 34 /** |
37 * @type {!Array} The selected indexes. | 35 * @type {!Array} The selected indexes. |
38 */ | 36 */ |
39 get selectedIndexes() { | 37 get selectedIndexes() { |
40 var i = this.selectedIndex; | 38 var i = this.selectedIndex; |
41 return i != -1 ? [this.selectedIndex] : []; | 39 return i != -1 ? [this.selectedIndex] : []; |
42 }, | 40 }, |
43 set selectedIndexes(indexes) { | 41 set selectedIndexes(indexes) { |
44 this.selectedIndex = indexes.length ? indexes[0] : -1; | 42 this.selectedIndex = indexes.length ? indexes[0] : -1; |
45 }, | 43 }, |
46 | 44 |
47 /** | 45 /** |
48 * Convenience getter which returns the first selected index. | 46 * Convenience getter which returns the first selected index. |
49 * Setter also changes lead and anchor indexes if value is nonegative. | 47 * Setter also changes lead and anchor indexes if value is nonegative. |
50 * @type {number} | 48 * @type {number} |
51 */ | 49 */ |
52 get selectedIndex() { | 50 get selectedIndex() { return this.selectedIndex_; }, |
53 return this.selectedIndex_; | |
54 }, | |
55 set selectedIndex(selectedIndex) { | 51 set selectedIndex(selectedIndex) { |
56 var oldSelectedIndex = this.selectedIndex; | 52 var oldSelectedIndex = this.selectedIndex; |
57 var i = Math.max(-1, Math.min(this.length_ - 1, selectedIndex)); | 53 var i = Math.max(-1, Math.min(this.length_ - 1, selectedIndex)); |
58 | 54 |
59 if (i != oldSelectedIndex) { | 55 if (i != oldSelectedIndex) { |
60 this.beginChange(); | 56 this.beginChange(); |
61 this.selectedIndex_ = i; | 57 this.selectedIndex_ = i; |
62 this.leadIndex = i >= 0 ? i : this.leadIndex; | 58 this.leadIndex = i >= 0 ? i : this.leadIndex; |
63 this.endChange(); | 59 this.endChange(); |
64 } | 60 } |
(...skipping 23 matching lines...) Expand all Loading... |
88 clear: function() { | 84 clear: function() { |
89 this.beginChange(); | 85 this.beginChange(); |
90 this.length_ = 0; | 86 this.length_ = 0; |
91 this.selectedIndex = this.anchorIndex = this.leadIndex = -1; | 87 this.selectedIndex = this.anchorIndex = this.leadIndex = -1; |
92 this.endChange(); | 88 this.endChange(); |
93 }, | 89 }, |
94 | 90 |
95 /** | 91 /** |
96 * Unselects all selected items. | 92 * Unselects all selected items. |
97 */ | 93 */ |
98 unselectAll: function() { | 94 unselectAll: function() { this.selectedIndex = -1; }, |
99 this.selectedIndex = -1; | |
100 }, | |
101 | 95 |
102 /** | 96 /** |
103 * Sets the selected state for an index. | 97 * Sets the selected state for an index. |
104 * @param {number} index The index to set the selected state for. | 98 * @param {number} index The index to set the selected state for. |
105 * @param {boolean} b Whether to select the index or not. | 99 * @param {boolean} b Whether to select the index or not. |
106 */ | 100 */ |
107 setIndexSelected: function(index, b) { | 101 setIndexSelected: function(index, b) { |
108 // Only allow selection | 102 // Only allow selection |
109 var oldSelected = index == this.selectedIndex_; | 103 var oldSelected = index == this.selectedIndex_; |
110 if (oldSelected == b) | 104 if (oldSelected == b) |
111 return; | 105 return; |
112 | 106 |
113 if (b) | 107 if (b) |
114 this.selectedIndex = index; | 108 this.selectedIndex = index; |
115 else if (index == this.selectedIndex_) | 109 else if (index == this.selectedIndex_) |
116 this.selectedIndex = -1; | 110 this.selectedIndex = -1; |
117 }, | 111 }, |
118 | 112 |
119 /** | 113 /** |
120 * Whether a given index is selected or not. | 114 * Whether a given index is selected or not. |
121 * @param {number} index The index to check. | 115 * @param {number} index The index to check. |
122 * @return {boolean} Whether an index is selected. | 116 * @return {boolean} Whether an index is selected. |
123 */ | 117 */ |
124 getIndexSelected: function(index) { | 118 getIndexSelected: function(index) { return index == this.selectedIndex_; }, |
125 return index == this.selectedIndex_; | |
126 }, | |
127 | 119 |
128 /** | 120 /** |
129 * This is used to begin batching changes. Call {@code endChange} when you | 121 * This is used to begin batching changes. Call {@code endChange} when you |
130 * are done making changes. | 122 * are done making changes. |
131 */ | 123 */ |
132 beginChange: function() { | 124 beginChange: function() { |
133 if (!this.changeCount_) { | 125 if (!this.changeCount_) { |
134 this.changeCount_ = 0; | 126 this.changeCount_ = 0; |
135 this.selectedIndexBefore_ = this.selectedIndex_; | 127 this.selectedIndexBefore_ = this.selectedIndex_; |
136 } | 128 } |
(...skipping 17 matching lines...) Expand all Loading... |
154 } | 146 } |
155 }, | 147 }, |
156 | 148 |
157 /** | 149 /** |
158 * Creates event with specified name and fills its {changes} property. | 150 * Creates event with specified name and fills its {changes} property. |
159 * @param {string} eventName Event name. | 151 * @param {string} eventName Event name. |
160 */ | 152 */ |
161 createChangeEvent: function(eventName) { | 153 createChangeEvent: function(eventName) { |
162 var e = new Event(eventName); | 154 var e = new Event(eventName); |
163 var indexes = [this.selectedIndexBefore_, this.selectedIndex_]; | 155 var indexes = [this.selectedIndexBefore_, this.selectedIndex_]; |
164 e.changes = indexes.filter(function(index) { | 156 e.changes = |
165 return index != -1; | 157 indexes.filter(function(index) { return index != -1; }) |
166 }).map(function(index) { | 158 .map(function(index) { |
167 return { | 159 return {index: index, selected: index == this.selectedIndex_}; |
168 index: index, | 160 }, this); |
169 selected: index == this.selectedIndex_ | |
170 }; | |
171 }, this); | |
172 | 161 |
173 return e; | 162 return e; |
174 }, | 163 }, |
175 | 164 |
176 leadIndex_: -1, | 165 leadIndex_: -1, |
177 | 166 |
178 /** | 167 /** |
179 * The leadIndex is used with multiple selection and it is the index that | 168 * The leadIndex is used with multiple selection and it is the index that |
180 * the user is moving using the arrow keys. | 169 * the user is moving using the arrow keys. |
181 * @type {number} | 170 * @type {number} |
182 */ | 171 */ |
183 get leadIndex() { | 172 get leadIndex() { return this.leadIndex_; }, |
184 return this.leadIndex_; | |
185 }, | |
186 set leadIndex(leadIndex) { | 173 set leadIndex(leadIndex) { |
187 var li = this.adjustIndex_(leadIndex); | 174 var li = this.adjustIndex_(leadIndex); |
188 if (li != this.leadIndex_) { | 175 if (li != this.leadIndex_) { |
189 var oldLeadIndex = this.leadIndex_; | 176 var oldLeadIndex = this.leadIndex_; |
190 this.leadIndex_ = li; | 177 this.leadIndex_ = li; |
191 cr.dispatchPropertyChange(this, 'leadIndex', li, oldLeadIndex); | 178 cr.dispatchPropertyChange(this, 'leadIndex', li, oldLeadIndex); |
192 cr.dispatchPropertyChange(this, 'anchorIndex', li, oldLeadIndex); | 179 cr.dispatchPropertyChange(this, 'anchorIndex', li, oldLeadIndex); |
193 } | 180 } |
194 }, | 181 }, |
195 | 182 |
196 adjustIndex_: function(index) { | 183 adjustIndex_: function(index) { |
197 index = Math.max(-1, Math.min(this.length_ - 1, index)); | 184 index = Math.max(-1, Math.min(this.length_ - 1, index)); |
198 if (!this.independentLeadItem_) | 185 if (!this.independentLeadItem_) |
199 index = this.selectedIndex; | 186 index = this.selectedIndex; |
200 return index; | 187 return index; |
201 }, | 188 }, |
202 | 189 |
203 /** | 190 /** |
204 * The anchorIndex is used with multiple selection. | 191 * The anchorIndex is used with multiple selection. |
205 * @type {number} | 192 * @type {number} |
206 */ | 193 */ |
207 get anchorIndex() { | 194 get anchorIndex() { return this.leadIndex; }, |
208 return this.leadIndex; | 195 set anchorIndex(anchorIndex) { this.leadIndex = anchorIndex; }, |
209 }, | |
210 set anchorIndex(anchorIndex) { | |
211 this.leadIndex = anchorIndex; | |
212 }, | |
213 | 196 |
214 /** | 197 /** |
215 * Whether the selection model supports multiple selected items. | 198 * Whether the selection model supports multiple selected items. |
216 * @type {boolean} | 199 * @type {boolean} |
217 */ | 200 */ |
218 get multiple() { | 201 get multiple() { return false; }, |
219 return false; | |
220 }, | |
221 | 202 |
222 /** | 203 /** |
223 * Adjusts the selection after reordering of items in the table. | 204 * Adjusts the selection after reordering of items in the table. |
224 * @param {!Array<number>} permutation The reordering permutation. | 205 * @param {!Array<number>} permutation The reordering permutation. |
225 */ | 206 */ |
226 adjustToReordering: function(permutation) { | 207 adjustToReordering: function(permutation) { |
227 if (this.leadIndex != -1) | 208 if (this.leadIndex != -1) |
228 this.leadIndex = permutation[this.leadIndex]; | 209 this.leadIndex = permutation[this.leadIndex]; |
229 | 210 |
230 var oldSelectedIndex = this.selectedIndex; | 211 var oldSelectedIndex = this.selectedIndex; |
231 if (oldSelectedIndex != -1) { | 212 if (oldSelectedIndex != -1) { |
232 this.selectedIndex = permutation[oldSelectedIndex]; | 213 this.selectedIndex = permutation[oldSelectedIndex]; |
233 } | 214 } |
234 }, | 215 }, |
235 | 216 |
236 /** | 217 /** |
237 * Adjusts selection model length. | 218 * Adjusts selection model length. |
238 * @param {number} length New selection model length. | 219 * @param {number} length New selection model length. |
239 */ | 220 */ |
240 adjustLength: function(length) { | 221 adjustLength: function(length) { this.length_ = length; } |
241 this.length_ = length; | |
242 } | |
243 }; | 222 }; |
244 | 223 |
245 return { | 224 return {ListSingleSelectionModel: ListSingleSelectionModel}; |
246 ListSingleSelectionModel: ListSingleSelectionModel | |
247 }; | |
248 }); | 225 }); |
OLD | NEW |