2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
26 var noResult = {l: "No results found"};
27 var catModules = "Modules";
28 var catPackages = "Packages";
29 var catTypes = "Types";
30 var catMembers = "Members";
31 var catSearchTags = "SearchTags";
32 var highlight = "<span class=\"resultHighlight\">$&</span>";
33 var camelCaseRegexp = "";
34 var secondaryMatcher = "";
35 function getHighlightedText(item) {
36 var ccMatcher = new RegExp(camelCaseRegexp);
37 var label = item.replace(ccMatcher, highlight);
39 label = item.replace(secondaryMatcher, highlight);
43 function getURLPrefix(ui) {
45 if (useModuleDirectories) {
47 if (ui.item.category === catModules) {
48 return ui.item.l + slash;
49 } else if (ui.item.category === catPackages && ui.item.m) {
50 return ui.item.m + slash;
51 } else if ((ui.item.category === catTypes && ui.item.p) || ui.item.category === catMembers) {
52 $.each(packageSearchIndex, function(index, item) {
53 if (item.m && ui.item.p == item.l) {
54 urlPrefix = item.m + slash;
64 var watermark = 'Search';
67 $("#search").prop("disabled", false);
68 $("#reset").prop("disabled", false);
69 $("#search").val(watermark).addClass('watermark');
70 $("#search").blur(function() {
71 if ($(this).val().length == 0) {
72 $(this).val(watermark).addClass('watermark');
75 $("#search").on('click keydown', function() {
76 if ($(this).val() == watermark) {
77 $(this).val('').removeClass('watermark');
80 $("#reset").click(function() {
85 $("#search")[0].setSelectionRange(0, 0);
87 $.widget("custom.catcomplete", $.ui.autocomplete, {
90 this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)");
92 _renderMenu: function(ul, items) {
95 rMenu.menu.bindings = $();
96 $.each(items, function(index, item) {
98 if (item.l !== noResult.l && item.category !== currentCategory) {
99 ul.append("<li class=\"ui-autocomplete-category\">" + item.category + "</li>");
100 currentCategory = item.category;
102 li = rMenu._renderItemData(ul, item);
104 li.attr("aria-label", item.category + " : " + item.l);
105 li.attr("class", "resultItem");
107 li.attr("aria-label", item.l);
108 li.attr("class", "resultItem");
112 _renderItem: function(ul, item) {
114 if (item.category === catModules) {
115 label = getHighlightedText(item.l);
116 } else if (item.category === catPackages) {
118 ? getHighlightedText(item.m + "/" + item.l)
119 : getHighlightedText(item.l);
120 } else if (item.category === catTypes) {
122 ? getHighlightedText(item.p + "." + item.l)
123 : getHighlightedText(item.l);
124 } else if (item.category === catMembers) {
125 label = getHighlightedText(item.p + "." + (item.c + "." + item.l));
126 } else if (item.category === catSearchTags) {
127 label = getHighlightedText(item.l);
131 var li = $("<li/>").appendTo(ul);
132 var div = $("<div/>").appendTo(li);
133 if (item.category === catSearchTags) {
135 div.html(label + "<span class=\"searchTagHolderResult\"> (" + item.h + ")</span><br><span class=\"searchTagDescResult\">"
136 + item.d + "</span><br>");
138 div.html(label + "<span class=\"searchTagHolderResult\"> (" + item.h + ")</span>");
147 $("#search").catcomplete({
150 source: function(request, response) {
151 var result = new Array();
152 var presult = new Array();
153 var tresult = new Array();
154 var mresult = new Array();
155 var tgresult = new Array();
156 var secondaryresult = new Array();
157 var displayCount = 0;
158 var exactMatcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term) + "$", "i");
159 camelCaseRegexp = ($.ui.autocomplete.escapeRegex(request.term)).split(/(?=[A-Z])/).join("([a-z0-9_$]*?)");
160 var camelCaseMatcher = new RegExp("^" + camelCaseRegexp);
161 secondaryMatcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
163 // Return the nested innermost name from the specified object
164 function nestedName(e) {
165 return e.l.substring(e.l.lastIndexOf(".") + 1);
168 function concatResults(a1, a2) {
174 if (moduleSearchIndex) {
176 $.each(moduleSearchIndex, function(index, item) {
177 item.category = catModules;
178 if (exactMatcher.test(item.l)) {
181 } else if (camelCaseMatcher.test(item.l)) {
183 } else if (secondaryMatcher.test(item.l)) {
184 secondaryresult.push(item);
187 displayCount = mdleCount;
188 result = concatResults(result, secondaryresult);
190 if (packageSearchIndex) {
193 $.each(packageSearchIndex, function(index, item) {
194 item.category = catPackages;
196 ? (item.m + "/" + item.l)
198 if (exactMatcher.test(item.l)) {
201 } else if (camelCaseMatcher.test(pkg)) {
203 } else if (secondaryMatcher.test(pkg)) {
204 secondaryresult.push(item);
207 result = result.concat(concatResults(presult, secondaryresult));
208 displayCount = (pCount > displayCount) ? pCount : displayCount;
210 if (typeSearchIndex) {
212 $.each(typeSearchIndex, function(index, item) {
213 item.category = catTypes;
214 var s = nestedName(item);
215 if (exactMatcher.test(s)) {
218 } else if (camelCaseMatcher.test(s)) {
220 } else if (secondaryMatcher.test(item.p + "." + item.l)) {
221 secondaryresult.push(item);
224 result = result.concat(concatResults(tresult, secondaryresult));
225 displayCount = (tCount > displayCount) ? tCount : displayCount;
227 if (memberSearchIndex) {
229 $.each(memberSearchIndex, function(index, item) {
230 item.category = catMembers;
231 var s = nestedName(item);
232 if (exactMatcher.test(s)) {
235 } else if (camelCaseMatcher.test(s)) {
237 } else if (secondaryMatcher.test(item.c + "." + item.l)) {
238 secondaryresult.push(item);
241 result = result.concat(concatResults(mresult, secondaryresult));
242 displayCount = (mCount > displayCount) ? mCount : displayCount;
244 if (tagSearchIndex) {
246 $.each(tagSearchIndex, function(index, item) {
247 item.category = catSearchTags;
248 if (exactMatcher.test(item.l)) {
251 } else if (secondaryMatcher.test(item.l)) {
252 secondaryresult.push(item);
255 result = result.concat(concatResults(tgresult, secondaryresult));
256 displayCount = (tgCount > displayCount) ? tgCount : displayCount;
258 displayCount = (displayCount > 500) ? displayCount : 500;
259 var counter = function() {
260 var count = {Modules: 0, Packages: 0, Types: 0, Members: 0, SearchTags: 0};
261 var f = function(item) {
262 count[item.category] += 1;
263 return (count[item.category] <= displayCount);
267 response(result.filter(counter));
269 response: function(event, ui) {
270 if (!ui.content.length) {
271 ui.content.push(noResult);
273 $("#search").empty();
280 select: function(event, ui) {
281 if (ui.item.l !== noResult.l) {
282 var url = getURLPrefix(ui);
283 if (ui.item.category === catModules) {
284 if (useModuleDirectories) {
285 url += "module-summary.html";
287 url = ui.item.l + "-summary.html";
289 } else if (ui.item.category === catPackages) {
293 url += ui.item.l.replace(/\./g, '/') + "/package-summary.html";
295 } else if (ui.item.category === catTypes) {
298 } else if (ui.item.p === "<Unnamed>") {
299 url += ui.item.l + ".html";
301 url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.l + ".html";
303 } else if (ui.item.category === catMembers) {
304 if (ui.item.p === "<Unnamed>") {
305 url += ui.item.c + ".html" + "#";
307 url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.c + ".html" + "#";
314 } else if (ui.item.category === catSearchTags) {
317 if (top !== window) {
318 parent.classFrame.location = pathtoroot + url;
320 window.location.href = pathtoroot + url;
322 $("#search").focus();