~yogesh-srikrishnan/openstack-manuals/yogirackspace

« back to all changes in this revision

Viewing changes to doc/source/docbkx/incubation/keystone/devguide/target/docbkx/webhelp/identity-client-devguide/content/search/nwSearchFnt.js

  • Committer: Ziad Sawalha
  • Date: 2011-08-23 04:44:33 UTC
  • Revision ID: ziad@sawalha.com-20110823044433-s0ftekvrnoptr692
Doc updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
8
 
 */
9
 
 
10
 
//string initialization
11
 
var htmlfileList = "htmlFileList.js";
12
 
var htmlfileinfoList = "htmlFileInfoList.js";
13
 
var useCJKTokenizing = false;
14
 
 
15
 
/* Cette fonction verifie la validite de la recherche entrre par l utilisateur */
16
 
function Verifie(ditaSearch_Form) {
17
 
 
18
 
    // Check browser compatibitily
19
 
    if (navigator.userAgent.indexOf("Konquerer") > -1) {
20
 
 
21
 
        alert(txt_browser_not_supported);
22
 
        return;
23
 
    }
24
 
 
25
 
 
26
 
    var expressionInput = document.ditaSearch_Form.textToSearch.value.replace(/,/g,'');
27
 
    //Set a cookie to store the searched keywords
28
 
    $.cookie('textToSearch', expressionInput);
29
 
 
30
 
 
31
 
    if (expressionInput.length < 1) {
32
 
 
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();
37
 
    }
38
 
    else {
39
 
 
40
 
        // Effectuer la recherche
41
 
        Effectuer_recherche(expressionInput);
42
 
 
43
 
        //Track with Google Analytics:
44
 
        _gaq.push(['_trackEvent', 'Search', expressionInput, 'null', 1]);
45
 
 
46
 
 
47
 
        // reactive la fenetre de search (utile car cadres)
48
 
        document.ditaSearch_Form.textToSearch.focus();
49
 
    }
50
 
}
51
 
 
52
 
var stemQueryMap = new Array();  // A hashtable which maps stems to query words
53
 
 
54
 
/* This function parses the search expression, loads the indices and displays the results*/
55
 
function Effectuer_recherche(expressionInput) {
56
 
 
57
 
    /* Display a waiting message */
58
 
    //DisplayWaitingMessage();
59
 
 
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 = "";
69
 
 
70
 
 
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_");
73
 
 
74
 
    searchFor = searchFor.replace(/  +/g, " ");
75
 
    searchFor = searchFor.replace(/ $/, "").replace(/^ /, "");
76
 
 
77
 
    wordsList = searchFor.split(" ");
78
 
    wordsList.sort();
79
 
 
80
 
    //set the tokenizing method
81
 
    if(typeof indexerLanguage != "undefined" && (indexerLanguage=="zh" || indexerLanguage=="ja" ||indexerLanguage=="ko")){
82
 
        useCJKTokenizing=true;
83
 
    } else {
84
 
        useCJKTokenizing=false;
85
 
    }
86
 
    //If Lucene CJKTokenizer was used as the indexer, then useCJKTokenizing will be true. Else, do normal tokenizing.
87
 
    // 2-gram tokenizinghappens in CJKTokenizing,  
88
 
    if(useCJKTokenizing){
89
 
        finalWordsList = cjkTokenize(wordsList);
90
 
    } else { 
91
 
        finalWordsList = tokenize(wordsList);
92
 
    }
93
 
 
94
 
    //load the scripts with the indices: the following lines do not work on the server. To be corrected
95
 
    /*if (IEBrowser) {
96
 
     scriptsarray = loadTheIndexScripts (scriptLetterTab);
97
 
     } */
98
 
 
99
 
    /**
100
 
     * Compare with the indexed words (in the w[] array), and push words that are in it to tempTab.
101
 
     */
102
 
    var tempTab = new Array();
103
 
    for (var t in finalWordsList) {
104
 
        if (w[finalWordsList[t].toString()] == undefined) {
105
 
            txt_wordsnotfound += finalWordsList[t] + " ";
106
 
        } else {
107
 
            tempTab.push(finalWordsList[t]);
108
 
        }
109
 
    }
110
 
    finalWordsList = tempTab;
111
 
 
112
 
    if (finalWordsList.length) {
113
 
 
114
 
        //search 'and' and 'or' one time
115
 
        fileAndWordList = SortResults(finalWordsList);
116
 
 
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>");
121
 
 
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);
132
 
 
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>";
138
 
                    }
