|
| 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 | + |
1 | 108 | window.trame.utils.quickview = { |
2 | 109 | formatRange(value, useLog) { |
3 | 110 | if (value === null || value === undefined || isNaN(value)) { |
@@ -41,4 +148,8 @@ window.trame.utils.quickview = { |
41 | 148 | } |
42 | 149 | return 4; |
43 | 150 | }, |
| 151 | + filter(value, query, item) { |
| 152 | + QUERY_FILTER.updateQuery(query); |
| 153 | + return QUERY_FILTER.keepItem(item.raw); |
| 154 | + }, |
44 | 155 | }; |
0 commit comments