This repository was archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmulti-select-menu.html
229 lines (221 loc) · 8.62 KB
/
multi-select-menu.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../paper-menu-button/paper-menu-button.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-menu/paper-menu.html">
<link rel="import" href="../paper-item/paper-item.html">
<!--
Paper card with a collapse/expand button in the header.
Collapsing will hide the content but keep the header
-->
<dom-module id="multi-select-menu">
<template>
<style>
:host {
--paper-item: {
padding: 0 0.5em;
};
}
:host iron-icon {
color: var(--secondary-text-color);
}
:host paper-menu-button {
padding: 0;
width: 100%;
}
:host .list-item {
cursor: pointer;
}
:host .list-item iron-icon {
margin-right: 0.5em;
}
:host .list-item:not(.iron-selected) iron-icon {
visibility: hidden;
}
:host #trigger {
--paper-input-container-input: {
cursor: pointer;
};
}
:host #filter {
--paper-input-container-label: {
padding: 0 1em;
};
--paper-input-container-input: {
padding: 0 1em;
};
}
:host .check-icon {
min-width: 1.7em;
min-height: 1.7em;
}
</style>
<paper-menu-button ignore-select vertical-offset="50"
on-paper-dropdown-close="_clearFilter"
on-paper-dropdown-open="_focusFilter">
<div class="dropdown-trigger">
<!-- Mimic paper-dropdown-menu trigger -->
<paper-input id="trigger" type="text" readonly label="[[label]]"
value="[[selectedText]]" invalid="{{invalid}}">
<iron-icon icon="arrow-drop-down" suffix></iron-icon>
</paper-input>
</div>
<div class="dropdown-content">
<paper-input id="filter" label="Filter the [[itemType]]"
value="{{filter}}" tabindex="0"></paper-input>
<paper-menu id="items" multi
on-iron-select="_updateSelectedValues"
on-iron-deselect="_updateSelectedValues">
<template is="dom-repeat" items="{{filteredItems}}" as="item">
<paper-item class="list-item" data-index$="{{item.index}}">
<iron-icon class="check-icon" icon="check"></iron-icon>
<span>[[item.value]]</span>
</paper-item>
</template>
</paper-menu>
</div>
</paper-menu-button>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'multi-select-menu',
properties: {
// items to select
items: {
type: Array,
value: function() {
return [];
}
},
// store original item indices so filtered selection works
itemIndices: {
type: Array,
computed: '_computeItemIndices(items)'
},
// type of items, used when selecting more than 1 item,
// e.g. "3 items" or "3 carrots"
itemType: {
type: String,
value: 'items'
},
// list filter text
filter: {
type: String,
value: '',
notify: true
},
// items filtered by the filter property
filteredItems: {
type: Array,
computed: '_computeFilteredItems(filter, itemIndices)'
},
// set the invalid state
invalid: {
type: Boolean,
value: false
},
// input label
label: {
type: String,
value: 'multi-select'
},
// gets and sets the selected values
selected: {
type: Array,
value: function() {
return [];
},
notify: true
},
// selected text is value if 1 item selected, else is count
selectedText: {
type: String,
value: '',
readOnly: true
}
},
observers: [
'_updateSelection(selected.*,items.*)'
],
_updateSelection: function() {
// set selected items
if (!this._noUpdate) {
var currentlySelectedIndices = [].concat(this.$.items.selectedValues || []);
var selectedIndices = (this.selected || []).map(function(item) {
return this.items.indexOf(item);
}, this);
// deselect currently selected indices that are not selected
var selectedIndicesThatStaySelected =
currentlySelectedIndices.filter(function(i) {
if (selectedIndices.indexOf(i) < 0) {
this.$.items.selectIndex(i);
} else {
return true;
}
}, this);
// select selected indices that aren't currently selected
selectedIndices.forEach(function(i) {
if (selectedIndicesThatStaySelected.indexOf(i) < 0) {
this.$.items.selectIndex(i);
}
}, this);
}
},
_clearFilter: function() {
// use timeout so it's cleared after menu hide animation
window.setTimeout(function() {
this.filter = '';
}.bind(this), 250);
},
_computeFilteredItems: function(filter, itemIndices) {
if (filter) {
// non-adjacent regex
var regex = new RegExp('.*' + filter.split('').join('.*') + '.*');
return itemIndices.filter(function(item) {
return regex.test(item.value);
});
} else {
return itemIndices;
}
},
_computeItemIndices: function(items) {
return items.map(function(item, i) {
return { value: item, index: i };
});
},
_focusFilter: function() {
// use timeout so menu is open and filter input can be focused
window.setTimeout(function() {
this.$.filter.focus();
}.bind(this), 250);
},
_updateSelectedValues: function() {
// get menu item selection indices
var filteredIndices = this.$.items.selectedValues || [];
// menu items could be filtered, so map to real indices
var selectedIndices = filteredIndices.map(function(i) {
return this.$.items.items[i].dataset.index;
}, this);
// map item indices to values
var selectedValues = selectedIndices.map(function(index) {
return this.items[index];
}, this);
// set selected items without triggering the observer
this._noUpdate = true;
this.set('selected', selectedValues);
this._noUpdate = false;
// set selection label based on how many items are selected
if (this.selected.length === 0) {
this._setSelectedText('');
} else if (this.selected.length === 1) {
this._setSelectedText(this.selected[0]);
} else {
this._setSelectedText(this.selected.length + ' ' + this.itemType);
}
}
});
})();
</script>
</dom-module>