forked from Collaborne/paper-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaper-search-panel.html
301 lines (263 loc) · 6.67 KB
/
paper-search-panel.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!doctype html>
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../paper-more-button/paper-more-button.html">
<link rel="import" href="../paper-styles/color.html">
<link rel="import" href="paper-search-bar.html">
<link rel="import" href="paper-filter-dialog.html">
<!--
Panel with a search bar and filtering dialog.
### Example
```html
<paper-search-panel
search="{{search}}"
count="{{count}}"
items="[[items]]"
has-more="[[hasMore]]"
loading="[[loading]]"
filters="[[filters]]"
selected-filters="{{selectedFilters}}"
on-change-request-params="loadData">
<div>
Show your [[count]] results for "[[search]]"
</div>
</paper-search-panel>
```
The panel shows the search configuration, and considers the `items` property to know whether
the lister is currently showing any results.
The content is hidden if no results are available. Content with attribute `fixed`
is shown even without no results (check the demo for an example).
@demo demo/paper-search-panel.html
-->
<dom-module id="paper-search-panel">
<template>
<style>
:host,.items {
@apply --layout-vertical;
}
.search {
border-bottom: 1px solid #eee;
}
.noResults {
padding: 20px;
}
.more {
padding: 10px 0;
text-align: center;
}
.filter-dialog {
--paper-filter-dialog-background-color: #eee;
--paper-filter-toolbar-background: var(--paper-grey-200);
--paper-filter-dialog: {
margin: 0!important;
};
--paper-filter-toolbar: {
color: var(--paper-grey-900);
}
}
</style>
<paper-search-bar
id="paperSearchBar"
class="search"
icon="[[icon]]"
placeholder="[[placeholder]]"
hide-filter-button="[[hideFilterButton]]"
disable-filter-button="[[_disableFilterButton(filters)]]"
query="{{search}}"
nr-selected-filters="[[_getNrSelectedFilters(selectedFilters)]]"
on-paper-search-search="_onSearch"
on-paper-search-filter="_onFilter">
</paper-search-bar>
<paper-filter-dialog
id="filterDialog"
class="filter-dialog"
filters="[[filters]]"
selected-filters="{{selectedFilters}}"
reset-button="[[resetButton]]"
save-button="[[saveButton]]"
no-values-label="[[noValuesLabel]]">
</paper-filter-dialog>
<template is="dom-if" if="{{_hasItems}}">
<div class="items flex">
<slot select=":not([fixed])"></slot>
</div>
</template>
<slot select="[fixed]"></slot>
<template is="dom-if" if="{{_showNoResults(_hasItems,loading)}}">
<div class="noResults">[[noResultsText]]</div>
</template>
<template is="dom-if" if="{{hasMore}}">
<div class="more">
<paper-more-button has-more="[[hasMore]]" loading="[[loading]]" on-tap-more="_loadMore">[[moreButton]]</paper-more-button>
</div>
</template>
</template>
</dom-module>
<script>
(function() {
Polymer({
is: 'paper-search-panel',
/**
* Fired when the user changes the parameter defining the currently shown items
*
* @event change-request-params
*/
/**
* Fired when the user requests to search for a query
*
* @event search
*/
properties: {
/**
* Query for which the user was searching
*/
search: {
type: String,
observer: '_onChangeRequest',
notify: true
},
/**
* All filters from which the user can choose
*/
filters: Object,
/**
* All filters that have been selected by the user, e.g. `{ age: [ "child", "teen" ] }`
*/
selectedFilters: {
type: Object,
observer: '_onChangeRequest',
notify: true,
value: {}
},
/**
* Items that are currently shown in the lister
*/
items: Array,
/**
* True if further items could be loaded
*/
hasMore: {
type: Boolean,
value: false
},
/**
* True if items are currently loaded
*/
loading: {
type: Boolean,
value: false
},
/**
* Whether to hide the Filter button. Set attribute "hide-filter-button" to do so.
*/
hideFilterButton: {
type: Boolean,
value: false
},
/**
* Number of items loaded per page (i.e. for each click on [more])
*/
count: {
type: Number,
notify: true,
value: 20
},
/**
* Icon shown in the search background
*/
icon: {
type: String,
value: 'search'
},
/**
* Text shown in the search box if the user didn't enter any query
*/
placeholder: {
type: String,
value: 'Search'
},
/**
* Text shown if no results are found. Use this property to localize the element.
*/
noResultsText: {
type: String,
value: 'No matching results found.'
},
/**
* Text for the more button to load more data. Use this property to localize the element.
*/
moreButton: {
type: String,
value: 'More'
},
/**
* Text for the reset button in the filter dialog. Use this property to localize the element.
*/
resetButton: String,
/**
* Text for the save button in the filter dialog. Use this property to localize the element.
*/
saveButton: String,
/**
* Label shown if no values are selected for a filter. Use this property to localize the element.
*/
noValuesLabel: String,
_hasItems: {
type: Boolean,
computed: '_computeHasItems(items)',
value: false
}
},
getPaperSearchBarInstance: function() {
return this.$.paperSearchBar;
},
// Private methods
_loadMore: function() {
this.count += 20;
this._updateData();
},
_computeHasItems: function(items) {
return typeof items !== 'undefined' && items.length > 0;
},
_showNoResults: function(_hasItems, loading) {
return !_hasItems && !loading;
},
_onChangeRequest: function(newValue, oldValue) {
// Ignore initial setting of properties (caller is supposed to trigger this call automatically)
if (typeof oldValue !== 'undefined') {
// Set back to default to avoid endless listers
this.count = 20;
this._updateData();
}
},
_updateData: function() {
this.fire('change-request-params');
},
_onFilter: function() {
this.$.filterDialog.open();
},
_onSearch: function() {
this.fire('search');
},
// Counts the selected filters
_getNrSelectedFilters: function(selectedFilters) {
if (Object.keys(selectedFilters).length <= 0) {
return 0;
}
var nrSelectedFilters = Object.keys(selectedFilters)
.map(function(key) {
// Returns number of selected value for a filter
return selectedFilters[key].length;
})
.reduce(function(sum, value) {
// Sum up the selected values across filters
return sum + value;
});
return nrSelectedFilters;
},
_disableFilterButton: function(filters) {
return !(filters && filters.length > 0);
}
});
})();
</script>