search.js 6.6 KB
Newer Older
1
/*
2 3 4 5
 * Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License, 
 * Version 1.0, and under the Eclipse Public License, Version 1.0 
 * (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
 */

var pages=new Array();
var ref=new Array();
var firstLink = null;
var firstLinkWord = null;

String.prototype.endsWith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
      return false;
    }
    return (this.lastIndexOf(suffix, startPos) == startPos);
};

function listWords(value, open) {
  value = replaceOtherChars(value);
  value = trim(value);
  if(pages.length==0) {
    load();
  }
  var table = document.getElementById('result');
  while(table.rows.length > 0) {
    table.deleteRow(0);
  }
  firstLink=null;
  var clear = document.getElementById('clear');
  if(value.length == 0) {
    clear.style.display = 'none';
    return true;
  }
  clear.style.display = '';
  var keywords = value.split(' ');
  if(keywords.length > 1) {
    listAnd(keywords);
    return true;
  }
  if(value.length < 3) {
    max = 100;
  } else {
    max = 1000;
  }
  value = value.toLowerCase();
  var r = ref[value.substring(0,1)];
  if(r==undefined) {
    return true;
  }
  var x=0;
  var words = r.split(';');
  var count=0;
  for(var i=0; i<words.length; i++) {
    var wordRef = words[i];
    if(wordRef.toLowerCase().indexOf(value)==0) {
      count++;
    }
  }
  for(var i=0; i<words.length && (x<=max); i++) {
    var wordRef = words[i];
    if(wordRef.toLowerCase().indexOf(value)==0) {
      word = wordRef.split("=")[0];
      var tr = table.insertRow(x++);
      var td = document.createElement('td');
68 69 70
      var tdClass = document.createAttribute('class');
      tdClass.nodeValue = 'searchKeyword';
      td.setAttributeNode(tdClass);
71 72

      var ah = document.createElement('a');
73 74
      var href = document.createAttribute('href');
      href.nodeValue = 'javascript:set("' + word + '");';
75
      var link = document.createTextNode(word);
76
      ah.setAttributeNode(href);
77 78 79
      ah.appendChild(link);
      td.appendChild(ah);
      tr.appendChild(td);
80
      piList = wordRef.split("=")[1].split(",");
81
      if(count<20 || open==word) {
82
        x = addReferences(x, piList, word);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      }
    }
  }
  if(x==0) {
      noResults(table);
  }
  return true;
}

function set(v) {
    if(pages.length==0) {
        load();
    }
    var search = document.getElementById('search').value;
    listWords(search, v);
    document.getElementById('search').focus();
    window.scrollBy(-20, 0);
}

function goFirst() {
    var table = document.getElementById('result');
    if(firstLink != null) {
        go(firstLink, firstLinkWord);
    }
    return false;
}

function go(pageId, word) {
    var page = pages[pageId];
      var load = '../' + page.file + '?highlight=' + encodeURIComponent(word);
      if(!top.main.location.href.endsWith(page.file)) {
        top.main.location = load;
      }
}

function listAnd(keywords) {
  var count = new Array();
  var weight = new Array();
  for(var i=0; i<pages.length; i++) {
    count[i] = 0;
    weight[i] = 0;
  }
  for(var i=0; i<keywords.length; i++) {
    var value = keywords[i].toLowerCase();
    var r = ref[value.substring(0,1)];
    if(r==undefined) {
      return true;
    }
    var words = r.split(';');
    for(var j=0; j<words.length; j++) {
      var wordRef = words[j];
      if(wordRef.toLowerCase().indexOf(value)==0) {
135
        piList = wordRef.split("=")[1].split(",");
136
        var w=1;
137 138
        for(var k=0; k<piList.length; k++) {
          var pi = piList[k];
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
          if(pi.charAt(0) == 't') {
            pi = pi.substring(1);
            w=10000;
          } else if(pi.charAt(0) == 'h') {
            pi = pi.substring(1);
            w=100;
          } else if(pi.charAt(0) == 'r') {
            pi = pi.substring(1);
            w=1;
          }
          if(count[pi]>=i) {
            if(count[pi]==i) {
              count[pi]++;
            }
            weight[pi]+=w;
          }
        }
      }
    }
  }
  var x = 0;
  var table = document.getElementById('result');
161 162
  var piList = new Array();
  var piWeight = new Array();
163 164
  for(var i=0; i<pages.length; i++) {
    if(count[i] >= keywords.length) {
165 166
      piList[x] = '' + i;
      piWeight[x] = weight[i];
167 168 169 170 171
      x++;
    }
  }
  // sort
  for (var i = 1, j; i < x; i++) {
172 173 174 175 176
    var tw = piWeight[i];
    var ti = piList[i];
    for (j = i - 1; j >= 0 && (piWeight[j] < tw); j--) {
      piWeight[j + 1] = piWeight[j];
      piList[j + 1] = piList[j];
177
    }
178 179
    piWeight[j + 1] = tw;
    piList[j + 1] = ti;
180
  }
181 182
  addReferences(0, piList, keywords);
  if(piList.length == 0) {
183 184 185 186
    noResults(table);
  }
}

187
function addReferences(x, piList, word) {
188
  var table = document.getElementById('result');
189 190
  for(var j=0; j<piList.length; j++) {
    var pi = piList[j];
191 192 193 194 195 196 197 198 199
    if(pi.charAt(0) == 't') {
      pi = pi.substring(1);
    } else if(pi.charAt(0) == 'h') {
      pi = pi.substring(1);
    } else if(pi.charAt(0) == 'r') {
      pi = pi.substring(1);
    }
    var tr = table.insertRow(x++);
    var td = document.createElement('td');
200 201 202
    var tdClass = document.createAttribute('class');
    tdClass.nodeValue = 'searchLink';
    td.setAttributeNode(tdClass);
203
    var ah = document.createElement('a');
204
    var href = document.createAttribute('href');
205 206 207 208 209
    var thisLink = 'javascript:go(' + pi + ', "' + word + '")';
    if(firstLink==null) {
      firstLink = pi;
      firstLinkWord = word;
    }
210 211
    href.nodeValue = thisLink;
    ah.setAttributeNode(href);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    var page = pages[pi];
    var link = document.createTextNode(page.title);
    ah.appendChild(link);
    td.appendChild(ah);
    tr.appendChild(td);
  }
  return x;
}

function trim(s) {
    while(s.charAt(0)==' ' && s.length>0) {
        s=s.substring(1);
    }
    while(s.charAt(s.length-1)==' ' && s.length>0) {
        s=s.substring(0, s.length-1);
    }
    return s;
}

function replaceOtherChars(s) {
    var x = "";
    for(var i=0; i<s.length; i++) {
        var c = s.charAt(i);
        if("\t\r\n\"'.,:;!&/\\?%@`[]{}()+-=<>|*^~#$".indexOf(c) >= 0) {
            c = " ";
        }
        x += c;
    }
    return x;
}

function noResults(table) {
  var tr = table.insertRow(0);
  var td = document.createElement('td');
246 247 248
  var tdClass = document.createAttribute('class');
  tdClass.nodeValue = 'searchKeyword';
  td.setAttributeNode(tdClass);
249 250 251 252 253
  var text = document.createTextNode('No results found');
  td.appendChild(text);
  tr.appendChild(td);
}