-
Notifications
You must be signed in to change notification settings - Fork 3
/
goto-box.js
167 lines (126 loc) · 4.29 KB
/
goto-box.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// goto-box.js
// (C) Leo C. Stein ([email protected])
// 2013 May
//
// Part of the arXiv-keys extension
//
// This work is licensed as Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
// For full details see http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US
// Module export gotoBox
// exports:
// function showGotoBox()
var gotoBox = (function($){
var my = {};
////////////////////////////////////////////////////////////
var gotoBox, gotoInput;
function gotoCompletions( request, response ) {
// Approach: If the request has no '/' character in it, then match it against a category.
// If it does have a '/' character, then:
// if the part before the '/' is a valid category,
// then match against <cat>/new, <cat>/recent, <cat>/current
var slashInd = request.term.indexOf('/');
var hasSlash = (slashInd != -1);
if (!hasSlash) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( categories, function( item ){
return matcher.test( item );
}) );
} else {
var cat = request.term.substring(0,slashInd);
if ($.inArray(cat, categories) != -1) { // It is a valid category
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( catEndings(cat), function( item ){
return matcher.test( item );
}) );
} else { // invalid category
response();
};
};
};
// Returns a category with the possible endings
function catEndings(cat) {
return [ cat + '/new',
cat + '/recent',
cat + '/current' ];
};
function gotoKeyDown(event) {
var menuOpen = gotoInput.autocomplete( "widget" ).is(":visible");
// Don't do anything if the menu is open and there is an item selected
if (menuOpen && ($(".ui-state-focus").length != 0))
return;
if ((event.keyCode == 27) && !menuOpen) { // ESC
hideGotoBox();
} else if (event.keyCode == 13) { // ENTER
gotoTryNavigate();
};
return;
};
my.showGotoBox =
function showGotoBox() {
$("#gotoError").hide();
gotoInput.val("");
gotoBox.style.display = "block";
gotoInput[0].focus();
};
function hideGotoBox() {
gotoBox.style.display = "none";
gotoInput[0].blur();
document.body.focus();
};
function gotoTryNavigate() {
if (isGotoInputValid()) {
var splitInput = gotoInput.val().split("/");
// If there is no slash, or there is nothing after the slash, go to /new
if ((splitInput.length == 1) ||
((splitInput.length == 2) && (splitInput[1] == "")))
splitInput = [ splitInput[0], "new" ];
window.location = "/list/" + splitInput[0] + "/" + splitInput[1];
} else {
$("#gotoError").show();
};
};
function isGotoInputValid() {
// The gotoInput must either be the name of a category, or
// <cat>/, <cat>/new, <cat>/recent, or <cat>/current
var splitInput = gotoInput.val().split("/");
if (splitInput.length > 2 || splitInput.length == 0)
return false;
if ($.inArray(splitInput[0], categories) == -1)
return false;
if (splitInput.length == 1)
return true;
// Only get here if splitInput.length == 2 and splitInput[0] is a valid cat
return (splitInput[1] == '') || (splitInput[1] == 'new')
|| (splitInput[1] == 'recent') || (splitInput[1] == 'current');
};
////////////////////////////////////////////////////////////
// install goto box
////////////////////////////////////////////////////////////
gotoBox = document.createElement("div");
gotoBox.id = "gotoBox";
var innerHTML = '\
<label for="gotoInput">\
Type a category to go to, optionally followed by /new, /recent, or /current.<br>\
</label>\
<input id="gotoInput"/>\
<div id="gotoError">\
Invalid category!\
</div>';
gotoBox.innerHTML = innerHTML;
// Initially hidden
gotoBox.style.display = "none";
// Insert it into the document
document.body.appendChild(gotoBox);
gotoInput = $("#gotoInput");
// Note: it is important that this is registered first,
// so that gotoKeyDown can tell if the menu is open
// *before* the autocomplete widget hides it.
gotoInput.on("keydown", gotoKeyDown);
// Use jQuery autocomplete widget
gotoInput.autocomplete({
source: gotoCompletions
});
////////////////////////////////////////////////////////////
// export module definitions
return my;
}(jQuery));