refactor controller code to controller from directive

This commit is contained in:
Manuel Araoz 2014-08-13 18:42:04 -04:00
commit 967457764c
4 changed files with 71 additions and 66 deletions

View file

@ -80,8 +80,7 @@ angular.module('copayApp.controllers').controller('SendController',
var message = 'The transaction proposal has been created';
if (merchantData) {
if (merchantData.pr.ca) {
message += ' This payment protocol transaction'
+ ' has been verified through ' + merchantData.pr.ca + '.';
message += ' This payment protocol transaction' + ' has been verified through ' + merchantData.pr.ca + '.';
}
message += ' Message from server: ' + merchantData.ack.memo;
message += ' For merchant: ' + merchantData.pr.pd.payment_url;
@ -94,8 +93,7 @@ angular.module('copayApp.controllers').controller('SendController',
var message = 'Transaction id: ' + txid;
if (merchantData) {
if (merchantData.pr.ca) {
message += ' This payment protocol transaction'
+ ' has been verified through ' + merchantData.pr.ca + '.';
message += ' This payment protocol transaction' + ' has been verified through ' + merchantData.pr.ca + '.';
}
message += ' Message from server: ' + merchantData.ack.memo;
message += ' For merchant: ' + merchantData.pr.pd.payment_url;
@ -115,9 +113,13 @@ angular.module('copayApp.controllers').controller('SendController',
if (address.indexOf('bitcoin:') === 0) {
uri = copay.HDPath.parseBitcoinURI(address);
} else if (address.indexOf('Merchant: ') === 0) {
uri = { merchant: address.split(/\s+/)[1] };
uri = {
merchant: address.split(/\s+/)[1]
};
} else if (/^https?:\/\//.test(address)) {
uri = { merchant: address };
uri = {
merchant: address
};
}
if (uri && uri.merchant) {
@ -352,12 +354,11 @@ angular.module('copayApp.controllers').controller('SendController',
notification.error('Error', 'There was an error sending the transaction');
} else {
if (!merchantData) {
notification.success('Transaction broadcast', 'Transaction id: '+txid);
notification.success('Transaction broadcast', 'Transaction id: ' + txid);
} else {
var message = 'Transaction ID: ' + txid;
if (merchantData.pr.ca) {
message += ' This payment protocol transaction'
+ ' has been verified through ' + merchantData.pr.ca + '.';
message += ' This payment protocol transaction' + ' has been verified through ' + merchantData.pr.ca + '.';
}
message += ' Message from server: ' + merchantData.ack.memo;
message += ' For merchant: ' + merchantData.pr.pd.payment_url;
@ -399,4 +400,62 @@ angular.module('copayApp.controllers').controller('SendController',
$scope.loadTxs();
};
$scope.onChanged = function() {
var scope = $scope;
notification.info('Fetching Payment',
'Retrieving Payment Request from ' + uri.merchant);
// Payment Protocol URI (BIP-72)
scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) {
var balance = $rootScope.availableBalance;
var available = +(balance * config.unitToSatoshi).toFixed(0);
if (merchantData && available < +merchantData.total) {
err = new Error('No unspent outputs available.');
}
if (err) {
scope.sendForm.address.$isValid = false;
notification.error('Error', err.message || 'Bad payment server.');
return;
}
merchantData.unitTotal = (+merchantData.total / config.unitToSatoshi) + '';
merchantData.expiration = new Date(
merchantData.pr.pd.expires * 1000).toISOString();
$rootScope.merchant = merchantData;
scope.sendForm.address.$isValid = true;
scope.sendForm.amount.$setViewValue(merchantData.unitTotal);
scope.sendForm.amount.$render();
scope.sendForm.amount.$isValid = true;
// If the address changes to a non-payment-protocol one,
// delete the `merchant` property from the scope.
var unregister = scope.$watch('address', function() {
var val = scope.sendForm.address.$viewValue || '';
var uri = copay.HDPath.parseBitcoinURI(val);
if (!uri || !uri.merchant) {
delete $rootScope.merchant;
scope.sendForm.amount.$setViewValue('');
scope.sendForm.amount.$render();
unregister();
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
}
}
});
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
}
notification.info('Payment Request',
'Server is requesting ' + merchantData.unitTotal + ' ' + config.unitName + '.' + ' Message: ' + merchantData.pr.pd.memo);
});
};
});

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.directives')
.directive('validAddress', function($rootScope, notification) {
.directive('validAddress', function() {
var bitcore = require('bitcore');
var Address = bitcore.Address;
var bignum = bitcore.Bignum;
@ -27,62 +27,8 @@ angular.module('copayApp.directives')
return value;
}
notification.info('Fetching Payment',
'Retrieving Payment Request from ' + uri.merchant);
// Payment Protocol URI (BIP-72)
scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) {
var balance = $rootScope.availableBalance;
var available = +(balance * config.unitToSatoshi).toFixed(0);
if (merchantData && available < +merchantData.total) {
err = new Error('No unspent outputs available.');
}
if (err) {
scope.sendForm.address.$isValid = false;
notification.error('Error', err.message || 'Bad payment server.');
return;
}
merchantData.unitTotal = (+merchantData.total / config.unitToSatoshi) + '';
merchantData.expiration = new Date(
merchantData.pr.pd.expires * 1000).toISOString();
$rootScope.merchant = merchantData;
scope.sendForm.address.$isValid = true;
scope.sendForm.amount.$setViewValue(merchantData.unitTotal);
scope.sendForm.amount.$render();
scope.sendForm.amount.$isValid = true;
// If the address changes to a non-payment-protocol one,
// delete the `merchant` property from the scope.
var unregister = scope.$watch('address', function() {
var val = scope.sendForm.address.$viewValue || '';
var uri = copay.HDPath.parseBitcoinURI(val);
if (!uri || !uri.merchant) {
delete $rootScope.merchant;
scope.sendForm.amount.$setViewValue('');
scope.sendForm.amount.$render();
unregister();
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
}
}
});
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
}
notification.info('Payment Request',
'Server is requesting ' + merchantData.unitTotal + ' ' + config.unitName + '.' + ' Message: ' + merchantData.pr.pd.memo);
});
ctrl.$setValidity('validAddress', true);
return 'Merchant: ' + uri.merchant;
};

View file

@ -43,9 +43,9 @@ module.exports = function(config) {
//App-specific Code
'js/app.js',
'js/routes.js',
'js/services/*.js',
'js/directives.js',
'js/filters.js',
'js/services/*.js',
'js/controllers/*.js',
'js/init.js',

View file

@ -21,7 +21,7 @@
</label>
<div class="small-10 columns">
<input type="text" id="address" name="address" ng-disabled="loading"
placeholder="Send to" ng-model="address" valid-address required>
placeholder="Send to" ng-model="address" ngChange="onChanged()" valid-address required>
<small class="icon-input" ng-show="!sendForm.address.$invalid && address"><i class="fi-check"></i></small>
<small class="icon-input" ng-show="sendForm.address.$invalid && address"><i class="fi-x"></i></small>
</div>