33 |
var query_left = ""; |
var query_left = ""; |
34 |
var search_err = ""; |
var search_err = ""; |
35 |
var results = null; |
var results = null; |
36 |
|
var index_path = ""; |
37 |
|
|
38 |
var watchdog_id = 0; |
var watchdog_id = 0; |
39 |
var watchdog_callback = null; |
var watchdog_callback = null; |
79 |
From David Flanagan's, _Javascript:_The Definitive_Guide_, pg. 294-5, |
From David Flanagan's, _Javascript:_The Definitive_Guide_, pg. 294-5, |
80 |
published by O'Reilly, 4th edition, 2002 |
published by O'Reilly, 4th edition, 2002 |
81 |
*/ |
*/ |
82 |
|
|
83 |
|
var debug_div = null; |
84 |
|
|
85 |
function debug(msg) |
function debug(msg) |
86 |
{ |
{ |
87 |
// return; // Disable debugging |
// return; // Disable debugging |
88 |
|
|
89 |
if(!debug.box) |
if (! debug_div) debug_div = document.getElementById('debug'); |
|
{ |
|
|
debug.box = document.createElement("div"); |
|
|
debug.box.setAttribute("style", |
|
|
"background-color:white" + |
|
|
"font-family: monospace; " + |
|
|
"border: solid black 3px; "+ |
|
|
"padding: 10px;"); |
|
|
|
|
|
document.body.appendChild(debug.box); |
|
|
debug.box.innerHTML = "<h1 style='test-align:cent'>Debugging Output</h1>"; |
|
|
} |
|
|
|
|
|
var p = document.createElement("p"); |
|
|
p.appendChild(document.createTextNode(msg)); |
|
|
debug.box.appendChild(p); |
|
|
} |
|
90 |
|
|
91 |
// |
// this will create debug div if it doesn't exist. |
92 |
|
if (! debug_div) { |
93 |
|
debug_div = document.createElement('div'); |
94 |
|
document.body.appendChild(debug_div); |
95 |
|
} |
96 |
|
if (debug_div) { |
97 |
|
debug_div.appendChild(document.createTextNode(msg)); |
98 |
|
debug_div.appendChild(document.createElement("br")); |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
// Convert a number into a base 62 alphanumeric number string |
// Convert a number into a base 62 alphanumeric number string |
103 |
function convert(num) |
function convert(num) |
129 |
watchdog_callback(new Array()); |
watchdog_callback(new Array()); |
130 |
} |
} |
131 |
|
|
132 |
|
var xmldoc; |
133 |
|
|
134 |
// This function loads the XML document from the specified URL, and when |
// This function loads the XML document from the specified URL, and when |
135 |
// it is fully loaded, passes that document and the url to the specified |
// it is fully loaded, passes that document and the url to the specified |
136 |
// handler function. This function works with any XML document |
// handler function. This function works with any XML document |
150 |
if (document.implementation && document.implementation.createDocument) |
if (document.implementation && document.implementation.createDocument) |
151 |
{ |
{ |
152 |
// Create a new Document object |
// Create a new Document object |
153 |
var xmldoc = document.implementation.createDocument("", "", null); |
xmldoc = document.implementation.createDocument("", "", null); |
154 |
|
|
155 |
// Specify what should happen when it finishes loading |
// Specify what should happen when it finishes loading |
156 |
xmldoc.onload = function() { handler(xmldoc, url, data, result_handler); } |
xmldoc.onload = function() { handler(xmldoc, url, data, result_handler); } |
160 |
|
|
161 |
// And tell it what URL to load |
// And tell it what URL to load |
162 |
xmldoc.load(url); |
xmldoc.load(url); |
163 |
|
return true; |
164 |
} |
} |
165 |
// Otherwise use Microsoft's proprietary API for Internet Explorer |
// Otherwise use Microsoft's proprietary API for Internet Explorer |
166 |
// Something about not following standards once again |
// Something about not following standards once again |
167 |
else if (window.ActiveXObject) |
else if (window.ActiveXObject) |
168 |
{ |
{ |
169 |
//var xmldoc = new ActiveXObject("MSXML2.DOMDocument"); // Create doc. |
xmldoc = new ActiveXObject("Microsoft.XMLDOM"); // Create doc. |
170 |
var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); // Create doc. |
if (! xmldoc) xmldoc = new ActiveXObject("MSXML2.DOMDocument"); // Create doc. |
171 |
// Specify onload |
// Specify onload |
172 |
xmldoc.onreadystatechange = function() |
xmldoc.onreadystatechange = function() |
173 |
{ |
{ |
174 |
if (xmldoc.readyState == 4) handler(xmldoc, url, data, result_handler); |
if (xmldoc.readyState == 4) handler(xmldoc, url, data, result_handler); |
175 |
} |
} |
176 |
xmldoc.load(url); // Start loading! |
xmldoc.load(url); // Start loading! |
177 |
|
return true; |
178 |
} |
} |
179 |
|
// else fallback on usage of iframes to load xml (Opera 7.53 without Java and maybe old Mac browsers) |
180 |
|
else { |
181 |
|
debug("using iframe xml loader - experimental and slow"); |
182 |
|
if (! window.xml_iframe) { |
183 |
|
debug("creating iframe"); |
184 |
|
window.xml_iframe = document.createElement('div'); |
185 |
|
window.xml_iframe.innerHTML = '<iframe src="'+url+'" name="xml_iframe" height="0" width="0" style="display: none;"></iframe>'; |
186 |
|
document.body.appendChild(window.xml_iframe); |
187 |
|
} else { |
188 |
|
debug("loading xml in existing iframe"); |
189 |
|
window.frames.xml_iframe.window.document.location.href = url; |
190 |
|
} |
191 |
|
|
192 |
|
// set timeout to re-check if iframe is loaded |
193 |
|
window.iframe_timeout = window.setInterval('iframe_xml_loaded();',100); |
194 |
|
|
195 |
|
// save some data for iframe_xml_loaded() |
196 |
|
window.xml_handler = handler; |
197 |
|
window.xml_url = url; |
198 |
|
window.xml_data = data; |
199 |
|
window.xml_result_handler = result_handler; |
200 |
|
return true; |
201 |
|
} |
202 |
|
clearTimeout(watchdog_id); |
203 |
|
debug("Browser incompatilibity: can't request XML document by one of supported methods"); |
204 |
|
return false; |
205 |
} |
} |
206 |
catch(ex) |
catch(ex) |
207 |
{ |
{ |
215 |
return true; |
return true; |
216 |
} |
} |
217 |
|
|
218 |
|
function iframe_xml_loaded() { |
219 |
|
debug("iframe_xmldoc_loaded"); |
220 |
|
if (! window.frames['xml_iframe']) return; |
221 |
|
var xml = eval('window.frames.xml_iframe.window.document'); |
222 |
|
if (xml) { |
223 |
|
clearTimeout(window.iframe_timeout); |
224 |
|
debug("calling handler with ("+window.xml_url+","+window.xml_data+",...)"); |
225 |
|
window.xml_handler(window.frames.xml_iframe.window.document, window.xml_url, window.xml_data, window.xml_result_handler); |
226 |
|
} else { |
227 |
|
debug("can't eval iframe with xml"); |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
function loadData(xmldoc, url, pos, result_handler) |
function loadData(xmldoc, url, pos, result_handler) |
232 |
{ |
{ |
233 |
clearTimeout(watchdog_id); |
clearTimeout(watchdog_id); |
257 |
|
|
258 |
if(query_left.length > 0) |
if(query_left.length > 0) |
259 |
{ |
{ |
260 |
doSearch(query_left, result_handler); |
doSearch(index_path, query_left, result_handler); |
261 |
} |
} |
262 |
else |
else |
263 |
{ |
{ |
302 |
traverseTree,query,result_handler)) |
traverseTree,query,result_handler)) |
303 |
{ |
{ |
304 |
debug("Unable to locate key "+query); |
debug("Unable to locate key "+query); |
305 |
|
result_handler(new Array()); |
306 |
} |
} |
307 |
// make sure of garbage collection |
// make sure of garbage collection |
308 |
xmldoc=null; |
xmldoc=null; |
316 |
loadData, i, result_handler)) |
loadData, i, result_handler)) |
317 |
{ |
{ |
318 |
debug("ERROR: Unable to locate data "+query); |
debug("ERROR: Unable to locate data "+query); |
319 |
|
result_handler(new Array()); |
320 |
} |
} |
321 |
// make sure of garbage collection |
// make sure of garbage collection |
322 |
xmldoc=null; |
xmldoc=null; |
325 |
} |
} |
326 |
} |
} |
327 |
// Look past the end... |
// Look past the end... |
328 |
if(!loadXML(url.replace(".xml","/"+convert(i)+".xml"), |
if(keys.length == 0 || !loadXML(url.replace(".xml","/"+convert(i)+".xml"), |
329 |
traverseTree,query,result_handler)) |
traverseTree,query,result_handler)) |
330 |
{ |
{ |
331 |
debug("Unable to locate key "+query); |
debug("Unable to locate key "+query); |
332 |
|
result_handler(new Array()); |
333 |
} |
} |
334 |
// make sure of garbage collection |
// make sure of garbage collection |
335 |
xmldoc=null; |
xmldoc=null; |
336 |
return; |
return; |
337 |
} |
} |
338 |
|
|
339 |
function doSearch(query, result_func) |
function doSearch(index_name,query, result_func) |
340 |
{ |
{ |
341 |
//alert("doSearch("+query+")"); |
//alert("doSearch("+index_name+","+query+")"); |
342 |
var pos=query.search(/[\s\+]/); |
var pos=query.search(/[\s\+]/); |
343 |
|
if (index_name) index_path = index_name+'/'; |
344 |
|
|
345 |
if(pos < 0) |
if(pos < 0) |
346 |
{ |
{ |
352 |
query = query.slice(0,pos); |
query = query.slice(0,pos); |
353 |
} |
} |
354 |
|
|
355 |
if(!loadXML("0.xml", traverseTree, query.toLowerCase(), result_func)) |
if(!loadXML(index_path+"0.xml", traverseTree, query.toLowerCase(), result_func)) |
356 |
{ |
{ |
357 |
debug("ERROR: Couldn't find main index 0.xml"); |
debug("ERROR: Couldn't find main index 0.xml"); |
358 |
search_err = "INTERNAL ERROR: Unable to load main index 0.xml"; |
search_err = "INTERNAL ERROR: Unable to load main index 0.xml"; |