diff options
author | 2019-12-18 21:04:38 +0200 | |
---|---|---|
committer | 2019-12-22 14:01:45 +0200 | |
commit | 4c87ecaa8cce40f078f8540a15928e7e805e4ef7 (patch) | |
tree | f31c55200451d13f108c8583b8d275ae7a7c8bab /vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts | |
parent | 54af5cdca044c7b486f02f4163d16c39e990f701 (diff) |
Refactor Generic Generators
Issue-ID: VID-731
Signed-off-by: Yoav Schneiderman <yoav.schneiderman@intl.att.com>
Change-Id: Ib851ff4056d64e12eaf415c5d689c832196ec8b0
Signed-off-by: Yoav Schneiderman <yoav.schneiderman@intl.att.com>
Diffstat (limited to 'vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts')
-rw-r--r-- | vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts new file mode 100644 index 000000000..e82ea5d85 --- /dev/null +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts @@ -0,0 +1,189 @@ +import {Injectable} from "@angular/core"; +import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model"; +import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum"; +import { + FormControlModel, + ValidatorModel, + ValidatorOptions +} from "../../../../models/formControlModels/formControl.model"; +import {NgRedux} from "@angular-redux/store"; +import {AppState} from "../../../../store/reducers"; +import {AaiService} from "../../../../services/aaiService/aai.service"; +import {ControlGeneratorUtil, SDN_C_PRE_LOAD} from "../control.generator.util.service"; +import * as _ from "lodash"; +import {FormGroup} from "@angular/forms"; +import {Constants} from "../../../../utils/constants"; +import {CheckboxFormControl} from "../../../../models/formControlModels/checkboxFormControl.model"; +import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model"; +import {NodeModel} from "../../../../models/nodeModel"; + +@Injectable() +export class SharedControllersService { + constructor(private _store : NgRedux<AppState>, + private _aaiService : AaiService, + private _basicControlGenerator : ControlGeneratorUtil){} + + + getLineOfBusinessControl = (instance?: any): DropdownFormControl => { + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'lineOfBusiness', + displayName: 'Line of business', + dataTestId: 'lineOfBusiness', + placeHolder: 'Select Line Of Business', + isDisabled: false, + name: "lineOfBusiness", + value: instance ? instance.lineOfBusiness : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInitSelectedField: ['lineOfBusinessList'], + onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters) + }) + }; + + getTenantControl = (serviceId: string, instance?: any): DropdownFormControl => { + const service = this._store.getState().service.serviceInstance[serviceId]; + const globalCustomerId: string = service.globalSubscriberId; + const serviceType: string = service.subscriptionServiceType; + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'tenantId', + displayName: 'Tenant', + dataTestId: 'tenant', + placeHolder: 'Select Tenant', + name: 'tenant', + isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId), + onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null, + value: instance ? instance.tenantId : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInit: instance ? this._basicControlGenerator.getSubscribeInitResult.bind( + this._aaiService, + this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : () => { + } + }) + }; + + getRollbackOnFailureControl = (instance?: any): DropdownFormControl => { + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'rollbackOnFailure', + displayName: 'Rollback on failure', + dataTestId: 'rollback', + placeHolder: 'Rollback on failure', + isDisabled: false, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + value: instance ? instance.rollbackOnFailure : 'true', + onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._basicControlGenerator.getRollBackOnFailureOptions) + }) + }; + + getLegacyRegion(instance: any): FormControlModel { + return new InputFormControl({ + controlName: 'legacyRegion', + displayName: 'Legacy Region', + dataTestId: 'lcpRegionText', + placeHolder: 'Type Legacy Region', + validations: [], + isVisible: this._basicControlGenerator.isLegacyRegionShouldBeVisible(instance), + isDisabled : _.isNil(instance) ? true : Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId), + value: instance ? instance.legacyRegion : null + }); + } + + getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { + const service = this._store.getState().service.serviceInstance[serviceId]; + const globalCustomerId: string = service.globalSubscriberId; + const serviceType: string = service.subscriptionServiceType; + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'lcpCloudRegionId', + displayName: 'LCP region', + dataTestId: 'lcpRegion', + placeHolder: 'Select LCP Region', + name: "lcpRegion", + isDisabled: false, + value: instance ? instance.lcpCloudRegionId : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInitSelectedField: ['lcpRegionList'], + onInit: this._basicControlGenerator.getSubscribeInitResult.bind( + this._aaiService, + this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)), + onChange: (param: string, form: FormGroup) => { + form.controls['tenantId'].enable(); + form.controls['tenantId'].reset(); + if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) { + this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => { + controls.find(item => item.controlName === 'tenantId')['options$'] = res.lcpRegionsTenantsMap[param]; + if (res.lcpRegionsTenantsMap[param]) { + controls.find(item => item.controlName === 'tenantId')['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0; + } + })); + } + + if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) { + form.controls['legacyRegion'].enable(); + controls.find(item => item.controlName === 'legacyRegion').isVisible = true; + + } else { + controls.find(item => item.controlName === 'legacyRegion').isVisible = false; + form.controls['legacyRegion'].setValue(null); + form.controls['legacyRegion'].reset(); + form.controls['legacyRegion'].disable(); + } + } + }) + }; + + 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')] + }) + }; + + 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._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getProductFamilies), + }) + }; + + getInstanceNameController(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, model: NodeModel): FormControlModel { + let validations: ValidatorModel[] = this._basicControlGenerator.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._basicControlGenerator.getDefaultInstanceName(instance, model) : null, + onKeypress : (event) => { + const pattern:RegExp = ControlGeneratorUtil.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; + } +} |