• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2<!--
3@license
4Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8Code distributed by Google as part of the polymer project is also
9subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10-->
11
12<link rel="import" href="../polymer/polymer.html">
13
14<script>
15
16  /**
17   * @param {!Function} selectCallback
18   * @constructor
19   */
20  Polymer.IronSelection = function(selectCallback) {
21    this.selection = [];
22    this.selectCallback = selectCallback;
23  };
24
25  Polymer.IronSelection.prototype = {
26
27    /**
28     * Retrieves the selected item(s).
29     *
30     * @method get
31     * @returns Returns the selected item(s). If the multi property is true,
32     * `get` will return an array, otherwise it will return
33     * the selected item or undefined if there is no selection.
34     */
35    get: function() {
36      return this.multi ? this.selection.slice() : this.selection[0];
37    },
38
39    /**
40     * Clears all the selection except the ones indicated.
41     *
42     * @method clear
43     * @param {Array} excludes items to be excluded.
44     */
45    clear: function(excludes) {
46      this.selection.slice().forEach(function(item) {
47        if (!excludes || excludes.indexOf(item) < 0) {
48          this.setItemSelected(item, false);
49        }
50      }, this);
51    },
52
53    /**
54     * Indicates if a given item is selected.
55     *
56     * @method isSelected
57     * @param {*} item The item whose selection state should be checked.
58     * @returns Returns true if `item` is selected.
59     */
60    isSelected: function(item) {
61      return this.selection.indexOf(item) >= 0;
62    },
63
64    /**
65     * Sets the selection state for a given item to either selected or deselected.
66     *
67     * @method setItemSelected
68     * @param {*} item The item to select.
69     * @param {boolean} isSelected True for selected, false for deselected.
70     */
71    setItemSelected: function(item, isSelected) {
72      if (item != null) {
73        if (isSelected !== this.isSelected(item)) {
74          // proceed to update selection only if requested state differs from current
75          if (isSelected) {
76            this.selection.push(item);
77          } else {
78            var i = this.selection.indexOf(item);
79            if (i >= 0) {
80              this.selection.splice(i, 1);
81            }
82          }
83          if (this.selectCallback) {
84            this.selectCallback(item, isSelected);
85          }
86        }
87      }
88    },
89
90    /**
91     * Sets the selection state for a given item. If the `multi` property
92     * is true, then the selected state of `item` will be toggled; otherwise
93     * the `item` will be selected.
94     *
95     * @method select
96     * @param {*} item The item to select.
97     */
98    select: function(item) {
99      if (this.multi) {
100        this.toggle(item);
101      } else if (this.get() !== item) {
102        this.setItemSelected(this.get(), false);
103        this.setItemSelected(item, true);
104      }
105    },
106
107    /**
108     * Toggles the selection state for `item`.
109     *
110     * @method toggle
111     * @param {*} item The item to toggle.
112     */
113    toggle: function(item) {
114      this.setItemSelected(item, !this.isSelected(item));
115    }
116
117  };
118
119</script>
120