copay/js/directives.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-03-14 13:38:27 -07:00
'use strict';
2014-04-24 08:01:06 -07:00
angular.module('copay.directives')
.directive('validAddress', [function() {
var bitcore = require('bitcore');
var Address = bitcore.Address;
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', function($rootScope){
return {
restrict: 'A',
link: function(scope, element, attrs, ctrl) {
setTimeout(function(){
scope.$apply(function() {
$rootScope.flashMessage = {};
});
}, 5000);
}
};
}])
.directive('enoughAmount', ['$rootScope', function($rootScope){
return {
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
var val = function(value) {
var vStr = new String(value);
var vNum = Number(vStr);
if (typeof vNum == "number" && vNum > 0) {
if ($rootScope.availableBalance <= vNum) {
ctrl.$setValidity('enoughAmount', false);
2014-04-23 10:41:27 -07:00
scope.notEnoughAmount = 'Insufficient funds!';
}
else {
ctrl.$setValidity('enoughAmount', true);
2014-04-23 10:41:27 -07:00
scope.notEnoughAmount = null;
}
} else {
2014-04-23 10:41:27 -07:00
scope.notEnoughAmount = null;
}
return value;
}
ctrl.$parsers.unshift(val);
ctrl.$formatters.unshift(val);
}
};
}])
2014-04-24 18:43:19 -07:00
.directive('loading', function () {
return {
restrict: 'A',
link: function (scope, element, attr) {
var a = element.html();
var text = attr.loading;
scope.$watch('loading', function (val) {
if (val) {
element.html('<img src="img/loading.gif"> ' + text + '...' );
}
else {
element.html(a);
}
});
}
}
})
;