aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifact-form.component.ts
blob: 905d1a25ad216322f799394a28023ac44dd383d4 (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
/**
 * Created by rc2122 on 5/31/2018.
 */
import { Component, Input } from '@angular/core';
import * as _ from 'lodash';
import { IDropDownOption } from 'onap-ui-angular/dist/form-elements/dropdown/dropdown-models';
import { Subject } from 'rxjs/Subject';
import { ArtifactModel } from '../../../../models';
import { ArtifactType, ComponentType } from '../../../../utils';
import { Dictionary } from '../../../../utils/dictionary/dictionary';
import { CacheService } from '../../../services/cache.service';

@Component({
    selector: 'artifact-form',
    templateUrl: './artifact-form.component.html',
    styleUrls: ['./artifact-form.component.less']
})
export class ArtifactFormComponent {

    @Input() artifact: ArtifactModel;
    @Input() artifactType: ArtifactType;
    @Input() componentType: string;
    @Input() instanceId: string;
    @Input() isViewOnly: boolean;

    public artifactTypesOptions: IDropDownOption[] = [];
    public validationPatterns: Dictionary<string, RegExp>;
    public selectedFileType: IDropDownOption;
    public showTypeFields: boolean;
    private onValidationChange: Subject<boolean> = new Subject();
    private descriptionIsValid: boolean;
    private labelIsValid: boolean;

    constructor(private cacheService: CacheService) {
    }

    ngOnInit(): void {
        this.validationPatterns = this.cacheService.get('validation').validationPatterns;
        this.initArtifactTypes();
        this.artifact.artifactGroupType = this.artifact.artifactGroupType || this.artifactType.toString();
        this.showTypeFields = (this.artifact.artifactGroupType === 'DEPLOYMENT' || !this.artifact.mandatory) && this.artifact.artifactGroupType !== 'SERVICE_API';
    }

    public onTypeChange = (selectedFileType: IDropDownOption) => {
        this.artifact.artifactType = selectedFileType.value;
        this.verifyTypeAndFileWereFilled();
    }

    public onUploadFile = (file) => {
        if (file) {
            this.artifact.artifactName = file.filename;
            this.artifact.payloadData = file.base64;
            console.log('FILE UPLOADED', file);
        } else {
            this.artifact.artifactName = null;
        }
        this.verifyTypeAndFileWereFilled();
    }

    private initArtifactTypes = (): void => {
        const artifactTypes: any = this.cacheService.get('UIConfiguration');
        let validExtensions: string[];
        let artifactTypesList: string[];

        switch (this.artifactType) {
            case ArtifactType.DEPLOYMENT:
                if (this.artifact.artifactType === ArtifactType.HEAT_ENV || this.instanceId) {
                    validExtensions = artifactTypes.artifacts.deployment.resourceInstanceDeploymentArtifacts;
                } else if (this.componentType === ComponentType.RESOURCE) {
                    validExtensions = artifactTypes.artifacts.deployment.resourceDeploymentArtifacts;
                } else {
                    validExtensions = artifactTypes.artifacts.deployment.serviceDeploymentArtifacts;
                }
                if (validExtensions) {
                    artifactTypesList = Object.keys(validExtensions);
                }
                break;
            case ArtifactType.INFORMATION:
                artifactTypesList = artifactTypes.artifacts.other.map((element: any) => {
                    return element.name;
                });
                _.remove(artifactTypesList, (item: string) => {
                    return _.has(ArtifactType.THIRD_PARTY_RESERVED_TYPES, item) ||
                        _.has(ArtifactType.TOSCA, item);
                });
                break;
        }

        _.forEach(artifactTypesList, (artifactType: string) => {
            this.artifactTypesOptions.push({ label: artifactType, value: artifactType });
        });

        this.selectedFileType = _.find(this.artifactTypesOptions, (artifactType) => {
            return artifactType.value === this.artifact.artifactType;
        });

    }

    // Verify that the Type and the Name (file) are filled in the Modal
    // For Description and Label - I used this.descriptionIsValid:boolean & this.labelIsValid:boolean as part of the sdc-validation Element
    private verifyTypeAndFileWereFilled = () => {
        if (this.artifact.artifactType === 'DEPLOYMENT' || !this.artifact.mandatory && this.artifact.artifactGroupType !== 'SERVICE_API') {
            // In case of all fields are required:
            // File, Description, Type and Label
            if (this.artifact.artifactType && this.artifact.artifactName && this.descriptionIsValid && this.labelIsValid) {
                this.onValidationChange.next(true);
            } else {
                this.onValidationChange.next(false);
            }
        } else {
            // In case of like Information Artifact
            // Only file and description are required
            if (this.descriptionIsValid && this.artifact.artifactName) {
                this.onValidationChange.next(true);
            } else {
                this.onValidationChange.next(false);
            }
        }
    }

    // sdc-validation for Description
    private onDescriptionChange = (isValid: boolean): void => {
        this.descriptionIsValid = isValid;
        this.onValidationChange.next(isValid) && this.verifyTypeAndFileWereFilled();
    }

    // sdc-validation for Label
    private onLabelChange = (isValid: boolean): void => {
        this.labelIsValid = isValid;
        this.onValidationChange.next(isValid) && this.verifyTypeAndFileWereFilled();
    }
}