-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
241 lines (196 loc) · 5.28 KB
/
script.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
global.$ = $;
global.currentFocus = null;
global.quickJumpList = [];
var remote = require('remote');
var Menu = remote.require('menu');
var BrowserWindow = remote.require('browser-window');
var MenuItem = remote.require('menu-item');
var path = remote.require('path');
var shell = require('shell');
var abar = require('address_bar');
var folder_view = require('folder_view');
var mime = remote.require('mime');
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushUnique = function(element) {
if (this.indexOf(element) === -1) {
this.push(element);
}
};
// append default actions to menu for OSX
var initMenu = function() {
try {
var nativeMenuBar = new Menu();
if (process.platform == "darwin") {
if (nativeMenuBar.createMacBuiltin) nativeMenuBar.createMacBuiltin("FileExplorer");
}
} catch (error) {
console.error(error);
setTimeout(function() {
throw error;
}, 1);
}
};
var aboutWindow = null;
var App = {
// show "about" window
about: function() {
var params = {
toolbar: false,
resizable: false,
show: true,
height: 150,
width: 400
};
aboutWindow = new BrowserWindow(params);
aboutWindow.loadUrl('file://' + __dirname + '/about.html');
},
// change folder for sidebar links
cd: function(anchor) {
anchor = $(anchor);
$('#sidebar li').removeClass('active');
$('#sidebar i').removeClass('icon-white');
anchor.closest('li').addClass('active');
anchor.find('i').addClass('icon-white');
this.setPath(anchor.attr('nw-path'));
},
// set path for file explorer
setPath: function(path) {
if (path.indexOf('~') === 0) {
path = path.replace('~', process.env.HOME);
}
quickJumpList.pushUnique(path);
this.folder.open(path);
this.addressbar.set(path);
}
};
var preventDrop = function() {
document.addEventListener('drop', function(e) {
e.preventDefault();
e.stopPropagation();
});
document.addEventListener('dragover', function(e) {
e.preventDefault();
e.stopPropagation();
});
};
$(document).ready(function() {
initMenu();
preventDrop();
var folder = new folder_view.Folder($('#files'));
var addressbar = new abar.AddressBar($('#addressbar'));
userhome = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
folder.open(userhome);
addressbar.set(userhome);
App.folder = folder;
App.addressbar = addressbar;
folder.on('navigate', function(dir, mime) {
$('#context_dropdown').hide();
if (mime.type == 'folder') {
console.log(JSON.stringify(mime));
quickJumpList.pushUnique(mime.path);
addressbar.enter(mime);
} else {
shell.openItem(mime.path);
}
});
addressbar.on('navigate', function(dir) {
folder.open(dir);
});
// sidebar favorites
$('[nw-path]').bind('click', function(event) {
event.preventDefault();
App.cd(this);
});
initTypeAhead();
$('body').on('keydown', function(e) {
if (e.ctrlKey) {
switch (e.which) {
case 80:
e.preventDefault();
typeahead_elem.focus();
break;
case 67:
// emit event
e.preventDefault();
console.log('copy...');
break;
default:
}
}
// make sure that tt menu is not active FIXME
var current = $(this).find('.focus');
var file_path = current.attr('data-path');
var newelem = [];
if (e.which == 40) {
newelem = current.next();
} else if (e.which == 38) {
newelem = current.prev();
} else if (e.which == 13) { //enter
folder.emit('navigate', file_path, mime.stat(file_path));
}
if (newelem.length > 0) {
e.preventDefault();
if (currentFocus === null || currentFocus === 'files') {
newelem.addClass('focus');
current.removeClass('focus');
newelem.scrollintoview();
}
}
// TODO add keyboard navigation in context menu.
});
$('#context_dropdown .context-action').on('click', function(e) {
console.log($(this).attr('data-action'));
newelem.addClass('focus');
current.removeClass('focus');
newelem.scrollintoview();
});
attachDragger();
});
function initTypeAhead() {
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var typeahead_elem = $('#the-basics .typeahead');
typeahead_elem.typeahead({
hint: true,
highlight: true,
autoselect: true,
minLength: 0,
}, {
name: 'quickJumpList',
source: substringMatcher(quickJumpList),
templates: {
suggestion: function(data) {
var jsonpath = path.parse(data);
return '<div style="border-bottom:solid 1px #AAA;"><strong> ' + jsonpath.base + '</strong><br><span class="small">' + path.join(jsonpath.dir, jsonpath.base) + '</span> </div>';
}
},
});
typeahead_elem.bind('focus', function() {
$(this).val('');
});
typeahead_elem.bind('typeahead:selected', function(obj, datum, name) {
App.setPath($(this).val());
$(this).blur();
});
}
function attachDragger() {
// dragula({
// containers: [document.getElementById('taskstack'), document.getElementById('files')],
// copy: true
// });
}