paypro: minor refactor - clean up whitespace in directives.

This commit is contained in:
Christopher Jeffrey 2014-08-07 09:17:03 -07:00 committed by Manuel Araoz
commit 80ceca3b73

View file

@ -12,146 +12,153 @@ angular.module('copayApp.directives')
require: 'ngModel', require: 'ngModel',
link: function(scope, elem, attrs, ctrl) { link: function(scope, elem, attrs, ctrl) {
var validator = function(value) { var validator = function(value) {
var uri = copay.HDPath.parseBitcoinURI(value); var uri;
// Is this a payment protocol URI (BIP-72)? if (/^https?:\/\//.test(value)) {
if (uri && uri.merchant) { uri = { merchant: value };
scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) { } else {
var balance = $rootScope.availableBalance; uri = copay.HDPath.parseBitcoinURI(value);
var available = +(balance * config.unitToSatoshi).toFixed(0); }
if ((err && err.message === 'No unspent outputs.') // Regular Address
|| available < +merchantData.total) { if (!uri || !uri.merchant) {
// TODO: Actually display a notification window here var a = new Address(value);
// instead of simply saying the URI is invalid. ctrl.$setValidity('validAddress', a.isValid() && a.network().name === config.networkName);
ctrl.$setValidity('validAddress', false); return value;
return; }
}
if (err) { // Payment Protocol URI (BIP-72)
if (scope._resetPayPro) scope._resetPayPro(); scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) {
ctrl.$setValidity('validAddress', false); var balance = $rootScope.availableBalance;
return; var available = +(balance * config.unitToSatoshi).toFixed(0);
}
var expires = new Date(merchantData.pr.pd.expires * 1000); if ((err && err.message === 'No unspent outputs.')
var memo = merchantData.pr.pd.memo; || available < +merchantData.total) {
var payment_url = merchantData.pr.pd.payment_url; // TODO: Actually display a notification window here
var total = merchantData.total; // instead of simply saying the URI is invalid.
ctrl.$setValidity('validAddress', false);
return;
}
if (typeof total === 'string') { if (err) {
total = bignum(total, 10).toBuffer({ if (scope._resetPayPro) scope._resetPayPro();
endian: 'little', ctrl.$setValidity('validAddress', false);
size: 1 return;
}); }
}
total = bignum var expires = new Date(merchantData.pr.pd.expires * 1000);
.fromBuffer(total, { var memo = merchantData.pr.pd.memo;
endian: 'little', var payment_url = merchantData.pr.pd.payment_url;
size: 1 var total = merchantData.total;
})
.toString(10);
// XXX There needs to be a better way to do this: if (typeof total === 'string') {
total = +total / config.unitToSatoshi; total = bignum(total, 10).toBuffer({
endian: 'little',
size: 1
});
}
// XXX Pretty much all of this code accesses the raw DOM. It's total = bignum
// very bad, there's probably a better, more angular-y way to .fromBuffer(total, {
// do things here. endian: 'little',
size: 1
})
.toString(10);
var address = angular.element( // XXX There needs to be a better way to do this:
document.querySelector('input#address')); total = +total / config.unitToSatoshi;
var amount = angular.element( // XXX Pretty much all of this code accesses the raw DOM. It's
document.querySelector('input#amount')); // very bad, there's probably a better, more angular-y way to
amount.val(total); // do things here.
if (+merchantData.total !== 0) {
amount.attr('disabled', true);
}
var sendto = angular.element(document var address = angular.element(
.querySelector('div.send-note > p[ng-class]:first-of-type')); document.querySelector('input#address'));
sendto.html(sendto.html() + '<br><b>Server:</b> ' + memo);
var tamount = angular.element(document var amount = angular.element(
.querySelector('div.send-note > p[ng-class]:nth-of-type(2)')); document.querySelector('input#amount'));
var ca = merchantData.pr.ca amount.val(total);
|| '<span style="color:red;">Untrusted</span>'; if (+merchantData.total !== 0) {
tamount.attr('class', amount.attr('disabled', true);
tamount.attr('class').replace(' hidden', '')) }
tamount.html(total + ' (CA: ' + ca
+ '. Expires: '
+ expires.toISOString()
+ ')');
var submit = angular.element( var sendto = angular.element(document
document.querySelector('button[type=submit]')); .querySelector('div.send-note > p[ng-class]:first-of-type'));
submit.attr('disabled', false); sendto.html(sendto.html() + '<br><b>Server:</b> ' + memo);
var sendall = angular.element( var tamount = angular.element(document
document.querySelector('[title="Send all funds"]')); .querySelector('div.send-note > p[ng-class]:nth-of-type(2)'));
sendall.attr('class', sendall.attr('class') + ' hidden'); var ca = merchantData.pr.ca
|| '<span style="color:red;">Untrusted</span>';
tamount.attr('class',
tamount.attr('class').replace(' hidden', ''))
tamount.html(total + ' (CA: ' + ca
+ '. Expires: '
+ expires.toISOString()
+ ')');
// Reset all the changes from the payment protocol weirdness. var submit = angular.element(
// XXX Bad hook. Find a better more angular-y way of doing this. document.querySelector('button[type=submit]'));
// This will also closure scope every variable above forever. submit.attr('disabled', false);
if (!scope._resetPayPro) {
scope._resetPayPro = function() { var sendall = angular.element(
var val = address.val(); document.querySelector('[title="Send all funds"]'));
var uri = copay.HDPath.parseBitcoinURI(val || ''); sendall.attr('class', sendall.attr('class') + ' hidden');
if (!uri || !uri.merchant) {
if (amount.attr('disabled')) { // Reset all the changes from the payment protocol weirdness.
amount.val(''); // XXX Bad hook. Find a better more angular-y way of doing this.
amount.attr('disabled', false); // This will also closure scope every variable above forever.
} if (!scope._resetPayPro) {
sendto.html(sendto.html().replace(/<br><b>Server:.*$/, '')); scope._resetPayPro = function() {
if (!/hidden/.test(tamount.attr('class'))) { var val = address.val();
tamount.attr(tamount.attr('class') + ' hidden'); var uri = copay.HDPath.parseBitcoinURI(val || '');
} if (!uri || !uri.merchant) {
if (~tamount.html().indexOf('(CA: ')) { if (amount.attr('disabled')) {
tamount.html(''); amount.val('');
} amount.attr('disabled', false);
if (!submit.attr('disabled')) {
submit.attr('disabled', true);
}
if (/ hidden$/.test(sendall.attr('class'))) {
sendall.attr('class',
sendall.attr('class').replace(' hidden', ''));
}
} }
// TODO: Check paymentRequest expiration, sendto.html(sendto.html().replace(/<br><b>Server:.*$/, ''));
// delete if beyond expiration date. if (!/hidden/.test(tamount.attr('class'))) {
}; tamount.attr(tamount.attr('class') + ' hidden');
scope.$watch('address',scope._resetPayPro); }
} if (~tamount.html().indexOf('(CA: ')) {
tamount.html('');
ctrl.$setValidity('validAddress', true); }
if (!submit.attr('disabled')) {
// XXX With PayPro, since amount is already filled in among submit.attr('disabled', true);
// other field oddities, the form is always invalid. Make it }
// valid. if (/ hidden$/.test(sendall.attr('class'))) {
scope.sendForm.$valid = true; sendall.attr('class',
scope.sendForm.$invalid = false; sendall.attr('class').replace(' hidden', ''));
scope.sendForm.$pristine = true; }
}
scope.sendForm.address.$valid = true; // TODO: Check paymentRequest expiration,
scope.sendForm.address.$invalid = false; // delete if beyond expiration date.
scope.sendForm.address.$pristine = true; };
scope.$watch('address',scope._resetPayPro);
scope.sendForm.amount.$valid = true; }
scope.sendForm.amount.$invalid = false;
scope.sendForm.amount.$pristine = true;
});
ctrl.$setValidity('validAddress', true); ctrl.$setValidity('validAddress', true);
return 'Merchant: '+ uri.merchant; // XXX With PayPro, since amount is already filled in among
} // other field oddities, the form is always invalid. Make it
// valid.
scope.sendForm.$valid = true;
scope.sendForm.$invalid = false;
scope.sendForm.$pristine = true;
var a = new Address(value); scope.sendForm.address.$valid = true;
ctrl.$setValidity('validAddress', a.isValid() && a.network().name === config.networkName); scope.sendForm.address.$invalid = false;
return value; scope.sendForm.address.$pristine = true;
scope.sendForm.amount.$valid = true;
scope.sendForm.amount.$invalid = false;
scope.sendForm.amount.$pristine = true;
});
ctrl.$setValidity('validAddress', true);
return 'Merchant: '+ uri.merchant;
}; };
ctrl.$parsers.unshift(validator); ctrl.$parsers.unshift(validator);