forked from vaadin/vaadin-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaadin-grid-selection-mixin.html
98 lines (88 loc) · 2.34 KB
/
vaadin-grid-selection-mixin.html
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
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<script>
window.Vaadin = window.Vaadin || {};
Vaadin.Grid = Vaadin.Grid || {};
/**
* @polymerMixin
*/
Vaadin.Grid.SelectionMixin = superClass => class SelectionMixin extends superClass {
static get properties() {
return {
/**
* An array that contains the selected items.
*/
selectedItems: {
type: Object,
notify: true,
value: () => []
}
};
}
static get observers() {
return [
'_selectedItemsChanged(selectedItems.*)'
];
}
_isSelected(item) {
return this.selectedItems && this._getItemIndexInArray(item, this.selectedItems) > -1;
}
/**
* Selects the given item.
*
* @method selectItem
* @param {Object} item The item object
*/
selectItem(item) {
if (!this._isSelected(item)) {
this.push('selectedItems', item);
}
}
/**
* Deselects the given item if it is already selected.
*
* @method deselect
* @param {Object} item The item object
*/
deselectItem(item) {
const index = this._getItemIndexInArray(item, this.selectedItems);
if (index > -1) {
this.splice('selectedItems', index, 1);
}
}
/**
* Toggles the selected state of the given item.
*
* @method toggle
* @param {Object} item The item object
*/
_toggleItem(item) {
const index = this._getItemIndexInArray(item, this.selectedItems);
if (index === -1) {
this.selectItem(item);
} else {
this.deselectItem(item);
}
}
_selectedItemsChanged(e) {
if (this.$.items.children.length && (e.path === 'selectedItems' || e.path === 'selectedItems.splices')) {
Array.from(this.$.items.children).forEach(row => {
this._toggleRowSelected(row, this._isSelected(row._item));
});
}
}
_selectedInstanceChangedCallback(instance, value) {
if (super._selectedInstanceChangedCallback) {
super._selectedInstanceChangedCallback(instance, value);
}
if (value) {
this.selectItem(instance.item);
} else {
this.deselectItem(instance.item);
}
}
};
</script>