139
 
                    linkString += "</li>";
140
 
                    linkTab.push(linkString);
141
 
                }
142
 
                linkTab.push("</ul>");
143
 
            }
144
 
        }
145
 
    }
146
 
 
147
 
    var results = "";
148
 
    if (linkTab.length > 0) { 
149
 
        /*writeln ("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">"  + cleanwordsList + "</span>" + "<br/>"+"</p>");*/
150
 
        results = "<p>";
151
 
        //write("<ul class='searchresult'>");
152
 
        for (t in linkTab) {
153
 
            results += linkTab[t].toString();
154
 
        }
155
 
        results += "</p>";
156
 
    } else {
157
 
        results = "<p>" + "Your search returned no results for " + "<span class=\"searchExpression\">" + txt_wordsnotfound + "</span>" + "</p>";
158
 
    }
159
 
    //alert(results);
160
 
    document.getElementById('searchResults').innerHTML = results; 
161
 
}
162
 
 
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;
170
 
        } else {
171
 
            stemQueryMap[word] = word;
172
 
        }
173
 
    } 
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]);
180
 
        }
181
 
    }
182
 
 
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);
188
 
        }
189
 
    } else {
190
 
        stemmedWordsList = cleanwordsList;
191
 
    }
192
 
    return stemmedWordsList;
193
 
}
194
 
 
195
 
//Invoker of CJKTokenizer class methods.
196
 
function cjkTokenize(wordsList){
197
 
    var allTokens= new Array();
198
 
    var notCJKTokens= new Array();
199
 
    var j=0;
200
 
    for(j=0;j<wordsList.length;j++){
201
 
        var word = wordsList[j];
202
 
        if(getAvgAsciiValue(word) < 127){
203
 
            notCJKTokens.push(word);
204
 
        } else { 
205
 
            var tokenizer = new CJKTokenizer(word);
206
 
            var tokensTmp = tokenizer.getAllTokens();
207
 
            allTokens = allTokens.concat(tokensTmp);
208
 
        }
209
 
    }
210
 
    allTokens = allTokens.concat(tokenize(notCJKTokens));
211
 
    return allTokens;
212
 
}
213
 
 
214
 
//A simple way to determine whether the query is in english or not.
215
 
function getAvgAsciiValue(word){
216
 
    var tmp = 0;
217
 
    var num = word.length < 5 ? word.length:5;
218
 
    for(var i=0;i<num;i++){
219
 
        if(i==5) break;
220
 
        tmp += word.charCodeAt(i);
221
 
    }
222
 
    return tmp/num;
223
 
}
224
 
 
225
 
//CJKTokenizer
226
 
function CJKTokenizer(input){
227
 
    this.input = input;
228
 
    this.offset=-1;
229
 
    this.tokens = new Array(); 
230
 
    this.incrementToken = incrementToken;
231
 
    this.tokenize = tokenize;
232
 
    this.getAllTokens = getAllTokens;
233
 
    this.unique = unique;
234
 
 
235
 
    function incrementToken(){
236
 
                if(this.input.length - 2 <= this.offset){
237
 
                //      console.log("false "+offset);
238
 
                        return false;
239
 
                }
240
 
                else {
241
 
                        this.offset+=1;
242
 
                        return true;
243
 
                }
244
 
        }
245
 
 
246
 
        function tokenize(){
247
 
                //document.getElementById("content").innerHTML += x.substring(offset,offset+2)+"<br>";
248
 
                return this.input.substring(this.offset,this.offset+2);
249
 
        }
250
 
 
251
 
        function getAllTokens(){
252
 
                while(this.incrementToken()){
253
 
                        var tmp = this.tokenize();
254
 
                        this.tokens.push(tmp);
255
 
                }
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];
263
 
 
264
 
//                      document.getElementById("content").innerHTML += "<br>dada"+un[i]+"- "+stems[i]+"&nbsp;&nbsp;&nbsp;"+ ss;
265
 
                        document.getElementById("content").innerHTML += "<br>"+sortedTokens[i];
266
 
                }*/
