aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts
blob: 7ab64753aca816a82f5c2b4e17c126aac59d2b39 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import {Injectable} from "@angular/core";
import {DropdownFormControl} from "../../../models/formControlModels/dropdownFormControl.model";
import {FormGroup} from "@angular/forms";
import {CustomValidatorOptions, FormControlModel, ValidatorModel, ValidatorOptions} from "../../../models/formControlModels/formControl.model";
import {InputFormControl} from "../../../models/formControlModels/inputFormControl.model";
import {AppState} from "../../../store/reducers";
import {NgRedux} from "@angular-redux/store";
import {NumberFormControl} from "../../../models/formControlModels/numberFormControl.model";
import {FormControlType} from "../../../models/formControlModels/formControlTypes.enum";
import {FileFormControl} from "../../../models/formControlModels/fileFormControl.model";
import {SelectOption} from "../../../models/selectOption";
import * as _ from 'lodash';
import {DynamicInputLabelPipe} from "../../../pipes/dynamicInputLabel/dynamic-input-label.pipe";
import {AaiService} from "../../../services/aaiService/aai.service";
import {FormGeneralErrorsService} from "../../formGeneralErrors/formGeneralErrors.service";
import {Observable, of} from "rxjs";
import {NodeModel} from "../../../models/nodeModel";
import {Constants} from "../../../utils/constants";
import {FileUnit} from "../../formControls/component/file/fileUnit.enum";
import {CheckboxFormControl} from "../../../models/formControlModels/checkboxFormControl.model";

export const SUPPLEMENTARY_FILE = 'supplementaryFile';
export const SDN_C_PRE_LOAD = 'sdncPreLoad';

@Injectable()
export class BasicControlGenerator {

  public static readonly INSTANCE_NAME_REG_EX:RegExp = /^[a-zA-Z0-9._-]*$/;
  public static readonly GENERATED_NAME_REG_EX:RegExp = /[^a-zA-Z0-9._-]/g;

  constructor(private _store : NgRedux<AppState>,
              private _aaiService : AaiService){}
  getSubscribeResult(subscribeFunction : Function, control : DropdownFormControl) : Observable<any>{
    return subscribeFunction(this).subscribe((res) => {
      control.options$ = res;
      control.hasEmptyOptions = res.length === 0;
      FormGeneralErrorsService.checkForErrorTrigger.next();
      return of(res);
    });
  }

  getSubscribeInitResult(subscribeFunction : Function, control : DropdownFormControl, form : FormGroup) : Observable<any>{
    return subscribeFunction(this).subscribe((res) => {
      if(!_.isNil(control['onInitSelectedField'])){
        let result = res;
        for(let key of control['onInitSelectedField']){
          result = !_.isNil(result[key]) ? result[key] : [];
        }
        control.options$ = result;
        control.hasEmptyOptions = _.isNil(result) || result.length === 0;
      } else{
        control.options$ = !_.isNil(res) ? res : [];
        control.hasEmptyOptions = _.isNil(res) || res.length === 0;
      }

      FormGeneralErrorsService.checkForErrorTrigger.next();
      return of(res);
    });
  }

  getInstanceNameController(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, model: NodeModel): FormControlModel {
    let validations: ValidatorModel[] = this.createValidationsForInstanceName(instance, serviceId, isEcompGeneratedNaming);
    return new InputFormControl({
      controlName: 'instanceName',
      displayName: 'Instance name',
      dataTestId: 'instanceName',
      placeHolder: (!isEcompGeneratedNaming) ? 'Instance name' : 'Automatically generated when not provided',
      validations: validations,
      isVisible : true,
      value : (!isEcompGeneratedNaming || (!_.isNil(instance) && !_.isNil(instance.instanceName)))
        ? this.getDefaultInstanceName(instance, model) : null,
      onKeypress : (event) => {
        const pattern:RegExp = BasicControlGenerator.INSTANCE_NAME_REG_EX;
        if(pattern){
          if(!pattern.test(event['key'])){
            event.preventDefault();
          }
        }
        return event;
      }
    });
  }

