From fb6752c1437634f16bd236f5dc037e463e5d7814 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Mon, 8 Oct 2018 11:14:14 +0300 Subject: [PATCH] feat(agent): browse files relative to root --- .../host-browser/host-browser-controller.js | 34 +++++++++++-------- .../components/host-browser/host-browser.html | 2 +- .../node-browser/node-browser-controller.js | 12 +------ .../nodes/node-browser/node-browser.html | 1 + 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/app/agent/components/host-browser/host-browser-controller.js b/app/agent/components/host-browser/host-browser-controller.js index 7ff2943f5..a50cb6b6e 100644 --- a/app/agent/components/host-browser/host-browser-controller.js +++ b/app/agent/components/host-browser/host-browser-controller.js @@ -2,8 +2,9 @@ angular.module('portainer.agent').controller('HostBrowserController', [ 'HostBrowserService', 'Notifications', 'FileSaver', 'ModalService', function HostBrowserController(HostBrowserService, Notifications, FileSaver, ModalService) { var ctrl = this; + var ROOT_PATH = '/host'; ctrl.state = { - path: '/' + path: ROOT_PATH }; ctrl.goToParent = goToParent; @@ -14,13 +15,21 @@ angular.module('portainer.agent').controller('HostBrowserController', [ ctrl.isRoot = isRoot; ctrl.onFileSelectedForUpload = onFileSelectedForUpload; 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() { getFilesForPath(parentPath(this.state.path)); } function isRoot() { - return ctrl.state.path === '/'; + return ctrl.state.path === ROOT_PATH; } function browse(folder) { @@ -39,15 +48,12 @@ angular.module('portainer.agent').controller('HostBrowserController', [ } function renameFile(name, newName) { - var isRoot = ctrl.isRoot(); - var filePath = isRoot ? '/' + name : this.state.path + '/' + file; - var newFilePath = isRoot - ? '/' + newName - : this.state.path + '/' + newName; + var filePath = buildPath(ctrl.state.path, name); + var newFilePath = buildPath(ctrl.state.path, newName); HostBrowserService.rename(filePath, newFilePath) .then(function onRenameSuccess() { - Notifications.success('File successfully renamed', newFilePath); + Notifications.success('File successfully renamed', getRelativePath(newFilePath)); return HostBrowserService.ls(ctrl.state.path); }) .then(function onFilesLoaded(files) { @@ -76,7 +82,7 @@ angular.module('portainer.agent').controller('HostBrowserController', [ var filePath = buildPath(ctrl.state.path, name); ModalService.confirmDeletion( - 'Are you sure that you want to delete ' + filePath + ' ?', + 'Are you sure that you want to delete ' + getRelativePath(filePath) + ' ?', function onConfirm(confirmed) { if (!confirmed) { return; @@ -89,7 +95,7 @@ angular.module('portainer.agent').controller('HostBrowserController', [ function deleteFile(path) { HostBrowserService.delete(path) .then(function onDeleteSuccess() { - Notifications.success('File successfully deleted', path); + Notifications.success('File successfully deleted', getRelativePath(path)); return HostBrowserService.ls(ctrl.state.path); }) .then(function onFilesLoaded(data) { @@ -101,12 +107,12 @@ angular.module('portainer.agent').controller('HostBrowserController', [ } function $onInit() { - getFilesForPath('/'); + getFilesForPath(ROOT_PATH); } function parentPath(path) { - if (path.lastIndexOf('/') === 0) { - return '/'; + if (path === ROOT_PATH) { + return ROOT_PATH; } var split = _.split(path, '/'); @@ -114,7 +120,7 @@ angular.module('portainer.agent').controller('HostBrowserController', [ } function buildPath(parent, file) { - if (parent === '/') { + if (parent.lastIndexOf('/') === parent.length - 1) { return parent + file; } return parent + '/' + file; diff --git a/app/agent/components/host-browser/host-browser.html b/app/agent/components/host-browser/host-browser.html index 3209e2c6b..cf7b59306 100644 --- a/app/agent/components/host-browser/host-browser.html +++ b/app/agent/components/host-browser/host-browser.html @@ -1,5 +1,5 @@