nwSearchFnt.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. //string initialization
  10. var htmlfileList = "htmlFileInfoList.js";
  11. var htmlfileinfoList = "htmlFileInfoList.js";
  12. var useCJKTokenizing = false;
  13. var w = new Object();
  14. var scoring = new Object();
  15. var searchTextField = '';
  16. var no = 0;
  17. var noWords = 0;
  18. var partialSearch = "<font class=\"highlightText\">There is no page containing all the search terms.<br>Partial results:</font>";
  19. var warningMsg = '<div style="padding: 5px;margin-right:5px;;background-color:#FFFF00;">';
  20. warningMsg+='<b>Please note that due to security settings, Google Chrome does not highlight';
  21. warningMsg+=' the search results in the right frame.</b><br>';
  22. warningMsg+='This happens only when the WebHelp files are loaded from the local file system.<br>';
  23. warningMsg+='Workarounds:';
  24. warningMsg+='<ul>';
  25. warningMsg+='<li>Try using another web browser.</li>';
  26. warningMsg+='<li>Deploy the WebHelp files on a web server.</li>';
  27. warningMsg+='</div>';
  28. txt_filesfound = 'Results';
  29. txt_enter_at_least_1_char = "You must enter at least one character.";
  30. txt_enter_more_than_10_words = "Only first 10 words will be processed.";
  31. txt_browser_not_supported = "Your browser is not supported. Use of Mozilla Firefox is recommended.";
  32. txt_please_wait = "Please wait. Search in progress...";
  33. txt_results_for = "Results for: ";
  34. /* This function verify the validity of search input by the user
  35. Cette fonction verifie la validite de la recherche entrre par l utilisateur */
  36. function Verifie(searchForm) {
  37. // Check browser compatibility
  38. if (navigator.userAgent.indexOf("Konquerer") > -1) {
  39. alert(txt_browser_not_supported);
  40. return;
  41. }
  42. searchTextField = trim(document.searchForm.textToSearch.value);
  43. searchTextField = searchTextField.replace(/['"]/g,'');
  44. var expressionInput = searchTextField;
  45. $.cookie('textToSearch', expressionInput);
  46. if (expressionInput.length < 1) {
  47. // expression is invalid
  48. alert(txt_enter_at_least_1_char);
  49. // reactive la fenetre de search (utile car cadres)
  50. document.searchForm.textToSearch.focus();
  51. }
  52. else {
  53. var splitSpace = searchTextField.split(" ");
  54. var splitWords = [];
  55. for (var i = 0 ; i < splitSpace.length ; i++) {
  56. var splitDot = splitSpace[i].split(".");
  57. if(!(splitDot.length == 1)){
  58. splitWords.push(splitSpace[i]);
  59. }
  60. for (var i1 = 0; i1 < splitDot.length; i1++) {
  61. var splitColon = splitDot[i1].split(":");
  62. for (var i2 = 0; i2 < splitColon.length; i2++) {
  63. var splitDash = splitColon[i2].split("-");
  64. for (var i3 = 0; i3 < splitDash.length; i3++) {
  65. if (splitDash[i3].split("").length > 0) {
  66. splitWords.push(splitDash[i3]);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. noWords = splitWords;
  73. if (noWords.length > 9){
  74. // Allow to search maximum 10 words
  75. alert(txt_enter_more_than_10_words);
  76. expressionInput = '';
  77. for (var x = 0 ; x < 10 ; x++){
  78. expressionInput = expressionInput + " " + noWords[x];
  79. }
  80. Effectuer_recherche(expressionInput);
  81. document.searchForm.textToSearch.focus();
  82. } else {
  83. // Effectuer la recherche
  84. expressionInput = '';
  85. for (var x = 0 ; x < noWords.length ; x++) {
  86. expressionInput = expressionInput + " " + noWords[x];
  87. }
  88. Effectuer_recherche(expressionInput);
  89. // reactive la fenetre de search (utile car cadres)
  90. document.searchForm.textToSearch.focus();
  91. }
  92. }
  93. }
  94. var stemQueryMap = new Array(); // A hashtable which maps stems to query words
  95. /* This function parses the search expression, loads the indices and displays the results*/
  96. function Effectuer_recherche(expressionInput) {
  97. /* Display a waiting message */
  98. //DisplayWaitingMessage();
  99. /*data initialisation*/
  100. var searchFor = ""; // expression en lowercase et sans les caracte res speciaux
  101. //w = new Object(); // hashtable, key=word, value = list of the index of the html files
  102. scriptLetterTab = new Scriptfirstchar(); // Array containing the first letter of each word to look for
  103. var wordsList = new Array(); // Array with the words to look for
  104. var finalWordsList = new Array(); // Array with the words to look for after removing spaces
  105. var linkTab = new Array();
  106. var fileAndWordList = new Array();
  107. var txt_wordsnotfound = "";
  108. // --------------------------------------
  109. // Begin Thu's patch
  110. /*nqu: expressionInput, la recherche est lower cased, plus remplacement des char speciaux*/
  111. //The original replacement expression is:
  112. //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_");
  113. //The above expression was error prone because it did not deal with words that have a . as part of the word correctly, for example, document.txt
  114. //Do not automatically replace a . with a space
  115. 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_");
  116. //If it ends with a period, replace it with a space
  117. searchFor = searchFor.replace(/[.]$/,"");
  118. // End Thu's Patch
  119. // ------------------------------------------
  120. searchFor = searchFor.replace(/ +/g, " ");
  121. searchFor = searchFor.replace(/ $/, "").replace(/^ /, "");
  122. wordsList = searchFor.split(" ");
  123. wordsList.sort();
  124. //set the tokenizing method
  125. useCJKTokenizing = typeof indexerLanguage != "undefined" && (indexerLanguage == "zh" || indexerLanguage == "ja" || indexerLanguage == "ko");
  126. //If Lucene CJKTokenizer was used as the indexer, then useCJKTokenizing will be true. Else, do normal tokenizing.
  127. // 2-gram tokenizinghappens in CJKTokenizing,
  128. //If doStem then make tokenize with Stemmer
  129. var finalArray;
  130. if (doStem){
  131. if(useCJKTokenizing){
  132. finalWordsList = cjkTokenize(wordsList);
  133. finalArray = finalWordsList;
  134. } else {
  135. finalWordsList = tokenize(wordsList);
  136. finalArray = finalWordsList;
  137. }
  138. } else if(useCJKTokenizing){
  139. finalWordsList = cjkTokenize(wordsList);
  140. finalArray = finalWordsList;
  141. } else{
  142. //load the scripts with the indices: the following lines do not work on the server. To be corrected
  143. /*if (IEBrowser) {
  144. scriptsarray = loadTheIndexScripts (scriptLetterTab);
  145. } */
  146. /**
  147. * Compare with the indexed words (in the w[] array), and push words that are in it to tempTab.
  148. */
  149. var tempTab = new Array();
  150. // ---------------------------------------
  151. // Thu's patch
  152. //Do not use associative array in for loop, for example:
  153. //for(var t in finalWordsList)
  154. //it causes errors when finalWordList contains
  155. //stemmed words such as: kei from the stemmed word: key
  156. for(var t=0;t<finalWordsList.length;++t){
  157. var aWord=finalWordsList[t];
  158. //w is a Map like Object, use the current word in finalWordList as the key
  159. if(w[aWord] == undefined){
  160. txt_wordsnotfound += aWord + " ";
  161. }
  162. else{
  163. tempTab.push(aWord);
  164. }
  165. }
  166. finalWordsList = tempTab;
  167. //Check all the inputs to see if the root words are in the finalWordsList, if not add them there
  168. var inputs = expressionInput.split(' ');
  169. // Thu's Patch
  170. // -------------------------------------------
  171. txt_wordsnotfound = expressionInput;
  172. finalWordsList = removeDuplicate(finalWordsList);
  173. }
  174. if (finalWordsList.length) {
  175. //search 'and' and 'or' one time
  176. fileAndWordList = SortResults(finalWordsList);
  177. if (fileAndWordList == undefined){
  178. var cpt = 0;
  179. } else {
  180. var cpt = fileAndWordList.length;
  181. var maxNumberOfWords = fileAndWordList[0][0].motsnb;
  182. }
  183. if (cpt > 0){
  184. var searchedWords = noWords.length;
  185. var foundedWords = fileAndWordList[0][0].motslisteDisplay.split(",").length;
  186. //console.info("search : " + noWords.length + " found : " + fileAndWordList[0][0].motslisteDisplay.split(",").length);
  187. if (searchedWords != foundedWords){
  188. linkTab.push(partialSearch);
  189. }
  190. }
  191. for (var i = 0; i < cpt; i++) {
  192. var hundredProcent = fileAndWordList[i][0].scoring + 100 * fileAndWordList[i][0].motsnb;
  193. var ttScore_first = fileAndWordList[i][0].scoring;
  194. var numberOfWords = fileAndWordList[i][0].motsnb;
  195. if (fileAndWordList[i] != undefined) {
  196. linkTab.push("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">" + fileAndWordList[i][0].motslisteDisplay + "</span>" + "</p>");
  197. linkTab.push("<ul class='searchresult'>");
  198. for (t in fileAndWordList[i]) {
  199. //linkTab.push("<li><a href=\"../"+fl[fileAndWordList[i][t].filenb]+"\">"+fl[fileAndWordList[i][t].filenb]+"</a></li>");
  200. var ttInfo = fileAndWordList[i][t].filenb;
  201. // Get scoring
  202. var ttScore = fileAndWordList[i][t].scoring;
  203. var tempInfo = fil[ttInfo];
  204. var pos1 = tempInfo.indexOf("@@@");
  205. var pos2 = tempInfo.lastIndexOf("@@@");
  206. var tempPath = tempInfo.substring(0, pos1);
  207. var tempTitle = tempInfo.substring(pos1 + 3, pos2);
  208. var tempShortdesc = tempInfo.substring(pos2 + 3, tempInfo.length);
  209. // toc.html will not be displayed on search result
  210. if (tempPath == 'toc.html'){
  211. continue;
  212. }
  213. /*
  214. //file:///home/kasun/docbook/WEBHELP/webhelp-draft-output-format-idea/src/main/resources/web/webhelp/installation.html
  215. var linkString = "<li><a href=" + tempPath + ">" + tempTitle + "</a>";
  216. // var linkString = "<li><a href=\"installation.html\">" + tempTitle + "</a>";
  217. */
  218. var split = fileAndWordList[i][t].motsliste.split(",");
  219. // var splitedValues = expressionInput.split(" ");
  220. // var finalArray = split.concat(splitedValues);
  221. arrayString = 'Array(';
  222. for(var x in finalArray){
  223. if (finalArray[x].length > 2 || useCJKTokenizing){
  224. arrayString+= "'" + finalArray[x] + "',";
  225. }
  226. }
  227. arrayString = arrayString.substring(0,arrayString.length - 1) + ")";
  228. var idLink = 'foundLink' + no;
  229. var linkString = '<li><a id="' + idLink + '" href="' + tempPath + '" class="foundResult">' + tempTitle + '</a>';
  230. var starWidth = (ttScore * 100/ hundredProcent)/(ttScore_first/hundredProcent) * (numberOfWords/maxNumberOfWords);
  231. starWidth = starWidth < 10 ? (starWidth + 5) : starWidth;
  232. // Keep the 5 stars format
  233. if (starWidth > 85){
  234. starWidth = 85;
  235. }
  236. /*
  237. var noFullStars = Math.ceil(starWidth/17);
  238. var fullStar = "curr";
  239. var emptyStar = "";
  240. if (starWidth % 17 == 0){
  241. // am stea plina
  242. } else {
  243. }
  244. console.info(noFullStars);
  245. */
  246. // Also check if we have a valid description
  247. if ((tempShortdesc != "null" && tempShortdesc != '...')) {
  248. linkString += "\n<div class=\"shortdesclink\">" + tempShortdesc + "</div>";
  249. }
  250. linkString += "</li>";
  251. // Add rating values for scoring at the list of matches
  252. linkString += "<div id=\"rightDiv\">";
  253. linkString += "<div id=\"star\">";
  254. //linkString += "<div style=\"color: rgb(136, 136, 136);\" id=\"starUser0\" class=\"user\">"
  255. // + ((ttScore * 100/ hundredProcent)/(ttScore_first/hundredProcent)) * 1 + "</div>";
  256. linkString += "<ul id=\"star0\" class=\"star\">";
  257. linkString += "<li id=\"starCur0\" class=\"curr\" style=\"width: " + starWidth + "px;\"></li>";
  258. linkString += "</ul>";
  259. linkString += "<br style=\"clear: both;\">";
  260. linkString += "</div>";
  261. linkString += "</div>";
  262. //linkString += '<b>Rating: ' + ttScore + '</b>';
  263. linkTab.push(linkString);
  264. no++;
  265. }
  266. linkTab.push("</ul>");
  267. }
  268. }
  269. }
  270. var results = "";
  271. if (linkTab.length > 0) {
  272. /*writeln ("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">" + cleanwordsList + "</span>" + "<br/>"+"</p>");*/
  273. results = "<p>";
  274. //write("<ul class='searchresult'>");
  275. for (t in linkTab) {
  276. results += linkTab[t].toString();
  277. }
  278. results += "</p>";
  279. } else {
  280. results = "<p>" + localeresource.search_no_results + " <span class=\"searchExpression\">" + txt_wordsnotfound + "</span>" + "</p>";
  281. }
  282. // Verify if the browser is Google Chrome and the WebHelp is used on a local machine
  283. // If browser is Google Chrome and WebHelp is used on a local machine a warning message will appear
  284. // Highlighting will not work in this conditions. There is 2 workarounds
  285. if (verifyBrowser()){
  286. document.getElementById('searchResults').innerHTML = results;
  287. } else {
  288. document.getElementById('searchResults').innerHTML = warningMsg + results;
  289. }
  290. }
  291. // Verify if the stemmed word is aproximately the same as the searched word
  292. function verifyWord(word, arr){
  293. for (var i = 0 ; i < arr.length ; i++){
  294. if (word[0] == arr[i][0]
  295. && word[1] == arr[i][1]
  296. //&& word[2] == arr[i][2]
  297. ){
  298. return true;
  299. }
  300. }
  301. return false;
  302. }
  303. // Look for elements that start with searchedValue.
  304. function wordsStartsWith(searchedValue){
  305. var toReturn = '';
  306. for (var sv in w){
  307. if (searchedValue.length < 3){
  308. continue;
  309. } else {
  310. if (sv.toLowerCase().indexOf(searchedValue.toLowerCase()) == 0){
  311. toReturn+=sv + ",";
  312. }
  313. }
  314. }
  315. return toReturn.length > 0 ? toReturn : undefined;
  316. }
  317. function tokenize(wordsList){
  318. var stemmedWordsList = new Array(); // Array with the words to look for after removing spaces
  319. var cleanwordsList = new Array(); // Array with the words to look for
  320. // -------------------------------------------------
  321. // Thu's patch
  322. for(var j=0;j<wordsList.length;++j){
  323. var word = wordsList[j];
  324. var originalWord=word;
  325. if(typeof stemmer != "undefined" ){
  326. var stemmedWord=stemmer(word);
  327. if(w[stemmedWord]!=undefined){
  328. stemQueryMap[stemmer(word)] = word;
  329. }
  330. else{
  331. stemQueryMap[originalWord]=originalWord;
  332. }
  333. } else {
  334. if(w[word]!=undefined){
  335. stemQueryMap[word] = word;
  336. }
  337. else{
  338. stemQueryMap[originalWord]=originalWord;
  339. }
  340. }
  341. }
  342. //stemmedWordsList is the stemmed list of words separated by spaces.
  343. for (var t=0;t<wordsList.length;++t) {
  344. wordsList[t] = wordsList[t].replace(/(%22)|^-/g, "");
  345. if (wordsList[t] != "%20") {
  346. scriptLetterTab.add(wordsList[t].charAt(0));
  347. cleanwordsList.push(wordsList[t]);
  348. }
  349. }
  350. if(typeof stemmer != "undefined" ){
  351. //Do the stemming using Porter's stemming algorithm
  352. for (var i = 0; i < cleanwordsList.length; i++) {
  353. var stemWord = stemmer(cleanwordsList[i]);
  354. if(w[stemWord]!=undefined){
  355. stemmedWordsList.push(stemWord);
  356. }
  357. else{
  358. stemmedWordsList.push(cleanwordsList[i]);
  359. }
  360. }
  361. // End Thu's patch
  362. // -------------------------------------------
  363. } else {
  364. stemmedWordsList = cleanwordsList;
  365. }
  366. return stemmedWordsList;
  367. }
  368. //Invoker of CJKTokenizer class methods.
  369. function cjkTokenize(wordsList){
  370. var allTokens= new Array();
  371. var notCJKTokens= new Array();
  372. var j=0;
  373. for(j=0;j<wordsList.length;j++){
  374. var word = wordsList[j];
  375. if(getAvgAsciiValue(word) < 127){
  376. notCJKTokens.push(word);
  377. } else {
  378. var tokenizer = new CJKTokenizer(word);
  379. var tokensTmp = tokenizer.getAllTokens();
  380. allTokens = allTokens.concat(tokensTmp);
  381. }
  382. }
  383. allTokens = allTokens.concat(tokenize(notCJKTokens));
  384. return allTokens;
  385. }
  386. //A simple way to determine whether the query is in english or not.
  387. function getAvgAsciiValue(word){
  388. var tmp = 0;
  389. var num = word.length < 5 ? word.length:5;
  390. for(var i=0;i<num;i++){
  391. if(i==5) break;
  392. tmp += word.charCodeAt(i);
  393. }
  394. return tmp/num;
  395. }
  396. //CJKTokenizer
  397. function CJKTokenizer(input){
  398. this.input = input;
  399. this.offset=-1;
  400. this.tokens = new Array();
  401. this.incrementToken = incrementToken;
  402. this.tokenize = tokenize;
  403. this.getAllTokens = getAllTokens;
  404. this.unique = unique;
  405. function incrementToken(){
  406. if(this.input.length - 2 <= this.offset){
  407. // console.log("false "+offset);
  408. return false;
  409. }
  410. else {
  411. this.offset+=1;
  412. return true;
  413. }
  414. }
  415. function tokenize(){
  416. //document.getElementById("content").innerHTML += x.substring(offset,offset+2)+"<br>";
  417. return this.input.substring(this.offset,this.offset+2);
  418. }
  419. function getAllTokens(){
  420. while(this.incrementToken()){
  421. var tmp = this.tokenize();
  422. this.tokens.push(tmp);
  423. }
  424. return this.unique(this.tokens);
  425. // document.getElementById("content").innerHTML += tokens+" ";
  426. // document.getElementById("content").innerHTML += "<br>dada"+sortedTokens+" ";
  427. // console.log(tokens.length+"dsdsds");
  428. /*for(i=0;i<tokens.length;i++){
  429. console.log(tokens[i]);
  430. var ss = tokens[i] == sortedTokens[i];
  431. // document.getElementById("content").innerHTML += "<br>dada"+un[i]+"- "+stems[i]+"&nbsp;&nbsp;&nbsp;"+ ss;
  432. document.getElementById("content").innerHTML += "<br>"+sortedTokens[i];
  433. }*/
  434. }
  435. function unique(a)
  436. {
  437. var r = new Array();
  438. o:for(var i = 0, n = a.length; i < n; i++)
  439. {
  440. for(var x = 0, y = r.length; x < y; x++)
  441. {
  442. if(r[x]==a[i]) continue o;
  443. }
  444. r[r.length] = a[i];
  445. }
  446. return r;
  447. }
  448. }
  449. /* Scriptfirstchar: to gather the first letter of index js files to upload */
  450. function Scriptfirstchar() {
  451. this.strLetters = "";
  452. this.add = addLettre;
  453. }
  454. function addLettre(caract) {
  455. if (this.strLetters == 'undefined') {
  456. this.strLetters = caract;
  457. } else if (this.strLetters.indexOf(caract) < 0) {
  458. this.strLetters += caract;
  459. }
  460. return 0;
  461. }
  462. /* end of scriptfirstchar */
  463. /*main loader function*/
  464. /*tab contains the first letters of each word looked for*/
  465. function loadTheIndexScripts(tab) {
  466. //alert (tab.strLetters);
  467. var scriptsarray = new Array();
  468. for (var i = 0; i < tab.strLetters.length; i++) {
  469. scriptsarray[i] = "..\/search" + "\/" + tab.strLetters.charAt(i) + ".js";
  470. }
  471. // add the list of html files
  472. i++;
  473. scriptsarray[i] = "..\/search" + "\/" + htmlfileList;
  474. //debug
  475. for (var t in scriptsarray) {
  476. //alert (scriptsarray[t]);
  477. }
  478. tab = new ScriptLoader();
  479. for (t in scriptsarray) {
  480. tab.add(scriptsarray[t]);
  481. }
  482. tab.load();
  483. //alert ("scripts loaded");
  484. return (scriptsarray);
  485. }
  486. /* ScriptLoader: to load the scripts and wait that it's finished */
  487. function ScriptLoader() {
  488. this.cpt = 0;
  489. this.scriptTab = new Array();
  490. this.add = addAScriptInTheList;
  491. this.load = loadTheScripts;
  492. this.onScriptLoaded = onScriptLoadedFunc;
  493. }
  494. function addAScriptInTheList(scriptPath) {
  495. this.scriptTab.push(scriptPath);
  496. }
  497. function loadTheScripts() {
  498. var script;
  499. var head;
  500. head = document.getElementsByTagName('head').item(0);
  501. //script = document.createElement('script');
  502. for (var el in this.scriptTab) {
  503. //alert (el+this.scriptTab[el]);
  504. script = document.createElement('script');
  505. script.src = this.scriptTab[el];
  506. script.type = 'text/javascript';
  507. script.defer = false;
  508. head.appendChild(script);
  509. }
  510. }
  511. function onScriptLoadedFunc(e) {
  512. e = e || window.event;
  513. var target = e.target || e.srcElement;
  514. var isComplete = true;
  515. if (typeof target.readyState != undefined) {
  516. isComplete = (target.readyState == "complete" || target.readyState == "loaded");
  517. }
  518. if (isComplete) {
  519. ScriptLoader.cpt++;
  520. if (ScriptLoader.cpt == ScriptLoader.scripts.length) {
  521. ScriptLoader.onLoadComplete();
  522. }
  523. }
  524. }
  525. /*
  526. function onLoadComplete() {
  527. alert("loaded !!");
  528. } */
  529. /* End of scriptloader functions */
  530. // Array.unique( strict ) - Remove duplicate values
  531. function unique(tab) {
  532. var a = new Array();
  533. var i;
  534. var l = tab.length;
  535. if (tab[0] != undefined) {
  536. a[0] = tab[0];
  537. }
  538. else {
  539. return -1;
  540. }
  541. for (i = 1; i < l; i++) {
  542. if (indexof(a, tab[i], 0) < 0) {
  543. a.push(tab[i]);
  544. }
  545. }
  546. return a;
  547. }
  548. function indexof(tab, element, begin) {
  549. for (var i = begin; i < tab.length; i++) {
  550. if (tab[i] == element) {
  551. return i;
  552. }
  553. }
  554. return -1;
  555. }
  556. /* end of Array functions */
  557. /*
  558. Param: mots= list of words to look for.
  559. This function creates an hashtable:
  560. - The key is the index of a html file which contains a word to look for.
  561. - The value is the list of all words contained in the html file.
  562. Return value: the hashtable fileAndWordList
  563. */
  564. function SortResults(mots) {
  565. var fileAndWordList = new Object();
  566. if (mots.length == 0 || mots[0].length == 0) {
  567. return null;
  568. }
  569. // In generated js file we add scoring at the end of the word
  570. // Example word1*scoringForWord1,word2*scoringForWord2 and so on
  571. // Split after * to obtain the right values
  572. var scoringArr = Array();
  573. for (var t in mots) {
  574. // get the list of the indices of the files.
  575. var listNumerosDesFicStr = w[mots[t].toString()];
  576. if (listNumerosDesFicStr != undefined) {
  577. //alert ("listNumerosDesFicStr "+listNumerosDesFicStr);
  578. var tab = listNumerosDesFicStr.split(",");
  579. //for each file (file's index):
  580. for (var t2 in tab) {
  581. var tmp = '';
  582. var idx = '';
  583. var temp = tab[t2].toString();
  584. if (temp.indexOf('*') != -1) {
  585. idx = temp.indexOf('*');
  586. tmp = temp.substring(idx + 3, temp.length);
  587. temp = temp.substring(0, idx);
  588. }
  589. scoringArr.push(tmp);
  590. if (fileAndWordList[temp] == undefined) {
  591. fileAndWordList[temp] = "" + mots[t];
  592. } else {
  593. fileAndWordList[temp] += "," + mots[t];
  594. }
  595. //console.info("fileAndWordList[" + temp + "]=" + fileAndWordList[temp] + " : " + tmp);
  596. }
  597. }
  598. }
  599. var fileAndWordListValuesOnly = new Array();
  600. // sort results according to values
  601. var temptab = new Array();
  602. finalObj = new Array();
  603. for (t in fileAndWordList) {
  604. finalObj.push(new newObj(t,fileAndWordList[t]));
  605. }
  606. if ( finalObj.length == 0 ) { // None of the queried words are not in the index (stemmed or not)
  607. return null;
  608. }
  609. finalObj = removeDerivates(finalObj);
  610. for (t in finalObj) {
  611. tab = finalObj[t].wordList.split(',');
  612. var tempDisplay = new Array();
  613. for (var x in tab) {
  614. if(stemQueryMap[tab[x]] != undefined && doStem){
  615. tempDisplay.push(stemQueryMap[tab[x]]); //get the original word from the stem word.
  616. } else {
  617. tempDisplay.push(tab[x]); //no stem is available. (probably a CJK language)
  618. }
  619. }
  620. var tempDispString = tempDisplay.join(", ");
  621. var index;
  622. for (x in fileAndWordList) {
  623. if (x === finalObj[t].filesNo) {
  624. index = x;
  625. break;
  626. }
  627. }
  628. var scoring = findRating(fileAndWordList[index], index);
  629. temptab.push(new resultPerFile(finalObj[t].filesNo, finalObj[t].wordList, tab.length, tempDispString, scoring));
  630. fileAndWordListValuesOnly.push(finalObj[t].wordList);
  631. }
  632. fileAndWordListValuesOnly = unique(fileAndWordListValuesOnly);
  633. fileAndWordListValuesOnly = fileAndWordListValuesOnly.sort(compare_nbMots);
  634. var listToOutput = new Array();
  635. for (var fawlvoIdx in fileAndWordListValuesOnly) {
  636. for (t in temptab) {
  637. if (temptab[t].motsliste == fileAndWordListValuesOnly[fawlvoIdx]) {
  638. if (listToOutput[fawlvoIdx] == undefined) {
  639. listToOutput[fawlvoIdx] = new Array(temptab[t]);
  640. } else {
  641. listToOutput[fawlvoIdx].push(temptab[t]);
  642. }
  643. }
  644. }
  645. }
  646. // Sort results by scoring, descending on the same group
  647. for (var ltoIdx in listToOutput) {
  648. listToOutput[ltoIdx].sort(function(a, b){
  649. return b.scoring - a.scoring;
  650. });
  651. }
  652. // If we have groups with same number of words,
  653. // will sort groups by higher scoring of each group
  654. for (var i = 0; i < listToOutput.length - 1; i++) {
  655. for (var j = i + 1; j < listToOutput.length; j++) {
  656. if (listToOutput[i][0].motsnb < listToOutput[j][0].motsnb
  657. || (listToOutput[i][0].motsnb == listToOutput[j][0].motsnb
  658. && listToOutput[i][0].scoring < listToOutput[j][0].scoring)
  659. ) {
  660. var x = listToOutput[i];
  661. listToOutput[i] = listToOutput[j];
  662. listToOutput[j] = x;
  663. }
  664. }
  665. }
  666. return listToOutput;
  667. }
  668. // Remove derivates words from the list of words
  669. function removeDerivates(obj){
  670. var toResultObject = new Array();
  671. for (i in obj){
  672. var filesNo = obj[i].filesNo;
  673. var wordList = obj[i].wordList;
  674. var wList = wordList.split(",");
  675. var searchedWords = searchTextField.toLowerCase().split(" ");
  676. for (var k = 0 ; k < searchedWords.length ; k++){
  677. for (var j = 0 ; j < wList.length ; j++){
  678. if (wList[j].startsWith(searchedWords[k])){
  679. wList[j] = searchedWords[k];
  680. }
  681. }
  682. }
  683. wList = removeDuplicate(wList);
  684. var recreateList = '';
  685. for(var x in wList){
  686. recreateList+=wList[x] + ",";
  687. }
  688. recreateList = recreateList.substr(0, recreateList.length - 1);
  689. toResultObject.push(new newObj(filesNo, recreateList));
  690. }
  691. return toResultObject;
  692. }
  693. function newObj(filesNo, wordList){
  694. this.filesNo = filesNo;
  695. this.wordList = wordList;
  696. }
  697. // Add a new parameter. Scoring.
  698. function resultPerFile(filenb, motsliste, motsnb, motslisteDisplay, scoring, group) {
  699. //10 - spring,time - 2 - spring, time - 55 - 3
  700. this.filenb = filenb;
  701. this.motsliste = motsliste;
  702. this.motsnb = motsnb;
  703. this.motslisteDisplay= motslisteDisplay;
  704. this.scoring = scoring;
  705. }
  706. function findRating(words, nr){
  707. var sum = 0;
  708. var xx = words.split(',');
  709. for (jj = 0 ; jj < xx.length ; jj++){
  710. var wrd = w[xx[jj]].split(',');
  711. for (var ii = 0 ; ii < wrd.length ; ii++){
  712. var wrdno = wrd[ii].split('*');
  713. if (wrdno[0] == nr){
  714. sum+=parseInt(wrdno[1]);
  715. }
  716. }
  717. }
  718. return sum;
  719. }
  720. function compare_nbMots(s1, s2) {
  721. var t1 = s1.split(',');
  722. var t2 = s2.split(',');
  723. //alert ("s1:"+t1.length + " " +t2.length)
  724. if (t1.length == t2.length) {
  725. return 0;
  726. } else if (t1.length > t2.length) {
  727. return 1;
  728. } else {
  729. return -1;
  730. }
  731. //return t1.length - t2.length);
  732. }
  733. // return false if browser is Google Chrome and WebHelp is used on a local machine, not a web server
  734. function verifyBrowser(){
  735. var returnedValue = true;
  736. var browser = BrowserDetect.browser;
  737. var addressBar = window.location.href;
  738. if (browser == 'Chrome' && addressBar.indexOf('file://') === 0){
  739. returnedValue = false;
  740. }
  741. return returnedValue;
  742. }
  743. // Remove duplicate values from an array
  744. function removeDuplicate(arr) {
  745. var r = new Array();
  746. o:for(var i = 0, n = arr.length; i < n; i++) {
  747. for(var x = 0, y = r.length; x < y; x++) {
  748. if(r[x]==arr[i]) continue o;
  749. }
  750. r[r.length] = arr[i];
  751. }
  752. return r;
  753. }
  754. // Create startsWith method
  755. String.prototype.startsWith = function(str) {
  756. return (this.match("^"+str)==str);
  757. }
  758. function trim(str, chars) {
  759. return ltrim(rtrim(str, chars), chars);
  760. }
  761. function ltrim(str, chars) {
  762. chars = chars || "\\s";
  763. return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
  764. }
  765. function rtrim(str, chars) {
  766. chars = chars || "\\s";
  767. return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
  768. }