Files
cloudron-box/webadmin/src/js/setup.js

171 lines
5.4 KiB
JavaScript
Raw Normal View History

'use strict';
// create main application module
var app = angular.module('Application', ['angular-md5', 'ui-notification']);
app.directive('ngEnter', function () {
return function (scope, element, attrs) {
element.bind('keydown keypress', function (event) {
if(event.which === 13) {
scope.$apply(function (){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
// app.service('Wizard', [ function () {
// var instance = null;
// function Wizard() {
// this.username = '';
// this.email = '';
// this.password = '';
// this.displayName = '';
// this.setupToken = null;
// this.provider = null;
// this.apiServerOrigin = null;
// this.createAppstoreAccount = true;
// this.dnsConfig = null;
// }
// instance = new Wizard();
// return instance;
// }]);
// app.controller('StepController', ['$scope', '$route', '$location', 'Wizard', function ($scope, $route, $location, Wizard) {
// $scope.wizard = Wizard;
// $scope.next = function (bad) {
// if (bad) return;
// var current = $location.path();
// var next = '';
// if (current === '/step1') {
// next = '/step2';
// } else if (current === '/step2') {
// if (Wizard.dnsConfig === null) next = '/step4';
// else next = '/step3';
// } else if (current === '/step3') {
// next = '/step4';
// } else {
// next = '/step1';
// }
// $location.path(next);
// };
// $scope.focusNext = function (elemId, bad) {
// if (!bad) $('#' + elemId).focus();
// };
// }]);
// app.controller('FinishController', ['$scope', '$location', '$http', 'Wizard', 'Client', function ($scope, $location, $http, Wizard, Client) {
// $scope.wizard = Wizard;
// function setDnsConfigIfNeeded(callback) {
// if ($scope.wizard.dnsConfig === null) return callback(null);
// Client.setDnsConfig($scope.wizard.dnsConfig, callback);
// }
// }]);
app.controller('SetupController', ['$scope', '$http', 'Client', function ($scope, $http, Client) {
// Stupid angular location provider either wants html5 location mode or not, do the query parsing on my own
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.busy = false;
$scope.account = {
email: '',
displayName: '',
requireEmail: false,
username: '',
password: ''
};
$scope.error = null;
$scope.provider = '';
$scope.apiServerOrigin = '';
$scope.setupToken = '';
$scope.createAppstoreAccount = true;
$scope.showDNSSetup = false;
$scope.activateCloudron = function () {
$scope.busy = true;
function registerAppstoreAccountIfNeeded(callback) {
if (!$scope.createAppstoreAccount) return callback(null);
$http.post($scope.apiServerOrigin + '/api/v1/users', { email: $scope.account.email, password: $scope.account.password }).success(function (data, status) {
if (status !== 201) return callback({ status: status, data: data });
Client.setAppstoreConfig({ userId: data.userId, token: data.accessToken }, callback);
}).error(function (data, status) {
callback({ status: status, data: data });
});
}
Client.createAdmin($scope.account.username, $scope.account.password, $scope.account.email, $scope.account.displayName, $scope.setupToken, function (error) {
if (error) {
console.error('Internal error', error);
$scope.error = error;
$scope.busy = false;
return;
}
// for caas we are done here
if ($scope.provider === 'caas') {
window.location.href = '/';
return;
}
registerAppstoreAccountIfNeeded(function (error) {
if (error) console.error('Unable to create appstore account.', error); // this is not fatal
2015-10-27 12:04:27 -07:00
$scope.busy = false;
$scope.showDNSSetup = true;
2015-10-27 12:04:27 -07:00
});
});
};
2015-12-29 11:00:32 +01:00
Client.getStatus(function (error, status) {
if (error) {
window.location.href = '/error.html';
return;
}
2015-12-29 11:00:32 +01:00
if (status.activated) {
window.location.href = '/';
return;
}
if (status.provider === 'caas') {
if (!search.setupToken) {
window.location.href = '/error.html?errorCode=2';
return;
}
2015-12-29 11:00:32 +01:00
if (!search.email) {
window.location.href = '/error.html?errorCode=3';
return;
}
2015-12-29 11:00:32 +01:00
$scope.setupToken = search.setupToken;
$scope.createAppstoreAccount = false;
2015-12-29 11:00:32 +01:00
}
$scope.account.email = search.email || $scope.account.email;
$scope.account.displayName = search.displayName || $scope.account.displayName;
$scope.account.requireEmail = !search.email;
$scope.provider = status.provider;
$scope.apiServerOrigin = status.apiServerOrigin;
$scope.initialized = true;
});
}]);