-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
336 lines (265 loc) · 8.06 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets */
define(function (require, exports, module)
{
"use strict";
var ProjectManager = brackets.getModule("project/ProjectManager");
var CommandManager = brackets.getModule("command/CommandManager");
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
//var EditorManager = brackets.getModule("editor/EditorManager");
var Menus = brackets.getModule("command/Menus");
var SYMB_NL = "\n";
var KS_EXCLUDE = "!";
var KS_ESCAPE = "\\";
var CMD_NEW_ID = "Productivity.ProjectFiles.StartSearching";
var CMD_NEW_NAME = "Start Searching in Project Files";
var CMD_NEW_KEY = "Alt-s";
var CMD_CONTINUE_ID = "Productivity.ProjectFiles.ContinueSearching";
var CMD_CONTINUE_NAME = "Continue Searching in Project Files";
var CMD_CONTINUE_KEY = "Alt-c";
var CMD_CLEAN_ID = "Productivity.ProjectFiles.CleanSearchQuery";
var CMD_CLEAN_NAME = "Clean Project Files Search Query";
var CMD_CLEAN_KEY = "Alt-x";
var PROJECT_FILES_HEADER_ID = "project-files-header";
var PROJECT_FILES_CONTAINER_ID = "project-files-container";
var PROJECT_FILES_SEARCH_ID = "productivity-project-files-search";
var BAR_ID = "productivity-project-files-search-bar";
var BAR_EL = "input#productivity-project-files-search-bar";
var BAR_TYPE = "text";
var BAR_TXT = "Alt + S/C/X to start/continue/clean";
var BAR_FOCUSED_TXT = "Separate by space, ESC to clean, ! - exclude, / - dir sep, \\ - esc";
var BAR_TITLE = BAR_TXT + SYMB_NL + BAR_FOCUSED_TXT
var SEARCH_TIMEOUT = 250;
var FILETREE_ROOT_EL = "ul.jstree-brackets";
var FILETREE_DIR_OPENED_CLASS = "jstree-open";
var FILETREE_DIR_OPENED_EL = "ul.jstree-brackets li.jstree-open";
var FILETREE_DIR_CLOSED_CLASS = "jstree-closed";
var FILETREE_DIR_CLOSED_EL = "ul.jstree-brackets li.jstree-closed";
var FILETREE_FILE_CLASS = "jstree-leaf";
var FILETREE_FILE_EL = "ul.jstree-brackets li.jstree-leaf";
var FILETREE_FILE_LOCAL_EL = "li.jstree-leaf";
var FILETREE_FILE_LOCAL_EXCLUDED_EL = "li.jstree-leaf.search-exclude";
var FILETREE_EXCLUDED_El = "ul.jstree-brackets li.search-exclude";
var FILETREE_ANY_El = "ul.jstree-brackets li";
var FILETREE_SELECTION_EL = "div.filetree-selection";
var FILETREE_SELECTION_EXTENSION_EL = "div.filetree-selection-extension";
var FILETREE_DATA_SEARCH_ATTR = "data-search";
var KEY_ESC = 27;
var EL_EXCLUDE = "search-exclude";
var searchTimeout = null;
var util = {};
util.str = new function ()
{
this.toArrSplitBySpaceLc = function (str)
{
return str.toLowerCase().split(" ").filter(Boolean);
}
this.contains = function (str, what)
{
return str.indexOf(what) < 0 ? false : true;
}
this.hasFirstChar = function (str, char)
{
return str.charAt(0) === char ? true : false;
}
this.replace = function (str, ch1, ch2)
{
return str.replace(ch1, ch2);
}
this.escape = function (str)
{
return str.replace(KS_ESCAPE, '');
}
this.delFirstChar = function (str)
{
return str.substring(1);
}
}
util.searcher = new function ()
{
function isValidStrWithNode (str, node)
{
if (node.include) {
if (util.str.contains(str, node.str)) return true;
else return false;
} else {
if (util.str.contains(str, node.str)) return false;
else return true;
}
}
function formatNode (node)
{
if (util.str.hasFirstChar(node, KS_EXCLUDE)) {
return {
str : util.str.escape(util.str.delFirstChar(node)),
include : false
}
} else {
return {
str : util.str.escape(node),
include : true
}
}
}
this.isValidStrWithNodes = function (str, nodes)
{
for (let node of nodes) if (!isValidStrWithNode(str, node)) return false;
return true;
}
this.buildNodes = function (str)
{
return util.str.toArrSplitBySpaceLc(str).map((v) => formatNode(v));
}
};
start();
function start()
{
loadStyles()
registerMenuCommands();
registerMenuItems();
createSearchBar();
listenProjectManager();
}
function loadStyles ()
{
ExtensionUtils.loadStyleSheet(module, "style.css");
}
function listenProjectManager ()
{
ProjectManager.on("projectOpen", cleanSearchInput);
}
function cleanSearchInput ()
{
$(BAR_EL).val("");
startSearchTimeout();
}
function fileTreeSelectionHide ()
{
$(FILETREE_SELECTION_EL).css("display", "none");
$(FILETREE_SELECTION_EXTENSION_EL).css("display", "none");
}
function createSearchBar ()
{
var container = document.getElementById(PROJECT_FILES_CONTAINER_ID);
var parent = container.parentNode;
var searcher = document.createElement("div");
searcher.id = PROJECT_FILES_SEARCH_ID;
var bar = document.createElement("input");
bar.id = BAR_ID;
bar.title = BAR_TITLE;
bar.type = BAR_TYPE;
bar.setAttribute("placeholder", BAR_TXT);
searcher.appendChild(bar);
parent.insertBefore(searcher, container);
$(BAR_EL)
.on("blur", function () {
$(this).attr("placeholder", BAR_TXT);
})
.on("focus", function () {
$(this).attr("placeholder", BAR_FOCUSED_TXT);
})
.on("keyup", onSearchBarKeyUp);
}
function searchProjectFiles ()
{
var search = $(BAR_EL).val();
fileTreeSelectionHide();
if (search.length > 0) {
fileTreeConfigure(FILETREE_ROOT_EL);
fileTreeSearch(search);
fileTreeDirs();
} else {
fileTreeConfigure(FILETREE_ROOT_EL);
fileTreeOpenAll();
}
}
function fileTreeOpenAll ()
{
$(FILETREE_EXCLUDED_El).each((i, e) => $(e).removeClass(EL_EXCLUDE));
}
function fileTreeDirs ()
{
$(FILETREE_DIR_OPENED_EL).each(function(i, e){
var dir = $(e);
if(dir.find(FILETREE_FILE_LOCAL_EL).length === dir.find(FILETREE_FILE_LOCAL_EXCLUDED_EL).length) dir.addClass(EL_EXCLUDE);
else dir.removeClass(EL_EXCLUDE);
})
}
function fileTreeSearch(search)
{
var nodes = util.searcher.buildNodes(search);
//console.log(nodes);
$(FILETREE_FILE_EL).each(
function (i, e)
{
if (util.searcher.isValidStrWithNodes($(e).attr(FILETREE_DATA_SEARCH_ATTR), nodes)) $(e).removeClass(EL_EXCLUDE);
else $(e).addClass(EL_EXCLUDE);
}
);
}
function fileTreeTagGet (e)
{
var o='';
$(e).children("a").first().children("span").each((i, e)=>{ o+=$(e).text() });
return o;
}
function fileTreeTagClean (...i)
{
var o = [];
for( let e of i) if(e) o.push(e.toLowerCase());
return o.join('/');
}
function fileTreeConfigure (i, tags)
{
$(i).children("li").each(
function (i,e)
{
if ($(e).hasClass(FILETREE_DIR_CLOSED_CLASS)) {
$(e).click();//.attr("data-closed",true);
fileTreeConfigure($(e).children("ul").first(), fileTreeTagClean(tags, fileTreeTagGet(e)));
} else if ($(e).hasClass(FILETREE_DIR_OPENED_CLASS)) {
fileTreeConfigure($(e).children("ul").first(), fileTreeTagClean(tags, fileTreeTagGet(e)));
} else if ($(e).hasClass(FILETREE_FILE_CLASS)) {
$(e).attr(FILETREE_DATA_SEARCH_ATTR, fileTreeTagClean(tags, fileTreeTagGet(e)));
} else $(e).removeAttr(FILETREE_DATA_SEARCH_ATTR);
}
);
}
function onSearchBarKeyUp (e)
{
if(e.keyCode === KEY_ESC) cleanSearchInput();
else startSearchTimeout();
}
function startSearchTimeout ()
{
clearTimeout(searchTimeout);
searchTimeout = setTimeout(searchProjectFiles, SEARCH_TIMEOUT);
}
//// Commands
function commandStartSearching ()
{
var input = $(BAR_EL).focus();
input = input[0];
input.setSelectionRange(0, input.value.length);
}
function commandContinueSearching () {
var val = $(BAR_EL).val();
var e = $(BAR_EL).focus();
e.val(val);
}
//// Menu
function registerMenuCommands ()
{
CommandManager.register(CMD_NEW_NAME, CMD_NEW_ID, commandStartSearching);
CommandManager.register(CMD_CONTINUE_NAME, CMD_CONTINUE_ID, commandContinueSearching);
CommandManager.register(CMD_CLEAN_NAME, CMD_CLEAN_ID, cleanSearchInput);
}
function registerMenuItems ()
{
var menu = Menus.getMenu(Menus.AppMenuBar.FIND_MENU);
menu.addMenuDivider();
menu.addMenuItem(CMD_NEW_ID, [{key:CMD_NEW_KEY}]);
menu.addMenuItem(CMD_CONTINUE_ID, [{key:CMD_CONTINUE_KEY}]);
menu.addMenuItem(CMD_CLEAN_ID, [{key:CMD_CLEAN_KEY}]);
}
});