aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/models/formControlModels/formControl.model.ts
blob: b3a53d9f29e38a82e8d88840e58b7c59634d5ce9 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {FormGroup, ValidatorFn, Validators} from "@angular/forms";
import {Subject} from "rxjs/Subject";
import {FormControlType} from "./formControlTypes.enum";
import {CustomValidators} from "../../validators/uniqueName/uniqueName.validator";
import * as _ from 'lodash';

export class  FormControlModel {
  formGroup : FormGroup;
  controlName : string;
  type : FormControlType;
  displayName : string;
  dataTestId : string;
  placeHolder? : string;
  tooltip? :string;
  isDisabled? : boolean;
  validations? : ValidatorModel[];
  isVisible? : boolean;
  value? : any;
  originalValue?: any;
  minLength?: number;
  maxLength?: number;
  onChange? : Function;
  onBlur? : Function;
  preventionsAttribute : AttributeMap[] = [];
  waitFor? : Subject<string>[] = [];
  hasErrors : () =>  string[];


  constructor(data: any){
    this.type = data.type;
    this.displayName = data.displayName;
    this.dataTestId = data.dataTestId;
    this.placeHolder = data.placeHolder;
    this.tooltip = data.tooltip;
    this.isDisabled = data.isDisabled || false;
    this.validations = data.validations || [];
    this.isVisible = !_.isNil(data.isVisible)  ? data.isVisible : true;
    this.value = data.value;
    this.originalValue = data.value;
    this.controlName = data.controlName;
    this.minLength = data.minLength;
    this.maxLength = data.maxLength;
    this.preventionsAttribute = data.preventionsAttribute || [];
    this.onBlur = function(){};
    this.onChange = data.onChange ? data.onChange: function () {}

  }

  isRequired() : boolean {
    for(let validtorsModel of this.validations){
      let required = 'required';
      if(validtorsModel.validatorName.toString() === required){
        return true;
      }
    }
    return false;
  }

  getPreventionAttribute() : AttributeMap[] {
    let result : AttributeMap[] = [new AttributeMap('data-tests-id', this.dataTestId)];
    return this.preventionsAttribute.concat(result);
  }
}



export class ValidatorModel {
  validator : ValidatorFn;
  validatorArg? : any;
  validatorName : ValidatorOptions | CustomValidatorOptions;
  errorMsg : string;

  constructor(validatorName : ValidatorOptions | CustomValidatorOptions, errorMsg : string, validatorArg: any = null){
    this.validatorName = validatorName;
    this.validator = this.setValidator(validatorName, validatorArg);
    this.errorMsg = errorMsg;
    this.validatorArg = validatorArg;
  }


  setValidator(validatorName : ValidatorOptions | CustomValidatorOptions, validatorArg: any = null) : ValidatorFn {
    if(validatorName in ValidatorOptions){
      return validatorArg ? Validators[validatorName](validatorArg) : Validators[validatorName];
    }else {
      return validatorArg ? CustomValidators[CustomValidatorOptions[validatorName]].apply(this, validatorArg) : CustomValidators[CustomValidatorOptions[validatorName]];
    }
  }
}



export enum ValidatorOptions {
  required = 'required',
  minLength = 'minLength',
  maxLength = 'maxLength',
  pattern = 'pattern',
  nullValidator = 'nullValidator'
}



export enum CustomValidatorOptions {
  uniqueInstanceNameValidator = 'uniqueInstanceNameValidator',
  emptyDropdownOptions = 'emptyDropdownOptions',
  isValidJson = 'isValidJson',
  isFileTooBig = 'isFileTooBig',
  isStringContainTags = 'isStringContainTags'
}

export class AttributeMap {
  key : string;
  value : string;

  constructor(key : string, value? : string){
    this.key = key;
    this.value = value ? value : '';
  }
}