2014-03-14 13:38:27 -07:00
|
|
|
'use strict';
|
2014-04-22 12:07:14 -07:00
|
|
|
|
2014-09-05 15:54:44 -07:00
|
|
|
var bitcore = require('bitcore');
|
|
|
|
var Address = bitcore.Address;
|
|
|
|
var bignum = bitcore.Bignum;
|
|
|
|
var preconditions = require('preconditions').singleton();
|
|
|
|
|
2014-06-03 12:39:06 -07:00
|
|
|
angular.module('copayApp.directives')
|
2014-04-22 12:07:14 -07:00
|
|
|
|
2014-09-12 06:24:27 -07:00
|
|
|
.directive('validAddress', ['$rootScope',
|
|
|
|
function($rootScope) {
|
2014-08-13 15:07:57 -07:00
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
|
|
|
var validator = function(value) {
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-09-08 16:51:37 -07:00
|
|
|
// If we're setting the domain, ignore the change.
|
2014-09-12 06:24:27 -07:00
|
|
|
if ($rootScope.merchant && $rootScope.merchant.domain && value === $rootScope.merchant.domain) {
|
2014-09-08 16:51:37 -07:00
|
|
|
ctrl.$setValidity('validAddress', true);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-08-19 11:06:44 -07:00
|
|
|
// Regular url
|
2014-08-13 15:07:57 -07:00
|
|
|
if (/^https?:\/\//.test(value)) {
|
2014-08-19 11:06:44 -07:00
|
|
|
ctrl.$setValidity('validAddress', true);
|
|
|
|
return value;
|
2014-08-13 15:07:57 -07:00
|
|
|
}
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-09-12 06:24:27 -07:00
|
|
|
|
2014-08-19 11:06:44 -07:00
|
|
|
// Bip21 uri
|
|
|
|
if (/^bitcoin:/.test(value)) {
|
|
|
|
var uri = new bitcore.BIP21(value);
|
2014-09-11 11:19:19 -07:00
|
|
|
var hasAddress = uri.address && uri.isValid() && uri.address.network().name === $rootScope.wallet.getNetworkName();
|
2014-08-19 11:06:44 -07:00
|
|
|
ctrl.$setValidity('validAddress', uri.data.merchant || hasAddress);
|
2014-08-13 15:07:57 -07:00
|
|
|
return value;
|
|
|
|
}
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-08-19 11:06:44 -07:00
|
|
|
// Regular Address
|
|
|
|
var a = new Address(value);
|
2014-09-11 11:19:19 -07:00
|
|
|
ctrl.$setValidity('validAddress', a.isValid() && a.network().name === $rootScope.wallet.getNetworkName());
|
2014-08-19 11:06:44 -07:00
|
|
|
return value;
|
2014-08-13 15:07:57 -07:00
|
|
|
};
|
|
|
|
|
2014-09-12 06:24:27 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
ctrl.$parsers.unshift(validator);
|
|
|
|
ctrl.$formatters.unshift(validator);
|
|
|
|
}
|
|
|
|
};
|
2014-09-12 06:24:27 -07:00
|
|
|
}
|
|
|
|
])
|
2014-10-15 14:16:37 -07:00
|
|
|
.directive('validUrl', [
|
|
|
|
|
|
|
|
function() {
|
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
|
|
|
var validator = function(value) {
|
|
|
|
// Regular url
|
|
|
|
if (/^https?:\/\//.test(value)) {
|
|
|
|
ctrl.$setValidity('validUrl', true);
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
ctrl.$setValidity('validUrl', false);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ctrl.$parsers.unshift(validator);
|
|
|
|
ctrl.$formatters.unshift(validator);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
])
|
|
|
|
.directive('validAmount', ['$rootScope', '$locale',
|
2014-10-07 08:39:16 -07:00
|
|
|
function($rootScope, locale) {
|
2014-09-04 07:12:08 -07:00
|
|
|
var w = $rootScope.wallet;
|
2014-09-05 15:54:44 -07:00
|
|
|
preconditions.checkState(w);
|
|
|
|
preconditions.checkState(w.settings.unitToSatoshi);
|
2014-10-07 08:39:16 -07:00
|
|
|
var formats = locale.NUMBER_FORMATS;
|
2014-09-05 15:54:44 -07:00
|
|
|
|
2014-05-07 15:04:36 -07:00
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function(scope, element, attrs, ctrl) {
|
|
|
|
var val = function(value) {
|
2014-09-04 07:12:08 -07:00
|
|
|
var vNum = Number((value * w.settings.unitToSatoshi).toFixed(0));
|
2014-08-15 19:00:12 -07:00
|
|
|
|
2014-10-07 08:39:16 -07:00
|
|
|
if (typeof value == 'undefined') {
|
|
|
|
ctrl.$pristine = true;
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:04:36 -07:00
|
|
|
if (typeof vNum == "number" && vNum > 0) {
|
2014-10-07 08:39:16 -07:00
|
|
|
var decimals = Number(w.settings.unitDecimals);
|
|
|
|
var sep_index = ('' + value).indexOf(formats.DECIMAL_SEP);
|
2014-10-15 14:16:37 -07:00
|
|
|
var str_value = ('' + value).substring(sep_index + 1);
|
2014-10-07 08:39:16 -07:00
|
|
|
if (sep_index > 0 && str_value.length > decimals) {
|
|
|
|
ctrl.$setValidity('validAmount', false);
|
|
|
|
scope.notValidAmount = true;
|
2014-05-07 15:04:36 -07:00
|
|
|
} else {
|
2014-10-07 08:39:16 -07:00
|
|
|
ctrl.$setValidity('validAmount', true);
|
|
|
|
scope.notValidAmount = null;
|
2014-05-07 15:04:36 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-10-07 08:39:16 -07:00
|
|
|
ctrl.$setValidity('validAmount', false);
|
|
|
|
scope.notValidAmount = null;
|
2014-04-23 09:16:20 -07:00
|
|
|
}
|
2014-05-07 15:04:36 -07:00
|
|
|
return value;
|
2014-04-23 09:16:20 -07:00
|
|
|
}
|
2014-05-07 15:04:36 -07:00
|
|
|
ctrl.$parsers.unshift(val);
|
|
|
|
ctrl.$formatters.unshift(val);
|
2014-04-23 09:16:20 -07:00
|
|
|
}
|
2014-05-07 15:04:36 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
])
|
2014-10-05 11:59:41 -07:00
|
|
|
.directive('walletSecret', function() {
|
2014-05-14 14:24:24 -07:00
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
|
|
|
var validator = function(value) {
|
2014-09-19 08:13:09 -07:00
|
|
|
var a = new Address(value);
|
2014-10-05 11:59:41 -07:00
|
|
|
ctrl.$setValidity('walletSecret', !a.isValid() && Boolean(copay.Wallet.decodeSecret(value)));
|
2014-05-14 14:24:24 -07:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
ctrl.$parsers.unshift(validator);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2014-10-05 11:59:41 -07:00
|
|
|
)
|
2014-05-07 15:04:36 -07:00
|
|
|
.directive('loading', function() {
|
2014-04-24 18:43:19 -07:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2014-05-16 14:33:06 -07:00
|
|
|
link: function($scope, element, attr) {
|
2014-04-24 18:43:19 -07:00
|
|
|
var a = element.html();
|
|
|
|
var text = attr.loading;
|
2014-05-16 14:33:06 -07:00
|
|
|
element.on('click', function() {
|
2014-06-16 08:44:18 -07:00
|
|
|
element.html('<i class="size-21 fi-bitcoin-circle icon-rotate spinner"></i> ' + text + '...');
|
2014-05-16 14:33:06 -07:00
|
|
|
});
|
|
|
|
$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() {
|
2014-04-25 15:11:56 -07:00
|
|
|
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
|
|
|
})
|
2014-06-23 13:34:54 -07:00
|
|
|
.directive('contact', function() {
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
2014-06-24 08:36:32 -07:00
|
|
|
link: function(scope, element, attrs) {
|
2014-06-23 13:34:54 -07:00
|
|
|
if (!scope.wallet) return;
|
|
|
|
|
|
|
|
var address = attrs.address;
|
|
|
|
var contact = scope.wallet.addressBook[address];
|
2014-07-06 22:21:29 -07:00
|
|
|
if (contact && !contact.hidden) {
|
2014-06-23 13:34:54 -07:00
|
|
|
element.append(contact.label);
|
|
|
|
attrs['tooltip'] = attrs.address;
|
|
|
|
} else {
|
|
|
|
element.append(address);
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 08:36:32 -07:00
|
|
|
};
|
2014-06-23 13:34:54 -07:00
|
|
|
})
|
2014-06-11 13:49:14 -07:00
|
|
|
.directive('highlightOnChange', function() {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
link: function(scope, element, attrs) {
|
2014-06-16 08:44:18 -07:00
|
|
|
scope.$watch(attrs.highlightOnChange, function(newValue, oldValue) {
|
2014-06-11 13:49:14 -07:00
|
|
|
element.addClass('highlight');
|
2014-06-16 08:44:18 -07:00
|
|
|
setTimeout(function() {
|
|
|
|
element.removeClass('highlight');
|
|
|
|
}, 500);
|
2014-06-11 13:49:14 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
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) {
|
2014-06-16 08:44:18 -07:00
|
|
|
|
2014-07-14 07:59:47 -07:00
|
|
|
var MIN_LENGTH = 8;
|
|
|
|
var MESSAGES = ['Very Weak', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong'];
|
|
|
|
var COLOR = ['#dd514c', '#dd514c', '#faa732', '#faa732', '#5eb95e', '#5eb95e'];
|
2014-06-16 08:44:18 -07:00
|
|
|
|
2014-07-14 07:59:47 -07:00
|
|
|
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++;
|
2014-06-16 08:44:18 -07:00
|
|
|
} else {
|
2014-07-14 07:59:47 -07:00
|
|
|
if (!text) text = ', add characters';
|
2014-06-16 08:44:18 -07:00
|
|
|
}
|
2014-07-14 07:59:47 -07:00
|
|
|
} else {
|
|
|
|
text = ', that\'s short';
|
|
|
|
}
|
|
|
|
if (!text) text = '';
|
2014-06-16 08:44:18 -07:00
|
|
|
|
2014-07-14 07:59:47 -07:00
|
|
|
return {
|
|
|
|
strength: passwordStrength,
|
|
|
|
message: MESSAGES[passwordStrength] + text,
|
|
|
|
color: COLOR[passwordStrength]
|
2014-05-26 11:03:39 -07:00
|
|
|
}
|
2014-07-14 07:59:47 -07:00
|
|
|
}
|
2014-06-02 10:39:12 -07:00
|
|
|
|
2014-06-16 08:44:18 -07:00
|
|
|
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
|
2014-05-26 11:03:39 -07:00
|
|
|
if (newValue && newValue !== '') {
|
2014-07-14 07:59:47 -07:00
|
|
|
var info = evaluateMeter(newValue);
|
2014-06-16 08:44:18 -07:00
|
|
|
element.css({
|
2014-07-14 07:59:47 -07:00
|
|
|
'border-color': info.color
|
2014-06-16 08:44:18 -07:00
|
|
|
});
|
2014-07-14 07:59:47 -07:00
|
|
|
scope[attrs.checkStrength] = info.message;
|
2014-05-26 11:03:39 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-06-16 11:51:19 -07:00
|
|
|
})
|
2014-07-31 10:06:55 -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() {
|
2014-08-13 15:07:57 -07:00
|
|
|
window.open('bitcoin:' + attrs.address, '_blank');
|
2014-06-16 11:51:19 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-07-31 10:06:55 -07:00
|
|
|
})
|
2014-08-13 15:07:57 -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);
|
|
|
|
}
|
|
|
|
});
|
2014-07-31 10:06:55 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-13 15:07:57 -07:00
|
|
|
}
|
|
|
|
])
|
|
|
|
.directive('match', function() {
|
2014-07-18 13:56:50 -07:00
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
restrict: 'A',
|
|
|
|
scope: {
|
2014-08-13 15:07:57 -07:00
|
|
|
match: '='
|
2014-07-18 13:56:50 -07:00
|
|
|
},
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
2014-07-18 14:17:45 -07:00
|
|
|
scope.$watch(function() {
|
|
|
|
return (ctrl.$pristine && angular.isUndefined(ctrl.$modelValue)) || scope.match === ctrl.$modelValue;
|
|
|
|
}, function(currentValue) {
|
|
|
|
ctrl.$setValidity('match', currentValue);
|
|
|
|
});
|
2014-07-18 13:56:50 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})
|
2014-07-31 12:34:21 -07:00
|
|
|
.directive('clipCopy', function() {
|
|
|
|
ZeroClipboard.config({
|
2014-08-29 19:41:17 -07:00
|
|
|
moviePath: './lib/zeroclipboard/ZeroClipboard.swf',
|
2014-07-31 12:34:21 -07:00
|
|
|
trustedDomains: ['*'],
|
|
|
|
allowScriptAccess: 'always',
|
|
|
|
forceHandCursor: true
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
restric: 'A',
|
2014-08-13 15:07:57 -07:00
|
|
|
scope: {
|
|
|
|
clipCopy: '=clipCopy'
|
|
|
|
},
|
2014-07-31 12:34:21 -07:00
|
|
|
link: function(scope, elm) {
|
|
|
|
var client = new ZeroClipboard(elm);
|
|
|
|
|
2014-08-28 10:59:19 -07:00
|
|
|
client.on('load', function(client) {
|
|
|
|
|
|
|
|
client.on('datarequested', function(client) {
|
|
|
|
client.setText(scope.clipCopy);
|
2014-09-05 15:54:44 -07:00
|
|
|
});
|
2014-07-31 12:34:21 -07:00
|
|
|
|
2014-08-28 10:59:19 -07:00
|
|
|
client.on('complete', function(client, args) {
|
2014-07-31 12:34:21 -07:00
|
|
|
elm.removeClass('btn-copy').addClass('btn-copied').html('Copied!');
|
|
|
|
setTimeout(function() {
|
|
|
|
elm.addClass('btn-copy').removeClass('btn-copied').html('');
|
2014-08-28 11:06:11 -07:00
|
|
|
}, 1000);
|
2014-07-31 12:34:21 -07:00
|
|
|
});
|
|
|
|
});
|
2014-08-28 10:59:19 -07:00
|
|
|
client.on('wrongflash noflash', function() {
|
2014-09-08 12:47:10 -07:00
|
|
|
elm.removeClass('btn-copy').html('');
|
2014-07-31 12:34:21 -07:00
|
|
|
ZeroClipboard.destroy();
|
|
|
|
});
|
2014-08-28 10:59:19 -07:00
|
|
|
|
2014-07-31 12:34:21 -07:00
|
|
|
}
|
|
|
|
};
|
2014-07-31 10:06:55 -07:00
|
|
|
});
|