1
/*----------------------------------------------------------------------------
2
* JavaScript for webhelp search
3
*----------------------------------------------------------------------------
4
This file is part of the webhelpsearch plugin for DocBook WebHelp
5
Copyright (c) 2007-2008 NexWave Solutions All Rights Reserved.
6
www.nexwave.biz Nadege Quaine
7
http://kasunbg.blogspot.com/ Kasun Gajasinghe
10
//string initialization
11
var htmlfileList = "htmlFileList.js";
12
var htmlfileinfoList = "htmlFileInfoList.js";
13
var useCJKTokenizing = false;
15
/* Cette fonction verifie la validite de la recherche entrre par l utilisateur */
16
function Verifie(ditaSearch_Form) {
18
// Check browser compatibitily
19
if (navigator.userAgent.indexOf("Konquerer") > -1) {
21
alert(txt_browser_not_supported);
26
var expressionInput = document.ditaSearch_Form.textToSearch.value.replace(/,/g,'');
27
//Set a cookie to store the searched keywords
28
$.cookie('textToSearch', expressionInput);
31
if (expressionInput.length < 1) {
33
// expression is invalid
34
alert(txt_enter_at_least_1_char);
35
// reactive la fenetre de search (utile car cadres)
36
document.ditaSearch_Form.textToSearch.focus();
40
// Effectuer la recherche
41
Effectuer_recherche(expressionInput);
43
//Track with Google Analytics:
44
_gaq.push(['_trackEvent', 'Search', expressionInput, 'null', 1]);
47
// reactive la fenetre de search (utile car cadres)
48
document.ditaSearch_Form.textToSearch.focus();
52
var stemQueryMap = new Array(); // A hashtable which maps stems to query words
54
/* This function parses the search expression, loads the indices and displays the results*/
55
function Effectuer_recherche(expressionInput) {
57
/* Display a waiting message */
58
//DisplayWaitingMessage();
60
/*data initialisation*/
61
var searchFor = ""; // expression en lowercase et sans les caracte res speciaux
62
//w = new Object(); // hashtable, key=word, value = list of the index of the html files
63
scriptLetterTab = new Scriptfirstchar(); // Array containing the first letter of each word to look for
64
var wordsList = new Array(); // Array with the words to look for
65
var finalWordsList = new Array(); // Array with the words to look for after removing spaces
66
var linkTab = new Array();
67
var fileAndWordList = new Array();
68
var txt_wordsnotfound = "";
71
/*nqu: expressionInput, la recherche est lower cased, plus remplacement des char speciaux*/
72
searchFor = expressionInput.toLowerCase().replace(/<\//g, "_st_").replace(/\$_/g, "_di_").replace(/\.|%2C|%3B|%21|%3A|@|\/|\*/g, " ").replace(/(%20)+/g, " ").replace(/_st_/g, "</").replace(/_di_/g, "%24_");
74
searchFor = searchFor.replace(/ +/g, " ");
75
searchFor = searchFor.replace(/ $/, "").replace(/^ /, "");
77
wordsList = searchFor.split(" ");
80
//set the tokenizing method
81
if(typeof indexerLanguage != "undefined" && (indexerLanguage=="zh" || indexerLanguage=="ja" ||indexerLanguage=="ko")){
82
useCJKTokenizing=true;
84
useCJKTokenizing=false;
86
//If Lucene CJKTokenizer was used as the indexer, then useCJKTokenizing will be true. Else, do normal tokenizing.
87
// 2-gram tokenizinghappens in CJKTokenizing,
89
finalWordsList = cjkTokenize(wordsList);
91
finalWordsList = tokenize(wordsList);
94
//load the scripts with the indices: the following lines do not work on the server. To be corrected
96
scriptsarray = loadTheIndexScripts (scriptLetterTab);
100
* Compare with the indexed words (in the w[] array), and push words that are in it to tempTab.
102
var tempTab = new Array();
103
for (var t in finalWordsList) {
104
if (w[finalWordsList[t].toString()] == undefined) {
105
txt_wordsnotfound += finalWordsList[t] + " ";
107
tempTab.push(finalWordsList[t]);
110
finalWordsList = tempTab;
112
if (finalWordsList.length) {
114
//search 'and' and 'or' one time
115
fileAndWordList = SortResults(finalWordsList);
117
var cpt = fileAndWordList.length;
118
for (var i = cpt - 1; i >= 0; i--) {
119
if (fileAndWordList[i] != undefined) {
120
linkTab.push("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">" + fileAndWordList[i][0].motslisteDisplay + "</span>" + "</p>");
122
linkTab.push("<ul class='searchresult'>");
123
for (t in fileAndWordList[i]) {
124
//DEBUG: alert(": "+ fileAndWordList[i][t].filenb+" " +fileAndWordList[i][t].motsliste);
125
//linkTab.push("<li><a href=\"../"+fl[fileAndWordList[i][t].filenb]+"\">"+fl[fileAndWordList[i][t].filenb]+"</a></li>");
126
var tempInfo = fil[fileAndWordList[i][t].filenb];
127
var pos1 = tempInfo.indexOf("@@@");
128
var pos2 = tempInfo.lastIndexOf("@@@");
129
var tempPath = tempInfo.substring(0, pos1);
130
var tempTitle = tempInfo.substring(pos1 + 3, pos2);
131
var tempShortdesc = tempInfo.substring(pos2 + 3, tempInfo.length);
133
//file:///home/kasun/docbook/WEBHELP/webhelp-draft-output-format-idea/src/main/resources/web/webhelp/installation.html
134
var linkString = "<li><a href=" + tempPath + ">" + tempTitle + "</a>";
135
// var linkString = "<li><a href=\"installation.html\">" + tempTitle + "</a>";
136
if ((tempShortdesc != "null")) {
137
linkString += "\n<div class=\"shortdesclink\">" + tempShortdesc + "</div>";
139
linkString += "</li>";
140
linkTab.push(linkString);
142
linkTab.push("</ul>");
148
if (linkTab.length > 0) {
149
/*writeln ("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">" + cleanwordsList + "</span>" + "<br/>"+"</p>");*/
151
//write("<ul class='searchresult'>");
153
results += linkTab[t].toString();
157
results = "<p>" + "Your search returned no results for " + "<span class=\"searchExpression\">" + txt_wordsnotfound + "</span>" + "</p>";
160
document.getElementById('searchResults').innerHTML = results;
163
function tokenize(wordsList){
164
var stemmedWordsList = new Array(); // Array with the words to look for after removing spaces
165
var cleanwordsList = new Array(); // Array with the words to look for
166
for(var j in wordsList){
167
var word = wordsList[j];
168
if(typeof stemmer != "undefined" ){
169
stemQueryMap[stemmer(word)] = word;
171
stemQueryMap[word] = word;
174
//stemmedWordsList is the stemmed list of words separated by spaces.
175
for (var t in wordsList) {
176
wordsList[t] = wordsList[t].replace(/(%22)|^-/g, "");
177
if (wordsList[t] != "%20") {
178
scriptLetterTab.add(wordsList[t].charAt(0));
179
cleanwordsList.push(wordsList[t]);
183
if(typeof stemmer != "undefined" ){
184
//Do the stemming using Porter's stemming algorithm
185
for (var i = 0; i < cleanwordsList.length; i++) {
186
var stemWord = stemmer(cleanwordsList[i]);
187
stemmedWordsList.push(stemWord);
190
stemmedWordsList = cleanwordsList;
192
return stemmedWordsList;
195
//Invoker of CJKTokenizer class methods.
196
function cjkTokenize(wordsList){
197
var allTokens= new Array();
198
var notCJKTokens= new Array();
200
for(j=0;j<wordsList.length;j++){
201
var word = wordsList[j];
202
if(getAvgAsciiValue(word) < 127){
203
notCJKTokens.push(word);
205
var tokenizer = new CJKTokenizer(word);
206
var tokensTmp = tokenizer.getAllTokens();
207
allTokens = allTokens.concat(tokensTmp);
210
allTokens = allTokens.concat(tokenize(notCJKTokens));
214
//A simple way to determine whether the query is in english or not.
215
function getAvgAsciiValue(word){
217
var num = word.length < 5 ? word.length:5;
218
for(var i=0;i<num;i++){
220
tmp += word.charCodeAt(i);
226
function CJKTokenizer(input){
229
this.tokens = new Array();
230
this.incrementToken = incrementToken;
231
this.tokenize = tokenize;
232
this.getAllTokens = getAllTokens;
233
this.unique = unique;
235
function incrementToken(){
236
if(this.input.length - 2 <= this.offset){
237
// console.log("false "+offset);
247
//document.getElementById("content").innerHTML += x.substring(offset,offset+2)+"<br>";
248
return this.input.substring(this.offset,this.offset+2);
251
function getAllTokens(){
252
while(this.incrementToken()){
253
var tmp = this.tokenize();
254
this.tokens.push(tmp);
256
return this.unique(this.tokens);
257
// document.getElementById("content").innerHTML += tokens+" ";
258
// document.getElementById("content").innerHTML += "<br>dada"+sortedTokens+" ";
259
// console.log(tokens.length+"dsdsds");
260
/*for(i=0;i<tokens.length;i++){
261
console.log(tokens[i]);
262
var ss = tokens[i] == sortedTokens[i];
264
// document.getElementById("content").innerHTML += "<br>dada"+un[i]+"- "+stems[i]+" "+ ss;
265
document.getElementById("content").innerHTML += "<br>"+sortedTokens[i];
272
o:for(var i = 0, n = a.length; i < n; i++)
274
for(var x = 0, y = r.length; x < y; x++)
276
if(r[x]==a[i]) continue o;
285
/* Scriptfirstchar: to gather the first letter of index js files to upload */
286
function Scriptfirstchar() {
287
this.strLetters = "";
288
this.add = addLettre;
291
function addLettre(caract) {
293
if (this.strLetters == 'undefined') {
294
this.strLetters = caract;
295
} else if (this.strLetters.indexOf(caract) < 0) {
296
this.strLetters += caract;
301
/* end of scriptfirstchar */
303
/*main loader function*/
304
/*tab contains the first letters of each word looked for*/
305
function loadTheIndexScripts(tab) {
307
//alert (tab.strLetters);
308
var scriptsarray = new Array();
310
for (var i = 0; i < tab.strLetters.length; i++) {
312
scriptsarray[i] = "..\/search" + "\/" + tab.strLetters.charAt(i) + ".js";
314
// add the list of html files
316
scriptsarray[i] = "..\/search" + "\/" + htmlfileList;
319
for (var t in scriptsarray) {
320
//alert (scriptsarray[t]);
323
tab = new ScriptLoader();
324
for (t in scriptsarray) {
325
tab.add(scriptsarray[t]);
328
//alert ("scripts loaded");
329
return (scriptsarray);
332
/* ScriptLoader: to load the scripts and wait that it's finished */
333
function ScriptLoader() {
335
this.scriptTab = new Array();
336
this.add = addAScriptInTheList;
337
this.load = loadTheScripts;
338
this.onScriptLoaded = onScriptLoadedFunc;
341
function addAScriptInTheList(scriptPath) {
342
this.scriptTab.push(scriptPath);
345
function loadTheScripts() {
349
head = document.getElementsByTagName('head').item(0);
351
//script = document.createElement('script');
353
for (var el in this.scriptTab) {
354
//alert (el+this.scriptTab[el]);
355
script = document.createElement('script');
356
script.src = this.scriptTab[el];
357
script.type = 'text/javascript';
358
script.defer = false;
360
head.appendChild(script);
365
function onScriptLoadedFunc(e) {
366
e = e || window.event;
367
var target = e.target || e.srcElement;
368
var isComplete = true;
369
if (typeof target.readyState != undefined) {
371
isComplete = (target.readyState == "complete" || target.readyState == "loaded");
375
if (ScriptLoader.cpt == ScriptLoader.scripts.length) {
376
ScriptLoader.onLoadComplete();
382
function onLoadComplete() {
386
/* End of scriptloader functions */
388
// Array.unique( strict ) - Remove duplicate values
389
function unique(tab) {
394
if (tab[0] != undefined) {
401
for (i = 1; i < l; i++) {
402
if (indexof(a, tab[i], 0) < 0) {
408
function indexof(tab, element, begin) {
409
for (var i = begin; i < tab.length; i++) {
410
if (tab[i] == element) {
417
/* end of Array functions */
421
Param: mots= list of words to look for.
422
This function creates an hashtable:
423
- The key is the index of a html file which contains a word to look for.
424
- The value is the list of all words contained in the html file.
426
Return value: the hashtable fileAndWordList
428
function SortResults(mots) {
430
var fileAndWordList = new Object();
431
if (mots.length == 0) {
435
for (var t in mots) {
436
// get the list of the indices of the files.
437
var listNumerosDesFicStr = w[mots[t].toString()];
438
//alert ("listNumerosDesFicStr "+listNumerosDesFicStr);
439
var tab = listNumerosDesFicStr.split(",");
441
//for each file (file's index):
442
for (var t2 in tab) {
443
var temp = tab[t2].toString();
444
if (fileAndWordList[temp] == undefined) {
446
fileAndWordList[temp] = "" + mots[t];
449
fileAndWordList[temp] += "," + mots[t];
454
var fileAndWordListValuesOnly = new Array();
456
// sort results according to values
457
var temptab = new Array();
458
for (t in fileAndWordList) {
459
tab = fileAndWordList[t].split(',');
461
var tempDisplay = new Array();
463
if(stemQueryMap[tab[x]] != undefined){
464
tempDisplay.push(stemQueryMap[tab[x]]); //get the original word from the stem word.
466
tempDisplay.push(tab[x]); //no stem is available. (probably a CJK language)
469
var tempDispString = tempDisplay.join(", ");
471
temptab.push(new resultPerFile(t, fileAndWordList[t], tab.length, tempDispString));
472
fileAndWordListValuesOnly.push(fileAndWordList[t]);
476
//alert("t"+fileAndWordListValuesOnly.toString());
478
fileAndWordListValuesOnly = unique(fileAndWordListValuesOnly);
479
fileAndWordListValuesOnly = fileAndWordListValuesOnly.sort(compare_nbMots);
480
//alert("t: "+fileAndWordListValuesOnly.join(';'));
482
var listToOutput = new Array();
484
for (var j in fileAndWordListValuesOnly) {
486
if (temptab[t].motsliste == fileAndWordListValuesOnly[j]) {
487
if (listToOutput[j] == undefined) {
488
listToOutput[j] = new Array(temptab[t]);
490
listToOutput[j].push(temptab[t]);
498
function resultPerFile(filenb, motsliste, motsnb, motslisteDisplay) {
499
this.filenb = filenb;
500
this.motsliste = motsliste;
501
this.motsnb = motsnb;
502
this.motslisteDisplay= motslisteDisplay;
505
function compare_nbMots(s1, s2) {
506
var t1 = s1.split(',');
507
var t2 = s2.split(',');
508
//alert ("s1:"+t1.length + " " +t2.length)
509
if (t1.length == t2.length) {
511
} else if (t1.length > t2.length) {
516
//return t1.length - t2.length);
b'\\ No newline at end of file'