matrix-validation.js
2.8 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
/**
* Created by Techniv on 11/01/2017.
*/
angular.module('matrixValidation', []).directive('matrixValidator', [
'$timeout',
($timeout) => {
return {
restrict: 'A',
require: 'form',
link: (scope, element, attributes, ctrl) => {
let validator = scope.$eval(attributes.all);
let inputMatrix = [];
$timeout(()=>{
let inputs = angular.element(element[0].querySelectorAll('[data-row]'));
for(let i = 0; i < inputs.length; i++){
let input = inputs[i];
let name = input.name;
let $input = angular.element(input);
let inputCtrl = ctrl[name];
let inputScope = $input.scope();
let x = parseInt(inputScope.$eval(input.dataset.row));
let y = parseInt(inputScope.$eval(input.dataset.col));
if(isNaN(x) || isNaN(y) | ! inputCtrl) continue;
if( ! inputMatrix[x] ) inputMatrix[x] = [];
inputMatrix[x][y] = inputCtrl;
inputCtrl.$validators.matrixValidator = validationHandler.bind(inputCtrl);
}
validationHandler.call(false);
});
function validationHandler(){
let returnValidation, input, scope, xInput, yInput;
if(this && this.valueOf()){
returnValidation = false;
input = this.$$element[0];
scope = this.$$scope;
xInput = parseInt(scope.$eval(input.dataset.row));
yInput = parseInt(scope.$eval(input.dataset.col));
}
let matrix = inputMatrix.map((row) => {
return row.map((value) => value.$$rawModelValue);
});
let validationMatrix = matrix.map( row => row.map(() => null));
validationMatrix = validator(matrix, validationMatrix);
for(let x = 0; x < inputMatrix.length; x++){
let row = inputMatrix[x];
for(let y = 0; y < row.length; y++){
if(x == xInput && y == yInput){
returnValidation = validationMatrix[x][y];
continue;
}
let inputCtrl = row[y];
inputCtrl.$setValidity('matrixValidator',validationMatrix[x][y]);
}
}
return returnValidation;
}
}
}
}
]);