copay/js/directives.js

344 lines
11 KiB
JavaScript
Raw Normal View History

2014-03-14 13:38:27 -07:00
'use strict';
angular.module('copayApp.directives')
2014-05-07 15:04:36 -07:00
.directive('validAddress', [
function() {
2014-05-07 15:04:36 -07:00
var bitcore = require('bitcore');
var Address = bitcore.Address;
var bignum = bitcore.Bignum;
2014-05-07 15:04:36 -07:00
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
var validator = function(value) {
var uri = copay.HDPath.parseBitcoinURI(value);
2014-07-30 18:15:03 -07:00
// Is this a payment protocol URI (BIP-72)?
if (uri && uri.merchant) {
scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) {
if (err) {
2014-08-01 14:35:00 -07:00
ctrl.$setValidity('validAddress', false);
return;
}
var expires = new Date(merchantData.pr.pd.expires * 1000);
var memo = merchantData.pr.pd.memo;
var payment_url = merchantData.pr.pd.payment_url;
var total = merchantData.total;
if (typeof total === 'string') {
total = bignum(total, 10).toBuffer({
endian: 'little',
size: 1
});
}
total = bignum
.fromBuffer(total, {
endian: 'little',
size: 1
})
2014-07-30 18:15:03 -07:00
.toString(10);
2014-08-01 14:34:31 -07:00
// XXX There needs to be a better way to do this:
2014-07-31 17:01:35 -07:00
total = +total / config.unitToSatoshi;
var amount = angular.element(
document.querySelector('input#amount'));
amount.val(total);
2014-07-30 18:15:03 -07:00
amount.attr('disabled', true);
2014-07-31 16:38:08 -07:00
var sendto = angular.element(document
.querySelector('div.send-note > p[ng-class]:first-of-type'));
sendto.html(sendto.html() + '<br><b>Server:</b> ' + memo);
2014-07-31 16:38:08 -07:00
var tamount = angular.element(document
.querySelector('div.send-note > p[ng-class]:nth-of-type(2)'));
var ca = merchantData.pr.ca
|| '<span style="color:red;">Untrusted</span>';
tamount.attr('class',
tamount.attr('class').replace(' hidden', ''))
2014-07-31 16:38:08 -07:00
tamount.html(total + ' (CA: ' + ca
+ '. Expires: '
+ expires.toISOString()
2014-07-31 17:01:35 -07:00
+ ')');
var submit = angular.element(
document.querySelector('button[type=submit]'));
submit.attr('disabled', false);
var sendall = angular.element(
document.querySelector('[title="Send all funds"]'));
sendall.attr('class', sendall.attr('class') + ' hidden');
2014-08-01 14:35:00 -07:00
ctrl.$setValidity('validAddress', true);
});
2014-07-31 14:41:50 -07:00
ctrl.$setValidity('validAddress', true);
2014-07-31 16:38:08 -07:00
return 'Merchant: '+ uri.merchant;
}
2014-07-30 18:15:03 -07:00
2014-05-07 15:04:36 -07:00
var a = new Address(value);
2014-07-04 07:24:11 -07:00
ctrl.$setValidity('validAddress', a.isValid() && a.network().name === config.networkName);
2014-05-07 15:04:36 -07:00
return value;
};
ctrl.$parsers.unshift(validator);
ctrl.$formatters.unshift(validator);
}
};
}
])
.directive('enoughAmount', ['$rootScope',
function($rootScope) {
var bitcore = require('bitcore');
var feeSat = Number(bitcore.TransactionBuilder.FEE_PER_1000B_SAT);
2014-05-07 15:04:36 -07:00
return {
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
var val = function(value) {
var availableBalanceNum = Number(($rootScope.availableBalance * config.unitToSatoshi).toFixed(0));
var vNum = Number((value * config.unitToSatoshi).toFixed(0)) + feeSat;
2014-05-07 15:04:36 -07:00
if (typeof vNum == "number" && vNum > 0) {
if (availableBalanceNum < vNum) {
2014-05-07 15:04:36 -07:00
ctrl.$setValidity('enoughAmount', false);
scope.notEnoughAmount = true;
2014-05-07 15:04:36 -07:00
} else {
ctrl.$setValidity('enoughAmount', true);
scope.notEnoughAmount = null;
}
} else {
2014-04-23 10:41:27 -07:00
scope.notEnoughAmount = null;
}
2014-05-07 15:04:36 -07:00
return value;
}
2014-05-07 15:04:36 -07:00
ctrl.$parsers.unshift(val);
ctrl.$formatters.unshift(val);
}
2014-05-07 15:04:36 -07:00
};
}
])
2014-05-14 14:24:24 -07:00
.directive('walletSecret', ['walletFactory',
function(walletFactory) {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
var validator = function(value) {
ctrl.$setValidity('walletSecret', Boolean(walletFactory.decodeSecret(value)));
2014-05-14 14:24:24 -07:00
return value;
};
ctrl.$parsers.unshift(validator);
}
};
}
])
2014-05-07 15:04:36 -07:00
.directive('loading', function() {
2014-04-24 18:43:19 -07:00
return {
restrict: 'A',
link: function($scope, element, attr) {
2014-04-24 18:43:19 -07:00
var a = element.html();
var text = attr.loading;
element.on('click', function() {
element.html('<i class="size-21 fi-bitcoin-circle icon-rotate spinner"></i> ' + text + '...');
});
$scope.$watch('loading', function(val) {
if (!val) {
2014-04-24 18:43:19 -07:00
element.html(a);
}
});
}
}
})
2014-05-07 15:04:36 -07:00
.directive('ngFileSelect', function() {
return {
link: function($scope, el) {
el.bind('change', function(e) {
$scope.file = (e.srcElement || e.target).files[0];
$scope.getFile();
});
}
}
2014-05-26 11:03:39 -07:00
})
.directive('avatar', function($rootScope, controllerUtils) {
2014-05-07 15:04:36 -07:00
return {
link: function(scope, element, attrs) {
2014-05-08 11:48:00 -07:00
var peer = JSON.parse(attrs.peer)
var peerId = peer.peerId;
var nick = peer.nick;
element.addClass('video-small');
var muted = controllerUtils.getVideoMutedStatus(peerId);
2014-06-06 11:16:11 -07:00
if (true || muted) { // mute everyone for now
2014-05-07 15:04:36 -07:00
element.attr("muted", true);
}
}
}
2014-05-26 11:03:39 -07:00
})
.directive('contact', function() {
return {
restrict: 'E',
link: function(scope, element, attrs) {
if (!scope.wallet) return;
var address = attrs.address;
var contact = scope.wallet.addressBook[address];
if (contact && !contact.hidden) {
element.append(contact.label);
attrs['tooltip'] = attrs.address;
} else {
element.append(address);
}
}
};
})
.directive('highlightOnChange', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.$watch(attrs.highlightOnChange, function(newValue, oldValue) {
element.addClass('highlight');
setTimeout(function() {
element.removeClass('highlight');
}, 500);
});
}
}
})
2014-05-26 11:03:39 -07:00
.directive('checkStrength', function() {
return {
replace: false,
restrict: 'EACM',
2014-06-02 10:39:12 -07:00
require: 'ngModel',
2014-05-26 11:03:39 -07:00
link: function(scope, element, attrs) {
var MIN_LENGTH = 8;
var MESSAGES = ['Very Weak', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong'];
var COLOR = ['#dd514c', '#dd514c', '#faa732', '#faa732', '#5eb95e', '#5eb95e'];
function evaluateMeter(password) {
var passwordStrength = 0;
var text;
if (password.length > 0) passwordStrength = 1;
if (password.length >= MIN_LENGTH) {
if ((password.match(/[a-z]/)) && (password.match(/[A-Z]/))) {
passwordStrength++;
} else {
text = ', add mixed case';
}
if (password.match(/\d+/)) {
passwordStrength++;
} else {
if (!text) text = ', add numerals';
}
if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) {
passwordStrength++;
} else {
if (!text) text = ', add punctuation';
}
if (password.length > 12) {
passwordStrength++;
} else {
if (!text) text = ', add characters';
}
} else {
text = ', that\'s short';
}
if (!text) text = '';
return {
strength: passwordStrength,
message: MESSAGES[passwordStrength] + text,
color: COLOR[passwordStrength]
2014-05-26 11:03:39 -07:00
}
}
2014-06-02 10:39:12 -07:00
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
2014-05-26 11:03:39 -07:00
if (newValue && newValue !== '') {
var info = evaluateMeter(newValue);
element.css({
'border-color': info.color
});
scope[attrs.checkStrength] = info.message;
2014-05-26 11:03:39 -07:00
}
});
}
};
2014-06-16 11:51:19 -07:00
})
.directive('openExternal', function() {
2014-06-16 11:51:19 -07:00
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
window.open('bitcoin:'+attrs.address, '_blank');
2014-06-16 11:51:19 -07:00
});
}
}
})
// From https://gist.github.com/asafge/7430497
.directive('ngReallyClick', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
var message = attrs.ngReallyMessage;
if (message && confirm(message)) {
scope.$apply(attrs.ngReallyClick);
}
});
}
}
}
])
.directive('match', function () {
return {
require: 'ngModel',
restrict: 'A',
scope: {
match: '='
},
link: function(scope, elem, attrs, ctrl) {
scope.$watch(function() {
return (ctrl.$pristine && angular.isUndefined(ctrl.$modelValue)) || scope.match === ctrl.$modelValue;
}, function(currentValue) {
ctrl.$setValidity('match', currentValue);
});
}
};
})
.directive('clipCopy', function() {
ZeroClipboard.config({
moviePath: '/lib/zeroclipboard/dist/ZeroClipboard.swf',
trustedDomains: ['*'],
allowScriptAccess: 'always',
forceHandCursor: true
});
return {
restric: 'A',
scope: { clipCopy: '=clipCopy' },
link: function(scope, elm) {
var client = new ZeroClipboard(elm);
client.on( 'ready', function(event) {
client.on( 'copy', function(event) {
event.clipboardData.setData('text/plain', scope.clipCopy);
});
client.on( 'aftercopy', function(event) {
elm.removeClass('btn-copy').addClass('btn-copied').html('Copied!');
setTimeout(function() {
elm.addClass('btn-copy').removeClass('btn-copied').html('');
}, 1000);
});
});
client.on( 'error', function(event) {
console.log( 'ZeroClipboard error of type "' + event.name + '": ' + event.message );
ZeroClipboard.destroy();
});
}
};
});