aboutsummaryrefslogtreecommitdiff
blob: c56a31ae9c57d4a23c82095f68e25c3c9d3afd1f (plain)
1
2
3
4
5
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
/*
 * Copyright 2019 Gentoo Authors
 * Distributed under the terms of the GNU GPL version 2 or later
 */
"use strict";

var search_index = null;
var search_input = document.getElementById("searchInput");

search_input.addEventListener("keyup", function(event) {
  if(event.keyCode === 13) {
    event.preventDefault();
    document.getElementById("mw-searchButton").click();
  }
});

function buildIndex() {
  search_index = lunr(function () {
    this.ref('id');
    this.field('text');
    this.metadataWhitelist = ['position']

    documents.forEach(function (doc) {
      this.add(doc);
    }, this);
  });
}

function fetchDocuments() {
  document.getElementsByName("search")[0].onclick = null;
  if (search_index == null) {
    const script = document.createElement('script')
    script.src = documentsSrc;
    script.async = false;
    script.onload = function() {
      buildIndex();
    }
    document.body.appendChild(script);
  }
}

function getContents(docs, uid) {
  var contents = { name: "", text: "", url: "" };

  contents.name = docs[uid].name;
  contents.text = docs[uid].text;
  contents.url = docs[uid].url;

  return contents;
}

function escapeHTML(str) {
  return str.replace(/[&<"']/g, function(m) {
    switch (m) {
    case '&':
      return '&amp;';
    case '<':
      return '&lt;';
    case '"':
      return '&quot;';
    default:
      return '&#039;';
    }
  });
};

function search() {
  var term = document.getElementById("searchInput").value;
  if (term !== "") {
    var results = search_index.search(term);
    if (results.length > 0) {
      $("#searchResults .modal-body").empty();
      $.each(results, function(index, result) {
        var uid = result.ref;
        var contents = getContents(documents, uid);
        var stems = Object.keys(result.matchData.metadata);
        var positions = [];
        var text = "";
        var pos = 0;

        stems.forEach(function (stem) {
            positions = positions.concat(result.matchData.metadata[stem].text.position);
        });
        positions.sort(function(x, y) {
            if (x[0] < y[0]) { return -1; }
            else if (x[0] > y[0]) { return 1; }
            else { return 0; }
        });

        for (var i = 0; i < positions.length; i++) {
          text += escapeHTML(contents.text.substring(pos, positions[i][0]));
          pos = positions[i][0];
          text += "<span style='background-color: yellow;'>";
          text += escapeHTML(contents.text.substring(pos, pos + positions[i][1]));
          pos += positions[i][1];
          text += "</span>";
        }
        text += escapeHTML(contents.text.substring(pos));

        $("#searchResults .modal-body").append(`<article><h5><a href="${contents.url}">
                                                ${contents.name}</a></h5><p>${text}</p></article>`);
      });
    } else {
      $("#searchResults .modal-body").empty();
      $("#searchResults .modal-body").append("<p>No results found.</p>");
    }
  } else {
      $("#searchResults .modal-body").empty();
      $("#searchResults .modal-body").append("<p>No search term defined.</p>");
  }
  $("#searchResults").modal();
}