search.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  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.
  4. *
  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.
  10. *
  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).
  16. *
  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.
  20. *
  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
  23. * questions.
  24. */
  25. var noResult = {l: "No results found"};
  26. var catModules = "Modules";
  27. var catPackages = "Packages";
  28. var catTypes = "Types";
  29. var catMembers = "Members";
  30. var catSearchTags = "SearchTags";
  31. var highlight = "<span class=\"resultHighlight\">$&</span>";
  32. var camelCaseRegexp = "";
  33. var secondaryMatcher = "";
  34. function getHighlightedText(item) {
  35. var ccMatcher = new RegExp(camelCaseRegexp);
  36. var label = item.replace(ccMatcher, highlight);
  37. if (label === item) {
  38. label = item.replace(secondaryMatcher, highlight);
  39. }
  40. return label;
  41. }
  42. function getURLPrefix(ui) {
  43. var urlPrefix="";
  44. if (useModuleDirectories) {
  45. var slash = "/";
  46. if (ui.item.category === catModules) {
  47. return ui.item.l + slash;
  48. } else if (ui.item.category === catPackages && ui.item.m) {
  49. return ui.item.m + slash;
  50. } else if ((ui.item.category === catTypes && ui.item.p) || ui.item.category === catMembers) {
  51. $.each(packageSearchIndex, function(index, item) {
  52. if (ui.item.p == item.l) {
  53. urlPrefix = item.m + slash;
  54. }
  55. });
  56. return urlPrefix;
  57. } else {
  58. return urlPrefix;
  59. }
  60. }
  61. return urlPrefix;
  62. }
  63. var watermark = 'Search';
  64. $(function() {
  65. $("#search").val('');
  66. $("#search").prop("disabled", false);
  67. $("#reset").prop("disabled", false);
  68. $("#search").val(watermark).addClass('watermark');
  69. $("#search").blur(function() {
  70. if ($(this).val().length == 0) {
  71. $(this).val(watermark).addClass('watermark');
  72. }
  73. });
  74. $("#search").on('click keydown', function() {
  75. if ($(this).val() == watermark) {
  76. $(this).val('').removeClass('watermark');
  77. }
  78. });
  79. $("#reset").click(function() {
  80. $("#search").val('');
  81. $("#search").focus();
  82. });
  83. $("#search").focus();
  84. $("#search")[0].setSelectionRange(0, 0);
  85. });
  86. $.widget("custom.catcomplete", $.ui.autocomplete, {
  87. _create: function() {
  88. this._super();
  89. this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)");
  90. },
  91. _renderMenu: function(ul, items) {
  92. var rMenu = this,
  93. currentCategory = "";
  94. $.each(items, function(index, item) {
  95. var li;
  96. if (item.l !== noResult.l && item.category !== currentCategory) {
  97. ul.append("<li class=\"ui-autocomplete-category\">" + item.category + "</li>");
  98. currentCategory = item.category;
  99. }
  100. li = rMenu._renderItemData(ul, item);
  101. if (item.category) {
  102. li.attr("aria-label", item.category + " : " + item.l);
  103. li.attr("class", "resultItem");
  104. } else {
  105. li.attr("aria-label", item.l);
  106. li.attr("class", "resultItem");
  107. }
  108. });
  109. },
  110. _renderItem: function(ul, item) {
  111. var label = "";
  112. if (item.category === catModules) {
  113. label = getHighlightedText(item.l);
  114. } else if (item.category === catPackages) {
  115. label = (item.m)
  116. ? getHighlightedText(item.m + "/" + item.l)
  117. : getHighlightedText(item.l);
  118. } else if (item.category === catTypes) {
  119. label = (item.p)
  120. ? getHighlightedText(item.p + "." + item.l)
  121. : getHighlightedText(item.l);
  122. } else if (item.category === catMembers) {
  123. label = getHighlightedText(item.p + "." + (item.c + "." + item.l));
  124. } else if (item.category === catSearchTags) {
  125. label = getHighlightedText(item.l);
  126. } else {
  127. label = item.l;
  128. }
  129. $li = $("<li/>").appendTo(ul);
  130. if (item.category === catSearchTags) {
  131. if (item.d) {
  132. $("<a/>").attr("href", "#")
  133. .html(label + "<span class=\"searchTagHolderResult\"> (" + item.h + ")</span><br><span class=\"searchTagDescResult\">"
  134. + item.d + "</span><br>")
  135. .appendTo($li);
  136. } else {
  137. $("<a/>").attr("href", "#")
  138. .html(label + "<span class=\"searchTagHolderResult\"> (" + item.h + ")</span>")
  139. .appendTo($li);
  140. }
  141. } else {
  142. $("<a/>").attr("href", "#")
  143. .html(label)
  144. .appendTo($li);
  145. }
  146. return $li;
  147. }
  148. });
  149. $(function() {
  150. $("#search").catcomplete({
  151. minLength: 1,
  152. delay: 100,
  153. source: function(request, response) {
  154. var result = new Array();
  155. var presult = new Array();
  156. var tresult = new Array();
  157. var mresult = new Array();
  158. var tgresult = new Array();
  159. var secondaryresult = new Array();
  160. var displayCount = 0;
  161. var exactMatcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term) + "$", "i");
  162. camelCaseRegexp = ($.ui.autocomplete.escapeRegex(request.term)).split(/(?=[A-Z])/).join("([a-z0-9_$]*?)");
  163. var camelCaseMatcher = new RegExp("^" + camelCaseRegexp);
  164. secondaryMatcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
  165. // Return the nested innermost name from the specified object
  166. function nestedName(e) {
  167. return e.l.substring(e.l.lastIndexOf(".") + 1);
  168. }
  169. function concatResults(a1, a2) {
  170. a1 = a1.concat(a2);
  171. a2.length = 0;
  172. return a1;
  173. }
  174. if (moduleSearchIndex) {
  175. var mdleCount = 0;
  176. $.each(moduleSearchIndex, function(index, item) {
  177. item.category = catModules;
  178. if (exactMatcher.test(item.l)) {
  179. result.push(item);
  180. mdleCount++;
  181. } else if (camelCaseMatcher.test(item.l)) {
  182. result.push(item);
  183. } else if (secondaryMatcher.test(item.l)) {
  184. secondaryresult.push(item);
  185. }
  186. });
  187. displayCount = mdleCount;
  188. result = concatResults(result, secondaryresult);
  189. }
  190. if (packageSearchIndex) {
  191. var pCount = 0;
  192. var pkg = "";
  193. $.each(packageSearchIndex, function(index, item) {
  194. item.category = catPackages;
  195. pkg = (item.m)
  196. ? (item.m + "/" + item.l)
  197. : item.l;
  198. if (exactMatcher.test(item.l)) {
  199. presult.push(item);
  200. pCount++;
  201. } else if (camelCaseMatcher.test(pkg)) {
  202. presult.push(item);
  203. } else if (secondaryMatcher.test(pkg)) {
  204. secondaryresult.push(item);
  205. }
  206. });
  207. result = result.concat(concatResults(presult, secondaryresult));
  208. displayCount = (pCount > displayCount) ? pCount : displayCount;
  209. }
  210. if (typeSearchIndex) {
  211. var tCount = 0;
  212. $.each(typeSearchIndex, function(index, item) {
  213. item.category = catTypes;
  214. var s = nestedName(item);
  215. if (exactMatcher.test(s)) {
  216. tresult.push(item);
  217. tCount++;
  218. } else if (camelCaseMatcher.test(s)) {
  219. tresult.push(item);
  220. } else if (secondaryMatcher.test(item.p + "." + item.l)) {
  221. secondaryresult.push(item);
  222. }
  223. });
  224. result = result.concat(concatResults(tresult, secondaryresult));
  225. displayCount = (tCount > displayCount) ? tCount : displayCount;
  226. }
  227. if (memberSearchIndex) {
  228. var mCount = 0;
  229. $.each(memberSearchIndex, function(index, item) {
  230. item.category = catMembers;
  231. var s = nestedName(item);
  232. if (exactMatcher.test(s)) {
  233. mresult.push(item);
  234. mCount++;
  235. } else if (camelCaseMatcher.test(s)) {
  236. mresult.push(item);
  237. } else if (secondaryMatcher.test(item.c + "." + item.l)) {
  238. secondaryresult.push(item);
  239. }
  240. });
  241. result = result.concat(concatResults(mresult, secondaryresult));
  242. displayCount = (mCount > displayCount) ? mCount : displayCount;
  243. }
  244. if (tagSearchIndex) {
  245. var tgCount = 0;
  246. $.each(tagSearchIndex, function(index, item) {
  247. item.category = catSearchTags;
  248. if (exactMatcher.test(item.l)) {
  249. tgresult.push(item);
  250. tgCount++;
  251. } else if (secondaryMatcher.test(item.l)) {
  252. secondaryresult.push(item);
  253. }
  254. });
  255. result = result.concat(concatResults(tgresult, secondaryresult));
  256. displayCount = (tgCount > displayCount) ? tgCount : displayCount;
  257. }
  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);
  264. };
  265. return f;
  266. }();
  267. response(result.filter(counter));
  268. },
  269. response: function(event, ui) {
  270. if (!ui.content.length) {
  271. ui.content.push(noResult);
  272. } else {
  273. $("#search").empty();
  274. }
  275. },
  276. autoFocus: true,
  277. position: {
  278. collision: "flip"
  279. },
  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";
  286. } else {
  287. url = ui.item.l + "-summary.html";
  288. }
  289. } else if (ui.item.category === catPackages) {
  290. if (ui.item.url) {
  291. url = ui.item.url;
  292. } else {
  293. url += ui.item.l.replace(/\./g, '/') + "/package-summary.html";
  294. }
  295. } else if (ui.item.category === catTypes) {
  296. if (ui.item.url) {
  297. url = ui.item.url;
  298. } else if (ui.item.p === "<Unnamed>") {
  299. url += ui.item.l + ".html";
  300. } else {
  301. url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.l + ".html";
  302. }
  303. } else if (ui.item.category === catMembers) {
  304. if (ui.item.p === "<Unnamed>") {
  305. url += ui.item.c + ".html" + "#";
  306. } else {
  307. url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.c + ".html" + "#";
  308. }
  309. if (ui.item.url) {
  310. url += ui.item.url;
  311. } else {
  312. url += ui.item.l;
  313. }
  314. } else if (ui.item.category === catSearchTags) {
  315. url += ui.item.u;
  316. }
  317. if (top !== window) {
  318. parent.classFrame.location = pathtoroot + url;
  319. } else {
  320. window.location.href = pathtoroot + url;
  321. }
  322. }
  323. }
  324. });
  325. });