summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/workspace/deployment-artifacts/deployment-artifacts-page.component.ts
blob: 53b21b34b6f6e715db0492b9389cb89c6cbfd372 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Component, OnInit, ViewChild } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { ArtifactModel } from 'app/models';
import * as _ from 'lodash';
import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
import { Observable } from 'rxjs/index';
import { map } from 'rxjs/operators';
import { GabConfig } from '../../../../models/gab-config';
import { PathsAndNamesDefinition } from '../../../../models/paths-and-names';
import { GenericArtifactBrowserComponent } from '../../../../ng2/components/logic/generic-artifact-browser/generic-artifact-browser.component';
import { ArtifactGroupType, ArtifactType } from '../../../../utils/constants';
import { ArtifactsService } from '../../../components/forms/artifacts-form/artifacts.service';
import { PopoverContentComponent } from '../../../components/ui/popover/popover-content.component';
import { CacheService } from '../../../services/cache.service';
import { TranslateService } from '../../../shared/translator/translate.service';
import { GetArtifactsByTypeAction } from '../../../store/actions/artifacts.action';
import { ArtifactsState } from '../../../store/states/artifacts.state';
import { WorkspaceState, WorkspaceStateModel } from '../../../store/states/workspace.state';
import { WorkspaceService } from '../workspace.service';
import { ModalService } from 'app/ng2/services/modal.service';

export interface IPoint {
    x: number;
    y: number;
}

@Component({
    selector: 'deployment-artifact-page',
    templateUrl: './deployment-artifacts-page.component.html',
    styleUrls: ['./deployment-artifacts-page.component.less', '../../../../../assets/styles/table-style.less']
})
export class DeploymentArtifactsPageComponent implements OnInit {

    public componentId: string;
    public componentType: string;
    public deploymentArtifacts$: Observable<ArtifactModel[]>;
    public isComponentInstanceSelected: boolean;

    @Select(WorkspaceState) workspaceState$: Observable<WorkspaceStateModel>;
    @ViewChild('informationArtifactsTable') table: any;
    @ViewChild('popoverForm') popoverContentComponent: PopoverContentComponent;

    constructor(private workspaceService: WorkspaceService,
                private artifactsService: ArtifactsService,
                private store: Store,
                private popoverService: SdcUiServices.PopoverService,
                private cacheService: CacheService,
                private modalService: SdcUiServices.ModalService,
                private translateService: TranslateService) {
    }

    private getEnvArtifact = (heatArtifact: ArtifactModel, artifacts: ArtifactModel[]): ArtifactModel => {
        return _.find(artifacts, (item: ArtifactModel) => {
            return item.generatedFromId === heatArtifact.uniqueId;
        });
    };

    // we need to sort the artifact in a way that the env artifact is always under the artifact he is connected to- this is cause of the way the ngx databale work
    private sortArtifacts = ((artifacts) => {
        const sortedArtifacts = [];
        _.forEach(artifacts, (artifact: ArtifactModel): void => {
            const envArtifact = this.getEnvArtifact(artifact, artifacts);
            if (!artifact.generatedFromId) {
                sortedArtifacts.push(artifact);
            }
            if (envArtifact) {
                sortedArtifacts.push(envArtifact);
            }
        });
        return sortedArtifacts;
    })

    ngOnInit(): void {
        this.componentId = this.workspaceService.metadata.uniqueId;
        this.componentType = this.workspaceService.metadata.componentType;

        this.store.dispatch(new GetArtifactsByTypeAction({
            componentType: this.componentType,
            componentId: this.componentId,
            artifactType: ArtifactGroupType.DEPLOYMENT
        }));
        this.deploymentArtifacts$ = this.store.select(ArtifactsState.getArtifactsByType).pipe(map((filterFn) => filterFn(ArtifactType.DEPLOYMENT))).pipe(map(artifacts => {
            return this.sortArtifacts(artifacts);
        }));
    }

    onActivate(event) {
        if (event.type === 'click') {
            this.table.rowDetail.toggleExpandRow(event.row);
        }
    }

    public addOrUpdateArtifact = (artifact: ArtifactModel, isViewOnly: boolean) => {
        this.artifactsService.openArtifactModal(this.componentId, this.componentType, artifact, ArtifactGroupType.DEPLOYMENT, isViewOnly);
    }

    public deleteArtifact = (artifactToDelete) => {
        this.artifactsService.deleteArtifact(this.componentType, this.componentId, artifactToDelete);
    }

    private openPopOver = (title: string, content: string, positionOnPage: IPoint, location: string) => {
        this.popoverService.createPopOver(title, content, positionOnPage, location);
    }

    public updateEnvParams = (artifact: ArtifactModel, isViewOnly: boolean) => {
        this.artifactsService.openUpdateEnvParams(this.componentType, this.componentId, artifact );
    }

    private openGenericArtifactBrowserModal = (artifact: ArtifactModel): void => {
        const titleStr = 'Generic Artifact Browser';
        const modalConfig = {
            size: 'sdc-xl',
            title: titleStr,
            type: SdcUiCommon.ModalType.custom,
            buttons: [{
                id: 'closeGABButton',
                text: 'Close',
                size: 'sm',
                closeModal: true
            }] as SdcUiCommon.IModalButtonComponent[]
        };

        const uiConfiguration: any = this.cacheService.get('UIConfiguration');
        let noConfig: boolean = false;
        let pathsandnamesArr: PathsAndNamesDefinition[] = [];

        if (typeof uiConfiguration.gab === 'undefined') {
            noConfig = true;
        } else {
            const gabConfig: GabConfig = uiConfiguration.gab
                .find((config) => config.artifactType === artifact.artifactType);
            if (typeof gabConfig === 'undefined') {
                noConfig = true;
            } else {
                pathsandnamesArr = gabConfig.pathsAndNamesDefinitions;
            }
        }


        if (noConfig) {
            const msg = this.translateService.translate('DEPLOYMENT_ARTIFACT_GAB_NO_CONFIG');
            this.modalService.openAlertModal(titleStr, msg);
        }

        const modalInputs = {
            pathsandnames: pathsandnamesArr,
            artifactid: artifact.esId,
            resourceid: this.componentId
        };

        this.modalService.openCustomModal(modalConfig, GenericArtifactBrowserComponent, modalInputs);

    }

}