forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddox.js
140 lines (112 loc) · 3.58 KB
/
ddox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function setupDdox()
{
$(".tree-view").children(".package").click(toggleTree);
$(".tree-view.collapsed").children("ul").hide();
$("#symbolSearch").attr("tabindex", "1000");
updateSearchBox();
$('#sitesearch').change(updateSearchBox);
}
function updateSearchBox()
{
var ddox = $('#sitesearch').val() == "dlang.org/library";
$('#q').toggle(!ddox);
$('#symbolSearch').toggle(ddox);
}
function toggleTree()
{
node = $(this).parent();
node.toggleClass("collapsed");
if( node.hasClass("collapsed") ){
node.children("ul").hide();
} else {
node.children("ul").show();
}
return false;
}
var searchCounter = 0;
var lastSearchString = "";
function performSymbolSearch(maxlen)
{
if (maxlen === 'undefined') maxlen = 26;
var searchstring = $("#symbolSearch").val().toLowerCase();
if (searchstring == lastSearchString) return;
lastSearchString = searchstring;
var scnt = ++searchCounter;
$('#symbolSearchResults').hide();
$('#symbolSearchResults').empty();
var terms = $.trim(searchstring).split(/\s+/);
if (terms.length == 0 || (terms.length == 1 && terms[0].length < 2)) return;
var results = [];
for (i in symbols) {
var sym = symbols[i];
var all_match = true;
for (j in terms)
if (sym.name.toLowerCase().indexOf(terms[j]) < 0) {
all_match = false;
break;
}
if (!all_match) continue;
results.push(sym);
}
function compare(a, b) {
// prefer non-deprecated matches
var adep = a.attributes.indexOf("deprecated") >= 0;
var bdep = b.attributes.indexOf("deprecated") >= 0;
if (adep != bdep) return adep - bdep;
// normalize the names
var aname = a.name.toLowerCase();
var bname = b.name.toLowerCase();
var anameparts = aname.split(".");
var bnameparts = bname.split(".");
var asname = anameparts[anameparts.length-1];
var bsname = bnameparts[bnameparts.length-1];
// prefer exact matches
var aexact = terms.indexOf(asname) >= 0;
var bexact = terms.indexOf(bsname) >= 0;
if (aexact != bexact) return bexact - aexact;
// prefer elements with less nesting
if (anameparts.length < bnameparts.length) return -1;
if (anameparts.length > bnameparts.length) return 1;
// prefer matches with a shorter name
if (asname.length < bsname.length) return -1;
if (asname.length > bsname.length) return 1;
// sort the rest alphabetically
if (aname < bname) return -1;
if (aname > bname) return 1;
return 0;
}
results.sort(compare);
for (i = 0; i < results.length && i < 100; i++) {
var sym = results[i];
var el = $(document.createElement("li"));
el.addClass(sym.kind);
for (j in sym.attributes)
el.addClass(sym.attributes[j]);
var name = sym.name;
// compute a length limited representation of the full name
var nameparts = name.split(".");
var np = nameparts.length-1;
var shortname = "." + nameparts[np];
while (np > 0 && nameparts[np-1].length + shortname.length <= maxlen) {
np--;
shortname = "." + nameparts[np] + shortname;
}
if (np > 0) shortname = ".." + shortname;
else shortname = shortname.substr(1);
el.append('<a href="'+symbolSearchRootDir+sym.path+'" title="'+name+'" tabindex="1001">'+shortname+'</a>');
$('#symbolSearchResults').append(el);
}
if (results.length > 100) {
$('#symbolSearchResults').append("<li>…"+(results.length-100)+" additional results</li>");
}
$('#symbolSearchResults').show();
}
$(function(){
$("#search-box form").on("submit", function(e) {
var searchResults = $('#symbolSearchResults').children();
if (searchResults.length > 0) {
window.location = searchResults.first().find("a").attr("href");
e.preventDefault();
}
});
});