portainer/app/agent/components/host-browser/host-browser-controller.js

84 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-09-30 08:16:10 +00:00
angular.module('portainer.agent').controller('HostBrowserController', [
'HostBrowserService',
'Notifications',
function HostBrowserController(HostBrowserService, Notifications) {
var ctrl = this;
ctrl.state = {
path: '/'
};
2018-09-30 08:16:10 +00:00
ctrl.goToParent = goToParent;
ctrl.browse = browse;
ctrl.renameFile = renameFile;
ctrl.downloadFile = downloadFile;
ctrl.deleteFile = deleteFile;
ctrl.isRoot = isRoot;
ctrl.$onInit = $onInit;
function goToParent() {
2018-09-30 08:16:10 +00:00
getFilesForPath(parentPath(this.state.path));
}
2018-09-30 08:16:10 +00:00
function isRoot() {
return ctrl.state.path === '/';
}
2018-09-30 08:16:10 +00:00
function browse(folder) {
getFilesForPath(buildPath(ctrl.state.path, folder));
}
2018-09-30 08:16:10 +00:00
function getFilesForPath(path) {
HostBrowserService.ls(path)
.then(function onFilesLoaded(files) {
ctrl.state.path = path;
ctrl.files = files;
})
.catch(function onLoadingFailed(err) {
Notifications.error('Failure', err, 'Unable to browse');
});
}
2018-09-30 08:24:01 +00:00
function renameFile(name, newName) {
var isRoot = ctrl.isRoot();
var filePath = isRoot ? '/' + name : this.state.path + '/' + file;
var newFilePath = isRoot ? '/' + newName : this.state.path + '/' + newName;
HostBrowserService.rename(filePath, newFilePath)
.then(function onRenameSuccess() {
Notifications.success('File successfully renamed', newFilePath);
return HostBrowserService.ls(ctrl.state.path);
})
.then(function onFilesLoaded(files) {
ctrl.files = files;
})
.catch(function notifyOnError(err) {
Notifications.error('Failure', err, 'Unable to rename file');
});
}
2018-09-30 08:16:10 +00:00
function downloadFile(name) {}
2018-09-30 08:16:10 +00:00
function deleteFile(name) {}
2018-09-30 08:16:10 +00:00
function $onInit() {
getFilesForPath('/');
}
2018-09-30 08:16:10 +00:00
function parentPath(path) {
if (path.lastIndexOf('/') === 0) {
return '/';
}
2018-09-30 08:24:01 +00:00
2018-09-30 08:16:10 +00:00
var split = _.split(path, '/');
return _.join(_.slice(split, 0, split.length - 1), '/');
}
2018-09-30 08:16:10 +00:00
function buildPath(parent, file) {
if (parent === '/') {
return parent + file;
}
return parent + '/' + file;
}
}
]);