Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / components / forms / artifacts-form / artifact-form.component.ts
1 /**
2  * Created by rc2122 on 5/31/2018.
3  */
4 import { Component, Input } from '@angular/core';
5 import * as _ from 'lodash';
6 import { IDropDownOption } from 'onap-ui-angular/dist/form-elements/dropdown/dropdown-models';
7 import { Subject } from 'rxjs/Subject';
8 import { ArtifactModel } from '../../../../models';
9 import { ArtifactType, ComponentType } from '../../../../utils';
10 import { Dictionary } from '../../../../utils/dictionary/dictionary';
11 import { CacheService } from '../../../services/cache.service';
12
13 @Component({
14     selector: 'artifact-form',
15     templateUrl: './artifact-form.component.html',
16     styleUrls: ['./artifact-form.component.less']
17 })
18 export class ArtifactFormComponent {
19
20     @Input() artifact: ArtifactModel;
21     @Input() artifactType: ArtifactType;
22     @Input() componentType: string;
23     @Input() instanceId: string;
24     @Input() isViewOnly: boolean;
25
26     public artifactTypesOptions: IDropDownOption[] = [];
27     public validationPatterns: Dictionary<string, RegExp>;
28     public selectedFileType: IDropDownOption;
29     public showTypeFields: boolean;
30     private onValidationChange: Subject<boolean> = new Subject();
31     private descriptionIsValid: boolean;
32     private labelIsValid: boolean;
33
34     constructor(private cacheService: CacheService) {
35     }
36
37     ngOnInit(): void {
38         this.validationPatterns = this.cacheService.get('validation').validationPatterns;
39         this.initArtifactTypes();
40         this.artifact.artifactGroupType = this.artifact.artifactGroupType || this.artifactType.toString();
41         this.showTypeFields = (this.artifact.artifactGroupType === 'DEPLOYMENT' || !this.artifact.mandatory) && this.artifact.artifactGroupType !== 'SERVICE_API';
42     }
43
44     public onTypeChange = (selectedFileType: IDropDownOption) => {
45         this.artifact.artifactType = selectedFileType.value;
46         this.verifyTypeAndFileWereFilled();
47     }
48
49     public onUploadFile = (file) => {
50         if (file) {
51             this.artifact.artifactName = file.filename;
52             this.artifact.payloadData = file.base64;
53             console.log('FILE UPLOADED', file);
54         } else {
55             this.artifact.artifactName = null;
56         }
57         this.verifyTypeAndFileWereFilled();
58     }
59
60     private initArtifactTypes = (): void => {
61         const artifactTypes: any = this.cacheService.get('UIConfiguration');
62         let validExtensions: string[];
63         let artifactTypesList: string[];
64
65         switch (this.artifactType) {
66             case ArtifactType.DEPLOYMENT:
67                 if (this.artifact.artifactType === ArtifactType.HEAT_ENV || this.instanceId) {
68                     validExtensions = artifactTypes.artifacts.deployment.resourceInstanceDeploymentArtifacts;
69                 } else if (this.componentType === ComponentType.RESOURCE) {
70                     validExtensions = artifactTypes.artifacts.deployment.resourceDeploymentArtifacts;
71                 } else {
72                     validExtensions = artifactTypes.artifacts.deployment.serviceDeploymentArtifacts;
73                 }
74                 if (validExtensions) {
75                     artifactTypesList = Object.keys(validExtensions);
76                 }
77                 break;
78             case ArtifactType.INFORMATION:
79                 artifactTypesList = artifactTypes.artifacts.other.map((element: any) => {
80                     return element.name;
81                 });
82                 _.remove(artifactTypesList, (item: string) => {
83                     return _.has(ArtifactType.THIRD_PARTY_RESERVED_TYPES, item) ||
84                         _.has(ArtifactType.TOSCA, item);
85                 });
86                 break;
87         }
88
89         _.forEach(artifactTypesList, (artifactType: string) => {
90             this.artifactTypesOptions.push({ label: artifactType, value: artifactType });
91         });
92
93         this.selectedFileType = _.find(this.artifactTypesOptions, (artifactType) => {
94             return artifactType.value === this.artifact.artifactType;
95         });
96
97     }
98
99     // Verify that the Type and the Name (file) are filled in the Modal
100     // For Description and Label - I used this.descriptionIsValid:boolean & this.labelIsValid:boolean as part of the sdc-validation Element
101     private verifyTypeAndFileWereFilled = () => {
102         if (this.artifact.artifactType === 'DEPLOYMENT' || !this.artifact.mandatory && this.artifact.artifactGroupType !== 'SERVICE_API') {
103             // In case of all fields are required:
104             // File, Description, Type and Label
105             if (this.artifact.artifactType && this.artifact.artifactName && this.descriptionIsValid && this.labelIsValid) {
106                 this.onValidationChange.next(true);
107             } else {
108                 this.onValidationChange.next(false);
109             }
110         } else {
111             // In case of like Information Artifact
112             // Only file and description are required
113             if (this.descriptionIsValid && this.artifact.artifactName) {
114                 this.onValidationChange.next(true);
115             } else {
116                 this.onValidationChange.next(false);
117             }
118         }
119     }
120
121     // sdc-validation for Description
122     private onDescriptionChange = (isValid: boolean): void => {
123         this.descriptionIsValid = isValid;
124         this.onValidationChange.next(isValid) && this.verifyTypeAndFileWereFilled();
125     }
126
127     // sdc-validation for Label
128     private onLabelChange = (isValid: boolean): void => {
129         this.labelIsValid = isValid;
130         this.onValidationChange.next(isValid) && this.verifyTypeAndFileWereFilled();
131     }
132 }