File: search.js

package info (click to toggle)
kf6-kapidox 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,204 kB
  • sloc: python: 1,812; javascript: 617; sh: 206; xml: 182; makefile: 5
file content (216 lines) | stat: -rw-r--r-- 6,447 bytes parent folder | download | duplicates (4)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var entityMap = {
  '&': '&',
  '<': '&lt;',
  '>': '&gt;',
  '"': '&quot;',
  "'": '&#39;',
  '/': '&#x2F;',
  '`': '&#x60;',
  '=': '&#x3D;'
};

function escapeHtml (string) {
  return String(string).replace(/[&<>"'`=\/]/g, function (s) {
    return entityMap[s];
  });
}

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);

    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return escapeHtml(decodeURIComponent(sParameterName[1]));
        }
    }
    return ""
}

async function render_search(type)
{
    var query = GetURLParameter("query");
    if (query == "") {
        $( '.loader' ).remove()
        $( "#search-title" ).html("<i>...If you don't tell what to search, I can't find anything...</i>");
        return
    }
    $( "#search-input" ).val(query);
    $( "#search-title" ).append(" <i>" + query + "</i>");
    var json_path =  "searchdata.json";

    await do_search(json_path, query, type);
}

function do_search(json_path, query, type)
{
    var results = []
    $.ajax({
        url: json_path,
        dataType: 'json',
        error: function(data, status, err) {
            console.log(err)
        },
        success: function (json) {
            search_json(type, json, query)
        }
    });
}

function search_json(type, json, query)
{
    result_html = ""
    if (type == 'library') {
        results_html = search_json_library(json, query)
    } else if (type == 'group') {
        results_html = search_json_group(json, query)
    } else if (type == 'global') {
        results_html = search_json_global(json, query)
    }

    $( '.loader' ).remove()
    $( '#results' ).append(results_html)

    document.querySelectorAll('h1, .dynheader, el, #results li a').forEach(h => {
      h.innerText = h.innerText.replace('org::kde::kirigami::templates::', 'Kirigami.Templates.');
      h.innerText = h.innerText.replace('org::kde::kirigami::', 'Kirigami.');
    });

}

function search_json_library(json, query)
{
    var results_name = []
    var results_text = []
    $.each(json.docfields, function(key, val) {
        if ('name' in val) {
            if (val.name.search(new RegExp(query, "i")) != -1) {
                results_name.push(val)
            }
        }
        if ('text' in val) {
            if (val.text.search(new RegExp(query, "i")) != -1) {
                results_text.push(val)
            }
        }
    });

    var html_results = ""
    html_results += "<h3>Matches in names</h3>\n"
    html_results += "<ul>\n"
    $.each(results_name, function(key, result) {
        html_results += "\t<li><a href=\"" + result.url + "\">"+ result.name + "</a>\n"
    });
    html_results += "</ul>\n"

    html_results += "<h3>Matches in text</h3>\n"
    html_results += "<ul>\n"
    $.each(results_text, function(key, result) {
        html_results += "\t<li><a href=\"" + result.url + "\">"+ result.name + "</a>: " + result.text +"\n"
    });
    html_results += "</ul>\n"

    return html_results
}

function search_json_group(json, query)
{
    var results_name = []
    var results_text = []

    $.each(json.libraries, function(k, libval) {
        if ('fancyname' in libval) {
            libname = libval.fancyname
        }

        $.each(libval.docfields, function(key, val) {
            if ('name' in val) {
                if (val.name.search(new RegExp(query, "i")) != -1) {
                    val['libname'] = libname
                    results_name.push(val)
                }
            }
            if ('text' in val) {
                if (val.text.search(new RegExp(query, "i")) != -1) {
                    val['libname'] = libname
                    results_text.push(val)
                }
            }
        });
    });

    var html_results = ""
    html_results += "<h3>Matches in names</h3>\n"
    html_results += "<ul>\n"
    $.each(results_name, function(key, result) {
        html_results += "\t<li><a href=\"" + result.url + "\">"+ result.name + "</a> in <i>" + result.libname +"</i>\n"
    });
    html_results += "</ul>\n"

    html_results += "<h3>Matches in text</h3>\n"
    html_results += "<ul>\n"
    $.each(results_text, function(key, result) {
        html_results += "\t<li><a href=\"" + result.url + "\">"+ result.name + "</a> in <i>" + result.libname +"</i><br />" + result.text +"\n"
    });
    html_results += "</ul>\n"

    return html_results
}

function search_json_global(json, query)
{
    var results_name = []
    var results_text = []

    $.each(json.all, function(k, productval) {
        if ('fancyname' in productval) {
            productname = productval.fancyname
        }

        $.each(productval.libraries, function(k, libval) {
            if ('fancyname' in libval) {
                libname = libval.fancyname
            }

            $.each(libval.docfields, function(key, val) {
                if ('name' in val) {
                    if (val.name.search(new RegExp(query, "i")) != -1) {
                        val['libname'] = libname
                        val['productname'] = productname
                        results_name.push(val)
                    }
                }
                if ('text' in val) {
                    if (val.text.search(new RegExp(query, "i")) != -1) {
                        val['libname'] = libname
                        val['productname'] = productname
                        results_text.push(val)
                    }
                }
            });
        });
    });

    var html_results = ""
    html_results += "<h3>Matches in names</h3>\n"
    html_results += "<ul>\n"
    $.each(results_name, function(key, result) {
        html_results += "\t<li><a href=\"" + result.url + "\">"+ result.name +
            "</a> in <i>" + result.libname +"</i> from product <i>" +
            result.productname + "</i>\n"
    });
    html_results += "</ul>\n"

    html_results += "<h3>Matches in text</h3>\n"
    html_results += "<ul>\n"
    $.each(results_text, function(key, result) {
        html_results += "\t<li><a href=\"" + result.url + "\">"+ result.name +
            "</a> in <i>" + result.libname +"</i> from product <i>" +
            result.productname + "</i><br />" + result.text +"\n"
    });
    html_results += "</ul>\n"

    return html_results
}