Files
cloudron-box/src/views/app.js

983 lines
34 KiB
JavaScript
Raw Normal View History

'use strict';
/* global angular */
/* global $ */
/* global asyncSeries */
/* global asyncForEach */
2019-09-22 12:21:39 +02:00
/* global RSTATES */
/* global ISTATES */
// TODO use this once we enable custom SSL certificate ui again
// $scope.readCertificate = function (event) {
// $scope.$apply(function () {
// $scope.appConfigure.certificateFile = null;
// $scope.appConfigure.certificateFileName = event.target.files[0].name;
// var reader = new FileReader();
// reader.onload = function (result) {
// if (!result.target || !result.target.result) return console.error('Unable to read local file');
// $scope.appConfigure.certificateFile = result.target.result;
// };
// reader.readAsText(event.target.files[0]);
// });
// };
// $scope.readKey = function (event) {
// $scope.$apply(function () {
// $scope.appConfigure.keyFile = null;
// $scope.appConfigure.keyFileName = event.target.files[0].name;
// var reader = new FileReader();
// reader.onload = function (result) {
// if (!result.target || !result.target.result) return console.error('Unable to read local file');
// $scope.appConfigure.keyFile = result.target.result;
// };
// reader.readAsText(event.target.files[0]);
// });
// };
angular.module('Application').controller('AppController', ['$scope', '$location', '$timeout', '$interval', '$route', '$routeParams', 'Client', function ($scope, $location, $timeout, $interval, $route, $routeParams, Client) {
Client.onReady(function () { if (!Client.getUserInfo().admin) $location.path('/'); });
// Avoid full reload on path change
// https://stackoverflow.com/a/22614334
// reloadOnUrl: false in $routeProvider did not work!
var lastRoute = $route.current;
$scope.$on('$locationChangeSuccess', function (/* event */) {
if (lastRoute.$$route.originalPath === $route.current.$$route.originalPath) {
$route.current = lastRoute;
}
});
var appId = $routeParams.appId;
if (!appId) return $location.path('/apps');
$scope.view = '';
$scope.app = null;
$scope.config = Client.getConfig();
$scope.user = Client.getUserInfo();
$scope.domains = [];
$scope.groups = [];
$scope.users = [];
2019-09-19 18:41:03 +02:00
$scope.HOST_PORT_MIN = 1024;
$scope.HOST_PORT_MAX = 65535;
$scope.ROBOTS_DISABLE_INDEXING_TEMPLATE = '# Disable search engine indexing\n\nUser-agent: *\nDisallow: /';
2019-09-17 14:52:22 +02:00
$scope.setView = function (view) {
if ($scope.view === view) return;
// on error only allow uninstall or debug view
if ($scope.app.error && view !== 'uninstall') view = 'debug';
$route.updateParams({ view: view });
2019-09-17 14:52:22 +02:00
$scope[view].show();
$scope.view = view;
};
$scope.display = {
busy: false,
error: {},
success: false,
tags: '',
label: '',
icon: { data: null },
iconUrl: function () {
if (!$scope.app) return '';
if ($scope.display.icon.data === '__original__') { // user clicked reset
return $scope.app.iconUrl + '&original=true';
} else if ($scope.display.icon.data) { // user uploaded icon
return $scope.display.icon.data;
} else { // current icon
return $scope.app.iconUrl;
}
},
resetCustomIcon: function () {
$scope.display.icon.data = '__original__';
},
showCustomIconSelector: function () {
$('#iconFileInput').click();
},
show: function () {
var app = $scope.app;
2019-09-19 18:31:11 +02:00
$scope.display.error = {};
// translate for tag-input
$scope.display.tags = app.tags ? app.tags.join(',') : '';
$scope.display.label = $scope.app.label || '';
$scope.display.icon = { data: null };
},
submit: function () {
$scope.display.busy = true;
$scope.display.error = {};
2019-09-12 16:28:21 +02:00
function done() {
refreshApp($scope.display.show);
$timeout(function () {
$scope.display.busy = false;
$scope.display.success = true;
$scope.displayForm.$setPristine();
}, 1000);
2019-09-12 16:28:21 +02:00
}
// TODO break those apart
Client.configureApp($scope.app.id, 'label', { label: $scope.display.label }, function (error) {
if (error) return Client.error(error);
var tags = $scope.display.tags.split(',').map(function (t) { return t.trim(); }).filter(function (t) { return !!t; });
Client.configureApp($scope.app.id, 'tags', { tags: tags }, function (error) {
if (error) return Client.error(error);
// skip if icon is unchanged
2019-09-12 16:28:21 +02:00
if ($scope.display.icon.data === null) return done();
var icon;
if ($scope.display.icon.data === '__original__') { // user reset the icon
icon = '';
} else if ($scope.display.icon.data) { // user loaded custom icon
icon = $scope.display.icon.data.replace(/^data:image\/[a-z]+;base64,/, '');
}
Client.configureApp($scope.app.id, 'icon', { icon: icon }, function (error) {
if (error) return Client.error(error);
2019-09-12 16:28:21 +02:00
done();
});
});
});
}
};
$scope.location = {
busy: false,
error: {},
domainCollisions: [],
domain: null,
location: '',
alternateDomains: [],
portBindings: {},
portBindingsEnabled: {},
portBindingsInfo: {},
addAlternateDomain: function (event) {
event.preventDefault();
$scope.location.alternateDomains.push({
domain: $scope.domains[0],
subdomain: ''
});
},
delAlternateDomain: function (event, index) {
event.preventDefault();
$scope.location.alternateDomains.splice(index, 1);
},
show: function () {
var app = $scope.app;
2019-09-19 18:31:11 +02:00
$scope.location.error = {};
$scope.location.domainCollisions = [];
$scope.location.location = app.location;
$scope.location.domain = $scope.domains.filter(function (d) { return d.domain === app.domain; })[0];
$scope.location.portBindingsInfo = angular.extend({}, app.manifest.tcpPorts, app.manifest.udpPorts); // Portbinding map only for information
$scope.location.alternateDomains = app.alternateDomains.map(function (a) { return { subdomain: a.subdomain, domain: $scope.domains.filter(function (d) { return d.domain === a.domain; })[0] };});
// fill the portBinding structures. There might be holes in the app.portBindings, which signalizes a disabled port
for (var env in $scope.location.portBindingsInfo) {
if (app.portBindings && app.portBindings[env]) {
$scope.location.portBindings[env] = app.portBindings[env];
$scope.location.portBindingsEnabled[env] = true;
} else {
$scope.location.portBindings[env] = $scope.location.portBindingsInfo[env].defaultValue || 0;
$scope.location.portBindingsEnabled[env] = false;
}
}
},
submit: function (overwriteDns) {
$('#domainCollisionsModal').modal('hide');
$scope.location.busy = true;
$scope.location.error = {};
$scope.location.domainCollisions = [];
// only use enabled ports from portBindings
var portBindings = {};
for (var env in $scope.location.portBindings) {
if ($scope.location.portBindingsEnabled[env]) {
portBindings[env] = $scope.location.portBindings[env];
}
}
var data = {
overwriteDns: !!overwriteDns,
location: $scope.location.location,
domain: $scope.location.domain.domain,
portBindings: portBindings,
alternateDomains: $scope.location.alternateDomains.map(function (a) { return { subdomain: a.subdomain, domain: a.domain.domain };})
};
// pre-flight only for changed domains
var domains = [];
if ($scope.app.domain !== data.domain || $scope.app.location !== data.location) domains.push({ subdomain: data.location, domain: data.domain });
data.alternateDomains.forEach(function (a) {
if ($scope.app.alternateDomains.some(function (d) { return d.domain === a.domain && d.subdomain === a.subdomain; })) return;
domains.push({ subdomain: a.subdomain, domain: a.domain });
});
asyncForEach(domains, function (domain, callback) {
if (overwriteDns) return callback();
Client.getDNSRecords(domain.domain, domain.subdomain, 'A', function (error, result) {
// TODO handle credential errors
if (error) return callback(error);
if (result.length) $scope.location.domainCollisions.push(domain);
callback();
});
}, function (error) {
if (error) {
$scope.location.busy = false;
return Client.error(error);
}
if ($scope.location.domainCollisions.length) {
$scope.location.busy = false;
return $('#domainCollisionsModal').modal('show');
}
Client.configureApp($scope.app.id, 'location', data, function (error) {
if (error && (error.statusCode === 409 || error.statusCode === 400)) {
if ((error.location && error.domain) || error.field === 'location') {
$scope.location.error.location = error.message;
$scope.locationForm.$setPristine();
} else {
$scope.location.error.alternateDomains = error.message;
}
$scope.location.busy = false;
return;
}
if (error) return Client.error(error);
$scope.locationForm.$setPristine();
$scope.location.busy = false;
2019-09-11 21:24:25 +02:00
refreshApp();
});
});
}
};
$scope.access = {
busy: false,
error: {},
success: false,
ftp: false,
ssoAuth: false,
accessRestrictionOption: 'any',
accessRestriction: { users: [], groups: [] },
isAccessRestrictionValid: function () {
var tmp = $scope.access.accessRestriction;
return !!(tmp.users.length || tmp.groups.length);
},
show: function () {
var app = $scope.app;
2019-09-19 18:31:11 +02:00
$scope.access.error = {};
$scope.access.ftp = app.manifest.addons.localstorage && app.manifest.addons.localstorage.ftp;
$scope.access.ssoAuth = (app.manifest.addons['ldap'] || app.manifest.addons['oauth']) && app.sso;
$scope.access.accessRestrictionOption = app.accessRestriction ? 'groups' : 'any';
$scope.access.accessRestriction = { users: [], groups: [] };
if (app.accessRestriction) {
var userSet = { };
app.accessRestriction.users.forEach(function (uid) { userSet[uid] = true; });
$scope.users.forEach(function (u) { if (userSet[u.id] === true) $scope.access.accessRestriction.users.push(u); });
var groupSet = { };
app.accessRestriction.groups.forEach(function (gid) { groupSet[gid] = true; });
$scope.groups.forEach(function (g) { if (groupSet[g.id] === true) $scope.access.accessRestriction.groups.push(g); });
}
},
submit: function () {
$scope.access.busy = true;
$scope.access.error = {};
var accessRestriction = null;
if ($scope.access.accessRestrictionOption === 'groups') {
accessRestriction = { users: [], groups: [] };
accessRestriction.users = $scope.access.accessRestriction.users.map(function (u) { return u.id; });
accessRestriction.groups = $scope.access.accessRestriction.groups.map(function (g) { return g.id; });
}
Client.configureApp($scope.app.id, 'access_restriction', { accessRestriction: accessRestriction }, function (error) {
if (error) return Client.error(error);
$timeout(function () {
$scope.access.success = true;
$scope.access.busy = false;
}, 1000);
});
}
};
$scope.resources = {
busy: false,
2019-09-18 17:12:10 +02:00
busyDataDir: false,
error: {},
currentMemoryLimit: 0,
memoryLimit: 0,
memoryTicks: [],
dataDir: null,
show: function () {
var app = $scope.app;
2019-09-19 18:31:11 +02:00
$scope.resources.error = {};
$scope.resources.currentMemoryLimit = app.memoryLimit || app.manifest.memoryLimit || (256 * 1024 * 1024);
$scope.resources.memoryLimit = $scope.resources.currentMemoryLimit;
$scope.resources.dataDir = app.dataDir;
// create ticks starting from manifest memory limit. the memory limit here is currently split into ram+swap (and thus *2 below)
// TODO: the *2 will overallocate since 4GB is max swap that cloudron itself allocates
$scope.resources.memoryTicks = [];
var npow2 = Math.pow(2, Math.ceil(Math.log($scope.config.memory)/Math.log(2)));
for (var i = 256; i <= (npow2*2/1024/1024); i *= 2) {
if (i >= (app.manifest.memoryLimit/1024/1024 || 0)) $scope.resources.memoryTicks.push(i * 1024 * 1024);
}
if (app.manifest.memoryLimit && $scope.resources.memoryTicks[0] !== app.manifest.memoryLimit) {
$scope.resources.memoryTicks.unshift(app.manifest.memoryLimit);
}
},
2019-09-18 17:12:10 +02:00
submitMemoryLimit: function () {
$scope.resources.busy = true;
$scope.resources.error = {};
2019-09-11 21:24:25 +02:00
var memoryLimit = $scope.resources.memoryLimit === $scope.resources.memoryTicks[0] ? 0 : $scope.resources.memoryLimit;
Client.configureApp($scope.app.id, 'memory_limit', { memoryLimit: memoryLimit }, function (error) {
if (error) return Client.error(error);
$scope.resources.currentMemoryLimit = $scope.resources.memoryLimit;
$scope.resources.busy = false;
2019-09-18 17:12:10 +02:00
refreshApp();
});
},
submitDataDir: function () {
$scope.resources.busyDataDir = true;
$scope.resources.error = {};
Client.configureApp($scope.app.id, 'data_dir', { dataDir: $scope.resources.dataDir || null }, function (error) {
2019-09-18 17:12:10 +02:00
if (error && error.statusCode === 400) {
$scope.resources.error.dataDir = error.message;
$scope.resources.busyDataDir = false;
return;
}
if (error) return Client.error(error);
$scope.resourcesDataDirForm.$setPristine();
$scope.resources.busyDataDir = false;
refreshApp();
});
}
};
$scope.email = {
busy: false,
error: {},
mailboxName: '',
domain: '',
show: function () {
var app = $scope.app;
$scope.emailForm.$setPristine();
2019-09-19 18:31:11 +02:00
$scope.email.error = {};
$scope.email.mailboxName = app.mailboxName || '';
$scope.email.domain = $scope.domains.filter(function (d) { return d.domain === app.domain; })[0];
},
submit: function () {
2019-09-17 15:09:39 +02:00
$scope.email.error = {};
$scope.email.busy = true;
2019-09-11 21:24:25 +02:00
Client.configureApp($scope.app.id, 'mailbox', { mailboxName: $scope.email.mailboxName || null }, function (error) {
2019-09-17 15:09:39 +02:00
if (error && error.statusCode === 400) {
$scope.email.busy = false;
$scope.email.error.mailboxName = error.message;
$scope.emailForm.$setPristine();
return;
}
if (error) return Client.error(error);
2019-09-11 21:24:25 +02:00
$scope.emailForm.$setPristine();
2019-09-17 15:09:39 +02:00
$scope.email.busy = false;
2019-09-11 21:24:25 +02:00
refreshApp(function (error) {
if (error) return;
// when the mailboxName is 'reset', this will fill it up with the default again
$scope.email.mailboxName = $scope.app.mailboxName || '';
});
2019-09-17 15:09:39 +02:00
});
}
};
$scope.security = {
busy: false,
error: {},
success: false,
currentRobotsTxt: '',
robotsTxt: '',
show: function () {
var app = $scope.app;
2019-09-19 18:31:11 +02:00
$scope.security.error = {};
$scope.security.currentRobotsTxt = app.robotsTxt;
$scope.security.robotsTxt = $scope.security.currentRobotsTxt;
},
submit: function () {
$scope.security.busy = true;
$scope.security.error = {};
2019-09-13 15:51:36 +02:00
Client.configureApp($scope.app.id, 'robots_txt', { robotsTxt: $scope.security.robotsTxt }, function (error) {
if (error) return Client.error(error);
$scope.security.currentRobotsTxt = $scope.security.robotsTxt;
$timeout(function () {
$scope.security.success = true;
$scope.security.busy = false;
}, 1000);
});
}
};
$scope.updates = {
busy: false,
2019-09-17 16:16:48 +02:00
busyCheck: false,
busyUpdate: false,
enableAutomaticUpdate: false,
show: function () {
var app = $scope.app;
$scope.updates.enableAutomaticUpdate = app.enableAutomaticUpdate;
},
2019-09-17 16:16:48 +02:00
toggleAutomaticUpdates: function () {
$scope.updates.busy = true;
2019-09-17 16:16:48 +02:00
Client.configureApp($scope.app.id, 'automatic_update', { enable: !$scope.updates.enableAutomaticUpdate }, function (error) {
if (error) return Client.error(error);
2019-09-17 16:16:48 +02:00
$timeout(function () {
$scope.updates.enableAutomaticUpdate = !$scope.updates.enableAutomaticUpdate;
$scope.updates.busy = false;
}, 1000);
});
},
check: function () {
$scope.updates.busyCheck = true;
Client.checkForUpdates(function (error) {
if (error) Client.error(error);
$scope.updates.busyCheck = false;
});
},
askUpdate: function () {
$scope.updates.busyUpdate = false;
$('#updateModal').modal('show');
},
confirmUpdate: function () {
$scope.updates.busyUpdate = true;
Client.updateApp($scope.app.id, $scope.config.update.apps[$scope.app.id].manifest, function (error) {
$scope.updates.busyUpdate = false;
if (error) return Client.error(error);
$('#updateModal').modal('hide');
refreshApp();
});
}
};
$scope.backups = {
busy: false,
2019-09-20 00:03:52 +02:00
busyCreate: false,
error: {},
copyBackupIdDone: false,
enableBackup: false,
backups: [],
2019-09-20 00:03:52 +02:00
taskProgress: 0,
taskMessage: '',
copyBackupId: function (backup) {
var copyText = document.getElementById('backupIdHelper');
copyText.value = backup.id;
copyText.select();
document.execCommand('copy');
$scope.backups.copyBackupIdDone = true;
// reset after 2.5sec
$timeout(function () { $scope.backups.copyBackupIdDone = false; }, 2500);
},
2019-09-20 00:03:52 +02:00
trackBackupTask: function () {
$scope.backups.busyCreate = true;
refreshApp(function (error) {
if (error) Client.error(error);
2019-09-20 00:03:52 +02:00
$scope.backups.busyCreate = false;
trackAppTask(function (data) {
$scope.backups.taskProgress = data.percent;
$scope.backups.taskMessage = data.message;
}, function (error) {
2019-09-17 16:16:48 +02:00
if (error) return Client.error(error);
$scope.backups.show();
});
});
},
2019-09-20 00:03:52 +02:00
createBackup: function () {
$scope.backups.busyCreate = true;
$scope.backups.taskProgress = 0;
$scope.backups.taskMessage = '';
Client.backupApp($scope.app.id, function (error) {
if (error) Client.error(error);
$scope.backups.trackBackupTask();
});
},
show: function () {
var app = $scope.app;
2019-09-19 18:31:11 +02:00
$scope.backups.error = {};
$scope.backups.enableBackup = app.enableBackup;
Client.getAppBackups(app.id, function (error, backups) {
if (error) return Client.error(error);
$scope.backups.backups = backups;
});
},
2019-09-17 16:16:48 +02:00
toggleAutomaticBackups: function () {
$scope.backups.busy = true;
$scope.backups.error = {};
2019-09-17 16:16:48 +02:00
Client.configureApp($scope.app.id, 'automatic_backup', { enable: !$scope.backups.enableBackup }, function (error) {
if (error) return Client.error(error);
2019-09-17 16:16:48 +02:00
$timeout(function () {
$scope.backups.enableBackup = !$scope.backups.enableBackup;
$scope.backups.busy = false;
}, 1000);
});
},
restore: function (backup) {
Client.restoreApp($scope.app.id, backup.id, function (error) {
if (error) return Client.error(error);
backup.ackRestore = false;
refreshApp();
});
}
};
$scope.uninstall = {
busy: false,
error: {},
show: function () {
2019-09-19 18:31:11 +02:00
$scope.uninstall.error = {};
2019-09-17 15:40:04 +02:00
},
ask: function () {
$('#uninstallModal').modal('show');
},
submit: function () {
$scope.uninstall.busy = true;
Client.uninstallApp($scope.app.id, function (error) {
if (error && error.statusCode === 402) { // unpurchase failed
Client.error('Relogin to Cloudron App Store');
} else if (error) {
Client.error(error);
} else {
$('#uninstallModal').modal('hide');
Client.refreshAppCache($scope.app.id); // reflect the new app state immediately
$location.path('/apps');
}
$scope.uninstall.busy = false;
});
}
};
$scope.debug = {
2019-09-17 14:52:22 +02:00
show: function () {},
2019-09-19 18:41:03 +02:00
appIsRestarting: false,
2019-09-17 14:52:22 +02:00
stopAppTask: function (taskId) {
Client.stopTask(taskId, function (error) {
// we can ignore a call trying to cancel an already done task
if (error && error.statusCode !== 409) Client.error(error);
});
},
restartApp: function () {
2019-09-19 18:41:03 +02:00
$scope.debug.appIsRestarting = true;
2019-09-22 12:21:39 +02:00
function done(error) {
if (error) Client.error(error);
2019-09-19 18:41:03 +02:00
$scope.debug.appIsRestarting = false;
2019-09-22 12:21:39 +02:00
}
if ($scope.app.runState === RSTATES.STOPPED) Client.startApp($scope.app.id, done);
else Client.restartApp($scope.app.id, done);
}
};
2019-09-13 17:18:37 +02:00
$scope.clone = {
busy: true,
error: {},
backup: null,
location: '',
domain: null,
portBindings: {},
portBindingsInfo: {},
portBindingsEnabled: {},
show: function (backup) {
var app = $scope.app;
2019-09-19 18:31:11 +02:00
$scope.clone.error = {};
2019-09-13 17:18:37 +02:00
$scope.clone.backup = backup;
$scope.clone.domain = $scope.domains.find(function (d) { return app.domain === d.domain; }); // pre-select the app's domain
$scope.clone.portBindingsInfo = angular.extend({}, app.manifest.tcpPorts, app.manifest.udpPorts); // Portbinding map only for information
// set default ports
for (var env in $scope.clone.portBindingsInfo) {
$scope.clone.portBindings[env] = $scope.clone.portBindingsInfo[env].defaultValue || 0;
$scope.clone.portBindingsEnabled[env] = true;
}
$('#cloneModal').modal('show');
},
submit: function () {
$scope.clone.busy = true;
// only use enabled ports from portBindings
var finalPortBindings = {};
for (var env in $scope.clone.portBindings) {
if ($scope.clone.portBindingsEnabled[env]) {
finalPortBindings[env] = $scope.clone.portBindings[env];
}
}
var data = {
location: $scope.clone.location,
domain: $scope.clone.domain.domain,
portBindings: finalPortBindings,
backupId: $scope.clone.backup.id
};
2019-09-22 12:21:39 +02:00
Client.cloneApp($scope.app.id, data, function (error/*, clonedApp */) {
2019-09-13 17:18:37 +02:00
$scope.clone.busy = false;
if (error) {
if (error.statusCode === 409) {
if (error.portName) {
$scope.clone.error.port = error.message;
} else if (error.domain) {
$scope.clone.error.location = 'This location is already taken.';
$('#cloneLocationInput').focus();
} else {
Client.error(error);
}
} else {
Client.error(error);
}
return;
}
$('#cloneModal').modal('hide');
$location.path('/apps');
});
}
2019-09-23 10:16:19 -07:00
};
2019-09-13 17:18:37 +02:00
$scope.repair = {
busy: false,
error: {},
optionalDomains: [],
backups: [],
backupId: '',
2019-09-23 15:01:44 +02:00
// this prepares the repair dialog with whatever is required for repair action
show: function () {
$scope.repair.error = {};
$scope.repair.busy = false;
2019-09-23 15:01:44 +02:00
$scope.repair.optionalDomains = [];
$scope.repair.backupId = '';
2019-09-23 15:01:44 +02:00
var app = $scope.app;
2019-09-23 15:01:44 +02:00
// old versions may not have this set
if (!app.error.task) {
$('#repairModal').modal('show');
} else if (app.error.task.installationState === ISTATES.PENDING_UPDATE) {
$('#repairModal').modal('show');
} else if (app.error.task.installationState === ISTATES.PENDING_DATA_DIR_MIGRATION) {
$('#repairModal').modal('show');
} else if (app.error.task.installationState === ISTATES.PENDING_UNINSTALL) {
$('#repairModal').modal('show');
} else if (app.error.task.installationState === ISTATES.PENDING_DEBUG) {
$('#repairModal').modal('show');
} else if (app.error.task.installationState === ISTATES.PENDING_RESIZE) {
// memory
$('#repairModal').modal('show');
} else if (app.error.task.installationState === ISTATES.PENDING_RECREATE_CONTAINER) {
2019-09-23 15:01:44 +02:00
// email, mailbox
$('#repairModal').modal('show');
} else if (app.error.task.installationState === ISTATES.PENDING_LOCATION_CHANGE || app.error.task.installationState === ISTATES.PENDING_INSTALL) {
$scope.repair.optionalDomains = $scope.app.alternateDomains;
$scope.repair.optionalDomains.forEach(function (d) {
d.enabled = true;
});
$('#repairModal').modal('show');
} else if (app.error.task.installationState === ISTATES.PENDING_RESTORE || app.error.task.installationState === ISTATES.PENDING_CLONE) {
$scope.repair.optionalDomains = $scope.app.alternateDomains;
$scope.repair.optionalDomains.forEach(function (d) {
d.enabled = true;
});
2019-09-23 15:01:44 +02:00
Client.getAppBackups($scope.app.id, function (error, backups) {
if (error) return Client.error(error);
$scope.repair.backups = backups;
$scope.repair.backupId = '';
2019-09-23 15:01:44 +02:00
$('#repairModal').modal('show');
});
} else {
// unhandled case last resort to simply call repair
$('#repairModal').modal('show');
2019-09-23 15:01:44 +02:00
}
},
submit: function () {
$scope.repair.error = {};
$scope.repair.busy = true;
// TODO pass disabled domain info or backupId
Client.repairApp($scope.app.id, {}, function (error) {
if (error) return Client.error(error);
$scope.repair.busy = false;
$('#repairModal').modal('hide');
});
}
2019-09-23 10:16:19 -07:00
};
2019-09-19 22:49:21 +02:00
$scope.postInstallConfirm = {
message: '',
confirmed: false,
show: function () {
$scope.postInstallConfirm.message = $scope.app.manifest.postInstallMessage;
$scope.postInstallConfirm.confirmed = false;
$('#postInstallConfirmModal').modal('show');
return false; // prevent propagation and default
},
submit: function () {
if (!$scope.postInstallConfirm.confirmed) return;
$scope.app.pendingPostInstallConfirmation = false;
delete localStorage['confirmPostInstall_' + $scope.app.id];
$('#postInstallConfirmModal').modal('hide');
}
};
function fetchUsers(callback) {
Client.getUsers(function (error, users) {
if (error) return callback(error);
// ensure we have something to work with in the access restriction dropdowns
users.forEach(function (user) { user.display = user.username || user.email; });
$scope.users = users;
callback();
});
}
function fetchGroups(callback) {
Client.getGroups(function (error, groups) {
if (error) return callback(error);
$scope.groups = groups;
callback();
});
}
function getDomains(callback) {
Client.getDomains(function (error, result) {
if (error) return callback(error);
$scope.domains = result;
callback();
});
}
function getBackupConfig(callback) {
Client.getBackupConfig(function (error, backupConfig) {
if (error) return callback(error);
$scope.backupEnabled = backupConfig.provider !== 'noop';
callback();
});
}
2019-09-12 16:28:21 +02:00
function refreshApp(callback) {
callback = callback || function () {};
2019-09-18 15:53:57 +02:00
Client.getApp($scope.app.id, function (error, app) {
2019-09-12 16:28:21 +02:00
if (error) return callback(error);
$scope.app = app;
callback();
});
}
2019-09-20 00:03:52 +02:00
function trackAppTask(progressCallback, callback) {
progressCallback = progressCallback || function () {};
callback = callback || function () {};
2019-09-17 16:16:48 +02:00
2019-09-20 00:03:52 +02:00
if (!$scope.app.taskId) return callback();
Client.getTask($scope.app.taskId, function (error, result) {
if (error) console.error(error);
progressCallback(result);
// app will be refreshed on interval
$timeout(trackAppTask.bind(null, progressCallback, callback), 2000); // not yet done
});
2019-09-11 21:24:25 +02:00
}
Client.onReady(function () {
2019-09-11 21:24:25 +02:00
Client.getApp(appId, function (error, app) {
if (error && error.statusCode === 404) return $location.path('/apps');
if (error) return Client.error(error);
$scope.app = app;
$scope.setView($routeParams.view || 'display');
2019-09-20 00:03:52 +02:00
// track on page load backup if active
if (app.installationState === ISTATES.PENDING_BACKUP) $scope.backups.trackBackupTask();
asyncSeries([
fetchUsers,
fetchGroups,
getDomains,
getBackupConfig
], function (error) {
if (error) return Client.error(error);
$scope.display.show();
$scope.location.show();
$scope.resources.show();
$scope.access.show();
$scope.email.show();
$scope.security.show();
$scope.backups.show();
$scope.updates.show();
2019-09-19 18:41:03 +02:00
var refreshTimer = $interval(function () { refreshApp(); }, 3000); // call with inline function to avoid iteration argument passed see $interval docs
$scope.$on('$destroy', function () {
$interval.cancel(refreshTimer);
});
});
});
});
2019-09-12 16:28:21 +02:00
$('#iconFileInput').get(0).onchange = function (event) {
var fr = new FileReader();
fr.onload = function () {
$scope.$apply(function () {
// var file = event.target.files[0];
$scope.display.icon.data = fr.result;
});
};
fr.readAsDataURL(event.target.files[0]);
};
// setup all the dialog focus handling
2019-09-12 16:28:21 +02:00
['appUninstallModal', 'appUpdateModal', 'appRestoreModal'].forEach(function (id) {
$('#' + id).on('shown.bs.modal', function () {
2019-09-23 10:16:19 -07:00
$(this).find('[autofocus]:first').focus();
});
});
$('.modal-backdrop').remove();
}]);