mail/src/js/controller/login-initial.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-10-21 07:10:42 -04:00
define(function(require) {
'use strict';
2013-10-23 11:17:36 -04:00
var appController = require('js/app-controller'),
errorUtil = require('js/util/error'),
2013-10-23 11:17:36 -04:00
dl = require('js/util/download');
2013-10-21 07:10:42 -04:00
var LoginInitialCtrl = function($scope, $location) {
2013-10-23 11:17:36 -04:00
var emailDao = appController._emailDao,
states;
2013-11-11 07:28:05 -05:00
// global state... inherited to all child scopes
$scope.$root.state = {};
// attach global error handler
errorUtil.attachHandler($scope);
2013-11-11 07:28:05 -05:00
states = {
2013-10-23 11:17:36 -04:00
IDLE: 1,
PROCESSING: 2,
DONE: 4
};
2013-11-11 07:28:05 -05:00
$scope.state.ui = states.IDLE; // initial state
2013-10-23 11:17:36 -04:00
//
// scope functions
//
2013-10-21 07:10:42 -04:00
$scope.confirmPassphrase = function() {
2013-11-11 07:28:05 -05:00
var passphrase = $scope.state.passphrase,
confirmation = $scope.state.confirmation;
2013-10-21 07:10:42 -04:00
if (!passphrase || passphrase !== confirmation) {
return;
}
$scope.setState(states.PROCESSING);
2013-10-23 11:17:36 -04:00
setTimeout(function() {
emailDao.unlock({}, passphrase, function(err) {
if (err) {
$scope.setState(states.IDLE);
$scope.onError(err);
2013-10-23 11:17:36 -04:00
return;
}
2013-10-21 07:10:42 -04:00
$scope.setState(states.DONE);
$scope.$apply();
2013-10-23 11:17:36 -04:00
});
}, 500);
};
2013-10-21 07:10:42 -04:00
2013-10-23 11:17:36 -04:00
$scope.exportKeypair = function() {
// export keys from keychain
emailDao._crypto.exportKeys(function(err, keys) {
2013-10-21 07:10:42 -04:00
if (err) {
$scope.onError(err);
2013-10-21 07:10:42 -04:00
return;
}
2013-10-23 11:17:36 -04:00
var id = keys.keyId.substring(8, keys.keyId.length);
2013-11-04 08:20:14 -05:00
dl.createDownload({
content: keys.publicKeyArmored + keys.privateKeyArmored,
filename: id + '.asc',
contentType: 'text/plain'
}, onSave);
2013-10-23 11:17:36 -04:00
});
2013-11-04 08:20:14 -05:00
function onSave(err) {
2013-10-23 11:17:36 -04:00
if (err) {
$scope.onError(err);
2013-10-23 11:17:36 -04:00
return;
}
2013-11-04 08:20:14 -05:00
$scope.proceed();
$scope.$apply();
}
2013-10-21 07:10:42 -04:00
};
2013-11-04 08:20:14 -05:00
$scope.proceed = function() {
2013-10-21 07:10:42 -04:00
$location.path('/desktop');
2013-11-04 08:20:14 -05:00
};
2013-10-23 11:17:36 -04:00
$scope.setState = function(state) {
2013-11-11 07:28:05 -05:00
$scope.state.ui = state;
};
2013-10-21 07:10:42 -04:00
};
return LoginInitialCtrl;
2013-10-23 11:17:36 -04:00
});