]> git.stg.codes - stg.git/blobdiff - doc/help/xslt/webhelp/template/content/search/nwSearchFnt.js
DocBook documentation stub added
[stg.git] / doc / help / xslt / webhelp / template / content / search / nwSearchFnt.js
diff --git a/doc/help/xslt/webhelp/template/content/search/nwSearchFnt.js b/doc/help/xslt/webhelp/template/content/search/nwSearchFnt.js
new file mode 100755 (executable)
index 0000000..0111559
--- /dev/null
@@ -0,0 +1,513 @@
+/*----------------------------------------------------------------------------\r
+ * JavaScript for webhelp search\r
+ *----------------------------------------------------------------------------\r
+ This file is part of the webhelpsearch plugin for DocBook WebHelp\r
+ Copyright (c) 2007-2008 NexWave Solutions All Rights Reserved.\r
+ www.nexwave.biz Nadege Quaine\r
+ http://kasunbg.blogspot.com/ Kasun Gajasinghe\r
+ */\r
+\r
+//string initialization\r
+var htmlfileList = "htmlFileList.js";\r
+var htmlfileinfoList = "htmlFileInfoList.js";\r
+var useCJKTokenizing = false;\r
+\r
+/* Cette fonction verifie la validite de la recherche entrre par l utilisateur */\r
+function Verifie(ditaSearch_Form) {\r
+\r
+    // Check browser compatibitily\r
+    if (navigator.userAgent.indexOf("Konquerer") > -1) {\r
+\r
+        alert(txt_browser_not_supported);\r
+        return;\r
+    }\r
+\r
+\r
+    var expressionInput = document.ditaSearch_Form.textToSearch.value;\r
+    //Set a cookie to store the searched keywords\r
+    $.cookie('textToSearch', expressionInput);\r
+\r
+\r
+    if (expressionInput.length < 1) {\r
+\r
+        // expression is invalid\r
+        alert(txt_enter_at_least_1_char);\r
+        // reactive la fenetre de search (utile car cadres)\r
+        document.ditaSearch_Form.textToSearch.focus();\r
+    }\r
+    else {\r
+\r
+        // Effectuer la recherche\r
+        Effectuer_recherche(expressionInput);\r
+\r
+        // reactive la fenetre de search (utile car cadres)\r
+        document.ditaSearch_Form.textToSearch.focus();\r
+    }\r
+}\r
+\r
+var stemQueryMap = new Array();  // A hashtable which maps stems to query words\r
+\r
+/* This function parses the search expression, loads the indices and displays the results*/\r
+function Effectuer_recherche(expressionInput) {\r
+\r
+    /* Display a waiting message */\r
+    //DisplayWaitingMessage();\r
+\r
+    /*data initialisation*/\r
+    var searchFor = "";       // expression en lowercase et sans les caracte    res speciaux\r
+    //w = new Object();  // hashtable, key=word, value = list of the index of the html files\r
+    scriptLetterTab = new Scriptfirstchar(); // Array containing the first letter of each word to look for\r
+    var wordsList = new Array(); // Array with the words to look for\r
+    var finalWordsList = new Array(); // Array with the words to look for after removing spaces\r
+    var linkTab = new Array();\r
+    var fileAndWordList = new Array();\r
+    var txt_wordsnotfound = "";\r
+\r
+\r
+    /*nqu: expressionInput, la recherche est lower cased, plus remplacement des char speciaux*/\r
+    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_");\r
+\r
+    searchFor = searchFor.replace(/  +/g, " ");\r
+    searchFor = searchFor.replace(/ $/, "").replace(/^ /, "");\r
+\r
+    wordsList = searchFor.split(" ");\r
+    wordsList.sort();\r
+\r
+    //set the tokenizing method\r
+    if(typeof indexerLanguage != "undefined" && (indexerLanguage=="zh" || indexerLanguage=="ja" ||indexerLanguage=="ko")){\r
+        useCJKTokenizing=true;\r
+    } else {\r
+        useCJKTokenizing=false;\r
+    }\r
+    //If Lucene CJKTokenizer was used as the indexer, then useCJKTokenizing will be true. Else, do normal tokenizing.\r
+    // 2-gram tokenizinghappens in CJKTokenizing,  \r
+    if(useCJKTokenizing){\r
+        finalWordsList = cjkTokenize(wordsList);\r
+    } else { \r
+        finalWordsList = tokenize(wordsList);\r
+    }\r
+\r
+    //load the scripts with the indices: the following lines do not work on the server. To be corrected\r
+    /*if (IEBrowser) {\r
+     scriptsarray = loadTheIndexScripts (scriptLetterTab);\r
+     } */\r
+\r
+    /**\r
+     * Compare with the indexed words (in the w[] array), and push words that are in it to tempTab.\r
+     */\r
+    var tempTab = new Array();\r
+    for (var t in finalWordsList) {\r
+        if (w[finalWordsList[t].toString()] == undefined) {\r
+            txt_wordsnotfound += finalWordsList[t] + " ";\r
+        } else {\r
+            tempTab.push(finalWordsList[t]);\r
+        }\r
+    }\r
+    finalWordsList = tempTab;\r
+\r
+    if (finalWordsList.length) {\r
+\r
+        //search 'and' and 'or' one time\r
+        fileAndWordList = SortResults(finalWordsList);\r
+\r
+        var cpt = fileAndWordList.length;\r
+        for (var i = cpt - 1; i >= 0; i--) {\r
+            if (fileAndWordList[i] != undefined) {\r
+                linkTab.push("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">" + fileAndWordList[i][0].motslisteDisplay + "</span>" + "</p>");\r
+\r
+                linkTab.push("<ul class='searchresult'>");\r
+                for (t in fileAndWordList[i]) {\r
+                    //DEBUG: alert(": "+ fileAndWordList[i][t].filenb+" " +fileAndWordList[i][t].motsliste);\r
+                    //linkTab.push("<li><a href=\"../"+fl[fileAndWordList[i][t].filenb]+"\">"+fl[fileAndWordList[i][t].filenb]+"</a></li>");\r
+                    var tempInfo = fil[fileAndWordList[i][t].filenb];\r
+                    var pos1 = tempInfo.indexOf("@@@");\r
+                    var pos2 = tempInfo.lastIndexOf("@@@");\r
+                    var tempPath = tempInfo.substring(0, pos1);\r
+                    var tempTitle = tempInfo.substring(pos1 + 3, pos2);\r
+                    var tempShortdesc = tempInfo.substring(pos2 + 3, tempInfo.length);\r
+\r
+                    //file:///home/kasun/docbook/WEBHELP/webhelp-draft-output-format-idea/src/main/resources/web/webhelp/installation.html\r
+                    var linkString = "<li><a href=" + tempPath + ">" + tempTitle + "</a>";\r
+                    // var linkString = "<li><a href=\"installation.html\">" + tempTitle + "</a>";\r
+                    if ((tempShortdesc != "null")) {\r
+                        linkString += "\n<div class=\"shortdesclink\">" + tempShortdesc + "</div>";\r
+                    }\r
+                    linkString += "</li>";\r
+                    linkTab.push(linkString);\r
+                }\r
+                linkTab.push("</ul>");\r
+            }\r
+        }\r
+    }\r
+\r
+    var results = "";\r
+    if (linkTab.length > 0) { \r
+        /*writeln ("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">"  + cleanwordsList + "</span>" + "<br/>"+"</p>");*/\r
+        results = "<p>";\r
+        //write("<ul class='searchresult'>");\r
+        for (t in linkTab) {\r
+            results += linkTab[t].toString();\r
+        }\r
+        results += "</p>";\r
+    } else {\r
+        results = "<p>" + "Your search returned no results for " + "<span class=\"searchExpression\">" + txt_wordsnotfound + "</span>" + "</p>";\r
+    }\r
+    //alert(results);\r
+    document.getElementById('searchResults').innerHTML = results; \r
+}\r
+\r
+function tokenize(wordsList){\r
+    var stemmedWordsList = new Array(); // Array with the words to look for after removing spaces\r
+    var cleanwordsList = new Array(); // Array with the words to look for\r
+    for(var j in wordsList){\r
+        var word = wordsList[j];\r
+        if(typeof stemmer != "undefined" ){\r
+            stemQueryMap[stemmer(word)] = word;\r
+        } else {\r
+            stemQueryMap[word] = word;\r
+        }\r
+    } \r
+     //stemmedWordsList is the stemmed list of words separated by spaces.\r
+    for (var t in wordsList) {\r
+        wordsList[t] = wordsList[t].replace(/(%22)|^-/g, "");\r
+        if (wordsList[t] != "%20") {\r
+            scriptLetterTab.add(wordsList[t].charAt(0));\r
+            cleanwordsList.push(wordsList[t]);\r
+        }\r
+    }\r
+\r
+    if(typeof stemmer != "undefined" ){\r
+        //Do the stemming using Porter's stemming algorithm\r
+        for (var i = 0; i < cleanwordsList.length; i++) {\r
+            var stemWord = stemmer(cleanwordsList[i]);\r
+            stemmedWordsList.push(stemWord);\r
+        }\r
+    } else {\r
+        stemmedWordsList = cleanwordsList;\r
+    }\r
+    return stemmedWordsList;\r
+}\r
+\r
+//Invoker of CJKTokenizer class methods.\r
+function cjkTokenize(wordsList){\r
+    var allTokens= new Array();\r
+    var notCJKTokens= new Array();\r
+    var j=0;\r
+    for(j=0;j<wordsList.length;j++){\r
+        var word = wordsList[j];\r
+        if(getAvgAsciiValue(word) < 127){\r
+            notCJKTokens.push(word);\r
+        } else { \r
+            var tokenizer = new CJKTokenizer(word);\r
+            var tokensTmp = tokenizer.getAllTokens();\r
+            allTokens = allTokens.concat(tokensTmp);\r
+        }\r
+    }\r
+    allTokens = allTokens.concat(tokenize(notCJKTokens));\r
+    return allTokens;\r
+}\r
+\r
+//A simple way to determine whether the query is in english or not.\r
+function getAvgAsciiValue(word){\r
+    var tmp = 0;\r
+    var num = word.length < 5 ? word.length:5;\r
+    for(var i=0;i<num;i++){\r
+        if(i==5) break;\r
+        tmp += word.charCodeAt(i);\r
+    }\r
+    return tmp/num;\r
+}\r
+\r
+//CJKTokenizer\r
+function CJKTokenizer(input){\r
+    this.input = input;\r
+    this.offset=-1;\r
+    this.tokens = new Array(); \r
+    this.incrementToken = incrementToken;\r
+    this.tokenize = tokenize;\r
+    this.getAllTokens = getAllTokens;\r
+    this.unique = unique;\r
+\r
+    function incrementToken(){\r
+               if(this.input.length - 2 <= this.offset){\r
+               //      console.log("false "+offset);\r
+                       return false;\r
+               }\r
+               else {\r
+                       this.offset+=1;\r
+                       return true;\r
+               }\r
+       }\r
+\r
+       function tokenize(){\r
+               //document.getElementById("content").innerHTML += x.substring(offset,offset+2)+"<br>";\r
+               return this.input.substring(this.offset,this.offset+2);\r
+       }\r
+\r
+       function getAllTokens(){\r
+               while(this.incrementToken()){\r
+                       var tmp = this.tokenize();\r
+                       this.tokens.push(tmp);\r
+               }\r
+        return this.unique(this.tokens);\r
+//             document.getElementById("content").innerHTML += tokens+" ";\r
+//             document.getElementById("content").innerHTML += "<br>dada"+sortedTokens+" ";\r
+//             console.log(tokens.length+"dsdsds");\r
+               /*for(i=0;i<tokens.length;i++){\r
+                       console.log(tokens[i]);\r
+                       var ss = tokens[i] == sortedTokens[i];\r
+\r
+//                     document.getElementById("content").innerHTML += "<br>dada"+un[i]+"- "+stems[i]+"&nbsp;&nbsp;&nbsp;"+ ss;\r
+                       document.getElementById("content").innerHTML += "<br>"+sortedTokens[i];\r
+               }*/\r
+       }\r
+\r
+       function unique(a)\r
+       {\r
+          var r = new Array();\r
+          o:for(var i = 0, n = a.length; i < n; i++)\r
+          {\r
+             for(var x = 0, y = r.length; x < y; x++)\r
+             {\r
+                if(r[x]==a[i]) continue o;\r
+             }\r
+             r[r.length] = a[i];\r
+          }\r
+          return r;\r
+       } \r
+}\r
+\r
+\r
+/* Scriptfirstchar: to gather the first letter of index js files to upload */\r
+function Scriptfirstchar() {\r
+    this.strLetters = "";\r
+    this.add = addLettre;\r
+}\r
+\r
+function addLettre(caract) {\r
+\r
+    if (this.strLetters == 'undefined') {\r
+        this.strLetters = caract;\r
+    } else if (this.strLetters.indexOf(caract) < 0) {\r
+        this.strLetters += caract;\r
+    }\r
+\r
+    return 0;\r
+}\r
+/* end of scriptfirstchar */\r
+\r
+/*main loader function*/\r
+/*tab contains the first letters of each word looked for*/\r
+function loadTheIndexScripts(tab) {\r
+\r
+    //alert (tab.strLetters);\r
+    var scriptsarray = new Array();\r
+\r
+    for (var i = 0; i < tab.strLetters.length; i++) {\r
+\r
+        scriptsarray[i] = "..\/search" + "\/" + tab.strLetters.charAt(i) + ".js";\r
+    }\r
+    // add the list of html files\r
+    i++;\r
+    scriptsarray[i] = "..\/search" + "\/" + htmlfileList;\r
+\r
+    //debug\r
+    for (var t in scriptsarray) {\r
+        //alert (scriptsarray[t]);\r
+    }\r
+\r
+    tab = new ScriptLoader();\r
+    for (t in scriptsarray) {\r
+        tab.add(scriptsarray[t]);\r
+    }\r
+    tab.load();\r
+    //alert ("scripts loaded");\r
+    return (scriptsarray);\r
+}\r
+\r
+/* ScriptLoader: to load the scripts and wait that it's finished */\r
+function ScriptLoader() {\r
+    this.cpt = 0;\r
+    this.scriptTab = new Array();\r
+    this.add = addAScriptInTheList;\r
+    this.load = loadTheScripts;\r
+    this.onScriptLoaded = onScriptLoadedFunc;\r
+}\r
+\r
+function addAScriptInTheList(scriptPath) {\r
+    this.scriptTab.push(scriptPath);\r
+}\r
+\r
+function loadTheScripts() {\r
+    var script;\r
+    var head;\r
+\r
+    head = document.getElementsByTagName('head').item(0);\r
+\r
+    //script = document.createElement('script');\r
+\r
+    for (var el in this.scriptTab) {\r
+        //alert (el+this.scriptTab[el]);\r
+        script = document.createElement('script');\r
+        script.src = this.scriptTab[el];\r
+        script.type = 'text/javascript';\r
+        script.defer = false;\r
+\r
+        head.appendChild(script);\r
+    }\r
+\r
+}\r
+\r
+function onScriptLoadedFunc(e) {\r
+    e = e || window.event;\r
+    var target = e.target || e.srcElement;\r
+    var isComplete = true;\r
+    if (typeof target.readyState != undefined) {\r
+\r
+        isComplete = (target.readyState == "complete" || target.readyState == "loaded");\r
+    }\r
+    if (isComplete) {\r
+        ScriptLoader.cpt++;\r
+        if (ScriptLoader.cpt == ScriptLoader.scripts.length) {\r
+            ScriptLoader.onLoadComplete();\r
+        }\r
+    }\r
+}\r
+\r
+/*\r
+function onLoadComplete() {\r
+    alert("loaded !!");\r
+} */\r
+\r
+/* End of scriptloader functions */\r
\r
+// Array.unique( strict ) - Remove duplicate values\r
+function unique(tab) {\r
+    var a = new Array();\r
+    var i;\r
+    var l = tab.length;\r
+\r
+    if (tab[0] != undefined) {\r
+        a[0] = tab[0];\r
+    }\r
+    else {\r
+        return -1\r
+    }\r
+\r
+    for (i = 1; i < l; i++) {\r
+        if (indexof(a, tab[i], 0) < 0) {\r
+            a.push(tab[i]);\r
+        }\r
+    }\r
+    return a;\r
+}\r
+function indexof(tab, element, begin) {\r
+    for (var i = begin; i < tab.length; i++) {\r
+        if (tab[i] == element) {\r
+            return i;\r
+        }\r
+    }\r
+    return -1;\r
+\r
+}\r
+/* end of Array functions */\r
+\r
+\r
+/*\r
+ Param: mots= list of words to look for.\r
+ This function creates an hashtable:\r
+ - The key is the index of a html file which contains a word to look for.\r
+ - The value is the list of all words contained in the html file.\r
+\r
+ Return value: the hashtable fileAndWordList\r
+ */\r
+function SortResults(mots) {\r
+\r
+    var fileAndWordList = new Object();\r
+    if (mots.length == 0) {\r
+        return null;\r
+    }\r
+\r
+    for (var t in mots) {\r
+        // get the list of the indices of the files.\r
+        var listNumerosDesFicStr = w[mots[t].toString()];\r
+        //alert ("listNumerosDesFicStr "+listNumerosDesFicStr);\r
+        var tab = listNumerosDesFicStr.split(",");\r
+\r
+        //for each file (file's index):\r
+        for (var t2 in tab) {\r
+            var temp = tab[t2].toString();\r
+            if (fileAndWordList[temp] == undefined) {\r
+\r
+                fileAndWordList[temp] = "" + mots[t];\r
+            } else {\r
+\r
+                fileAndWordList[temp] += "," + mots[t];\r
+            }\r
+        }\r
+    }\r
+\r
+    var fileAndWordListValuesOnly = new Array();\r
+\r
+    // sort results according to values\r
+    var temptab = new Array();\r
+    for (t in fileAndWordList) {\r
+        tab = fileAndWordList[t].split(',');\r
+\r
+        var tempDisplay = new Array();\r
+        for (var x in tab) {\r
+            if(stemQueryMap[tab[x]] != undefined){\r
+                tempDisplay.push(stemQueryMap[tab[x]]); //get the original word from the stem word.\r
+            } else {\r
+                tempDisplay.push(tab[x]); //no stem is available. (probably a CJK language)\r
+            }\r
+        }\r
+        var tempDispString = tempDisplay.join(", ");\r
+\r
+        temptab.push(new resultPerFile(t, fileAndWordList[t], tab.length, tempDispString));\r
+        fileAndWordListValuesOnly.push(fileAndWordList[t]);\r
+    }\r
+\r
+\r
+    //alert("t"+fileAndWordListValuesOnly.toString());\r
+\r
+    fileAndWordListValuesOnly = unique(fileAndWordListValuesOnly);\r
+    fileAndWordListValuesOnly = fileAndWordListValuesOnly.sort(compare_nbMots);\r
+    //alert("t: "+fileAndWordListValuesOnly.join(';'));\r
+\r
+    var listToOutput = new Array();\r
+\r
+    for (var j in fileAndWordListValuesOnly) {\r
+        for (t in temptab) {\r
+            if (temptab[t].motsliste == fileAndWordListValuesOnly[j]) {\r
+                if (listToOutput[j] == undefined) {\r
+                    listToOutput[j] = new Array(temptab[t]);\r
+                } else {\r
+                    listToOutput[j].push(temptab[t]);\r
+                }\r
+            }\r
+        }\r
+    }\r
+    return listToOutput;\r
+}\r
+\r
+function resultPerFile(filenb, motsliste, motsnb, motslisteDisplay) {\r
+    this.filenb = filenb;\r
+    this.motsliste = motsliste;\r
+    this.motsnb = motsnb;\r
+    this.motslisteDisplay= motslisteDisplay;\r
+}\r
+\r
+function compare_nbMots(s1, s2) {\r
+    var t1 = s1.split(',');\r
+    var t2 = s2.split(',');\r
+    //alert ("s1:"+t1.length + " " +t2.length)\r
+    if (t1.length == t2.length) {\r
+        return 0;\r
+    } else if (t1.length > t2.length) {\r
+        return 1;\r
+    } else {\r
+        return -1;\r
+    }\r
+    //return t1.length - t2.length);\r
+}
\ No newline at end of file