1// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * @fileoverview This is a multiple selection model for table 7 */ 8cr.define('cr.ui.table', function() { 9 const ListSelectionModel = cr.ui.ListSelectionModel; 10 11 /** 12 * Creates a new selection model that is to be used with tables. 13 * This implementation supports multiple selection. 14 * Selected items are stored, not indexes, so selections are preserved 15 * after items reordering (e.g. because of sort). 16 * @param {number=} opt_length The number of items in the selection. 17 * @constructor 18 * @extends {!cr.EventTarget} 19 */ 20 function TableSelectionModel(opt_length) { 21 ListSelectionModel.apply(this, arguments); 22 } 23 24 TableSelectionModel.prototype = { 25 __proto__: ListSelectionModel.prototype, 26 27 28 /** 29 * Adjusts the selection after reordering of items in the table. 30 * @param {!Array.<number>} permutation The reordering permutation. 31 */ 32 adjustToReordering: function(permutation) { 33 var oldLeadIndex = this.leadIndex; 34 35 var oldSelectedIndexes = this.selectedIndexes; 36 this.selectedIndexes = oldSelectedIndexes.map(function(oldIndex) { 37 return permutation[oldIndex]; 38 }).filter(function(index) { 39 return index != -1; 40 }); 41 42 if (oldLeadIndex != -1) 43 this.leadIndex = permutation[oldLeadIndex]; 44 }, 45 46 /** 47 * Adjust the selection by adding or removing a certain numbers of items. 48 * This should be called by the owner of the selection model as items are 49 * added and removed from the underlying data model. 50 * This implementation updates selection model length only. The actual 51 * selected indexes changes are processed in adjustToReordering. 52 * @param {number} index The index of the first change. 53 * @param {number} itemsRemoved Number of items removed. 54 * @param {number} itemsAdded Number of items added. 55 */ 56 adjust: function(index, itemsRemoved, itemsAdded) { 57 ListSelectionModel.prototype.adjust.call( 58 this, this.length, itemsRemoved, itemsAdded); 59 } 60 }; 61 62 return { 63 TableSelectionModel: TableSelectionModel 64 }; 65}); 66