Wallet/src/js/controllers/create.js

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('createController',
function($scope, $rootScope, $location, $timeout, $log, lodash, go, profileService, configService, isMobile, isCordova, gettext) {
2015-03-06 12:00:10 -03:00
var self = this;
var defaults = configService.getDefaults();
2015-04-14 16:20:12 -03:00
this.isWindowsPhoneApp = isMobile.Windows() && isCordova;
2015-03-06 12:00:10 -03:00
/* For compressed keys, m*73 + n*34 <= 496 */
var COPAYER_PAIR_LIMITS = {
1: 1,
2: 2,
3: 3,
4: 4,
5: 4,
6: 4,
7: 3,
8: 3,
9: 2,
10: 2,
11: 1,
12: 1,
};
// ng-repeat defined number of times instead of repeating over array?
this.getNumber = function(num) {
return new Array(num);
}
var updateRCSelect = function(n) {
$scope.totalCopayers = n;
2015-03-06 12:00:10 -03:00
var maxReq = COPAYER_PAIR_LIMITS[n];
self.RCValues = lodash.range(1, maxReq + 1);
$scope.requiredCopayers = Math.min(parseInt(n / 2 + 1), maxReq);
};
this.TCValues = lodash.range(2, defaults.limits.totalCopayers + 1);
2015-03-06 12:00:10 -03:00
$scope.totalCopayers = defaults.wallet.totalCopayers;
this.setTotalCopayers = function(tc) {
updateRCSelect(tc);
};
2015-03-06 12:00:10 -03:00
this.create = function(form) {
if (form && form.$invalid) {
this.error = gettext('Please enter the required fields');
2015-03-06 12:00:10 -03:00
return;
}
var opts = {
m: $scope.requiredCopayers,
n: $scope.totalCopayers,
name: form.walletName.$modelValue,
extendedPrivateKey: form.privateKey.$modelValue,
myName: $scope.totalCopayers > 1 ? form.myName.$modelValue : null,
networkName: form.isTestnet.$modelValue ? 'testnet' : 'livenet'
};
self.loading = true;
$timeout(function() {
profileService.createWallet(opts, function(err, secret) {
self.loading = false;
if (err) {
$log.debug(err);
self.error = err;
2015-08-12 08:57:44 -03:00
$timeout(function() {
$rootScope.$apply();
});
2015-03-06 12:00:10 -03:00
}
else {
go.walletHome();
}
});
}, 100);
};
2015-04-14 16:20:12 -03:00
this.formFocus = function(what) {
if (!this.isWindowsPhoneApp) return
if (what && what == 'my-name') {
this.hideWalletName = true;
2015-05-20 15:16:39 -03:00
this.hideTabs = true;
}
else if (what && what == 'wallet-name'){
this.hideTabs = true;
2015-04-14 16:20:12 -03:00
}
else {
this.hideWalletName = false;
2015-05-20 15:16:39 -03:00
this.hideTabs = false;
2015-04-14 16:20:12 -03:00
}
$timeout(function() {
$rootScope.$digest();
}, 1);
};
2015-03-06 12:00:10 -03:00
$scope.$on("$destroy", function() {
$rootScope.hideWalletNavigation = false;
});
});