diff options
author | Ittay Stern <ittay.stern@att.com> | 2019-11-26 08:49:59 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-11-26 08:49:59 +0000 |
commit | c01c192ef8d52a6c228f7cb261620890f5c3319d (patch) | |
tree | e9a8667ae02b7563f5becd9a2d85c03c35ad33d8 /vid-webpack-master/src/app/shared/components/genericForm | |
parent | 88e5a40c125893a5600bdd81ef943b921e9f8f08 (diff) | |
parent | 3347f70dcd726595e8eef7f65fb80abf13f62845 (diff) |
Merge "Extract getSupplementaryFile out of VFM controller"
Diffstat (limited to 'vid-webpack-master/src/app/shared/components/genericForm')
3 files changed, 65 insertions, 49 deletions
diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts index fc2eed4ae..8705fa8ff 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts @@ -5,6 +5,7 @@ import {FeatureFlagsService} from "../../../services/featureFlag/feature-flags.s import {BasicControlGenerator} from "./basic.control.generator"; import {NgRedux} from '@angular-redux/store'; import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import {FileFormControl} from "../../../models/formControlModels/fileFormControl.model"; class MockAppStore<T> {} @@ -44,5 +45,13 @@ describe('Basic Control Generator', () => { const legacyRegionControl: FormControlModel = service.getLegacyRegion(instance); expect(legacyRegionControl.isVisible).toBeFalsy(); }); + + test('given instance, get supp file from getSupplementaryFile ', () => { + const instance = {}; + const suppFileForInstance: FileFormControl = service.getSupplementaryFile(instance); + expect(suppFileForInstance.isVisible).toBeTruthy(); + expect(suppFileForInstance.hiddenFile.length).toBeGreaterThanOrEqual(1); + expect(suppFileForInstance.hiddenFile[0].validations[0].validatorName).toEqual("isFileTooBig"); + }); }); diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts index cbbff3c39..0261a72a4 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts @@ -21,7 +21,9 @@ import {FormGeneralErrorsService} from "../../formGeneralErrors/formGeneralError import {Observable, of} from "rxjs"; import {NodeModel} from "../../../models/nodeModel"; import {Constants} from "../../../utils/constants"; +import {FileUnit} from "../../formControls/component/file/fileUnit.enum"; +const SUPPLEMENTARY_FILE = 'supplementaryFile'; @Injectable() export class BasicControlGenerator { @@ -237,4 +239,56 @@ export class BasicControlGenerator { return initialInstanceName; } + 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); + } + }; + } } diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts index 7b72d3bfa..a2dd52197 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts @@ -38,7 +38,6 @@ export enum FormControlNames { TENANT_ID = 'tenantId', ROLLBACK_ON_FAILURE = 'rollbackOnFailure', SDN_C_PRE_LOAD = 'sdncPreLoad', - SUPPLEMENTARY_FILE = 'supplementaryFile' } @@ -106,7 +105,7 @@ export class VfModuleControlGenerator { result = this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, false); } if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) { - let suppFileInput:FileFormControl = <FileFormControl>(this.getSupplementaryFile(vfModuleInstance)); + let suppFileInput:FileFormControl = <FileFormControl>(this._basicControlGenerator.getSupplementaryFile(vfModuleInstance)); result.push(suppFileInput); result = result.concat(suppFileInput.hiddenFile); } @@ -143,7 +142,7 @@ export class VfModuleControlGenerator { result.push(this.getRollbackOnFailureControl(vfModuleInstance, result)); result.push(this.getSDNCControl(vfModuleInstance, result)); if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) { - let suppFileInput:FileFormControl = <FileFormControl>(this.getSupplementaryFile(vfModuleInstance)); + let suppFileInput:FileFormControl = <FileFormControl>(this._basicControlGenerator.getSupplementaryFile(vfModuleInstance)); result.push(suppFileInput); result = result.concat(suppFileInput.hiddenFile); } @@ -211,52 +210,6 @@ export class VfModuleControlGenerator { } - getSupplementaryFile(instance: any): FormControlModel { - return new FileFormControl({ - controlName: FormControlNames.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: FormControlNames.SUPPLEMENTARY_FILE + "_hidden", - isVisible: false, - validations: [new ValidatorModel(CustomValidatorOptions.isFileTooBig, "File size exceeds 5MB.", [FileUnit.MB, 5])] - }), - new InputFormControl({ - controlName: FormControlNames.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 : (form : FormGroup) => { - form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(null); - form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null); - }, - onChange : (files: FileList, form : FormGroup) => { - if (files.length > 0) { - const file = files.item(0); - let reader = new FileReader(); - reader.onload = function(event) { - form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(reader.result); - form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(file); - }; - reader.readAsText(file); - } - else { - form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(null); - form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null); - } - } - }) - }; - getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { const service = this.store.getState().service.serviceInstance[serviceId]; const globalCustomerId: string = service.globalSubscriberId; |