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