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

91 lines
2.5 KiB
JavaScript

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