• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 single selection model for table
7 */
8cr.define('cr.ui.table', function() {
9  const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel;
10
11  /**
12   * Creates a new selection model that is to be used with tables.
13   * This implementation supports single selection.
14   * Selected item is stored, not index, so selection is 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 TableSingleSelectionModel(opt_length) {
21    ListSingleSelectionModel.apply(this, arguments);
22  }
23
24  TableSingleSelectionModel.prototype = {
25    __proto__: ListSingleSelectionModel.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      if (this.leadIndex != -1)
34        this.leadIndex = permutation[this.leadIndex];
35
36      var oldSelectedIndex = this.selectedIndex;
37      if (oldSelectedIndex != -1) {
38        this.selectedIndex = permutation[oldSelectedIndex];
39      }
40    },
41
42    /**
43     * Adjust the selection by adding or removing a certain numbers of items.
44     * This should be called by the owner of the selection model as items are
45     * added and removed from the underlying data model.
46     * @param {number} index The index of the first change.
47     * @param {number} itemsRemoved Number of items removed.
48     * @param {number} itemsAdded Number of items added.
49     */
50    adjust: function(index, itemsRemoved, itemsAdded) {
51      ListSingleSelectionModel.prototype.adjust.call(
52          this, this.length, itemsRemoved, itemsAdded);
53    }
54  };
55
56  return {
57    TableSingleSelectionModel: TableSingleSelectionModel
58  };
59});
60