langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/resources/search.js
changeset 33920 bd731341c405
equal deleted inserted replaced
33919:e9fccc09cfc6 33920:bd731341c405
       
     1 /*
       
     2  * Copyright (c) 2015, 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 
       
    26 var noResult = {l: "No results found"};
       
    27 var category = "category";
       
    28 var catPackages = "Packages";
       
    29 var catTypes = "Types";
       
    30 var catMembers = "Members";
       
    31 var catSearchTags = "SearchTags";
       
    32 function getName(name) {
       
    33     var anchor = "";
       
    34     var ch = '';
       
    35     for (i = 0; i < name.length; i++) {
       
    36         ch = name.charAt(i);
       
    37         switch (ch) {
       
    38             case '(':
       
    39             case ')':
       
    40             case '<':
       
    41             case '>':
       
    42             case ',':
       
    43                 anchor += "-";
       
    44                 break;
       
    45             case ' ':
       
    46             case '[':
       
    47                 break;
       
    48             case ']':
       
    49                 anchor += ":A";
       
    50                 break;
       
    51             case '$':
       
    52                 if (i == 0)
       
    53                     anchor += "Z:Z";
       
    54                 anchor += ":D";
       
    55                 break;
       
    56             case '_':
       
    57                 if (i == 0)
       
    58                     anchor += "Z:Z";
       
    59                 anchor += ch;
       
    60                 break;
       
    61             default:
       
    62                 anchor += ch;
       
    63         }
       
    64     }
       
    65     return anchor;
       
    66 }
       
    67 var watermark = 'Search';
       
    68 $(function() {
       
    69     $("#search").prop("disabled", false);
       
    70     $("#reset").prop("disabled", false);
       
    71     $("#search").val(watermark).addClass('watermark');
       
    72     $("#search").blur(function(){
       
    73         if ($(this).val().length == 0) {
       
    74             $(this).val(watermark).addClass('watermark');
       
    75         }
       
    76     });
       
    77     $("#search").keydown(function(){
       
    78        if ($(this).val() == watermark) {
       
    79             $(this).val('').removeClass('watermark');
       
    80         }
       
    81     });
       
    82     $("#reset").click(function(){
       
    83        $("#search").val('');
       
    84        $("#search").focus();
       
    85     });
       
    86     $("#search").focus();
       
    87     $("#search")[0].setSelectionRange(0,0);
       
    88 });
       
    89 $.widget("custom.catcomplete", $.ui.autocomplete, {
       
    90     _create: function() {
       
    91         this._super();
       
    92         this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)");
       
    93     },
       
    94     _renderMenu: function(ul, items) {
       
    95         var rMenu = this,
       
    96                 currentCategory = "";
       
    97         $.each(items, function(index, item) {
       
    98             var li;
       
    99             if (item.l !== noResult.l && item.category !== currentCategory) {
       
   100                 ul.append("<li class=\"ui-autocomplete-category\">" + item.category + "</li>");
       
   101                 currentCategory = item.category;
       
   102             }
       
   103             li = rMenu._renderItemData(ul, item);
       
   104             if (item.category) {
       
   105                 li.attr("aria-label", item.category + " : " + item.l);
       
   106                 li.attr("class", "resultItem");
       
   107             } else {
       
   108                 li.attr("aria-label", item.l);
       
   109                 li.attr("class", "resultItem");
       
   110             }
       
   111         });
       
   112     },
       
   113     _renderItem: function(ul, item) {
       
   114         var result = this.element.val();
       
   115         var regexp = new RegExp($.ui.autocomplete.escapeRegex(result), "i");
       
   116         highlight = "<span class=\"resultHighlight\">$&</span>";
       
   117         var label = "";
       
   118         if (item.category === catPackages) {
       
   119             label = item.l.replace(regexp, highlight);
       
   120         } else if (item.category === catTypes) {
       
   121             label += (item.p + "." + item.l).replace(regexp, highlight);
       
   122         } else if (item.category === catMembers) {
       
   123             label += item.p + "." + (item.c + "." + item.l).replace(regexp, highlight);
       
   124         } else if (item.category === catSearchTags) {
       
   125             label = item.l.replace(regexp, highlight);
       
   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 tresult = new Array();
       
   156             var mresult = new Array();
       
   157             var tgresult = new Array();
       
   158             var displayCount = 0;
       
   159             var exactMatcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term) + "$", "i");
       
   160             var secondaryMatcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
       
   161             if (packageSearchIndex) {
       
   162                 var pCount = 0;
       
   163                 $.each(packageSearchIndex, function(index, item) {
       
   164                     item[category] = catPackages;
       
   165                     if (exactMatcher.test(item.l)) {
       
   166                         result.unshift(item);
       
   167                         pCount++;
       
   168                     } else if (secondaryMatcher.test(item.l)) {
       
   169                         result.push(item);
       
   170                     }
       
   171                 });
       
   172                 displayCount = pCount;
       
   173             }
       
   174             if (typeSearchIndex) {
       
   175                 var tCount = 0;
       
   176                 $.each(typeSearchIndex, function(index, item) {
       
   177                     item[category] = catTypes;
       
   178                     if (exactMatcher.test(item.l)) {
       
   179                         tresult.unshift(item);
       
   180                         tCount++;
       
   181                     } else if (secondaryMatcher.test(item.p + "." + item.l)) {
       
   182                         tresult.push(item);
       
   183                     }
       
   184                 });
       
   185                 result = result.concat(tresult);
       
   186                 displayCount = (tCount > displayCount) ? tCount : displayCount;
       
   187             }
       
   188             if (memberSearchIndex) {
       
   189                 var mCount = 0;
       
   190                 $.each(memberSearchIndex, function(index, item) {
       
   191                     item[category] = catMembers;
       
   192                     if (exactMatcher.test(item.l)) {
       
   193                         mresult.unshift(item);
       
   194                         mCount++;
       
   195                     } else if (secondaryMatcher.test(item.c + "." + item.l)) {
       
   196                         mresult.push(item);
       
   197                     }
       
   198                 });
       
   199                 result = result.concat(mresult);
       
   200                 displayCount = (mCount > displayCount) ? mCount : displayCount;
       
   201             }
       
   202             if (tagSearchIndex) {
       
   203                 var tgCount = 0;
       
   204                 $.each(tagSearchIndex, function(index, item) {
       
   205                     item[category] = catSearchTags;
       
   206                     if (exactMatcher.test(item.l)) {
       
   207                         tgresult.unshift(item);
       
   208                         tgCount++;
       
   209                     } else if (secondaryMatcher.test(item.l)) {
       
   210                         tgresult.push(item);
       
   211                     }
       
   212                 });
       
   213                 result = result.concat(tgresult);
       
   214                 displayCount = (tgCount > displayCount) ? tgCount : displayCount;
       
   215             }
       
   216             displayCount = (displayCount > 500) ? displayCount : 500;
       
   217             var counter = function() {
       
   218                 var count = {Packages: 0, Types: 0, Members: 0, SearchTags: 0};
       
   219                 var f = function(item) {
       
   220                     count[item.category] += 1;
       
   221                     return (count[item.category] <= displayCount);
       
   222                 };
       
   223                 return f;
       
   224             }();
       
   225             response(result.filter(counter));
       
   226         },
       
   227         response: function(event, ui) {
       
   228             if (!ui.content.length) {
       
   229                 ui.content.push(noResult);
       
   230             } else {
       
   231                 $("#search").empty();
       
   232             }
       
   233         },
       
   234         autoFocus: true,
       
   235         position: {
       
   236             collision: "flip"
       
   237         },
       
   238         select: function(event, ui) {
       
   239             if (ui.item.l !== noResult.l) {
       
   240                 var url = "";
       
   241                 if (ui.item.category === catPackages) {
       
   242                     url = ui.item.l.replace(/\./g, '/') + "/package-summary.html";
       
   243                 } else if (ui.item.category === catTypes) {
       
   244                     if (ui.item.p === "<Unnamed>") {
       
   245                         url = "/" + ui.item.l + ".html";
       
   246                     } else {
       
   247                         url = ui.item.p.replace(/\./g, '/') + "/" + ui.item.l + ".html";
       
   248                     }
       
   249                 } else if (ui.item.category === catMembers) {
       
   250                     if (ui.item.p === "<Unnamed>") {
       
   251                         url = "/" + ui.item.c + ".html" + "#";
       
   252                     } else {
       
   253                         url = ui.item.p.replace(/\./g, '/') + "/" + ui.item.c + ".html" + "#";
       
   254                     }
       
   255                     if (ui.item.url) {
       
   256                         url += ui.item.url;
       
   257                     } else {
       
   258                         url += getName(ui.item.l);
       
   259                     }
       
   260                 } else if (ui.item.category === catSearchTags) {
       
   261                     url += ui.item.u;
       
   262                 }
       
   263                 if (top !== window) {
       
   264                     parent.classFrame.location = pathtoroot + url;
       
   265                 } else {
       
   266                     window.location.href = pathtoroot + url;
       
   267                 }
       
   268             }
       
   269         }
       
   270     });
       
   271 });