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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
jQuery(function($) {
"use strict";
var TEMPLATES = new LuigiTemplate.Cache({
file: [
"<a ",
"href='#' ",
"class='list-group-item' ",
"data-type='%{type|h}' ",
"data-name='%{name|h}' ",
"data-size='%{size|h}' ",
"data-path='%{path|h}' ",
"data-url='%{url|h}' ",
"title='%{name|h} (double-click to download).' ",
">",
"<i class='fa fa-file-o'></i>",
" ",
"%{name|h}",
" ",
"(%{size|h} bytes)",
"</a>",
],
dir: [
"<a ",
"href='#' ",
"class='list-group-item' ",
"data-type='%{type|h}' ",
"data-name='%{name|h}' ",
"data-size='%{size|h}' ",
"data-path='%{path|h}' ",
"title='Folder \"%{name|h}\" (double-click to view contents).' ",
">",
"<i class='fa fa-folder-o'></i>",
" ",
"%{name|h}",
"</a>",
],
no_files: [
"<span class='list-group-item disabled'>",
"<i class='fa fa-ban'></i>",
" ",
"No files.",
"</span>",
],
crumb: [
"<div class='btn-group btn-group-sm'>",
"<a ",
"href='#' ",
"class='crumb btn btn-default btn-sm' ",
"data-path='%{path|h}' ",
">",
"%{name|h}",
"</a>",
"</div><!-- btn-group -->",
],
move_prompt: [
"Enter destination path for \"%{name|h}\":",
],
delete_confirm: [
"Are you sure you want to delete \"%{name|h}\"?",
],
sites: [
"<div class='btn-group btn-group-sm'>",
"<a ",
"href='#' ",
"class='btn btn-default' ",
"title='Choose site.' ",
"data-toggle='dropdown' ",
">",
"Site: <span>%{name|h}</span>",
" ",
"<i class='fa fa-caret-down'></i>",
"</a>",
"<ul class='dropdown-menu'>",
"%{body}",
"</ul>",
"</div><!-- btn-group -->",
],
site: [
"<li class='%{css|h}'>",
"<a ",
"href='#' ",
"title='%{text|h}' ",
"data-id='%{site_id|h}' ",
"data-name='%{name|h}' ",
">",
"%{name|h}",
"</a>",
"</li>",
],
divider: [
"<div class='btn-group btn-group-sm'>",
"<span> </span>",
"</div>",
],
});
// cache templates
var NO_FILES = TEMPLATES.run('no_files'),
DIVIDER = TEMPLATES.run('divider');
function get_selected_site() {
var site_id = $('#files').data('site_id');
return $.grep(DATA.sites, function(row) {
return site_id == row.site_id;
})[0];
}
function make_site_dropdown() {
var curr_site = get_selected_site();
return TEMPLATES.run('sites', {
name: curr_site.name,
body: $.map(DATA.sites, function(row) {
return TEMPLATES.run('site', $.extend({}, row, {
css: (row.site_id == curr_site.site_id) ? 'active' : '',
text: 'Show files for this site.', // FIXME
}));
}).join(''),
});
}
function get_crumbs(path) {
var r = [];
$.each(path.replace(/^\//, '').split(/\//), function(i, part) {
if (part.length > 0)
r.push('/' + r.concat([part]).join('/'));
})
// prepend root
r.unshift('/');
return r;
}
function reload() {
var btns = $('#files-reload, #files-crumbs .btn'),
list = $('#files');
// show loading
btns.toggleClass('disabled').find('.loading').toggleClass('hidden');
// list.html(TEMPLATES.run('loading'));
send('file/list', {
site_id: $('#files').data('site_id'),
path: $('#files').data('path'),
}).always(function() {
btns.toggleClass('disabled').find('.loading').toggleClass('hidden');
// list.html('');
}).fail(function(r) {
gripe(r, 'list files');
}).done(function(r) {
// disable action buttons
$('#file-actions li').addClass('disabled');
// draw crumbs
var crumbs = get_crumbs($('#files').data('path'));
$('#files-crumbs').html([
make_site_dropdown(),
// DIVIDER,
].concat($.map(crumbs, function(crumb) {
return TEMPLATES.run('crumb', {
path: crumb,
name: crumb.replace(/^.*\/(.+)/, '$1'),
});
})).join(''));
// draw files
$('#files').html((r.length > 0) ? $.map(r, function(row) {
return TEMPLATES.run(row.type, row);
}).join('') : NO_FILES);
// update file count
$('#files-count').text(r.length);
});
// stop event
return false;
}
$('#files').on('dblclick', 'a.list-group-item', function() {
var data = $(this).data();
if (data.type == 'dir') {
// load directory
$('#files').data('path', data.path);
reload();
} else {
// select and download file
$(this).click();
$('#file-actions a[data-id="download"]').click();
}
// stop event
return false;
}).on('click', 'a.list-group-item', function() {
var data = $(this).data();
// disable file actions
$('#file-actions li').removeClass('disabled');
// update highlight
$('#files .active').removeClass('active');
$(this).addClass('active');
// stop event
return false;
});
$('#files-crumbs').on('click', 'a.crumb', function() {
$('#files').data('path', $(this).data('path'));
reload();
// stop event
return false;
});
$('#files-crumbs').on('click', 'ul.dropdown-menu a', function() {
var data = $(this).data(),
ul = $(this).parents('ul');
// hide dropdown
$('body').trigger('click');
// refresh selection
ul.find('li.active').removeClass('active');
$(this).parent('li').addClass('active');
// update button text
ul.prev('a').find('span').text(data.name);
// save site id and path
$('#files').data({
site_id: data.id,
path: '/',
});
// reload file list
reload();
// stop event
return false;
});
$('#file-actions').on('click', 'a', function() {
var action_id = $(this).data('id'),
data = $('#files .active').data();
// hide dropdown
$('body').trigger('click');
if (data) {
if (action_id == 'download') {
if (data.url)
location.href = data.url;
} else if (action_id == 'move') {
var dst_path = prompt(TEMPLATES.run('move_prompt', data), data.path);
if (dst_path) {
send('file/move', {
site_id: $('#files').data('site_id'),
src_path: data.path,
dst_path: dst_path,
}).always(function() {
// TODO: need loading handler
}).fail(function(r) {
gripe(r, 'move file');
}).done(function() {
// reload file list
reload();
});
}
} else if (action_id == 'delete') {
if (confirm(TEMPLATES.run('delete_confirm', data))) {
send('file/delete', {
site_id: $('#files').data('site_id'),
path: data.path,
}).always(function() {
// TODO: need loading handler
}).fail(function(r) {
gripe(r, 'delete file');
}).done(function() {
// reload file list
reload();
});
}
}
}
// stop event
return false;
});
$('#files-upload').on('click', 'a', function() {
// hide dropdown
$('body').trigger('click');
alert('TODO: upload');
// stop event
return false;
});
$('#files-mkdir').click(function() {
var me = $(this),
path = prompt('Name of new folder:', 'New Folder');
if (!path || !path.match(/\S/))
return false;
if (path.match(/\//)) {
alert("Error: Folder name cannot contain '/' characters");
return false;
}
// disable button, show spinner
me.toggleClass('disabled').find('.loading').toggleClass('hidden');
send('file/mkdir', {
site_id: $('#files').data('site_id'),
path: ($('#files').data('path') + '/' + path).replace(/\/\/+/, '/')
}).always(function() {
// enable button, hide spinner
me.toggleClass('disabled').find('.loading').toggleClass('hidden');
}).fail(function(r) {
gripe(r, 'create folder');
}).done(function(r) {
// reload file list
reload();
});
// stop event
return false;
});
// set defaults
$('#files').data({
path: '/',
site_id: DATA.default_site_id,
});
$('#files-reload').click(reload);
// load users
$('#admin-tab-files').on('show.bs.tab', function() {
reload();
});
});
|