-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
151 lines (143 loc) · 3.65 KB
/
search.js
File metadata and controls
151 lines (143 loc) · 3.65 KB
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
import {addclasses} from "./template.js";
function match(pattern,s){
// find pattern in s
if (s==null) {
return [false,[]];
}
const patterns=pattern.toLowerCase().split(/\s/);
s=s.toLowerCase();
const patternis=patterns.filter(p=>p.length>0).map(p=>[p,0]);
const matches=[];
for(let i=0;i<s.length;i++){
for (let j = 0; j < patternis.length; j++) {
const [pattern,idx] = patternis[j];
if(idx==pattern.length){
continue;
}
if(s.charAt(i)==pattern.charAt(idx)) {
if (matches.at(-1) != i) {
matches.push(i);
}
patternis[j][1]++;
}
}
}
if (patternis.every(([pattern,idx])=>idx==pattern.length)) {
return [true,matches];
}
return [false,[]];
}
function comparematch(m1,m2){
if(m1.length!=m2.length){
throw "unmatched lengths";
}
for(var i=0;i<m1.length;i++){
if(m1[i]<m2[i]){
return -1;
}else if(m1[i]>m2[i]){
return 1;
}
}
return 0;
}
function compare([,,,im1,lm1],[,,,im2,lm2]){
// compare the locale match first
var empty1=lm1.length==0;
var empty2=lm2.length==0;
if(empty1){
if(empty2){
return comparematch(im1,im2);
}else{
return 1;
}
}else{
if(empty2){
return -1;
}else{
var match=comparematch(lm1,lm2);
if(match==0){
return comparematch(im1,im2);
}
return match;
}
}
}
function locationOf(element, array, compare, start, end) {
var start = start || 0;
var end = end || array.length;
var pivot = (start+end)>>1;
if (end - start <= 1)
return array[pivot] > element ? pivot - 1 : pivot;
var comp=compare(array[pivot],element);
if (comp==0) return pivot;
if (comp<0) {
return locationOf(element, array, compare, pivot, end);
} else {
return locationOf(element, array, compare, start, pivot);
}
}
function insort(array, element, compare) {
var loc=locationOf(element, array, compare);
array.splice(loc + 1, 0, element);
return array;
}
function highlightletters(s,highlighted){
var out=document.createElement("span");
if (s==null) {
out.append("<no name>");
return out;
}
for(var i=0;i<s.length;i++){
if(highlighted.includes(i)){
var h=document.createElement("span");
h.textContent=s.charAt(i);
addclasses(h,["search-highlighted"]);
out.append(h);
}else{
out.append(s.charAt(i));
}
}
return out;
}
function itemsearch(element,locales,callback,renderer){
// callback is called to render a thing
const search=element.value;
const scores=[];
for(const [itype,iname,[lname,ldesc]] of locales){
const [imatched,imatch]=match(search,iname);
const [lmatched,lmatch]=match(search,lname);
if(imatched||lmatched){
insort(scores,[itype,iname,lname,imatch,imatch],compare);
}
}
console.log(scores);
const resultsdiv=element.nextElementSibling;
resultsdiv.textContent="";
for(const [itype,iname,lname,imatch,lmatch] of scores){
let postfix;
if (itype == "item"){
postfix = "";
} else if (itype == "recipe"){
postfix = " (recipe)";
} else {
throw "what is this??? "+itype;
}
const structure={
type:"div",
contents:[
{type:"span",contents:[highlightletters(lname,lmatch)]},
postfix,
{type:"span",contents:[highlightletters(iname,imatch)],classes:["internal-name"]},
{type:"icon",itype,name:iname}
],
onclick:function setitem() {
console.log("picked",iname);
element.value=lname;
itemsearch(element,locales,callback,renderer);
callback(itype,iname);
}
};
resultsdiv.append(renderer.render(structure));
}
}
export {itemsearch};