search.js 8.0 KB
Newer Older
1
/*
2
 * Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
3 4
 * and the EPL 1.0 (http://h2database.com/html/license.html).
 *  * Initial Developer: H2 Group
5 6
 */

Thomas Mueller's avatar
Thomas Mueller committed
7 8
var pages = new Array();
var ref = new Array();
Thomas Mueller's avatar
Thomas Mueller committed
9
var ignored = '';
10 11 12 13 14 15
var firstLink = null;
var firstLinkWord = null;

String.prototype.endsWith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
Thomas Mueller's avatar
Thomas Mueller committed
16
        return false;
17
    }
18
    return this.lastIndexOf(suffix, startPos) == startPos;
19 20 21
};

function listWords(value, open) {
Thomas Mueller's avatar
Thomas Mueller committed
22 23 24 25 26 27 28 29 30 31 32 33 34
    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';
35
        return;
Thomas Mueller's avatar
Thomas Mueller committed
36 37 38 39
    }
    clear.style.display = '';
    var keywords = value.split(' ');
    if (keywords.length > 1) {
40 41
        listMultipleWords(keywords);
        return;
Thomas Mueller's avatar
Thomas Mueller committed
42 43 44 45 46 47 48 49 50
    }
    if (value.length < 3) {
        max = 100;
    } else {
        max = 1000;
    }
    value = value.toLowerCase();
    var r = ref[value.substring(0, 1)];
    if (r == undefined) {
51
        return;
52
    }
Thomas Mueller's avatar
Thomas Mueller committed
53 54 55
    var x = 0;
    var words = r.split(';');
    var count = 0;
56
    for (var i = 0; i < words.length; i++) {
Thomas Mueller's avatar
Thomas Mueller committed
57 58 59 60 61
        var wordRef = words[i];
        if (wordRef.toLowerCase().indexOf(value) == 0) {
            count++;
        }
    }
62
    for (var i = 0; i < words.length && (x <= max); i++) {
Thomas Mueller's avatar
Thomas Mueller committed
63 64 65 66 67 68 69 70
        var wordRef = words[i];
        if (wordRef.toLowerCase().indexOf(value) == 0) {
            word = wordRef.split("=")[0];
            var tr = table.insertRow(x++);
            var td = document.createElement('td');
            var tdClass = document.createAttribute('class');
            tdClass.nodeValue = 'searchKeyword';
            td.setAttributeNode(tdClass);
71

Thomas Mueller's avatar
Thomas Mueller committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
            var ah = document.createElement('a');
            var href = document.createAttribute('href');
            href.nodeValue = 'javascript:set("' + word + '");';
            var link = document.createTextNode(word);
            ah.setAttributeNode(href);
            ah.appendChild(link);
            td.appendChild(ah);
            tr.appendChild(td);
            piList = wordRef.split("=")[1].split(",");
            if (count < 20 || open == word) {
                x = addReferences(x, piList, word);
            }
        }
    }
    if (x == 0) {
Thomas Mueller's avatar
Thomas Mueller committed
87 88 89 90 91
        if (ignored.indexOf(';' + value + ';') >= 0) {
            noResults(table, 'Common word (not indexed)');
        } else {
            noResults(table, 'No results found!');
        }
92 93 94 95
    }
}

