-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangular-qty.js
More file actions
45 lines (41 loc) · 1.41 KB
/
angular-qty.js
File metadata and controls
45 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
angular.module('qty', [])
.value('Qty', window.Qty)
.filter('qtyFormat', ['Qty', function(Qty){
return function(qty, format){
return Qty.new(qty).format(format);
};
}])
.filter('qtyConvertTo', ['Qty', function(Qty){
return function(qty, newUnit){
return Qty.new(qty).to(newUnit);
};
}])
.filter('qtyConvertToSI', ['Qty', function(Qty){
return function(qty){
return Qty.new(qty).toSI();
};
}])
.directive('qtyBind', ['$log', 'Qty', function ($log, Qty) {
return {
restrict: 'A',
link: function(scope, element, attrs){
var $elt = angular.element(element[0]);
scope.$watch(function () {
var val = scope.$eval(attrs.qtyBind) || 0;
var unit = scope.$eval(attrs.qtyUnit) || null;
var qty = Qty(val, unit);
if (attrs.qtyConvertTo){
var conversion = scope.$eval(attrs.qtyConvertTo);
try {
qty.convertTo(conversion);
}
catch(err){
$log.warn(err);
}
}
$elt.text(qty.format(scope.$eval(attrs.qtyFormat)));
});
}
};
}]);