diff options
Diffstat (limited to 'data/assets/js/admin/tabs/files.js')
-rw-r--r-- | data/assets/js/admin/tabs/files.js | 231 |
1 files changed, 231 insertions, 0 deletions
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: [ + "<a ", + "href='#' ", + "class='list-group-item' ", + "data-type='%{type|h}' ", + "data-name='%{name|h}' ", + "data-size='%{size|h}' ", + "data-path='%{path|h}' ", + ">", + "<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}' ", + ">", + "<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='btn btn-default btn-sm' ", + "data-path='%{path|h}' ", + ">", + "%{name|h}", + "</a>", + "</div><!-- btn-group -->", + ], + }); + + 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(); + }); +}); |