/
search.js
263 lines (199 loc) · 6.8 KB
/
search.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
var html; // accumulated results page
var searchString; // original search string
var nResults = 0; // number of search results
var searchTerms; // array of words in search string
// indices into a libdata entry
var URL = 0;
var NAME = 1;
var TYPE = 2;
var FILENAME = 3;
var AUTHORS = 4;
var ROUTINE_NAME = 5;
var COMMENTS = 6;
var PARAMETERS = 7;
var CATEGORIES = 8;
var ATTRIBUTES = 9;
var MATCH_TYPE = 10;
var N_MATCHES = 11;
var SCORE = 12;
var MATCHES = 13;
var SCORE_VALUES = new Array(0, 0, 0, 8, 6, 10, 4, 6, 8, 4);
/*
Find results from the search.
*/
// Returns true for a-zA-Z0-9 and &, false otherwise.
function isAlnum(ch) {
if ((ch >= "a" && ch <= "z") || (ch == "&") || (ch >= "A" && ch <= "Z") || (ch >= "0" && ch <="9")) {
return true;
} else {
return false;
}
}
// find all the matches in a single item
function searchElement(item, matchType, upperSearchString) {
var element = libdata[item][matchType].toUpperCase();
var pos, origPos = 0;
libdata[item][N_MATCHES] = 0;
pos = element.indexOf(upperSearchString);
while (pos >= 0) {
origPos += pos + 1;
libdata[item][MATCHES + libdata[item][N_MATCHES]] = origPos - 1;
libdata[item][N_MATCHES]++;
element = element.substring(pos + 1, element.length);
while (isAlnum(element.charAt(0)) && element.length > 0) {
element = element.substring(1, element.length);
origPos++;
}
pos = element.indexOf(upperSearchString);
}
}
function searchItem(item, upperSearchString) {
var matchType = TYPE;
//html += "Searching " + item + "<br/>";
// mark item as not matching
libdata[item][MATCH_TYPE] = -1;
libdata[item][SCORE] = 0;
// search FILENAME, AUTHORS, ROUTINE_NAME, COMMENTS, PARAMETERS,
// CATEGORIES, and ATTRIBUTES fields
while (++matchType <= ATTRIBUTES && libdata[item][MATCH_TYPE] == -1) {
searchElement(item, matchType, upperSearchString);
if (libdata[item][N_MATCHES] > 0) {
libdata[item][MATCH_TYPE] = matchType;
}
}
}
function sortByScore(a, b) {
return b[SCORE] - a[SCORE];
}
function sortResults() {
for (item = 0; item < libdata.length; item++) {
if (libdata[item][N_MATCHES] > 0) {
matchType = libdata[item][MATCH_TYPE];
typeMultiplier = SCORE_VALUES[matchType];
matchPercentage = libdata[item][N_MATCHES] * searchString.length / libdata[item][matchType].length;
libdata[item][SCORE] = typeMultiplier * matchPercentage;
}
}
libdata = libdata.sort(sortByScore);
}
function findResults() {
upperSearchString = searchString.toUpperCase();
for (var item = 0; item < libdata.length; item++) {
searchItem(item, upperSearchString);
}
sortResults();
}
/*
Create results web page. Results are written to the html variable
and then sent to the browser.
*/
function putHeader() {
html = "<html><head><title>Search results</title>";
html += "<link rel=\"stylesheet\" type=\"text/css\" href=\"idldoc-resources/main.css\" />";
html += "<style type=\"text/css\" media=\"all\">";
html += " span.score { color: black; }";
html += " span.context { color: #A0A0A0; }";
html += " span.term { color: black; background: yellow; }";
html += " li { margin-bottom: 0.5em; }";
html += "</style>";
html += "</head><body>";
html += "<div class=\"header smaller\">";
html += "<h1>" + title + "</h1>";
html += "<h2>" + subtitle + "</h2>";
html += "</div";
html += "<div class=\"content\">";
html += "<h2>Search results for \"" + searchString + "\"</h2>";
}
function putItem(item) {
mType = libdata[item][MATCH_TYPE];
width = Math.round(2 * libdata[item][SCORE]);
html += "<li>";
html += "<img src=\"idldoc-resources/searchbar.png\" height=\"10\" width=\"" + width + "\" /> ";
html += "<a href=\"" + libdata[item][URL] + "\" target=\"main_frame\">" + libdata[item][NAME] + "</a><br/>";
html += libdata[item][TYPE] + "<br/>";
nPreCharacters = 25;
nPostCharacters = 40;
html += "<span class=\"context\">";
if (libdata[item][MATCHES] > nPreCharacters) {
html += "...";
}
html += libdata[item][mType].substring(libdata[item][MATCHES] - nPreCharacters, libdata[item][MATCHES]);
html += "<span class=\"term\">";
html += libdata[item][mType].substring(libdata[item][MATCHES], libdata[item][MATCHES] + searchString.length);
html += "</span>";
html += libdata[item][mType].substring(libdata[item][MATCHES] + searchString.length, libdata[item][MATCHES] + searchString.length + nPostCharacters);
if (libdata[item][MATCHES] + searchString.length + nPostCharacters < libdata[item][mType].length) {
html += "...";
}
html += "</span><br/>";
html += "<span class=\"score\">Score: " + Math.round(10 * libdata[item][SCORE]) / 10;
plural = libdata[item][N_MATCHES] > 1 ? "es" : "";
html += " - " + libdata[item][N_MATCHES] + " match" + plural + " in ";
if (mType == 3) {
type = "the filename";
} else if (mType == 4) {
type = "the author names";
} else if (mType == 5) {
type = "the routine name";
} else if (mType == 6) {
type = "the comments";
} else if (mType == 7) {
type = "the parameters";
} else if (mType == 8) {
type = "the categories";
} else if (mType == 9) {
type = "the attributes";
} else {
type = "an unspecified comment";
}
html += type + "</span></li>";
}
function putResults() {
for (var item = 0; item < libdata.length; item++) {
if (libdata[item][N_MATCHES] > 0) {
nResults++;
}
}
if (nResults > 0) {
html += "<ol>";
}
for (var item = 0; item < libdata.length; item++) {
if (libdata[item][N_MATCHES] > 0) {
putItem(item);
}
}
if (nResults > 0) {
html += "</ol>";
}
}
function putFooter() {
var plural = nResults == 1 ? "" : "s";
html += "<p>" + nResults + " item" + plural + " found.</p>"
html += "</div></body></html>";
}
function writeResultsPage() {
var htmlCode = html;
iu = open("", "Object", "resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,width=475,height=600");
iu.document.open();
iu.document.write(htmlCode);
iu.document.close();
}
/*
Event handlers for forms on search page.
*/
function basicsearch() {
searchString = document.basicForm.basicText.value;
searchTerms = searchString.split(/\s/);
putHeader();
findResults();
putResults();
putFooter();
writeResultsPage();
}
function advancedsearch() {
routineName = document.advancedForm.routinename.value;
comments = document.advancedForm.comments.value;
parameters = document.advancedForm.parameters.value;
authors = document.advancedForm.authors.value;
alert("Advanced searching...\n\nRoutine name = " + routineName + "\nComments = " + comments + "\nParameters = " + parameters + "\nAuthors = " + authors);
}