267
 
        }
268
 
 
269
 
        function unique(a)
270
 
        {
271
 
           var r = new Array();
272
 
           o:for(var i = 0, n = a.length; i < n; i++)
273
 
           {
274
 
              for(var x = 0, y = r.length; x < y; x++)
275
 
              {
276
 
                 if(r[x]==a[i]) continue o;
277
 
              }
278
 
              r[r.length] = a[i];
279
 
           }
280
 
           return r;
281
 
        } 
282
 
}
283
 
 
284
 
 
285
 
/* Scriptfirstchar: to gather the first letter of index js files to upload */
286
 
function Scriptfirstchar() {
287
 
    this.strLetters = "";
288
 
    this.add = addLettre;
289
 
}
290
 
 
291
 
function addLettre(caract) {
292
 
 
293
 
    if (this.strLetters == 'undefined') {
294
 
        this.strLetters = caract;
295
 
    } else if (this.strLetters.indexOf(caract) < 0) {
296
 
        this.strLetters += caract;
297
 
    }
298
 
 
299
 
    return 0;
300
 
}
301
 
/* end of scriptfirstchar */
302
 
 
303
 
/*main loader function*/
304
 
/*tab contains the first letters of each word looked for*/
305
 
function loadTheIndexScripts(tab) {
306
 
 
307
 
    //alert (tab.strLetters);
308
 
    var scriptsarray = new Array();
309
 
 
310
 
    for (var i = 0; i < tab.strLetters.length; i++) {
311
 
 
312
 
        scriptsarray[i] = "..\/search" + "\/" + tab.strLetters.charAt(i) + ".js";
313
 
    }
314
 
    // add the list of html files
315
 
    i++;
316
 
    scriptsarray[i] = "..\/search" + "\/" + htmlfileList;
317
 
 
318
 
    //debug
319
 
    for (var t in scriptsarray) {
320
 
        //alert (scriptsarray[t]);
321
 
    }
322
 
 
323
 
    tab = new ScriptLoader();
324
 
    for (t in scriptsarray) {
325
 
        tab.add(scriptsarray[t]);
326
 
    }
327
 
    tab.load();
328
 
    //alert ("scripts loaded");
329
 
    return (scriptsarray);
330
 
}
331
 
 
332
 
/* ScriptLoader: to load the scripts and wait that it's finished */
333
 
function ScriptLoader() {
334
 
    this.cpt = 0;
335
 
    this.scriptTab = new Array();
336
 
    this.add = addAScriptInTheList;
337
 
    this.load = loadTheScripts;
338
 
    this.onScriptLoaded = onScriptLoadedFunc;
339
 
}
340
 
 
341
 
function addAScriptInTheList(scriptPath) {
342
 
    this.scriptTab.push(scriptPath);
343
 
}
344
 
 
345
 
function loadTheScripts() {
346
 
    var script;
347
 
    var head;
348
 
 
349
 
    head = document.getElementsByTagName('head').item(0);
350
 
 
351
 
    //script = document.createElement('script');
352
 
 
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;
359
 
 
360
 
        head.appendChild(script);
361
 
    }
362
 
 
363
 
}
364
 
 
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) {
370
 
 
371
 
        isComplete = (target.readyState == "complete" || target.readyState == "loaded");
372
 
    }
373
 
    if (isComplete) {
374
 
        ScriptLoader.cpt++;
375
 
        if (ScriptLoader.cpt == ScriptLoader.scripts.length) {
376
 
            ScriptLoader.onLoadComplete();
377
 
        }
378
 
    }
379
 
}
380
 
 
381
 
/*
382
 
function onLoadComplete() {
383
 
    alert("loaded !!");
384
 
} */
385
 
 
386
 
/* End of scriptloader functions */
387
 
 
388
 
// Array.unique( strict ) - Remove duplicate values
389
 
