This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearchbox-component.js
98 lines (90 loc) · 2.46 KB
/
searchbox-component.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
/*!
* Copyright (c) 2016-2017 Digital Bazaar, Inc. All rights reserved.
*/
import angular from 'angular';
export default {
bindings: {
options: '<?brOptions',
onSearch: '&brOnSearch'
},
controller: Ctrl,
templateUrl: 'bedrock-angular-searchbox/searchbox-component.html',
transclude: {
help: '?brSearchboxHelp'
}
};
/* @ngInject */
function Ctrl($filter, $q, $scope, $transclude, brAlertService) {
const self = this;
const defaultOptions = {
searchbox: {
placeholder: 'Search...'
},
additional: [
// {
// label: 'Issued',
// placeholder: 'monday, tuesday',
// prefix: 'issued'
// },
// {
// label: 'From',
// placeholder: 'sally, bob',
// prefix: 'from'
// }
]
};
self.$onInit = () => {
self.loading = false;
self.searchOptions = {};
self.searchText = '';
self.display = {
helpButton: false,
helpText: false
};
self.display.helpButton = $transclude.isSlotFilled('help');
angular.extend(self.searchOptions, self.options, defaultOptions);
self.searchFields = {};
self.searchOptions.additional.forEach(field => {
self.searchFields[field.prefix] = '';
});
};
self.submitSearch = () => {
brAlertService.clear();
const filteredSearch = $filter('brSearchFilters')(self.searchText.trim());
if('error' in filteredSearch) {
return brAlertService.add('error', filteredSearch.error);
}
self.loading = true;
$q.resolve(self.onSearch({query: filteredSearch}))
.catch(() => {})
.then(() => {
self.loading = false;
});
};
self.searchFieldChanged = () => {
self.searchText = '';
for(const key in self.searchFields) {
if(self.searchFields[key] === '') {
continue;
}
const fieldText = self.searchFields[key];
// Delimit by comma to construct prefix:text fields
const components = fieldText.split(',');
let queryText = '';
components.forEach(component => {
let trimmed = component.trim();
if(trimmed.length === 0) {
return;
}
if(trimmed.indexOf(' ') >= 0) {
trimmed = '"' + trimmed + '"';
} else {
// If it had quotes, and now has no spaces, remove the quotes
trimmed = trimmed.replace(/"/g, '');
}
queryText = queryText + key + ':' + trimmed + ' ';
});
self.searchText = self.searchText + queryText;
}
};
}