feat(agent): browse files relative to root

pull/2337/head
Chaim Lev-Ari 2018-10-08 11:14:14 +03:00
parent c50c5bed81
commit fb6752c143
4 changed files with 23 additions and 26 deletions

View File

@ -2,8 +2,9 @@ angular.module('portainer.agent').controller('HostBrowserController', [
'HostBrowserService', 'Notifications', 'FileSaver', 'ModalService', 'HostBrowserService', 'Notifications', 'FileSaver', 'ModalService',
function HostBrowserController(HostBrowserService, Notifications, FileSaver, ModalService) { function HostBrowserController(HostBrowserService, Notifications, FileSaver, ModalService) {
var ctrl = this; var ctrl = this;
var ROOT_PATH = '/host';
ctrl.state = { ctrl.state = {
path: '/' path: ROOT_PATH
}; };
ctrl.goToParent = goToParent; ctrl.goToParent = goToParent;
@ -14,13 +15,21 @@ angular.module('portainer.agent').controller('HostBrowserController', [
ctrl.isRoot = isRoot; ctrl.isRoot = isRoot;
ctrl.onFileSelectedForUpload = onFileSelectedForUpload; ctrl.onFileSelectedForUpload = onFileSelectedForUpload;
ctrl.$onInit = $onInit; ctrl.$onInit = $onInit;
ctrl.getRelativePath = getRelativePath;
function getRelativePath(path) {
path = path || ctrl.state.path;
var rootPathRegex = new RegExp('^' + ROOT_PATH + '\/?');
var relativePath = path.replace(rootPathRegex, '/');
return relativePath;
}
function goToParent() { function goToParent() {
getFilesForPath(parentPath(this.state.path)); getFilesForPath(parentPath(this.state.path));
} }
function isRoot() { function isRoot() {
return ctrl.state.path === '/'; return ctrl.state.path === ROOT_PATH;
} }
function browse(folder) { function browse(folder) {
@ -39,15 +48,12 @@ angular.module('portainer.agent').controller('HostBrowserController', [
} }
function renameFile(name, newName) { function renameFile(name, newName) {
var isRoot = ctrl.isRoot(); var filePath = buildPath(ctrl.state.path, name);
var filePath = isRoot ? '/' + name : this.state.path + '/' + file; var newFilePath = buildPath(ctrl.state.path, newName);
var newFilePath = isRoot
? '/' + newName
: this.state.path + '/' + newName;
HostBrowserService.rename(filePath, newFilePath) HostBrowserService.rename(filePath, newFilePath)
.then(function onRenameSuccess() { .then(function onRenameSuccess() {
Notifications.success('File successfully renamed', newFilePath); Notifications.success('File successfully renamed', getRelativePath(newFilePath));
return HostBrowserService.ls(ctrl.state.path); return HostBrowserService.ls(ctrl.state.path);
}) })
.then(function onFilesLoaded(files) { .then(function onFilesLoaded(files) {
@ -76,7 +82,7 @@ angular.module('portainer.agent').controller('HostBrowserController', [
var filePath = buildPath(ctrl.state.path, name); var filePath = buildPath(ctrl.state.path, name);
ModalService.confirmDeletion( ModalService.confirmDeletion(
'Are you sure that you want to delete ' + filePath + ' ?', 'Are you sure that you want to delete ' + getRelativePath(filePath) + ' ?',
function onConfirm(confirmed) { function onConfirm(confirmed) {
if (!confirmed) { if (!confirmed) {
return; return;
@ -89,7 +95,7 @@ angular.module('portainer.agent').controller('HostBrowserController', [
function deleteFile(path) { function deleteFile(path) {
HostBrowserService.delete(path) HostBrowserService.delete(path)
.then(function onDeleteSuccess() { .then(function onDeleteSuccess() {
Notifications.success('File successfully deleted', path); Notifications.success('File successfully deleted', getRelativePath(path));
return HostBrowserService.ls(ctrl.state.path); return HostBrowserService.ls(ctrl.state.path);
}) })
.then(function onFilesLoaded(data) { .then(function onFilesLoaded(data) {
@ -101,12 +107,12 @@ angular.module('portainer.agent').controller('HostBrowserController', [
} }
function $onInit() { function $onInit() {
getFilesForPath('/'); getFilesForPath(ROOT_PATH);
} }
function parentPath(path) { function parentPath(path) {
if (path.lastIndexOf('/') === 0) { if (path === ROOT_PATH) {
return '/'; return ROOT_PATH;
} }
var split = _.split(path, '/'); var split = _.split(path, '/');
@ -114,7 +120,7 @@ angular.module('portainer.agent').controller('HostBrowserController', [
} }
function buildPath(parent, file) { function buildPath(parent, file) {
if (parent === '/') { if (parent.lastIndexOf('/') === parent.length - 1) {
return parent + file; return parent + file;
} }
return parent + '/' + file; return parent + '/' + file;

View File

@ -1,5 +1,5 @@
<files-datatable <files-datatable
title-text="Host browser - {{$ctrl.state.path}}" title-icon="fa-file" title-text="Host browser - {{$ctrl.getRelativePath()}}" title-icon="fa-file"
dataset="$ctrl.files" table-key="host_browser" dataset="$ctrl.files" table-key="host_browser"
order-by="Dir" order-by="Dir"
is-root="$ctrl.isRoot()" is-root="$ctrl.isRoot()"

View File

@ -1,12 +1,8 @@
angular.module('portainer.docker').controller('NodeBrowserController', [ angular.module('portainer.docker').controller('NodeBrowserController', [
'NodeService', 'NodeService', 'HttpRequestHelper', '$stateParams',
'HttpRequestHelper',
'$stateParams',
function NodeBrowserController(NodeService, HttpRequestHelper, $stateParams) { function NodeBrowserController(NodeService, HttpRequestHelper, $stateParams) {
var ctrl = this; var ctrl = this;
ctrl.refreshList = refreshList;
ctrl.$onInit = $onInit; ctrl.$onInit = $onInit;
function $onInit() { function $onInit() {
@ -20,11 +16,5 @@ angular.module('portainer.docker').controller('NodeBrowserController', [
ctrl.node = node; ctrl.node = node;
}); });
} }
function refreshList() {
}
} }
]); ]);

View File

@ -8,6 +8,7 @@
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
<host-browser <host-browser
ng-if="$ctrl.node"
></host-browser> ></host-browser>
</div> </div>
</div> </div>