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. | 9 * Creates a new selection model that is to be used with lists. |
10 * | 10 * |
(...skipping 12 matching lines...) Expand all Loading... |
23 this.independentLeadItem_ = !cr.isMac && !cr.isChromeOS; | 23 this.independentLeadItem_ = !cr.isMac && !cr.isChromeOS; |
24 } | 24 } |
25 | 25 |
26 ListSelectionModel.prototype = { | 26 ListSelectionModel.prototype = { |
27 __proto__: EventTarget.prototype, | 27 __proto__: EventTarget.prototype, |
28 | 28 |
29 /** | 29 /** |
30 * The number of items in the model. | 30 * The number of items in the model. |
31 * @type {number} | 31 * @type {number} |
32 */ | 32 */ |
33 get length() { | 33 get length() { return this.length_; }, |
34 return this.length_; | |
35 }, | |
36 | 34 |
37 /** | 35 /** |
38 * The selected indexes. | 36 * The selected indexes. |
39 * Setter also changes lead and anchor indexes if value list is nonempty. | 37 * Setter also changes lead and anchor indexes if value list is nonempty. |
40 * @type {!Array} | 38 * @type {!Array} |
41 */ | 39 */ |
42 get selectedIndexes() { | 40 get selectedIndexes() { |
43 return Object.keys(this.selectedIndexes_).map(Number); | 41 return Object.keys(this.selectedIndexes_).map(Number); |
44 }, | 42 }, |
45 set selectedIndexes(selectedIndexes) { | 43 set selectedIndexes(selectedIndexes) { |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 * Call this after changes are done and it will dispatch a change event if | 220 * Call this after changes are done and it will dispatch a change event if |
223 * any changes were actually done. | 221 * any changes were actually done. |
224 */ | 222 */ |
225 endChange: function() { | 223 endChange: function() { |
226 this.changeCount_--; | 224 this.changeCount_--; |
227 if (!this.changeCount_) { | 225 if (!this.changeCount_) { |
228 // Calls delayed |dispatchPropertyChange|s, only when |leadIndex| or | 226 // Calls delayed |dispatchPropertyChange|s, only when |leadIndex| or |
229 // |anchorIndex| has been actually changed in the batch. | 227 // |anchorIndex| has been actually changed in the batch. |
230 this.leadIndex_ = this.adjustIndex_(this.leadIndex_); | 228 this.leadIndex_ = this.adjustIndex_(this.leadIndex_); |
231 if (this.leadIndex_ != this.oldLeadIndex_) { | 229 if (this.leadIndex_ != this.oldLeadIndex_) { |
232 cr.dispatchPropertyChange(this, 'leadIndex', | 230 cr.dispatchPropertyChange( |
233 this.leadIndex_, this.oldLeadIndex_); | 231 this, 'leadIndex', this.leadIndex_, this.oldLeadIndex_); |
234 } | 232 } |
235 this.oldLeadIndex_ = null; | 233 this.oldLeadIndex_ = null; |
236 | 234 |
237 this.anchorIndex_ = this.adjustIndex_(this.anchorIndex_); | 235 this.anchorIndex_ = this.adjustIndex_(this.anchorIndex_); |
238 if (this.anchorIndex_ != this.oldAnchorIndex_) { | 236 if (this.anchorIndex_ != this.oldAnchorIndex_) { |
239 cr.dispatchPropertyChange(this, 'anchorIndex', | 237 cr.dispatchPropertyChange( |
240 this.anchorIndex_, this.oldAnchorIndex_); | 238 this, 'anchorIndex', this.anchorIndex_, this.oldAnchorIndex_); |
241 } | 239 } |
242 this.oldAnchorIndex_ = null; | 240 this.oldAnchorIndex_ = null; |
243 | 241 |
244 var indexes = Object.keys(this.changedIndexes_); | 242 var indexes = Object.keys(this.changedIndexes_); |
245 if (indexes.length) { | 243 if (indexes.length) { |
246 var e = new Event('change'); | 244 var e = new Event('change'); |
247 e.changes = indexes.map(function(index) { | 245 e.changes = indexes.map(function(index) { |
248 return { | 246 return { |
249 index: Number(index), | 247 index: Number(index), |
250 selected: this.changedIndexes_[index] | 248 selected: this.changedIndexes_[index] |
251 }; | 249 }; |
252 }, this); | 250 }, this); |
253 this.dispatchEvent(e); | 251 this.dispatchEvent(e); |
254 } | 252 } |
255 this.changedIndexes_ = {}; | 253 this.changedIndexes_ = {}; |
256 } | 254 } |
257 }, | 255 }, |
258 | 256 |
259 leadIndex_: -1, | 257 leadIndex_: -1, |
260 oldLeadIndex_: null, | 258 oldLeadIndex_: null, |
261 | 259 |
262 /** | 260 /** |
263 * The leadIndex is used with multiple selection and it is the index that | 261 * The leadIndex is used with multiple selection and it is the index that |
264 * the user is moving using the arrow keys. | 262 * the user is moving using the arrow keys. |
265 * @type {number} | 263 * @type {number} |
266 */ | 264 */ |
267 get leadIndex() { | 265 get leadIndex() { return this.leadIndex_; }, |
268 return this.leadIndex_; | |
269 }, | |
270 set leadIndex(leadIndex) { | 266 set leadIndex(leadIndex) { |
271 var oldValue = this.leadIndex_; | 267 var oldValue = this.leadIndex_; |
272 var newValue = this.adjustIndex_(leadIndex); | 268 var newValue = this.adjustIndex_(leadIndex); |
273 this.leadIndex_ = newValue; | 269 this.leadIndex_ = newValue; |
274 // Delays the call of dispatchPropertyChange if batch is running. | 270 // Delays the call of dispatchPropertyChange if batch is running. |
275 if (!this.changeCount_ && newValue != oldValue) | 271 if (!this.changeCount_ && newValue != oldValue) |
276 cr.dispatchPropertyChange(this, 'leadIndex', newValue, oldValue); | 272 cr.dispatchPropertyChange(this, 'leadIndex', newValue, oldValue); |
277 }, | 273 }, |
278 | 274 |
279 anchorIndex_: -1, | 275 anchorIndex_: -1, |
280 oldAnchorIndex_: null, | 276 oldAnchorIndex_: null, |
281 | 277 |
282 /** | 278 /** |
283 * The anchorIndex is used with multiple selection. | 279 * The anchorIndex is used with multiple selection. |
284 * @type {number} | 280 * @type {number} |
285 */ | 281 */ |
286 get anchorIndex() { | 282 get anchorIndex() { return this.anchorIndex_; }, |
287 return this.anchorIndex_; | |
288 }, | |
289 set anchorIndex(anchorIndex) { | 283 set anchorIndex(anchorIndex) { |
290 var oldValue = this.anchorIndex_; | 284 var oldValue = this.anchorIndex_; |
291 var newValue = this.adjustIndex_(anchorIndex); | 285 var newValue = this.adjustIndex_(anchorIndex); |
292 this.anchorIndex_ = newValue; | 286 this.anchorIndex_ = newValue; |
293 // Delays the call of dispatchPropertyChange if batch is running. | 287 // Delays the call of dispatchPropertyChange if batch is running. |
294 if (!this.changeCount_ && newValue != oldValue) | 288 if (!this.changeCount_ && newValue != oldValue) |
295 cr.dispatchPropertyChange(this, 'anchorIndex', newValue, oldValue); | 289 cr.dispatchPropertyChange(this, 'anchorIndex', newValue, oldValue); |
296 }, | 290 }, |
297 | 291 |
298 /** | 292 /** |
(...skipping 11 matching lines...) Expand all Loading... |
310 var index2 = this.getNearestSelectedIndex_(index); | 304 var index2 = this.getNearestSelectedIndex_(index); |
311 index = index2; | 305 index = index2; |
312 } | 306 } |
313 return index; | 307 return index; |
314 }, | 308 }, |
315 | 309 |
316 /** | 310 /** |
317 * Whether the selection model supports multiple selected items. | 311 * Whether the selection model supports multiple selected items. |
318 * @type {boolean} | 312 * @type {boolean} |
319 */ | 313 */ |
320 get multiple() { | 314 get multiple() { return true; }, |
321 return true; | |
322 }, | |
323 | 315 |
324 /** | 316 /** |
325 * Adjusts the selection after reordering of items in the table. | 317 * Adjusts the selection after reordering of items in the table. |
326 * @param {!Array<number>} permutation The reordering permutation. | 318 * @param {!Array<number>} permutation The reordering permutation. |
327 */ | 319 */ |
328 adjustToReordering: function(permutation) { | 320 adjustToReordering: function(permutation) { |
329 this.beginChange(); | 321 this.beginChange(); |
330 var oldLeadIndex = this.leadIndex; | 322 var oldLeadIndex = this.leadIndex; |
331 var oldAnchorIndex = this.anchorIndex; | 323 var oldAnchorIndex = this.anchorIndex; |
332 var oldSelectedItemsCount = this.selectedIndexes.length; | 324 var oldSelectedItemsCount = this.selectedIndexes.length; |
333 | 325 |
334 this.selectedIndexes = this.selectedIndexes.map(function(oldIndex) { | 326 this.selectedIndexes = |
335 return permutation[oldIndex]; | 327 this.selectedIndexes |
336 }).filter(function(index) { | 328 .map(function(oldIndex) { return permutation[oldIndex]; }) |
337 return index != -1; | 329 .filter(function(index) { return index != -1; }); |
338 }); | |
339 | 330 |
340 // Will be adjusted in endChange. | 331 // Will be adjusted in endChange. |
341 if (oldLeadIndex != -1) | 332 if (oldLeadIndex != -1) |
342 this.leadIndex = permutation[oldLeadIndex]; | 333 this.leadIndex = permutation[oldLeadIndex]; |
343 if (oldAnchorIndex != -1) | 334 if (oldAnchorIndex != -1) |
344 this.anchorIndex = permutation[oldAnchorIndex]; | 335 this.anchorIndex = permutation[oldAnchorIndex]; |
345 | 336 |
346 if (oldSelectedItemsCount && !this.selectedIndexes.length && | 337 if (oldSelectedItemsCount && !this.selectedIndexes.length && |
347 this.length_ && oldLeadIndex != -1) { | 338 this.length_ && oldLeadIndex != -1) { |
348 // All selected items are deleted. We move selection to next item of | 339 // All selected items are deleted. We move selection to next item of |
349 // last selected item. | 340 // last selected item. |
350 this.selectedIndexes = [Math.min(oldLeadIndex, this.length_ - 1)]; | 341 this.selectedIndexes = [Math.min(oldLeadIndex, this.length_ - 1)]; |
351 } | 342 } |
352 | 343 |
353 this.endChange(); | 344 this.endChange(); |
354 }, | 345 }, |
355 | 346 |
356 /** | 347 /** |
357 * Adjusts selection model length. | 348 * Adjusts selection model length. |
358 * @param {number} length New selection model length. | 349 * @param {number} length New selection model length. |
359 */ | 350 */ |
360 adjustLength: function(length) { | 351 adjustLength: function(length) { this.length_ = length; } |
361 this.length_ = length; | |
362 } | |
363 }; | 352 }; |
364 | 353 |
365 return { | 354 return {ListSelectionModel: ListSelectionModel}; |
366 ListSelectionModel: ListSelectionModel | |
367 }; | |
368 }); | 355 }); |
OLD | NEW |