This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
search.js
136 lines (122 loc) · 3.64 KB
/
search.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
var search_input = $('#fsearch');
var search_window = $('#fsearch_window');
var content_frame = $('#content_frame');
var page_link = $('#pageLink');
var lis = null;
change_page = function (pagename) {
content_frame.attr ('src', 'monodoc.ashx?link=' + pagename);
page_link.attr ('href', '?link=' + pagename);
if (window.history && window.history.pushState)
window.history.pushState (null, '', '/?link=' + pagename);
};
page_link.attr ('href', document.location.search);
var is_shown = false;
var hide = function () {
if (!is_shown)
return;
search_window.css ({'display' : 'none', 'opacity' : 0});
is_shown = false;
};
var show = function () {
if (is_shown)
return;
search_window.css ({'display' : 'block', 'height' : 'auto', 'opacity' : 1.0, 'width': search_input.width() + 'px'});
is_shown = true;
};
search_input.blur (function () {
window.setTimeout (hide, 200);
if (search_input.val ().length == 0)
search_input.css ('width', '19em');
});
search_input.focus (function () {
search_input.css ('width', '29em');
if (search_window.text().length > 0 && search_input.val().length > 0)
show ();
window.setTimeout (function () {
search_input[0].select ();
}, 10);
});
search_input.keyup (function (event) {
if ($(this).val () == "")
hide();
// Only process if we receive an alnum or return or del
if (event.which != 8 && event.which != 46
&& (event.which < 'A'.charCodeAt(0) || event.which > 'Z'.charCodeAt(0))
&& (event.which < '0'.charCodeAt(0) || event.which > '9'.charCodeAt(0)))
return;
var callback = function (data) {
if (data == null || data.length == 0)
return;
var items = [];
$.each (data, function(key, val) {
var item = val.name;
var url = val.url.replace (/[<>]/g, function (c) { return c == '<' ? '{' : '}'; });
items.push('<li><a href="#" onclick="change_page(\''+url+'\')" title="'+(val.fulltitle == '' ? val.name : val.fulltitle)+'">' + item + '</a></li>');
});
var uls = $('<ul/>', { html: items.join (''), 'style': 'list-style-type:none; margin: 0; padding:0' });
lis = uls.children ('li');
var companion = $('#fsearch_companion');
lis.hover (function () {
var childA = $(this).children('a');
var offset = childA.offset ();
companion.css ({ 'top': offset.top + 'px', 'right': $('html').outerWidth () - offset.left + 10, 'display': 'block'});
companion.text(childA.attr ('title'));
}, function () {
companion.css ('display', 'none');
});
search_window.empty();
uls.appendTo ('#fsearch_window');
show ();
};
$.getJSON ('monodoc.ashx?fsearch=' + $(this).val (), callback);
});
document.getElementById ('fsearch').onsearch = function () {
if (search_input.val () == "") {
hide ();
search_input.blur ();
}
};
search_input.keydown (function (event) {
if (lis == null)
return;
var selected = lis.filter('.selected');
var newSelection = null;
$('#fsearch_companion').css ('display', 'none');
switch (event.which)
{
case 13: // return
if (selected.length != 0) {
selected.children ('a').click ();
hide ();
search_input.blur ();
}
return false;
case 38: // up
if (selected.length != 0) {
var prev = selected.prev ();
if (prev.length != 0)
newSelection = prev;
} else {
newSelection = lis.last ();
}
break;
case 40: // down
if (selected.length != 0) {
var next = selected.next ();
if (next.length != 0)
newSelection = next;
} else {
newSelection = lis.first ();
}
break;
}
if (newSelection != null) {
newSelection.addClass ('selected');
if (selected != null) {
selected.removeClass ('selected');
selected.mouseleave();
}
newSelection.mouseenter();
selected = newSelection;
}
});