-
Notifications
You must be signed in to change notification settings - Fork 214
/
searchFilter.js
175 lines (153 loc) · 4.81 KB
/
searchFilter.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
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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
import * as Environments from '../environments/environments.js';
import * as Utils from '../utils.js';
import * as Things from './things.js';
const filterExamples = [
'eq(attributes/location,"kitchen")',
'ge(thingId,"myThing1")',
'gt(_created,"2020-08-05T12:17")',
'exists(features/featureId)',
'and(eq(attributes/location,"kitchen"),eq(attributes/color,"red"))',
'or(eq(attributes/location,"kitchen"),eq(attributes/location,"living-room"))',
'like(attributes/key1,"known-chars-at-start*")',
];
let keyStrokeTimeout;
let lastSearch = '';
const dom = {
filterList: null,
favIcon: null,
searchFilterEdit: null,
searchThings: null,
searchFavourite: null,
tabThings: null,
pinnedThings: null,
};
/**
* Initializes components. Should be called after DOMContentLoaded event
*/
export async function ready() {
Environments.addChangeListener(onEnvironmentChanged);
Utils.getAllElementsById(dom);
dom.filterList.addEventListener('click', (event) => {
dom.searchFilterEdit.value = event.target.textContent;
checkIfFavourite();
Things.searchThings(event.target.textContent);
});
dom.searchThings.onclick = () => {
searchTriggered(dom.searchFilterEdit.value);
};
dom.searchFavourite.onclick = () => {
if (toggleFilterFavourite(dom.searchFilterEdit.value)) {
dom.favIcon.classList.toggle('bi-star');
dom.favIcon.classList.toggle('bi-star-fill');
};
};
dom.searchFilterEdit.onkeyup = (event) => {
if (event.key === 'Enter' || event.code === 13) {
searchTriggered(dom.searchFilterEdit.value);
} else {
clearTimeout(keyStrokeTimeout);
keyStrokeTimeout = setTimeout(checkIfFavourite, 1000);
}
};
dom.searchFilterEdit.onclick = (event) => {
if (event.target.selectionStart === event.target.selectionEnd) {
event.target.select();
};
};
dom.pinnedThings.onclick = pinnedTriggered;
};
/**
* Callback to initialize searchFilters if the environment changed
*/
function onEnvironmentChanged() {
if (!Environments.current()['filterList']) {
Environments.current().filterList = [];
};
if (!Environments.current()['pinnedThings']) {
Environments.current().pinnedThings = [];
};
updateFilterList();
dom.searchFilterEdit.focus();
};
/**
* Tests if the search filter is an RQL. If yes, things search is called otherwise just things get
* @param {String} filter search filter string containing an RQL or a thingId
*/
function searchTriggered(filter) {
lastSearch = filter;
const regex = /^(eq\(|ne\(|gt\(|ge\(|lt\(|le\(|in\(|like\(|exists\(|and\(|or\(|not\().*/;
if (filter === '' || regex.test(filter)) {
Things.searchThings(filter);
} else {
Things.getThings([filter]);
}
}
/**
* Gets the list of pinned things
*/
function pinnedTriggered() {
lastSearch = 'pinned';
dom.searchFilterEdit.value = null;
Things.getThings(Environments.current()['pinnedThings']);
}
/**
* Performs the last search by the user using the last used filter.
* If the user used pinned things last time, the pinned things are reloaded
*/
export function performLastSearch() {
if (lastSearch === 'pinned') {
pinnedTriggered();
} else {
searchTriggered(lastSearch);
}
}
/**
* Updates the UI filterList
*/
function updateFilterList() {
dom.filterList.innerHTML = '';
Utils.addDropDownEntries(dom.filterList, ['Favourite search filters'], true);
Utils.addDropDownEntries(dom.filterList, Environments.current().filterList);
Utils.addDropDownEntries(dom.filterList, ['Example search filters'], true);
Utils.addDropDownEntries(dom.filterList, filterExamples);
};
/**
* Adds or removes the given filter from the list of search filters
* @param {String} filter filter
* @return {boolean} true if the filter was toggled
*/
function toggleFilterFavourite(filter) {
if (!filter || filter === '') {
return false;
}
const i = Environments.current().filterList.indexOf(filter);
if (i >= 0) {
Environments.current().filterList.splice(i, 1);
} else {
Environments.current().filterList.push(filter);
}
Environments.environmentsJsonChanged();
return true;
};
/**
* Initializes the UI for the favicon dependent on wether the search filter is in the search filters
*/
function checkIfFavourite() {
if (Environments.current().filterList.indexOf(dom.searchFilterEdit.value) >= 0) {
dom.favIcon.classList.replace('bi-star', 'bi-star-fill');
} else {
dom.favIcon.classList.replace('bi-star-fill', 'bi-star');
}
}