function set(v) {
Thomas Mueller's avatar
Thomas Mueller committed
96
    if (pages.length == 0) {
97 98 99 100 101 102 103 104 105 106
        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');
Thomas Mueller's avatar
Thomas Mueller committed
107
    if (firstLink != null) {
108 109 110 111 112 113 114 115
        go(firstLink, firstLinkWord);
    }
    return false;
}

function go(pageId, word) {
    var page = pages[pageId];
    var load = '../' + page.file + '?highlight=' + encodeURIComponent(word);
Thomas Mueller's avatar
Thomas Mueller committed
116 117
    if (top.main) {
        if (!top.main.location.href.endsWith(page.file)) {
118 119 120
            top.main.location = load;
        }
    } else {
Thomas Mueller's avatar
Thomas Mueller committed
121
        if (!document.location.href.endsWith(page.file)) {
122 123 124 125 126 127
            var search = document.getElementById('search').value;
            document.location = load + '&search=' + encodeURIComponent(search);
        }
    }
}

128
function listMultipleWords(keywords) {
Thomas Mueller's avatar
Thomas Mueller committed
129 130
    var count = new Array();
    var weight = new Array();
131
    for (var i = 0; i < pages.length; i++) {
Thomas Mueller's avatar
Thomas Mueller committed
132
        count[i] = 0;
133
        weight[i] = 0.0;
134
    }
135
    for (var i = 0; i < keywords.length; i++) {
Thomas Mueller's avatar
Thomas Mueller committed
136
        var value = keywords[i].toLowerCase();
137 138 139
        if (value.length <= 1) {
            continue;
        }
Thomas Mueller's avatar
Thomas Mueller committed
140 141
        var r = ref[value.substring(0, 1)];
        if (r == undefined) {
142
            continue;
Thomas Mueller's avatar
Thomas Mueller committed
143 144
        }
        var words = r.split(';');
145
        for (var j = 0; j < words.length; j++) {
Thomas Mueller's avatar
Thomas Mueller committed
146 147
            var wordRef = words[j];
            if (wordRef.toLowerCase().indexOf(value) == 0) {
148
                var word = wordRef.split("=")[0].toLowerCase();
Thomas Mueller's avatar
Thomas Mueller committed
149 150
                piList = wordRef.split("=")[1].split(",");
                var w = 1;
151
                for (var k = 0; k < piList.length; k++) {
Thomas Mueller's avatar
Thomas Mueller committed
152 153 154 155 156 157 158 159 160 161 162
                    var pi = piList[k];
                    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;
                    }
163 164 165 166 167
                    if (w > 0) {
                        if (word != value) {
                            // if it's only the start of the word,
                            // reduce the weight
                            w /= 10.0;
Thomas Mueller's avatar
Thomas Mueller committed
168
                        }
169 170 171
                        // higher weight for longer words
                        w += w * word.length / 10.0;
                        count[pi]++;
Thomas Mueller's avatar
Thomas Mueller committed
172 173 174
                        weight[pi] += w;
                    }
                }
175 176 177
            }
        }
    }
Thomas Mueller's avatar
Thomas Mueller committed
178 179 180 181
    var x = 0;
    var table = document.getElementById('result');
    var piList = new Array();
    var piWeight = new Array();
182 183 184
    for (var i = 0; i < pages.length; i++) {
        var w = weight[i];
        if (w > 0) {
Thomas Mueller's avatar
Thomas Mueller committed
185
            piList[x] = '' + i;
186
            piWeight[x] = w * count[i];
Thomas Mueller's avatar
Thomas Mueller committed
187 188 189 190
            x++;
        }
    }
    // sort
191
    for (var i = 1, j; i < x; i++) {
Thomas Mueller's avatar
Thomas Mueller committed
192 193 194 195 196 197 198 199
        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];
        }
        piWeight[j + 1] = tw;
        piList[j + 1] = ti;
200
    }
Thomas Mueller's avatar
Thomas Mueller committed
201 202
    addReferences(0, piList, keywords);
    if (piList.length == 0) {
Thomas Mueller's avatar
Thomas Mueller committed
203
        noResults(table, 'No results found');
204 205 206 207
    }
}

function addReferences(x, piList, word) {
Thomas Mueller's avatar
Thomas Mueller committed
208
    var table = document.getElementById('result');
209
    for (var j = 0; j < piList.length; j++) {
Thomas Mueller's avatar
Thomas Mueller committed
210 211 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
        var pi = piList[j];
        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');
        var tdClass = document.createAttribute('class');
        tdClass.nodeValue = 'searchLink';
        td.setAttributeNode(tdClass);
        var ah = document.createElement('a');
        var href = document.createAttribute('href');
        var thisLink = 'javascript:go(' + pi + ', "' + word + '")';
        if (firstLink == null) {
            firstLink = pi;
            firstLinkWord = word;
        }
        href.nodeValue = thisLink;
        ah.setAttributeNode(href);
        var page = pages[pi];
        var link = document.createTextNode(page.title);
        ah.appendChild(link);
        td.appendChild(ah);
        tr.appendChild(td);
237
    }
Thomas Mueller's avatar
Thomas Mueller committed
238
    return x;
239 240 241
}

function trim(s) {
Thomas Mueller's avatar
Thomas Mueller committed
242 243
    while (s.charAt(0) == ' ' && s.length > 0) {
        s = s.substring(1);
244
    }
Thomas Mueller's avatar
Thomas Mueller committed
245 246
    while (s.charAt(s.length - 1) == ' ' && s.length > 0) {
        s = s.substring(0, s.length - 1);
247 248 249 250 251 252
    }
    return s;
}

function replaceOtherChars(s) {
    var x = "";
253
    for (var i = 0; i < s.length; i++) {
254
        var c = s.charAt(i);
Thomas Mueller's avatar
Thomas Mueller committed
255
        if ("\t\r\n\"'.,:;!&/\\?%@`[]{}()+-=<>|*^~#$".indexOf(c) >= 0) {
256 257 258 259 260 261 262
            c = " ";
        }
        x += c;
    }
    return x;
}

Thomas Mueller's avatar
Thomas Mueller committed
263
function noResults(table, message) {
Thomas Mueller's avatar
Thomas Mueller committed
264 265 266 267 268
    var tr = table.insertRow(0);
    var td = document.createElement('td');
    var tdClass = document.createAttribute('class');
    tdClass.nodeValue = 'searchKeyword';
    td.setAttributeNode(tdClass);
Thomas Mueller's avatar
Thomas Mueller committed
269
    var text = document.createTextNode(message);
Thomas Mueller's avatar
Thomas Mueller committed
270 271
    td.appendChild(text);
    tr.appendChild(td);
272 273
}