/
quicksearch.js
293 lines (247 loc) · 8.31 KB
/
quicksearch.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* quicksearch Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
define(function(require, exports, module) {
var ide = require("core/ide");
var ext = require("core/ext");
var canon = require("pilot/canon");
var editors = require("ext/editors/editors");
var Search = require("ace/search").Search;
var skin = require("text!ext/quicksearch/skin.xml");
var markup = require("text!ext/quicksearch/quicksearch.xml");
module.exports = ext.register("ext/quicksearch/quicksearch", {
name : "quicksearch",
dev : "Ajax.org",
type : ext.GENERAL,
alone : true,
skin : skin,
markup : markup,
commands : {
"quicksearch": {hint: "quickly search for a string inside the active document, without further options (see 'search')"},
"find": {hint: "open the quicksearch dialog to quickly search for a phrase"},
"findnext": {
hint: "search for the next occurrence of the search query your entered last",
msg: "Navigating to next match."
},
"findprevious": {
hint: "search for the previous occurrence of the search query your entered last",
msg: "Navigating to previous match."
}
},
hotitems: {},
nodes : [],
hook : function(){
var _self = this;
canon.addCommand({
name: "find",
exec: function(env, args, request) {
_self.toggleDialog(1);
}
});
},
init : function(amlNode){
var _self = this;
txtQuickSearch.addEventListener("keydown", function(e){
switch (e.keyCode){
case 13: //ENTER
if (e.shiftKey)
_self.execSearch(false, true);
else
_self.execSearch(false, false);
return false;
break;
case 27: //ESCAPE
_self.toggleDialog(-1);
if (e.htmlEvent)
apf.stopEvent(e.htmlEvent)
else if (e.stop)
e.stop();
return false;
break;
case 38: //UP
_self.navigateList("prev");
break;
case 40: //DOWN
_self.navigateList("next");
break;
case 36: //HOME
if (!e.ctrlKey) return;
_self.navigateList("first");
break;
case 35: //END
if (!e.ctrlKey) return;
_self.navigateList("last");
break;
}
});
winQuickSearch.addEventListener("blur", function(e){
if (!apf.isChildOf(winQuickSearch, e.toElement))
_self.toggleDialog(-1);
});
var editor = editors.currentEditor;
if (editor && editor.ceEditor)
editor.ceEditor.parentNode.appendChild(winQuickSearch);
},
navigateList : function(type){
var settings = require("ext/settings/settings");
if (!settings) return;
var model = settings.model;
var lines = model.queryNodes("search/word");
var next;
if (type == "prev")
next = Math.max(0, this.position - 1);
else if (type == "next")
next = Math.min(lines.length - 1, this.position + 1);
else if (type == "last")
next = Math.max(lines.length - 1, 0);
else if (type == "first")
next = 0;
if (lines[next]) {
txtQuickSearch.setValue(lines[next].getAttribute("key"));
txtQuickSearch.select();
this.position = next;
}
},
handleQuicksearchEscape : function(e) {
if (e.keyCode == 27)
this.toggleDialog(-1);
},
toggleDialog: function(force) {
ext.initExtension(this);
if (this.control && this.control.stop)
this.control.stop();
var editorPage = tabEditors.getPage();
if (!editorPage) return;
var editor = editors.currentEditor;
if (!editor || !editor.ceEditor)
return;
if (!force && !winQuickSearch.visible || force > 0) {
this.position = 0;
var sel = editor.getSelection();
var doc = editor.getDocument();
var range = sel.getRange();
var value = doc.getTextRange(range);
if (!value && editor.ceEditor)
value = editor.ceEditor.getLastSearchOptions().needle;
if (value)
txtQuickSearch.setValue(value);
winQuickSearch.$ext.style.top = "-30px";
winQuickSearch.show();
txtQuickSearch.focus();
txtQuickSearch.select();
//Animate
apf.tween.single(winQuickSearch, {
type : "top",
anim : apf.tween.easeInOutCubic,
from : -30,
to : 5,
steps : 8,
interval : 10,
control : (this.control = {})
});
}
else if (winQuickSearch.visible) {
txtQuickSearch.focus();
txtQuickSearch.select();
//Animate
apf.tween.single(winQuickSearch, {
type : "top",
anim : apf.tween.NORMAL,
from : winQuickSearch.$ext.offsetTop,
to : -30,
steps : 8,
interval : 10,
control : (this.control = {}),
onfinish : function(){
winQuickSearch.hide();
editor.ceEditor.focus();
}
});
}
return false;
},
quicksearch : function(){
this.toggleDialog(1);
},
execSearch: function(close, backwards) {
var editor = editors.currentEditor;
if (!editor || !editor.ceEditor)
return;
var ceEditor = editor.ceEditor;
var ace = ceEditor.$editor;
var txt = txtQuickSearch.getValue();
if (!txt)
return;
var options = {
backwards: backwards || false,
wrap: true,
caseSensitive: false,
wholeWord: false,
regExp: false,
scope: Search.ALL
};
if (this.$crtSearch != txt) {
this.$crtSearch = txt;
ace.find(txt, options);
}
else {
ace.find(txt, options);
}
var settings = require("ext/settings/settings");
if (settings.model) {
var history = settings.model;
var search = apf.createNodeFromXpath(history.data, "search");
if (!search.firstChild || search.firstChild.getAttribute("key") != txt) {
var keyEl = apf.getXml("<word />");
keyEl.setAttribute("key", txt);
apf.xmldb.appendChild(search, keyEl, search.firstChild);
}
}
if (close) {
winQuickSearch.hide();
ceEditor.focus();
}
},
find: function() {
this.toggleDialog(1);
return false;
},
findnext: function() {
var editor = editors.currentEditor;
if (!editor || !editor.ceEditor)
return;
var ceEditor = editor.ceEditor;
var ace = ceEditor.$editor;
ace.findNext();
return false;
},
findprevious: function() {
var editor = editors.currentEditor;
if (!editor || !editor.ceEditor)
return;
var ceEditor = editor.ceEditor;
var ace = ceEditor.$editor;
ace.findPrevious();
return false;
},
enable : function(){
this.nodes.each(function(item){
item.enable();
});
},
disable : function(){
this.nodes.each(function(item){
item.disable();
});
},
destroy : function(){
this.nodes.each(function(item){
item.destroy(true, true);
});
this.nodes = [];
}
});
});