search.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. rMenu.menu.bindings = $();
  95. $.each(items, function(index, item) {
  96. var li;
  97. if (item.l !== noResult.l && item.category !== currentCategory) {
  98. ul.append("<li class=\"ui-autocomplete-category\">" + item.category + "</li>");
  99. currentCategory = item.category;
  100. }
  101. li = rMenu._renderItemData(ul, item);
  102. if (item.category) {
  103. li.attr("aria-label", item.category + " : " + item.l);
  104. li.attr("class", "resultItem");
  105. } else {
  106. li.attr("aria-label", item.l);
  107. li.attr("class", "resultItem");
  108. }
  109. });
  110. },
  111. _renderItem: function(ul, item) {
  112. var label = "";
  113. if (item.category === catModules) {
  114. label = getHighlightedText(item.l);
  115. } else if (item.category === catPackages) {
  116. label = (item.m)
  117. ? getHighlightedText(item.m + "/" + item.l)
  118. : getHighlightedText(item.l);
  119. } else if (item.category === catTypes) {
  120. label = (item.p)
  121. ? getHighlightedText(item.p + "." + item.l)
  122. : getHighlightedText(item.l);
  123. } else if (item.category === catMembers) {
  124. label = getHighlightedText(item.p + "." + (item.c + "." + item.l));
  125. } else if (item.category === catSearchTags) {
  126. label = getHighlightedText(item.l);
  127. } else {
  128. label = item.l;
  129. }
  130. var li = $("<li/>").appendTo(ul);
  131. var div = $("<div/>").appendTo(li);
  132. if (item.category === catSearchTags) {
  133. if (item.d) {
  134. div.html(label + "<span class=\"searchTagHolderResult\"> (" + item.h + ")</span><br><span class=\"searchTagDescResult\">"
  135. + item.d + "</span><br>");
  136. } else {
  137. div.html(label + "<span class=\"searchTagHolderResult\"> (" + item.h + ")</span>");
  138. }
  139. } else {
  140. div.html(label);
  141. }
  142. return li;
  143. }
  144. });
  145. $(function() {
  146. $("#search").catcomplete({
  147. minLength: 1,
  148. delay: 100,
  149. source: function(request, response) {
  150. var result = new Array();
  151. var presult = new Array();
  152. var tresult = new Array();
  153. var mresult = new Array();
  154. var tgresult = new Array();
  155. var secondaryresult = new Array();
  156. var displayCount = 0;
  157. var exactMatcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term) + "$", "i");
  158. camelCaseRegexp = ($.ui.autocomplete.escapeRegex(request.term)).split(/(?=[A-Z])/).join("([a-z0-9_$]*?)");
  159. var camelCaseMatcher = new RegExp("^" + camelCaseRegexp);
  160. secondaryMatcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
  161. // Return the nested innermost name from the specified object
  162. function nestedName(e) {
  163. return e.l.substring(e.l.lastIndexOf(".") + 1);
  164. }
  165. function concatResults(a1, a2) {
  166. a1 = a1.concat(a2);
  167. a2.length = 0;
  168. return a1;
  169. }
  170. if (moduleSearchIndex) {
  171. var mdleCount = 0;
  172. $.each(moduleSearchIndex, function(index, item) {
  173. item.category = catModules;
  174. if (exactMatcher.test(item.l)) {
  175. result.push(item);
  176. mdleCount++;
  177. } else if (camelCaseMatcher.test(item.l)) {
  178. result.push(item);
  179. } else if (secondaryMatcher.test(item.l)) {
  180. secondaryresult.push(item);
  181. }
  182. });
  183. displayCount = mdleCount;
  184. result = concatResults(result, secondaryresult);
  185. }
  186. if (packageSearchIndex) {
  187. var pCount = 0;
  188. var pkg = "";
  189. $.each(packageSearchIndex, function(index, item) {
  190. item.category = catPackages;
  191. pkg = (item.m)
  192. ? (item.m + "/" + item.l)
  193. : item.l;
  194. if (exactMatcher.test(item.l)) {
  195. presult.push(item);
  196. pCount++;
  197. } else if (camelCaseMatcher.test(pkg)) {
  198. presult.push(item);
  199. } else if (secondaryMatcher.test(pkg)) {
  200. secondaryresult.push(item);
  201. }
  202. });
  203. result = result.concat(concatResults(presult, secondaryresult));
  204. displayCount = (pCount > displayCount) ? pCount : displayCount;
  205. }
  206. if (typeSearchIndex) {
  207. var tCount = 0;
  208. $.each(typeSearchIndex, function(index, item) {
  209. item.category = catTypes;
  210. var s = nestedName(item);
  211. if (exactMatcher.test(s)) {
  212. tresult.push(item);
  213. tCount++;
  214. } else if (camelCaseMatcher.test(s)) {
  215. tresult.push(item);
  216. } else if (secondaryMatcher.test(item.p + "." + item.l)) {
  217. secondaryresult.push(item);
  218. }
  219. });
  220. result = result.concat(concatResults(tresult, secondaryresult));
  221. displayCount = (tCount > displayCount) ? tCount : displayCount;
  222. }
  223. if (memberSearchIndex) {
  224. var mCount = 0;
  225. $.each(memberSearchIndex, function(index, item) {
  226. item.category = catMembers;
  227. var s = nestedName(item);
  228. if (exactMatcher.test(s)) {
  229. mresult.push(item);
  230. mCount++;
  231. } else if (camelCaseMatcher.test(s)) {
  232. mresult.push(item);
  233. } else if (secondaryMatcher.test(item.c + "." + item.l)) {
  234. secondaryresult.push(item);
  235. }
  236. });
  237. result = result.concat(concatResults(mresult, secondaryresult));
  238. displayCount = (mCount > displayCount) ? mCount : displayCount;
  239. }
  240. if (tagSearchIndex) {
  241. var tgCount = 0;
  242. $.each(tagSearchIndex, function(index, item) {
  243. item.category = catSearchTags;
  244. if (exactMatcher.test(item.l)) {
  245. tgresult.push(item);
  246. tgCount++;
  247. } else if (secondaryMatcher.test(item.l)) {
  248. secondaryresult.push(item);
  249. }
  250. });
  251. result = result.concat(concatResults(tgresult, secondaryresult));
  252. displayCount = (tgCount > displayCount) ? tgCount : displayCount;
  253. }
  254. displayCount = (displayCount > 500) ? displayCount : 500;
  255. var counter = function() {
  256. var count = {Modules: 0, Packages: 0, Types: 0, Members: 0, SearchTags: 0};
  257. var f = function(item) {
  258. count[item.category] += 1;
  259. return (count[item.category] <= displayCount);
  260. };
  261. return f;
  262. }();
  263. response(result.filter(counter));
  264. },
  265. response: function(event, ui) {
  266. if (!ui.content.length) {
  267. ui.content.push(noResult);
  268. } else {
  269. $("#search").empty();
  270. }
  271. },
  272. autoFocus: true,
  273. position: {
  274. collision: "flip"
  275. },
  276. select: function(event, ui) {
  277. if (ui.item.l !== noResult.l) {
  278. var url = getURLPrefix(ui);
  279. if (ui.item.category === catModules) {
  280. if (useModuleDirectories) {
  281. url += "module-summary.html";
  282. } else {
  283. url = ui.item.l + "-summary.html";
  284. }
  285. } else if (ui.item.category === catPackages) {
  286. if (ui.item.url) {
  287. url = ui.item.url;
  288. } else {
  289. url += ui.item.l.replace(/\./g, '/') + "/package-summary.html";
  290. }
  291. } else if (ui.item.category === catTypes) {
  292. if (ui.item.url) {
  293. url = ui.item.url;
  294. } else if (ui.item.p === "<Unnamed>") {
  295. url += ui.item.l + ".html";
  296. } else {
  297. url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.l + ".html";
  298. }
  299. } else if (ui.item.category === catMembers) {
  300. if (ui.item.p === "<Unnamed>") {
  301. url += ui.item.c + ".html" + "#";
  302. } else {
  303. url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.c + ".html" + "#";
  304. }
  305. if (ui.item.url) {
  306. url += ui.item.url;
  307. } else {
  308. url += ui.item.l;
  309. }
  310. } else if (ui.item.category === catSearchTags) {
  311. url += ui.item.u;
  312. }
  313. if (top !== window) {
  314. parent.classFrame.location = pathtoroot + url;
  315. } else {
  316. window.location.href = pathtoroot + url;
  317. }
  318. $("#search").focus();
  319. }
  320. }
  321. });
  322. });