function unique(tab) {
390
 
    var a = new Array();
391
 
    var i;
392
 
    var l = tab.length;
393
 
 
394
 
    if (tab[0] != undefined) {
395
 
        a[0] = tab[0];
396
 
    }
397
 
    else {
398
 
        return -1
399
 
    }
400
 
 
401
 
    for (i = 1; i < l; i++) {
402
 
        if (indexof(a, tab[i], 0) < 0) {
403
 
            a.push(tab[i]);
404
 
        }
405
 
    }
406
 
    return a;
407
 
}
408
 
function indexof(tab, element, begin) {
409
 
    for (var i = begin; i < tab.length; i++) {
410
 
        if (tab[i] == element) {
411
 
            return i;
412
 
        }
413
 
    }
414
 
    return -1;
415
 
 
416
 
}
417
 
/* end of Array functions */
418
 
 
419
 
 
420
 
/*
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.
425
 
 
426
 
 Return value: the hashtable fileAndWordList
427
 
 */
428
 
function SortResults(mots) {
429
 
 
430
 
    var fileAndWordList = new Object();
431
 
    if (mots.length == 0) {
432
 
        return null;
433
 
    }
434
 
 
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(",");
440
 
 
441
 
        //for each file (file's index):
442
 
        for (var t2 in tab) {
443
 
            var temp = tab[t2].toString();
444
 
            if (fileAndWordList[temp] == undefined) {
445
 
 
446
 
                fileAndWordList[temp] = "" + mots[t];
447
 
            } else {
448
 
 
449
 
                fileAndWordList[temp] += "," + mots[t];
450
 
            }
451
 
        }
452
 
    }
453
 
 
454
 
    var fileAndWordListValuesOnly = new Array();
455
 
 
456
 
    // sort results according to values
457
 
    var temptab = new Array();
458
 
    for (t in fileAndWordList) {
459
 
        tab = fileAndWordList[t].split(',');
460
 
 
461
 
        var tempDisplay = new Array();
462
 
        for (var x in tab) {
463
 
            if(stemQueryMap[tab[x]] != undefined){
464
 
                tempDisplay.push(stemQueryMap[tab[x]]); //get the original word from the stem word.
465
 
            } else {
466
 
                tempDisplay.push(tab[x]); //no stem is available. (probably a CJK language)
467
 
            }
468
 
        }
469
 
        var tempDispString = tempDisplay.join(", ");
470
 
 
471
 
        temptab.push(new resultPerFile(t, fileAndWordList[t], tab.length, tempDispString));
472
 
        fileAndWordListValuesOnly.push(fileAndWordList[t]);
473
 
    }
474
 
 
475
 
 
476
 
    //alert("t"+fileAndWordListValuesOnly.toString());
477
 
 
478
 
    fileAndWordListValuesOnly = unique(fileAndWordListValuesOnly);
479
 
    fileAndWordListValuesOnly = fileAndWordListValuesOnly.sort(compare_nbMots);
480
 
    //alert("t: "+fileAndWordListValuesOnly.join(';'));
481
 
 
482
 
    var listToOutput = new Array();
483
 
 
484
 
    for (var j in fileAndWordListValuesOnly) {
485
 
        for (t in temptab) {
486
 
            if (temptab[t].motsliste == fileAndWordListValuesOnly[j]) {
487
 
                if (listToOutput[j] == undefined) {
488
 
                    listToOutput[j] = new Array(temptab[t]);
489
 
                } else {
490
 
                    listToOutput[j].push(temptab[t]);
491
 
                }
492
 
            }
493
 
        }
494
 
    }
495
 
    return listToOutput;
496
 
}
497
 
 
498
 
function resultPerFile(filenb, motsliste, motsnb, motslisteDisplay) {
499
 
    this.filenb = filenb;
500
 
    this.motsliste = motsliste;
501
 
    this.motsnb = motsnb;
502
 
    this.motslisteDisplay= motslisteDisplay;
503
 
}
504
 
 
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) {
510
 
        return 0;
511
 
    } else if (t1.length > t2.length) {
512
 
        return 1;
513
 
    } else {
514
 
        return -1;
515
 
    }
516
 
    //return t1.length - t2.length);
517
 
}
 
 
b'\\ No newline at end of file'