From d1cbae09bf46999e023a7dd2291a534b9737c804 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 16 Jul 2016 21:21:33 -0400 Subject: add file handling and libmagic wrapper --- data/assets/js/admin/tabs/files.js | 231 +++++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 data/assets/js/admin/tabs/files.js (limited to 'data/assets') diff --git a/data/assets/js/admin/tabs/files.js b/data/assets/js/admin/tabs/files.js new file mode 100644 index 0000000..c3737d0 --- /dev/null +++ b/data/assets/js/admin/tabs/files.js @@ -0,0 +1,231 @@ +jQuery(function($) { + "use strict"; + + var TEMPLATES = new LuigiTemplate.Cache({ + file: [ + "", + "", + " ", + "%{name|h}", + " ", + "(%{size|h} bytes)", + "", + ], + + dir: [ + "", + "", + " ", + "%{name|h}", + "", + ], + + no_files: [ + "", + "", + " ", + "No files.", + "", + ], + + crumb: [ + "
", + "", + "%{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(); + $('#files-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 data = $(this).data(); + + // hide dropdown + $('body').trigger('click'); + + if (data.id == 'download') { + alert('TODO: download file'); + } else if (data.id == 'move') { + alert('TODO: move file'); + } else if (data.id == 'delete') { + alert('TODO: delete file'); + } + + // 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(); + }); +}); -- cgit v1.2.3