-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbulk-search.html
613 lines (553 loc) · 17.4 KB
/
bulk-search.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<!--
Copyright 2017 Next Century Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../polymer-fontawesome-iconset/fa-all.html">
<link rel="import" href="../button-action/button-action.html">
<link rel="import" href="../icon-button/icon-button.html">
<link rel="import" href="../iron-autogrow-textarea/iron-autogrow-textarea.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="../lodash-import/lodash.html">
<link rel="import" href="../styled-dialog/styled-dialog.html">
<link rel="import" href="../styled-dialog/styled-dialog-styles.html">
<link rel="import" href="../stylized-icon/stylized-icon.html">
<link rel="import" href="../terms-transformer/terms-transformer.html">
<link rel="import" href="../vaadin-upload/vaadin-upload.html">
<!--
A Polymer element that allows multiple searches simultaneously.
### Example
```js
var searchFields = [{
color: "cyan",
dateProperties: {},
facetTransform: transFunc(),
facets: true,
field:"knowledge_graph.caliber_gauge.key",
icon:"maps:local-offer",
key:"caliber_gauge",
link:"entity",
queryField:"knowledge_graph.caliber_gauge.value",
result:"header",
search:true,
sort: {
sortId:1,
sortType:"entity"
},
styleClass:"entity-cyan",
title:"Caliber or Gauge",
titlePlural:"Calibers or Gauges",
type:"string",
},
{
color: "pink",
dateProperties: {},
facetTransform: transFunc(),
facets: true,
field:"knowledge_graph.city.key",
icon:"maps:place",
key:"city",
link:"entity",
queryField:"knowledge_graph.city.value",
result:"header",
search:true,
sort: {
sortId:1,
sortType:"entity"
},
styleClass:"entity-pink",
title:"City",
titlePlural:"Cities",
type:"location",
}];
```
```html
<bulk-search
search-fields="[[searchFields]]"
parameter-key="[[searchFields.0.key]]"
search-parameters="{{searchParameters}}"
process-request="{{processRequest}}">
</bulk-search>
```
@demo demo/index.html
-->
<dom-module id="bulk-search">
<template>
<style include="iron-flex iron-flex-alignment"></style>
<style include="styled-dialog-styles"></style>
<style>
:host {
display: block;
}
stylized-icon {
margin: 0 10px;
}
.hide {
display: none;
}
.pointer > * {
cursor: pointer;
}
iron-autogrow-textarea {
border: unset;
display: block;
padding: 0;
--iron-autogrow-textarea: {
background-color: var(--paper-grey-100);
padding: 5px;
}
/* Subtract the padding for the inner textarea. */
height: calc(100% - 10px);
width: calc(100% - 10px);
}
vaadin-upload {
padding: 20px;
}
.warning.message {
--stylized-icon-color: #ff0000;
}
</style>
<terms-transformer category="[[_getCategory(searchFields, searchFieldsKeyProperty, searchFieldsTitleProperty, parameterKey)]]" search-default-value="optional"></terms-transformer>
<styled-dialog id="bulkSearchDialog" header="Bulk Search from Uploaded File or Text" fill>
<div class="layout horizontal">
<div class="layout vertical styled-dialog-right-space">
<span class="styled-dialog-name styled-dialog-tall styled-dialog-right-space">Instructions</span>
<span class="styled-dialog-name styled-dialog-tall styled-dialog-right-space">Supported file types</span>
<span class="styled-dialog-name styled-dialog-tall styled-dialog-right-space">Supported delimiters</span>
</div>
<div class="layout vertical">
<span class="styled-dialog-tall styled-dialog-text">Please enter or paste search terms in the text box below or upload your own files.</span>
<span class="styled-dialog-tall styled-dialog-text">plain text files (including .csv and .txt)</span>
<span class="styled-dialog-tall styled-dialog-text">commas, semicolons, whitespace (including spaces, tabs, and newlines)</span>
</div>
</div>
<div class="styled-dialog-divider"></div>
<dropdown-menu
data="[[_sortedSearchFields]]"
label="Field"
large
no-label-float
selected="{{parameterKey}}"
title-property="[[searchFieldsTitleProperty]]"
value-property="[[searchFieldsKeyProperty]]">
</dropdown-menu>
<template is="dom-if" if="[[!disableResetFields]]">
<div class="layout horizontal center pointer styled-dialog-tall">
<icon-button
active="[[_resetTerms]]"
bigger
class="styled-dialog-right-space"
icon="[[_getCheckboxIcon(_resetTerms)]]"
padded
title-tooltip="Reset Search Terms"
on-tap="_toggleResetTerms">
</icon-button>
<span class="styled-dialog-text styled-dialog-right-space" on-tap="_toggleResetTerms">Reset Search Terms</span>
</div>
</template>
<div class="styled-dialog-divider"></div>
<!-- paper-textarea is broken so use iron-autogrow-textarea -->
<iron-autogrow-textarea placeholder="Enter or Paste Search Terms" value="{{_termsInput}}" on-input="_handleParameterKeyOrTermsInputChange"></iron-autogrow-textarea>
<vaadin-upload
files="{{_files}}"
method="POST"
target="[[uploadEndpoint]]"
on-upload-success="_setTermsInputWithFileText">
</vaadin-upload>
<div class="layout horizontal center">
<button-action
class$="styled-dialog-right-space [[_getButtonStyleClass('clean', _cleanInput, parameterKey, _termsInput)]]"
disabled="[[_disableCleanButton(parameterKey, _termsInput)]]"
icon="fa:wrench"
text="Clean"
click-listener="[[_createCleanListener()]]">
</button-action>
<button-action
class$="styled-dialog-right-space [[_getButtonStyleClass('search', _cleanInput, parameterKey, _termsInput)]]"
disabled="[[!_cleanInput]]"
icon="fa:search"
text="Search"
click-listener="[[_createSearchListener()]]">
</button-action>
<button-action
class="styled-dialog-right-space"
icon="fa:undo"
text="Reset"
click-listener="[[_createResetListener()]]">
</button-action>
<stylized-icon class$="[[_getWarningMessageStyleClass(parameterKey, _termsInput)]]" icon="icons:warning"></stylized-icon>
<span class$="styled-dialog-text [[_getWarningMessageStyleClass(parameterKey, _termsInput)]]">You must select a Field before you may Search.</span>
<stylized-icon class$="[[_getWarningMessageStyleClass(_cleanInput, _termsInput)]]" icon="icons:warning"></stylized-icon>
<span class$="styled-dialog-text [[_getWarningMessageStyleClass(_cleanInput, _termsInput)]]">You must Clean before you may Search.</span>
</div>
</styled-dialog>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'bulk-search',
properties: {
/**
* Builds and returns the search state object from the given config object.
*
* @param {Object} config
* @return {Object}
*/
buildSearchState: {
type: Object
},
/**
* Whether the reset search fields is disabled.
*/
disableResetFields: {
type: Boolean,
value: false
},
/**
* The chosen key in the `searchParameters`.
*
* @type {String}
*/
parameterKey: {
observer: '_handleParameterKeyOrTermsInputChange',
type: String,
value: ''
},
/**
* Whether DIG may process the current query request.
*
* @type {Boolean}
*/
processRequest: {
notify: true,
type: Boolean
},
/**
* The list of search field objects to show in the dropdown menu.
*
* @type {Array}
*/
searchFields: {
type: Array
},
/**
* The property in the search field objects for the key.
*
* @type {String}
* @default 'key'
*/
searchFieldsKeyProperty: {
type: String,
value: 'key'
},
/**
* The property in the search field objects for the title.
*
* @type {String}
* @default 'title'
*/
searchFieldsTitleProperty: {
type: String,
value: 'title'
},
/**
* The search parameters.
*
* @type {Object}
*/
searchParameters: {
notify: true,
type: Object
},
/**
* The file upload endpoint.
*
* @type {String}
* @default '/upload'
*/
uploadEndpoint: {
type: String,
value: '/upload'
},
/**
* Whether the terms input is clean.
*
* @type {Boolean}
* @default false
* @private
*/
_cleanInput: {
type: Boolean,
value: false
},
/**
* The uploaded files.
*
* @type {Array}
* @private
*/
_files: {
type: Array
},
/**
* Whether to reset all the searchParameters whenever a search is run.
*
* @type {Boolean}
* @default true
*/
_resetTerms: {
type: Boolean,
value: true
},
/**
* The sorted list of search fields.
*
* @type {Array}
* @private
*/
_sortedSearchFields: {
computed: '_sortSearchFields(searchFields, searchFieldsTitleProperty)',
type: Array
},
/**
* The terms input for the dialog.
*
* @type {String}
* @default ''
* @private
*/
_termsInput: {
type: String,
value: ''
}
},
/**
* Returns the on-click listener object for the search button-action.
*
* @return {Object}
* @private
*/
_createCleanListener: function() {
var self = this;
return {
onClick: function() {
if(self.parameterKey && self._termsInput) {
self._termsInput = self._termsInput.trim();
// TODO Move out of element!
if(self.parameterKey === 'phone') {
self._termsInput = self._unformatPhones(self._termsInput);
}
self._cleanInput = !!self._termsInput;
}
}
};
},
/**
* Returns the on-click listener object for the search button-action.
*
* @return {Object}
* @private
*/
_createSearchListener: function() {
var self = this;
return {
onClick: function() {
if(self.parameterKey && self._termsInput && self._cleanInput) {
var termsTransformer = self.$$('terms-transformer');
var termsArray = self._termsInput.replace(/\n/g, ' ').split(' ');
var termsParameter = termsTransformer.createObjectFromList(termsArray);
self.$$('#bulkSearchDialog').close();
self.set('processRequest', false);
if(self._resetTerms) {
var parameters = {};
parameters[self.parameterKey] = termsParameter;
self.set('searchParameters', self.buildSearchState(parameters, self.searchParameters));
} else {
self.set('searchParameters.' + self.parameterKey, termsParameter);
}
self.set('processRequest', true);
self.set('_termsInput', '');
self.set('_files', []);
}
}
};
},
/**
* Returns the on-click listener object for the reset button-action.
*
* @return {Object}
* @private
*/
_createResetListener: function() {
var self = this;
return {
onClick: function() {
self.set('_termsInput', '');
self.set('_cleanInput', false);
self.set('_files', []);
}
};
},
/**
* Returns whether to disable the clean button.
*
* @param {String} parameterKey
* @param {String} termsInput
* @return {Boolean}
* @private
*/
_disableCleanButton: function(parameterKey, termsInput) {
return !parameterKey || !termsInput;
},
/**
* Returns the style class for the button of the given type.
*
* @param {String} type
* @param {Boolean} cleanInput
* @param {String} parameterKey
* @param {String} termsInput
* @return {String}
* @private
*/
_getButtonStyleClass: function(type, cleanInput, parameterKey, termsInput) {
if(type === 'clean') {
return (!!termsInput && !!parameterKey && !cleanInput ? 'active' : '');
}
if(type === 'search') {
return (!!termsInput && !!parameterKey && cleanInput ? 'active' : '');
}
return '';
},
/**
* Returns the category of the search field with the given parameter key.
*
* @param {Array} searchFields
* @param {String} keyProperty
* @param {String} titleProperty
* @param {String} parameterKey
* @return {String}
* @private
*/
_getCategory: function(searchFields, keyProperty, titleProperty, parameterKey) {
if(!searchFields || !keyProperty || !titleProperty || !parameterKey) {
return;
}
for(var i = 0; i < searchFields.length; ++i) {
if(searchFields[i][keyProperty] === parameterKey) {
return searchFields[i][titleProperty];
}
}
return undefined;
},
/**
* Returns the checkbox icon for the given setting.
*
* @param {Boolean} setting
* @return {String}
* @private
*/
_getCheckboxIcon: function(setting) {
return setting ? 'check-box' : 'check-box-outline-blank';
},
/**
* Returns the given property in the given item.
*
* @param {Object} item
* @param {String} property
* @return {Object}
* @private
*/
_getProperty: function(item, property) {
return item[property];
},
/**
* Returns the style class for whether to show or hide the warning message icon and text.
*
* @param {Boolean|String} prerequisite
* @param {String} termsInput
* @return {String}
* @private
*/
_getWarningMessageStyleClass: function(prerequisite, termsInput) {
return (!prerequisite && termsInput) ? 'warning message' : 'hide';
},
/**
* Handles change in the parameter key or terms input. Sets _cleanInput to false.
*
* @private
*/
_handleParameterKeyOrTermsInputChange: function() {
this._cleanInput = false;
},
/**
* Sets the terms input in the dialog with the file text.
*
* @event success
* @private
*/
_setTermsInputWithFileText: function(event) {
if(event.detail && event.detail.xhr && event.detail.xhr.response) {
this._termsInput = (this._termsInput || '') + event.detail.xhr.response;
this._cleanInput = false;
}
},
/**
* Returns the sorted list of search fields.
*
* @param {Array} searchFields
* @param {String} titleProperty
* @return {Array}
* @private
*/
_sortSearchFields: function(searchFields, titleProperty) {
// Use concat to create a copy.
if(!searchFields || !titleProperty) {
return;
}
return searchFields.concat().sort(function(a, b) {
return a[titleProperty].localeCompare(b[titleProperty]);
});
},
/**
* Toggles _resetTerms.
*
* @private
*/
_toggleResetTerms: function() {
this.set('_resetTerms', !this._resetTerms);
},
/**
* Returns a string of unformatted phones separated by newlines.
*
* @param {String} phone
* @return {String}
* @private
*/
_unformatPhones: function(phone) {
// Replace all delimiters (whitespace, commas, and, semicolons) with single spaces.
return phone.replace(/[\s,;]/g, ' ')
// Remove all non-digit, non-whitespace characters.
.replace(/[^\d\s]/g, '')
// Remove extra spaces.
.replace(/\s+/g, '\n')
.trim();
},
/**
* Opens the dialog.
*/
open: function() {
this.$$('#bulkSearchDialog').open();
}
});
})();
</script>
</dom-module>