-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathomnibar.js
116 lines (87 loc) · 3.18 KB
/
omnibar.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
$(document).ready(function() {
//hide by default
$('#omnibar').hide();
//if the list is not found, nothing happens
var list_node = $('#typelist');
if(list_node.length) {
var relpath = '';
var omnibar_visible = false;
var close_omnibar = function() {
if(omnibar_visible) {
omnibar_visible = false;
$('#omnibar_text').val('');
$('#omnibar_text').blur();
$('#omnibar').hide();
$('#omnibar_text').tooltipster('hide');
}
} //close_omnibar
var focus_omnibar = function(e) {
if(e && e.keyCode) {
switch(e.keyCode) {
case 32:
case 37:
case 38:
case 39:
case 40:
return;
break;
}
}
if(!omnibar_visible) {
omnibar_visible = true;
$('#omnibar').show();
}
$('#omnibar_text').focus();
$('#omnibar_text').tooltipster('show');
} //focus_omnibar
//on any keypress, show if hidden, or focus if showing
$('body').on('keypress', focus_omnibar);
$('#search_bar').on('click', focus_omnibar);
$('#omnibar_close').on('click', close_omnibar);
//list of types
var list = list_node.attr('data-types');
list = list.split(',');
//fetch the relative path
relpath = list_node.attr('data-relpath');
//create the tooltip to display the types
$('#omnibar_text').tooltipster({
position : 'bottom',
trigger : 'click',
interactive:true,
minWidth:500,
content:$('<span>start typing...</span>'),
theme:'tooltipster-shadow'
}); //create tooltipster
//hide on escape
$('body').on('keyup', function(e) {
if(e.keyCode == 27) {
close_omnibar();
}
}); //keyup
//on text input
$('#omnibar_text').on('input', function(e) {
var val = $('#omnibar_text').val();
//if value typed in
if(val) {
var results = list.filter(function(a){
var reg = new RegExp( val,'gi');
return reg.exec(a);
}); //filter by types
results = results.map(function(_type){
var _link = relpath + 'api/' + _type.replace(/\./gi, '/') + '.html';
_type = '<a href="' + _link + '">' + _type + '</a>' ;
return _type;
}); //map to the link types
if(results.length) {
val = results.join('<br/>');
} else {
val = 'no results';
}
} else {
val = 'start typing...';
}
//set the content
$('#omnibar_text').tooltipster('content', $( '<span>' + val + '</span>'));
}); // input
} //if list node found
}); //document ready