2 * Created by rc2122 on 5/31/2018.
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';
14 selector: 'artifact-form',
15 templateUrl: './artifact-form.component.html',
16 styleUrls: ['./artifact-form.component.less']
18 export class ArtifactFormComponent {
20 @Input() artifact: ArtifactModel;
21 @Input() artifactType: ArtifactType;
22 @Input() componentType: string;
23 @Input() instanceId: string;
24 @Input() isViewOnly: boolean;
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;
34 constructor(private cacheService: CacheService) {
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';
44 public onTypeChange = (selectedFileType: IDropDownOption) => {
45 this.artifact.artifactType = selectedFileType.value;
46 this.verifyTypeAndFileWereFilled();
49 public onUploadFile = (file) => {
51 this.artifact.artifactName = file.filename;
52 this.artifact.payloadData = file.base64;
53 console.log('FILE UPLOADED', file);
55 this.artifact.artifactName = null;
57 this.verifyTypeAndFileWereFilled();
60 private initArtifactTypes = (): void => {
61 const artifactTypes: any = this.cacheService.get('UIConfiguration');
62 let validExtensions: string[];
63 let artifactTypesList: string[];
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;
72 validExtensions = artifactTypes.artifacts.deployment.serviceDeploymentArtifacts;
74 if (validExtensions) {
75 artifactTypesList = Object.keys(validExtensions);
78 case ArtifactType.INFORMATION:
79 artifactTypesList = artifactTypes.artifacts.other.map((element: any) => {
82 _.remove(artifactTypesList, (item: string) => {
83 return _.has(ArtifactType.THIRD_PARTY_RESERVED_TYPES, item) ||
84 _.has(ArtifactType.TOSCA, item);
89 _.forEach(artifactTypesList, (artifactType: string) => {
90 this.artifactTypesOptions.push({ label: artifactType, value: artifactType });
93 this.selectedFileType = _.find(this.artifactTypesOptions, (artifactType) => {
94 return artifactType.value === this.artifact.artifactType;
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);
108 this.onValidationChange.next(false);
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);
116 this.onValidationChange.next(false);
121 // sdc-validation for Description
122 private onDescriptionChange = (isValid: boolean): void => {
123 this.descriptionIsValid = isValid;
124 this.onValidationChange.next(isValid) && this.verifyTypeAndFileWereFilled();
127 // sdc-validation for Label
128 private onLabelChange = (isValid: boolean): void => {
129 this.labelIsValid = isValid;
130 this.onValidationChange.next(isValid) && this.verifyTypeAndFileWereFilled();