jQuery(function($) {
"use strict";
var TEMPLATES = new LuigiTemplate.Cache({
file: [
"",
"",
" ",
"%{name|h}",
" ",
"(%{size|h} bytes)",
"",
],
dir: [
"",
"",
" ",
"%{name|h}",
"",
],
no_files: [
"",
"",
" ",
"No files.",
"",
],
crumb: [
"
",
],
delete_confirm: [
"Are you sure you want to delete \"%{name|h}\"?",
],
});
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;
}
var NO_FILES = TEMPLATES.run('no_files');
function reload() {
var btn = $('#files-reload'),
list = $('#files');
// show loading
btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
// list.html(TEMPLATES.run('loading'));
send('file/list', {
path: $('#files').data('path')
}).always(function() {
btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
// list.html('');
}).fail(function(r) {
var error = r.responseText;
try {
var data = $.parseJSON(r.responseText);
if (data.error)
error = data.error;
} catch (e) {}
alert("Error: " + error);
}).done(function(r) {
// disable action buttons
$('#file-actions li').addClass('disabled');
// draw crumbs
var crumbs = get_crumbs($('#files').data('path'));
$('#files-crumbs').html($.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.btn', function() {
$('#files').data('path', $(this).data('path'));
reload();
// stop event
return false;
});
$('#file-actions').on('click', 'a', function() {
var action_id = $(this).data('id');
// hide dropdown
$('body').trigger('click');
if (action_id == 'download') {
// alert('TODO: download file');
var url = $('#files .active').data('url');
if (url)
location.href = url;
} else if (action_id == 'move') {
alert('TODO: move file');
} else if (action_id == 'delete') {
var data = $('#files .active').data();
if (confirm(TEMPLATES.run('delete_confirm', data))) {
send('file/delete', {
path: data.path,
}).always(function() {
// TODO: need loading handler
}).fail(function(r) {
var error = r.responseText;
try {
var data = $.parseJSON(r.responseText);
if (data.error)
error = data.error;
} catch (e) {}
alert("Error: " + error);
}).done(function() {
// reload file list
reload();
});
}
}
// stop event
return false;
});
$('#files-upload').click(function() {
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', {
path: ($('#files').data('path') + '/' + path).replace(/\/\/+/, '/')
}).always(function() {
// enable button, hide spinner
me.toggleClass('disabled').find('.loading').toggleClass('hidden');
}).fail(function(r) {
var error = r.responseText;
try {
var data = $.parseJSON(r.responseText);
if (data.error)
error = data.error;
} catch (e) {}
alert("Error: " + error);
}).done(function(r) {
// reload file list
reload();
});
// stop event
return false;
});
$('#files').data('path', '/');
$('#files-reload').click(reload);
// load users
$('#admin-tab-files').on('show.bs.tab', function() {
reload();
});
});