2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// create main application module
|
|
|
|
|
var app = angular.module('Application', ['ngRoute', 'ngAnimate', '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();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// setup all major application routes
|
|
|
|
|
app.config(['$routeProvider', function ($routeProvider) {
|
|
|
|
|
$routeProvider.when('/', {
|
|
|
|
|
redirectTo: '/step1'
|
|
|
|
|
}).when('/step1', {
|
|
|
|
|
controller: 'StepController',
|
|
|
|
|
templateUrl: 'views/setup/step1.html'
|
|
|
|
|
}).when('/step2', {
|
|
|
|
|
controller: 'StepController',
|
|
|
|
|
templateUrl: 'views/setup/step2.html'
|
|
|
|
|
}).when('/step3', {
|
2015-10-30 21:04:05 +01:00
|
|
|
controller: 'StepController',
|
2015-07-29 15:11:34 -07:00
|
|
|
templateUrl: 'views/setup/step3.html'
|
2015-10-27 12:04:27 -07:00
|
|
|
}).when('/step4', {
|
|
|
|
|
controller: 'FinishController',
|
|
|
|
|
templateUrl: 'views/setup/step4.html'
|
2015-07-20 00:09:47 -07:00
|
|
|
}).otherwise({ redirectTo: '/'});
|
|
|
|
|
}]);
|
|
|
|
|
|
|
|
|
|
app.service('Wizard', [ function () {
|
|
|
|
|
var instance = null;
|
|
|
|
|
|
|
|
|
|
function Wizard() {
|
|
|
|
|
this.username = '';
|
|
|
|
|
this.email = '';
|
|
|
|
|
this.password = '';
|
|
|
|
|
this.availableAvatars = [{
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
|
|
|
|
url: '/img/avatars/avatar_0.png',
|
|
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
2015-07-29 11:06:33 +02:00
|
|
|
url: '/img/avatars/rubber-duck.png'
|
2015-07-20 00:09:47 -07:00
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
2015-07-29 11:06:33 +02:00
|
|
|
url: '/img/avatars/carrot.png'
|
2015-07-20 00:09:47 -07:00
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
2015-07-29 11:06:33 +02:00
|
|
|
url: '/img/avatars/cup.png'
|
2015-07-20 00:09:47 -07:00
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
2015-07-29 11:06:33 +02:00
|
|
|
url: '/img/avatars/football.png'
|
2015-07-20 00:09:47 -07:00
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
2015-07-29 11:06:33 +02:00
|
|
|
url: '/img/avatars/owl.png'
|
2015-07-20 00:09:47 -07:00
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
2015-07-29 11:06:33 +02:00
|
|
|
url: '/img/avatars/space-rocket.png'
|
|
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
|
|
|
|
url: '/img/avatars/armchair.png'
|
|
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
|
|
|
|
url: '/img/avatars/cap.png'
|
|
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
|
|
|
|
url: '/img/avatars/pan.png'
|
|
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
|
|
|
|
url: '/img/avatars/meat.png'
|
|
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
|
|
|
|
url: '/img/avatars/umbrella.png'
|
|
|
|
|
}, {
|
|
|
|
|
file: null,
|
|
|
|
|
data: null,
|
|
|
|
|
url: '/img/avatars/jar.png'
|
2015-07-20 00:09:47 -07:00
|
|
|
}];
|
|
|
|
|
this.avatar = {};
|
|
|
|
|
this.avatarBlob = null;
|
2015-10-27 12:04:27 -07:00
|
|
|
this.dnsConfig = null;
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Wizard.prototype.setPreviewAvatar = function (avatar) {
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
|
|
this.avatar = avatar;
|
|
|
|
|
|
2015-07-29 17:05:29 -07:00
|
|
|
// scale image and get the blob now. do not use the previewAvatar element here because it is not updated yet
|
|
|
|
|
var img = document.createElement('img');
|
|
|
|
|
img.src = avatar.data || avatar.url;
|
2015-07-20 00:09:47 -07:00
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
|
canvas.width = 256;
|
|
|
|
|
canvas.height = 256;
|
|
|
|
|
|
|
|
|
|
var imageDimensionRatio = img.width / img.height;
|
|
|
|
|
var canvasDimensionRatio = canvas.width / canvas.height;
|
|
|
|
|
var renderableHeight, renderableWidth, xStart, yStart;
|
|
|
|
|
|
|
|
|
|
if (imageDimensionRatio > canvasDimensionRatio) {
|
|
|
|
|
renderableHeight = canvas.height;
|
|
|
|
|
renderableWidth = img.width * (renderableHeight / img.height);
|
|
|
|
|
xStart = (canvas.width - renderableWidth) / 2;
|
|
|
|
|
yStart = 0;
|
|
|
|
|
} else if (imageDimensionRatio < canvasDimensionRatio) {
|
|
|
|
|
renderableWidth = canvas.width;
|
|
|
|
|
renderableHeight = img.height * (renderableWidth / img.width);
|
|
|
|
|
xStart = 0;
|
|
|
|
|
yStart = (canvas.height - renderableHeight) / 2;
|
|
|
|
|
} else {
|
|
|
|
|
renderableHeight = canvas.height;
|
|
|
|
|
renderableWidth = canvas.width;
|
|
|
|
|
xStart = 0;
|
|
|
|
|
yStart = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
ctx.drawImage(img, xStart, yStart, renderableWidth, renderableHeight);
|
|
|
|
|
|
|
|
|
|
canvas.toBlob(function (blob) {
|
|
|
|
|
that.avatarBlob = blob;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
instance = new Wizard();
|
|
|
|
|
return instance;
|
|
|
|
|
}]);
|
|
|
|
|
|
2015-07-29 17:05:29 -07:00
|
|
|
app.controller('StepController', ['$scope', '$route', '$location', 'Wizard', function ($scope, $route, $location, Wizard) {
|
2015-07-20 00:09:47 -07:00
|
|
|
$scope.wizard = Wizard;
|
|
|
|
|
|
2015-11-05 20:31:52 +01:00
|
|
|
$scope.next = function (bad) {
|
|
|
|
|
if (bad) return;
|
|
|
|
|
|
|
|
|
|
var current = $location.path();
|
|
|
|
|
var next = '';
|
|
|
|
|
|
|
|
|
|
if (current === '/step1') {
|
|
|
|
|
next = '/step2';
|
|
|
|
|
} else if (current === '/step2') {
|
2015-11-09 00:42:25 -08:00
|
|
|
if (Wizard.dnsConfig === null) next = '/step4';
|
2015-11-05 20:31:52 +01:00
|
|
|
else next = '/step3';
|
|
|
|
|
} else if (current === '/step3') {
|
|
|
|
|
next = '/step4';
|
|
|
|
|
} else {
|
|
|
|
|
next = '/step1';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$location.path(next);
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.focusNext = function (elemId, bad) {
|
|
|
|
|
if (!bad) $('#' + elemId).focus();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.$on('$viewContentLoaded', function () {
|
|
|
|
|
$('a[autofocus]').focus();
|
|
|
|
|
$('input[autofocus]').focus();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.showCustomAvatarSelector = function () {
|
|
|
|
|
$('#avatarFileInput').click();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// cheap way to detect if we are in avatar and name selection step
|
2015-07-29 17:05:29 -07:00
|
|
|
if ($route.current.templateUrl === 'views/setup/step1.html') {
|
2015-07-20 00:09:47 -07:00
|
|
|
$('#avatarFileInput').get(0).onchange = function (event) {
|
|
|
|
|
var fr = new FileReader();
|
|
|
|
|
fr.onload = function () {
|
|
|
|
|
$scope.$apply(function () {
|
|
|
|
|
var tmp = {
|
|
|
|
|
file: event.target.files[0],
|
|
|
|
|
data: fr.result,
|
|
|
|
|
url: null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.wizard.availableAvatars.push(tmp);
|
|
|
|
|
$scope.wizard.setPreviewAvatar(tmp);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
fr.readAsDataURL(event.target.files[0]);
|
|
|
|
|
};
|
2015-07-29 16:30:32 -07:00
|
|
|
|
|
|
|
|
// ensure image got loaded before setting the preview avatar
|
|
|
|
|
var image = document.createElement('img');
|
|
|
|
|
var randomIndex = Math.floor(Math.random() * $scope.wizard.availableAvatars.length);
|
|
|
|
|
image.onload = function() {
|
|
|
|
|
$scope.$apply(function () { $scope.wizard.setPreviewAvatar($scope.wizard.availableAvatars[randomIndex]); });
|
|
|
|
|
image = null;
|
|
|
|
|
};
|
|
|
|
|
image.src = $scope.wizard.availableAvatars[randomIndex].data || $scope.wizard.availableAvatars[randomIndex].url;
|
2015-11-09 00:42:25 -08:00
|
|
|
} else if ($route.current.templateUrl === 'views/setup/step3.html' && Wizard.dnsConfig === null) {
|
2015-10-30 21:04:05 +01:00
|
|
|
$location.path('/step4'); // not using custom domain
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
2015-07-29 15:43:13 -07:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
}]);
|
|
|
|
|
|
2015-10-27 12:39:06 -07:00
|
|
|
app.controller('FinishController', ['$scope', '$location', 'Wizard', 'Client', function ($scope, $location, Wizard, Client) {
|
2015-07-20 00:09:47 -07:00
|
|
|
$scope.wizard = Wizard;
|
|
|
|
|
|
2015-10-20 13:20:37 +02:00
|
|
|
Client.createAdmin($scope.wizard.username, $scope.wizard.password, $scope.wizard.email, $scope.setupToken, function (error) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) {
|
|
|
|
|
console.error('Internal error', error);
|
|
|
|
|
window.location.href = '/error.html';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.changeCloudronAvatar($scope.wizard.avatarBlob, function (error) {
|
|
|
|
|
if (error) return console.error('Unable to set avatar.', error);
|
|
|
|
|
|
2015-11-09 00:20:16 -08:00
|
|
|
if ($scope.wizard.dnsConfig === null) {
|
|
|
|
|
window.location.href = '/';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 12:04:27 -07:00
|
|
|
Client.setDnsConfig($scope.wizard.dnsConfig, function (error) {
|
|
|
|
|
if (error) return console.error('Unable to set dns config.', error);
|
|
|
|
|
|
|
|
|
|
window.location.href = '/';
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}]);
|
|
|
|
|
|
|
|
|
|
app.controller('SetupController', ['$scope', '$location', 'Client', 'Wizard', function ($scope, $location, Client, Wizard) {
|
|
|
|
|
$scope.initialized = false;
|
|
|
|
|
|
|
|
|
|
// 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; }, {});
|
|
|
|
|
|
2015-12-29 11:00:32 +01:00
|
|
|
Client.getStatus(function (error, status) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) {
|
|
|
|
|
window.location.href = '/error.html';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-29 11:00:32 +01:00
|
|
|
if (status.activated) {
|
2015-07-20 00:09:47 -07:00
|
|
|
window.location.href = '/';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-29 11:00:32 +01:00
|
|
|
if (!search.setupToken) {
|
|
|
|
|
window.location.href = '/error.html?errorCode=2';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!search.email) {
|
|
|
|
|
window.location.href = '/error.html?errorCode=3';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.setupToken = search.setupToken;
|
|
|
|
|
Wizard.email = search.email;
|
|
|
|
|
|
|
|
|
|
if (search.customDomain === 'true') {
|
|
|
|
|
Wizard.dnsConfig = {
|
|
|
|
|
provider: 'route53',
|
|
|
|
|
accessKeyId: null,
|
|
|
|
|
secretAccessKey: null
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
$location.path('/step1');
|
|
|
|
|
|
|
|
|
|
$scope.initialized = true;
|
|
|
|
|
});
|
|
|
|
|
}]);
|