'use strict'; /* global angular */ // create main application module var app = angular.module('Application', ['angular-md5', 'ui-notification']); angular.module('Application').filter('prettyOwner', function () { return function (uid) { if (uid === 1) return 'root'; if (uid === 33) return 'cloudron'; if (uid === 1000) return 'cloudron'; if (uid === 1001) return 'git'; return 'unkown'; } }); app.controller('FileManagerController', ['$scope', 'Client', function ($scope, Client) { var search = decodeURIComponent(window.location.search).slice(1).split('&').map(function (item) { return item.split('='); }).reduce(function (o, k) { o[k[0]] = k[1]; return o; }, {}); $scope.initialized = false; $scope.client = Client; $scope.cwd = '/'; $scope.appId = search.appId; $scope.app = null; $scope.entries = []; function sanitize(filePath) { filePath = filePath.split('/').filter(function (a) { return !!a; }).reduce(function (a, v) { if (v === '.'); // do nothing else if (v === '..') a.pop(); else a.push(v); return a; }, []).join('/'); return '/' + filePath; } $scope.refresh = function () { Client.filesGet($scope.appId, $scope.cwd, function (error, result) { if (error) return Client.error(error); console.log(result); $scope.entries = result.entries; }); }; $scope.uploadFile = function () { console.log('uploadFile'); }; $scope.uploadFolder = function () { console.log('uploadFolder'); }; $scope.remove = function (entry) { console.log('remove', entry); }; $scope.open = function (entry) { if (entry.isDirectory) { setDirectory($scope.cwd + '/' + entry.filePath); } else if (entry.isFile) { console.log('open', entry) } else {} }; $scope.goDirectoryUp = function () { setDirectory($scope.cwd + '/..') }; function setDirectory(path) { path = sanitize(path); if ($scope.cwd === path) return; $scope.cwd = path; $scope.refresh(); } function init() { Client.getStatus(function (error, status) { if (error) return Client.initError(error, init); if (!status.activated) { console.log('Not activated yet, redirecting', status); window.location.href = '/'; return; } // check version and force reload if needed if (!localStorage.version) { localStorage.version = status.version; } else if (localStorage.version !== status.version) { localStorage.version = status.version; window.location.reload(true); } console.log('Running filemanager version ', localStorage.version); // get user profile as the first thing. this populates the "scope" and affects subsequent API calls Client.refreshUserInfo(function (error) { if (error) return Client.initError(error, init); Client.refreshConfig(function (error) { if (error) return Client.initError(error, init); Client.getApp($scope.appId, function (error, result) { if (error) return Client.initError(error, init); $scope.app = result; // now mark the Client to be ready Client.setReady(); $scope.refresh(); $scope.initialized = true; }); }); }); }); } init(); }]);