copay/js/directives.js

121 lines
3.2 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')
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',
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);
scope.notEnoughAmount = 'Insufficient funds!';
} 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)));
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',
2014-05-07 15:04:36 -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-07 15:04:36 -07:00
scope.$watch('loading', function(val) {
2014-04-24 18:43:19 -07:00
if (val) {
element.html('<i class="size-21 fi-bitcoin-circle icon-rotate spinner"></i> ' + text + '...');
2014-05-07 15:04:36 -07:00
} else {
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-08 11:48:00 -07:00
}).directive('avatar', function($rootScope) {
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 = $rootScope.getVideoMutedStatus(peerId);
2014-05-07 15:04:36 -07:00
if (muted) {
element.attr("muted", true);
}
}
}
});