copay/js/directives.js

206 lines
6.1 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', [
2014-05-07 15:04:36 -07:00
function() {
2014-05-07 15:04:36 -07:00
var bitcore = require('bitcore');
var Address = bitcore.Address;
2014-05-07 15:04:36 -07:00
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
var validator = function(value) {
var a = new Address(value);
ctrl.$setValidity('validAddress', a.isValid());
return value;
};
ctrl.$parsers.unshift(validator);
ctrl.$formatters.unshift(validator);
}
};
}
])
.directive('notification', ['$rootScope',
2014-05-07 15:04:36 -07:00
function($rootScope) {
return {
restrict: 'A',
link: function(scope, element, attrs, ctrl) {
setTimeout(function() {
scope.$apply(function() {
2014-05-20 08:30:20 -07:00
$rootScope.$flashMessage = {};
2014-05-07 15:04:36 -07:00
});
}, 5000);
}
};
}
])
.directive('enoughAmount', ['$rootScope',
function($rootScope) {
var bitcore = require('bitcore');
var feeSat = 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 = ($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('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 strength = {
messages: ['very weak', 'weak', 'weak', 'medium', 'strong'],
2014-05-26 11:03:39 -07:00
colors: ['#c0392b', '#e74c3c', '#d35400', '#f39c12', '#27ae60'],
mesureStrength: function(p) {
var force = 0;
var regex = /[$-/:-?{-~!"^_`\[\]]/g;
var lowerLetters = /[a-z]+/.test(p);
var upperLetters = /[A-Z]+/.test(p);
var numbers = /[0-9]+/.test(p);
var symbols = regex.test(p);
var flags = [lowerLetters, upperLetters, numbers, symbols];
var passedMatches = flags.filter(function(el) {
return !!el;
}).length;
force = 2 * p.length + (p.length >= 10 ? 1 : 0);
force += passedMatches * 10;
2014-05-26 11:03:39 -07:00
// penality (short password)
force = (p.length <= 6) ? Math.min(force, 10) : force;
2014-05-26 11:03:39 -07:00
// penality (poor variety of characters)
force = (passedMatches == 1) ? Math.min(force, 10) : force;
force = (passedMatches == 2) ? Math.min(force, 20) : force;
force = (passedMatches == 3) ? Math.min(force, 40) : force;
return force;
2014-05-26 11:03:39 -07:00
},
getColor: function(s) {
2014-05-26 11:03:39 -07:00
var idx = 0;
if (s <= 10) {
idx = 0;
} else if (s <= 20) {
idx = 1;
} else if (s <= 30) {
idx = 2;
} else if (s <= 40) {
idx = 3;
} else {
idx = 4;
}
return {
idx: idx + 1,
col: this.colors[idx],
message: this.messages[idx]
};
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 c = strength.getColor(strength.mesureStrength(newValue));
element.css({
'border-color': c.col
});
2014-06-02 10:39:12 -07:00
scope[attrs.checkStrength] = c.message;
2014-05-26 11:03:39 -07:00
}
});
}
};
});