summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifact-form.component.ts
blob: f7dbf09e3f7eb599f994426641b6c1fbe03b9c3e (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
/**
 * 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';
import { ArtifactConfigService } from '../../../services/artifact-config.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() resourceType: 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,
                private artifactConfigService: ArtifactConfigService) {
    }

    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 => {
        let artifactTypesList: string[];
        switch (this.artifactType) {
            case ArtifactType.DEPLOYMENT:
                if (this.artifact.artifactType === ArtifactType.HEAT_ENV || this.instanceId) {
                    artifactTypesList = this.artifactConfigService.findAllTypeBy(this.artifactType, ComponentType.RESOURCE_INSTANCE, this.resourceType);
                } else {
                    artifactTypesList = this.artifactConfigService.findAllTypeBy(this.artifactType, this.componentType, this.resourceType);
                }
                break;
            case ArtifactType.INFORMATION:
                if (this.instanceId) {
                    artifactTypesList = this.artifactConfigService.findAllTypeBy(this.artifactType, ComponentType.RESOURCE_INSTANCE, this.resourceType);
                } else {
                    artifactTypesList = this.artifactConfigService.findAllTypeBy(this.artifactType, this.componentType, this.resourceType);
                }
                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();
    };
}