  getInstanceName(instance : any, serviceId : string, isEcompGeneratedNaming: boolean): FormControlModel {
    let formControlModel:FormControlModel = this.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, new NodeModel());
    formControlModel.value = instance ? instance.instanceName : null;
    return formControlModel;
  }

  isLegacyRegionShouldBeVisible(instance : any) : boolean {
    if(!_.isNil(instance) && !_.isNil(instance.lcpCloudRegionId))  {
      return Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId) !== -1;
    }
    return false;
  }

  getLegacyRegion(instance: any): FormControlModel {
    return new InputFormControl({
      controlName: 'legacyRegion',
      displayName: 'Legacy Region',
      dataTestId: 'lcpRegionText',
      placeHolder: 'Type Legacy Region',
      validations: [],
      isVisible: this.isLegacyRegionShouldBeVisible(instance),
      isDisabled : _.isNil(instance) ? true : Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId),
      value: instance ? instance.legacyRegion : null
    });
  }

  private createValidationsForInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): ValidatorModel[] {
    let validations: ValidatorModel[] = [
      new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', BasicControlGenerator.INSTANCE_NAME_REG_EX),
      new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'some error', [this._store, serviceId, instance && instance.instanceName])
    ];
    if (!isEcompGeneratedNaming) {
      validations.push(new ValidatorModel(ValidatorOptions.required, 'is required'));
    }
    return validations;
  }

  getInputsOptions = (options: any[]) : Observable<SelectOption[]> =>{
    let optionList: SelectOption[] = [];
    options.forEach((option) => {
      optionList.push(new SelectOption({
        id: option.id || option.name,
        name: option.name
      }));
    });
    return of(optionList);
  };

  getProductFamilyControl = (instance : any, controls : FormControlModel[], isMandatory?: boolean) : DropdownFormControl => {
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : 'productFamilyId',
      displayName : 'Product family',
      dataTestId : 'productFamily',
      placeHolder : 'Select Product Family',
      isDisabled : false,
      name : "product-family-select",
      value : instance ? instance.productFamilyId : null,
      validations : _.isNil(isMandatory) || isMandatory === true ? [new ValidatorModel(ValidatorOptions.required, 'is required')]: [],
      onInit : this.getSubscribeResult.bind(this, this._aaiService.getProductFamilies),
    })
  };



  getDynamicInputsByType(dynamicInputs : any, serviceModelId : string, storeKey : string, type: string ) : FormControlModel[] {
    let result : FormControlModel[] = [];
    if(dynamicInputs) {
      let nodeInstance = null;
      if (_.has(this._store.getState().service.serviceInstance[serviceModelId][type], storeKey)) {
        nodeInstance = Object.assign({}, this._store.getState().service.serviceInstance[serviceModelId][type][storeKey]);
      }
      result = this.getDynamicInputs(dynamicInputs, nodeInstance);
    }
    return result;
  }


  getServiceDynamicInputs(dynamicInputs : any, serviceModelId : string) : FormControlModel[] {
    let result: FormControlModel[] = [];
    if (dynamicInputs) {
      let serviceInstance = null;
      if (_.has(this._store.getState().service.serviceInstance, serviceModelId)) {
        serviceInstance = Object.assign({}, this._store.getState().service.serviceInstance[serviceModelId]);
      }
      result = this.getDynamicInputs(dynamicInputs, serviceInstance);
    }
    return result;
  }

  getDynamicInputs(dynamicInputs : any, instance :any)  : FormControlModel[]{
    let result : FormControlModel[] = [];
    if(dynamicInputs) {
      dynamicInputs.forEach((input)=> {
        let validations: ValidatorModel[] = [];
        if(input.isRequired) {
          validations.push(new ValidatorModel(ValidatorOptions.required, 'is required'))
        }
        if(input.minLength) {
          validations.push(new ValidatorModel(ValidatorOptions.minLength, '', input.minLength))
        }
        if(input.maxLength) {
          validations.push(new ValidatorModel(ValidatorOptions.maxLength, '', input.maxLength))
        }

        let dynamicInputLabelPipe: DynamicInputLabelPipe = new DynamicInputLabelPipe();
        let data:any = {
          controlName: input.name,
          displayName: dynamicInputLabelPipe.transform(input.name).slice(0, -1),
          dataTestId: input.id,
          placeHolder: input.prompt,
          tooltip: input.description,
          validations: validations,
          isVisible: input.isVisible,
          value: !_.isNil(instance) && !_.isNil(instance.instanceParams) && instance.instanceParams.length > 0 ? instance.instanceParams[0][input.name] : input.value
        };

        switch (input.type) {
          case 'select' :
          case 'boolean' :{
            data.value = data.value || input.optionList.filter((option) => option.isDefault ? option.id || option.name: null);
            data.onInit  = this.getSubscribeInitResult.bind(null, this.getInputsOptions.bind(this, input.optionList));
            result.push(new DropdownFormControl(data));
            break;
          }
          case 'checkbox': {
            data.type = FormControlType.CHECKBOX;
            result.push(new FormControlModel(data));
            break;
          }
          case 'number': {
            data.min = input.min;
            data.max = input.max;
            result.push(new NumberFormControl(data));
            break;
          }
          case 'file': {
            result.push(new FileFormControl(data));
            break;
          }
          default: {
            result.push(new InputFormControl(data));
          }
        }
      })
    }

    return result;
  }

  getDefaultInstanceName(instance: any, model: NodeModel) : string {
    const initialInstanceName = (!_.isNil(instance) && instance.instanceName) || (!_.isNil(model.name) ? model.name.replace(BasicControlGenerator.GENERATED_NAME_REG_EX, "") : model.name);
    return initialInstanceName;
  }

  concatSupplementaryFile(originalArray: FormControlModel[], vfModuleInstance): FormControlModel[] {
    let suppFileInput: FileFormControl = <FileFormControl>(this.getSupplementaryFile(vfModuleInstance));
    return originalArray.concat([suppFileInput], suppFileInput.hiddenFile);
  }

  getSDNCControl = (instance: any): FormControlModel => {
    return new CheckboxFormControl({
      controlName: SDN_C_PRE_LOAD,
      displayName: 'SDN-C pre-load',
      dataTestId: 'sdncPreLoad',
      value: instance ? instance.sdncPreLoad : false,
      validations: [new ValidatorModel(ValidatorOptions.required, 'is required')]
    })
  };

  getSupplementaryFile(instance: any): FileFormControl {
    return new FileFormControl({
      controlName: SUPPLEMENTARY_FILE,
      displayName: 'Supplementary Data File (JSON format)',
      dataTestId: 'SupplementaryFile',
      placeHolder: 'Choose file',
      selectedFile:  !_.isNil(instance) ? instance.supplementaryFileName: null,
      isVisible: true,
      acceptedExtentions: "application/json",
      hiddenFile : [new InputFormControl({
        controlName: SUPPLEMENTARY_FILE + "_hidden",
        isVisible: false,
        validations: [new ValidatorModel(CustomValidatorOptions.isFileTooBig, "File size exceeds 5MB.", [FileUnit.MB, 5])]
      }),
        new InputFormControl({
          controlName: SUPPLEMENTARY_FILE + "_hidden_content",
          isVisible: false,
          validations: [new ValidatorModel(CustomValidatorOptions.isValidJson,
            "File is invalid, please make sure a legal JSON file is uploaded using name:value pairs.",[]),
            new ValidatorModel(CustomValidatorOptions.isStringContainTags,
              "File is invalid, please remove tags <>.",[])],
          value: !_.isNil(instance) ? (instance.supplementaryFile_hidden_content): null,
        })
      ],
      onDelete : this.getOnDeleteForSupplementaryFile(),
      onChange : this.getOnChangeForSupplementaryFile()
    })
  };

  private getOnDeleteForSupplementaryFile() {
    return (form: FormGroup) => {
      form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
      form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
    };
  }

  private getOnChangeForSupplementaryFile() {
    return (files: FileList, form: FormGroup) => {
      if (files.length > 0) {
        const file = files.item(0);
        let reader = new FileReader();
        reader.onload = function (event) {
          form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(reader.result);
          form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(file);
        };
        reader.readAsText(file);
      } else {
        form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
        form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
      }
    };
  }
}