-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathkeytable.directive.js
More file actions
103 lines (87 loc) · 2.28 KB
/
keytable.directive.js
File metadata and controls
103 lines (87 loc) · 2.28 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(function() {
'use strict';
angular
.module('app')
.directive('b3KeyTable', keytable)
.controller('KeyTableController', KeyTableController);
keytable.$inject = ['$parse'];
function keytable($parse) {
var directive = {
require : '^ngModel',
restrict : 'EA',
replace : true,
bindToController : true,
controller : 'KeyTableController',
controllerAs : 'keytable',
templateUrl : 'directives/keytable.html',
link: link
};
return directive;
function link(scope, element, attrs) {
// get the value of the `ng-model` attribute
scope.keytable.heading = attrs.heading;
scope.keytable._onChange = $parse(attrs.ngChange);
var variable = attrs.ngModel;
scope.$watch(variable, function(model) {
scope.keytable.reset(model);
});
}
}
KeyTableController.$inject = ['$scope'];
function KeyTableController($scope) {
// HEAD //
var vm = this;
vm._onChange = null;
vm.model = $scope.keytable.model || $scope.model || null;
vm.rows = [];
vm.add = add;
vm.remove = remove;
vm.change = change;
vm.reset = reset;
_activate();
// BODY //
function _activate() {
if (vm.model) {
for (var key in vm.model) {
add(key, vm.model[key], false);
}
} else {
vm.model = {};
}
}
function reset(model) {
vm.rows = [];
vm.model = model;
_activate();
}
function add(key, value, fixed) {
vm.rows.push({key:key, value:value, fixed:fixed===true});
}
function remove(i) {
delete vm.model[vm.rows[i].key];
vm.rows.splice(i, 1);
if (vm._onChange) {
vm._onChange($scope);
}
}
function change() {
for (var key in vm.model){
if (vm.model.hasOwnProperty(key)){
delete vm.model[key];
}
}
for (var i=0; i<vm.rows.length; i++) {
var r = vm.rows[i];
if (! r.key) continue;
var value = r.value;
if (!isNaN(value) && value !== '') {
value = parseFloat(value);
}
vm.model[r.key] = value;
if (vm._onChange) {
vm._onChange($scope);
}
}
}
}
})();