Skip to content

Commit b044ac6

Browse files
committed
fix(fieldSelector): improve query handling
1 parent 68d9571 commit b044ac6

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

src/e3sm_quickview/components/drawers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def __init__(self, load_variables=None):
173173
classes="position-absolute show-scrollbar",
174174
hover=True,
175175
search=("variables_filter", ""),
176+
custom_filter=("utils.quickview.filter",),
176177
items_per_page=-1,
177178
hide_default_footer=True,
178179
):

src/e3sm_quickview/module/serve/utils.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,110 @@
1+
class QueryFilter {
2+
constructor() {
3+
this.exacts = [];
4+
this.regulars = [];
5+
this.startsWith = [];
6+
this.endsWith = [];
7+
this.currentQuery = null;
8+
}
9+
10+
updateQuery(query) {
11+
if (this.currentQuery === query) {
12+
return;
13+
}
14+
this.currentQuery = query;
15+
16+
const pendingExact = [];
17+
const exacts = [];
18+
const regulars = [];
19+
const startsWith = [];
20+
const endsWith = [];
21+
const tokens = query.split(" ");
22+
for (let i = 0; i < tokens.length; i++) {
23+
const token = tokens[i];
24+
25+
// Handle pending exact
26+
if (pendingExact.length) {
27+
pendingExact.push(token);
28+
29+
if (token.endsWith('"')) {
30+
exacts.push(pendingExact.join(" ").replaceAll('"', ""));
31+
pendingExact.length = 0;
32+
}
33+
continue;
34+
}
35+
36+
// Handle exact token
37+
if (token.startsWith('"')) {
38+
if (token.endsWith('"')) {
39+
exacts.push(token.replaceAll('"', ""));
40+
} else {
41+
pendingExact.push(token);
42+
}
43+
continue;
44+
}
45+
46+
// Handle startsWith
47+
if (token.startsWith("*")) {
48+
endsWith.push(token.replaceAll("*", "").toLowerCase());
49+
continue;
50+
}
51+
52+
// Handle endsWith
53+
if (token.endsWith("*")) {
54+
startsWith.push(token.replaceAll("*", "").toLowerCase());
55+
continue;
56+
}
57+
58+
regulars.push(token.toLowerCase());
59+
}
60+
// Clear pending
61+
if (pendingExact.length) {
62+
exacts.push(pendingExact.join(" ").replaceAll('"', ""));
63+
}
64+
65+
this.exacts = exacts;
66+
this.regulars = regulars;
67+
this.startsWith = startsWith;
68+
this.endsWith = endsWith;
69+
}
70+
71+
keepItem({ name, type }) {
72+
// Filter exact first
73+
for (let i = 0; i < this.exacts.length; i++) {
74+
const exact = this.exacts[i];
75+
if (name !== exact && type !== exact) {
76+
return false;
77+
}
78+
}
79+
80+
const nameLower = name.toLowerCase();
81+
const typeLower = type.toLowerCase();
82+
83+
// startsWith
84+
for (let i = 0; i < this.startsWith.length; i++) {
85+
if (!nameLower.startsWith(this.startsWith[i])) {
86+
return false;
87+
}
88+
}
89+
90+
// endsWith
91+
for (let i = 0; i < this.endsWith.length; i++) {
92+
if (!nameLower.endsWith(this.endsWith[i])) {
93+
return false;
94+
}
95+
}
96+
97+
if (this.regulars.length) {
98+
return this.regulars.every(
99+
(v) => nameLower.includes(v) || typeLower.includes(v),
100+
);
101+
}
102+
return true;
103+
}
104+
}
105+
106+
QUERY_FILTER = new QueryFilter();
107+
1108
window.trame.utils.quickview = {
2109
formatRange(value, useLog) {
3110
if (value === null || value === undefined || isNaN(value)) {
@@ -41,4 +148,8 @@ window.trame.utils.quickview = {
41148
}
42149
return 4;
43150
},
151+
filter(value, query, item) {
152+
QUERY_FILTER.updateQuery(query);
153+
return QUERY_FILTER.keepItem(item.raw);
154+
},
44155
};

0 commit comments

Comments
 (0)