Addressbook on SendPage

This commit is contained in:
Gustavo Cortez 2014-06-18 01:00:32 -03:00
commit f5aad7bd15
6 changed files with 184 additions and 4 deletions

View file

@ -2,7 +2,7 @@
var bitcore = require('bitcore');
angular.module('copayApp.controllers').controller('SendController',
function($scope, $rootScope, $window, $location, $timeout) {
function($scope, $rootScope, $window, $location, $timeout, $anchorScroll, $modal) {
$scope.title = 'Send';
$scope.loading = false;
var satToUnit = 1 / config.unitToSatoshi;
@ -187,4 +187,80 @@ angular.module('copayApp.controllers').controller('SendController',
}
}, 500);
};
$scope.deleteAddressBook = function(addressBook) {
var w = $rootScope.wallet;
$timeout(function() {
var errorMsg;
try {
w.deleteAddressBook(addressBook);
} catch (e) {
errorMsg = e.message;
}
$rootScope.$flashMessage = {
message: errorMsg ? errorMsg : 'Entry removed successful',
type: errorMsg ? 'error' : 'success'
};
$rootScope.$digest();
}, 500);
};
$scope.copyAddress = function(address) {
$scope.address = address;
$anchorScroll();
};
$scope.openAddressBookModal = function() {
var modalInstance = $modal.open({
templateUrl: 'addressBookModal.html',
windowClass: 'tiny',
controller: function($scope, $modalInstance) {
$scope.submitAddressBook = function(form) {
if (form.$invalid) {
$rootScope.$flashMessage = {
message: 'Complete required fields, please',
type: 'error'
};
return;
}
var entry = {
"address": form.newaddress.$modelValue,
"label": form.newlabel.$modelValue
};
form.newaddress.$pristine = form.newlabel.$pristine = true;
$modalInstance.close(entry);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
},
});
modalInstance.result.then(function(entry) {
var w = $rootScope.wallet;
$timeout(function() {
$scope.loading = false;
var errorMsg;
try {
w.setAddressBook(entry);
} catch (e) {
errorMsg = e.message;
}
$rootScope.$flashMessage = {
message: errorMsg ? errorMsg : 'New entry has been created',
type: errorMsg ? 'error' : 'success'
};
$rootScope.$digest();
}, 500);
$anchorScroll();
// reset fields
$scope.newaddress = $scope.newlabel = null;
});
};
});

View file

@ -38,12 +38,13 @@ function Wallet(opts) {
this.token = opts.token;
this.tokenTime = opts.tokenTime;
}
this.verbose = opts.verbose;
this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id;
this.network.maxPeers = this.totalCopayers;
this.registeredPeerIds = [];
this.addressBook = opts.addressBook || [];
}
Wallet.parent = EventEmitter;
@ -332,7 +333,8 @@ Wallet.prototype.toObj = function() {
opts: optsObj,
publicKeyRing: this.publicKeyRing.toObj(),
txProposals: this.txProposals.toObj(),
privateKey: this.privateKey ? this.privateKey.toObj() : undefined
privateKey: this.privateKey ? this.privateKey.toObj() : undefined,
addressBook: this.addressBook
};
return walletObj;
@ -343,6 +345,7 @@ Wallet.fromObj = function(o, storage, network, blockchain) {
opts.publicKeyRing = copay.PublicKeyRing.fromObj(o.publicKeyRing);
opts.txProposals = copay.TxProposals.fromObj(o.txProposals);
opts.privateKey = copay.PrivateKey.fromObj(o.privateKey);
opts.addressBook = o.addressBook;
opts.storage = storage;
opts.network = network;
@ -720,4 +723,26 @@ Wallet.prototype.getNetwork = function() {
return this.network;
};
Wallet.prototype._checkAddressBook = function(address) {
for(var i=0;i<this.addressBook.length; i++) {
if (this.addressBook[i].address == address) {
throw new Error('This address already exists in your Address Book: ' + address);
}
}
};
Wallet.prototype.setAddressBook = function(addressBook) {
this._checkAddressBook(addressBook.address);
this.addressBook.push(addressBook);
this.store();
};
Wallet.prototype.deleteAddressBook = function(addressBook) {
var index = this.addressBook.indexOf(addressBook);
if (index > -1) {
this.addressBook.splice(index, 1);
this.store();
}
};
module.exports = require('soop')(Wallet);

View file

@ -86,6 +86,7 @@ WalletFactory.prototype.read = function(walletId) {
obj.publicKeyRing = s.get(walletId, 'publicKeyRing');
obj.txProposals = s.get(walletId, 'txProposals');
obj.privateKey = s.get(walletId, 'privateKey');
obj.addressBook = s.get(walletId, 'addressBook');
var w = this.fromObj(obj);
return w;