diff options
Diffstat (limited to 'doc/script-files/search-page.js')
-rw-r--r-- | doc/script-files/search-page.js | 267 |
1 files changed, 267 insertions, 0 deletions
diff --git a/doc/script-files/search-page.js b/doc/script-files/search-page.js new file mode 100644 index 0000000..bcbd9b5 --- /dev/null +++ b/doc/script-files/search-page.js | |||
@@ -0,0 +1,267 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. | ||
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
4 | * | ||
5 | * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
6 | */ | ||
7 | |||
8 | "use strict"; | ||
9 | $(function() { | ||
10 | var copy = $("#page-search-copy"); | ||
11 | var expand = $("#page-search-expand"); | ||
12 | var searchLink = $("span#page-search-link"); | ||
13 | var redirect = $("input#search-redirect"); | ||
14 | function setSearchUrlTemplate() { | ||
15 | var href = document.location.href.split(/[#?]/)[0]; | ||
16 | href += "?q=" + "%s"; | ||
17 | if (redirect.is(":checked")) { | ||
18 | href += "&r=1"; | ||
19 | } | ||
20 | searchLink.html(href); | ||
21 | copy[0].onmouseenter(); | ||
22 | } | ||
23 | function copyLink(e) { | ||
24 | copyToClipboard(this.previousSibling.innerText); | ||
25 | switchCopyLabel(this, this.lastElementChild); | ||
26 | } | ||
27 | copy.click(copyLink); | ||
28 | copy[0].onmouseenter = function() {}; | ||
29 | redirect.click(setSearchUrlTemplate); | ||
30 | setSearchUrlTemplate(); | ||
31 | copy.prop("disabled", false); | ||
32 | redirect.prop("disabled", false); | ||
33 | expand.click(function (e) { | ||
34 | var searchInfo = $("div.page-search-info"); | ||
35 | if(this.parentElement.hasAttribute("open")) { | ||
36 | searchInfo.attr("style", "border-width: 0;"); | ||
37 | } else { | ||
38 | searchInfo.attr("style", "border-width: 1px;").height(searchInfo.prop("scrollHeight")); | ||
39 | } | ||
40 | }); | ||
41 | }); | ||
42 | $(window).on("load", function() { | ||
43 | var input = $("#page-search-input"); | ||
44 | var reset = $("#page-search-reset"); | ||
45 | var notify = $("#page-search-notify"); | ||
46 | var resultSection = $("div#result-section"); | ||
47 | var resultContainer = $("div#result-container"); | ||
48 | var searchTerm = ""; | ||
49 | var activeTab = ""; | ||
50 | var fixedTab = false; | ||
51 | var visibleTabs = []; | ||
52 | var feelingLucky = false; | ||
53 | function renderResults(result) { | ||
54 | if (!result.length) { | ||
55 | notify.html(messages.noResult); | ||
56 | } else if (result.length === 1) { | ||
57 | notify.html(messages.oneResult); | ||
58 | } else { | ||
59 | notify.html(messages.manyResults.replace("{0}", result.length)); | ||
60 | } | ||
61 | resultContainer.empty(); | ||
62 | var r = { | ||
63 | "types": [], | ||
64 | "members": [], | ||
65 | "packages": [], | ||
66 | "modules": [], | ||
67 | "searchTags": [] | ||
68 | }; | ||
69 | for (var i in result) { | ||
70 | var item = result[i]; | ||
71 | var arr = r[item.category]; | ||
72 | arr.push(item); | ||
73 | } | ||
74 | if (!activeTab || r[activeTab].length === 0 || !fixedTab) { | ||
75 | Object.keys(r).reduce(function(prev, curr) { | ||
76 | if (r[curr].length > 0 && r[curr][0].score > prev) { | ||
77 | activeTab = curr; | ||
78 | return r[curr][0].score; | ||
79 | } | ||
80 | return prev; | ||
81 | }, 0); | ||
82 | } | ||
83 | if (feelingLucky && activeTab) { | ||
84 | notify.html(messages.redirecting) | ||
85 | var firstItem = r[activeTab][0]; | ||
86 | window.location = getURL(firstItem.indexItem, firstItem.category); | ||
87 | return; | ||
88 | } | ||
89 | if (result.length > 20) { | ||
90 | if (searchTerm[searchTerm.length - 1] === ".") { | ||
91 | if (activeTab === "types" && r["members"].length > r["types"].length) { | ||
92 | activeTab = "members"; | ||
93 | } else if (activeTab === "packages" && r["types"].length > r["packages"].length) { | ||
94 | activeTab = "types"; | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | var categoryCount = Object.keys(r).reduce(function(prev, curr) { | ||
99 | return prev + (r[curr].length > 0 ? 1 : 0); | ||
100 | }, 0); | ||
101 | visibleTabs = []; | ||
102 | var tabContainer = $("<div class='table-tabs'></div>").appendTo(resultContainer); | ||
103 | for (var key in r) { | ||
104 | var id = "#result-tab-" + key.replace("searchTags", "search_tags"); | ||
105 | if (r[key].length) { | ||
106 | var count = r[key].length >= 1000 ? "999+" : r[key].length; | ||
107 | if (result.length > 20 && categoryCount > 1) { | ||
108 | var button = $("<button id='result-tab-" + key | ||
109 | + "' class='page-search-header'><span>" + categories[key] + "</span>" | ||
110 | + "<span style='font-weight: normal'> (" + count + ")</span></button>").appendTo(tabContainer); | ||
111 | button.click(key, function(e) { | ||
112 | fixedTab = true; | ||
113 | renderResult(e.data, $(this)); | ||
114 | }); | ||
115 | visibleTabs.push(key); | ||
116 | } else { | ||
117 | $("<span class='page-search-header active-table-tab'>" + categories[key] | ||
118 | + "<span style='font-weight: normal'> (" + count + ")</span></span>").appendTo(tabContainer); | ||
119 | renderTable(key, r[key]).appendTo(resultContainer); | ||
120 | tabContainer = $("<div class='table-tabs'></div>").appendTo(resultContainer); | ||
121 | |||
122 | } | ||
123 | } | ||
124 | } | ||
125 | if (activeTab && result.length > 20 && categoryCount > 1) { | ||
126 | $("button#result-tab-" + activeTab).addClass("active-table-tab"); | ||
127 | renderTable(activeTab, r[activeTab]).appendTo(resultContainer); | ||
128 | } | ||
129 | resultSection.show(); | ||
130 | function renderResult(category, button) { | ||
131 | activeTab = category; | ||
132 | setSearchUrl(); | ||
133 | resultContainer.find("div.summary-table").remove(); | ||
134 | renderTable(activeTab, r[activeTab]).appendTo(resultContainer); | ||
135 | button.siblings().removeClass("active-table-tab"); | ||
136 | button.addClass("active-table-tab"); | ||
137 | } | ||
138 | } | ||
139 | function selectTab(category) { | ||
140 | $("button#result-tab-" + category).click(); | ||
141 | } | ||
142 | function renderTable(category, items) { | ||
143 | var table = $("<div class='summary-table'>") | ||
144 | .addClass(category === "modules" | ||
145 | ? "one-column-search-results" | ||
146 | : "two-column-search-results"); | ||
147 | var col1, col2; | ||
148 | if (category === "modules") { | ||
149 | col1 = "Module"; | ||
150 | } else if (category === "packages") { | ||
151 | col1 = "Module"; | ||
152 | col2 = "Package"; | ||
153 | } else if (category === "types") { | ||
154 | col1 = "Package"; | ||
155 | col2 = "Class" | ||
156 | } else if (category === "members") { | ||
157 | col1 = "Class"; | ||
158 | col2 = "Member"; | ||
159 | } else if (category === "searchTags") { | ||
160 | col1 = "Location"; | ||
161 | col2 = "Name"; | ||
162 | } | ||
163 | $("<div class='table-header col-plain'>" + col1 + "</div>").appendTo(table); | ||
164 | if (category !== "modules") { | ||
165 | $("<div class='table-header col-plain'>" + col2 + "</div>").appendTo(table); | ||
166 | } | ||
167 | $.each(items, function(index, item) { | ||
168 | var rowColor = index % 2 ? "odd-row-color" : "even-row-color"; | ||
169 | renderItem(item, table, rowColor); | ||
170 | }); | ||
171 | return table; | ||
172 | } | ||
173 | function renderItem(item, table, rowColor) { | ||
174 | var label = getHighlightedText(item.input, item.boundaries, item.prefix.length, item.input.length); | ||
175 | var link = $("<a/>") | ||
176 | .attr("href", getURL(item.indexItem, item.category)) | ||
177 | .attr("tabindex", "0") | ||
178 | .addClass("search-result-link") | ||
179 | .html(label); | ||
180 | var container = getHighlightedText(item.input, item.boundaries, 0, item.prefix.length - 1); | ||
181 | if (item.category === "searchTags") { | ||
182 | container = item.indexItem.h || ""; | ||
183 | } | ||
184 | if (item.category !== "modules") { | ||
185 | $("<div/>").html(container).addClass("col-plain").addClass(rowColor).appendTo(table); | ||
186 | } | ||
187 | $("<div/>").html(link).addClass("col-last").addClass(rowColor).appendTo(table); | ||
188 | } | ||
189 | var timeout; | ||
190 | function schedulePageSearch() { | ||
191 | if (timeout) { | ||
192 | clearTimeout(timeout); | ||
193 | } | ||
194 | timeout = setTimeout(function () { | ||
195 | doPageSearch() | ||
196 | }, 100); | ||
197 | } | ||
198 | function doPageSearch() { | ||
199 | setSearchUrl(); | ||
200 | var term = searchTerm = input.val().trim(); | ||
201 | if (term === "") { | ||
202 | notify.html(messages.enterTerm); | ||
203 | activeTab = ""; | ||
204 | fixedTab = false; | ||
205 | resultContainer.empty(); | ||
206 | resultSection.hide(); | ||
207 | } else { | ||
208 | notify.html(messages.searching); | ||
209 | doSearch({ term: term, maxResults: 1200 }, renderResults); | ||
210 | } | ||
211 | } | ||
212 | function setSearchUrl() { | ||
213 | var query = input.val().trim(); | ||
214 | var url = document.location.pathname; | ||
215 | if (query) { | ||
216 | url += "?q=" + encodeURI(query); | ||
217 | if (activeTab && fixedTab) { | ||
218 | url += "&c=" + activeTab; | ||
219 | } | ||
220 | } | ||
221 | history.replaceState({query: query}, "", url); | ||
222 | } | ||
223 | input.on("input", function(e) { | ||
224 | feelingLucky = false; | ||
225 | schedulePageSearch(); | ||
226 | }); | ||
227 | $(document).keydown(function(e) { | ||
228 | if ((e.ctrlKey || e.metaKey) && (e.key === "ArrowLeft" || e.key === "ArrowRight")) { | ||
229 | if (activeTab && visibleTabs.length > 1) { | ||
230 | var idx = visibleTabs.indexOf(activeTab); | ||
231 | idx += e.key === "ArrowLeft" ? visibleTabs.length - 1 : 1; | ||
232 | selectTab(visibleTabs[idx % visibleTabs.length]); | ||
233 | return false; | ||
234 | } | ||
235 | } | ||
236 | }); | ||
237 | reset.click(function() { | ||
238 | notify.html(messages.enterTerm); | ||
239 | resultSection.hide(); | ||
240 | activeTab = ""; | ||
241 | fixedTab = false; | ||
242 | resultContainer.empty(); | ||
243 | input.val('').focus(); | ||
244 | setSearchUrl(); | ||
245 | }); | ||
246 | input.prop("disabled", false); | ||
247 | input.attr("autocapitalize", "off"); | ||
248 | reset.prop("disabled", false); | ||
249 | |||
250 | var urlParams = new URLSearchParams(window.location.search); | ||
251 | if (urlParams.has("q")) { | ||
252 | input.val(urlParams.get("q")) | ||
253 | } | ||
254 | if (urlParams.has("c")) { | ||
255 | activeTab = urlParams.get("c"); | ||
256 | fixedTab = true; | ||
257 | } | ||
258 | if (urlParams.get("r")) { | ||
259 | feelingLucky = true; | ||
260 | } | ||
261 | if (input.val()) { | ||
262 | doPageSearch(); | ||
263 | } else { | ||
264 | notify.html(messages.enterTerm); | ||
265 | } | ||
266 | input.select().focus(); | ||
267 | }); | ||