aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/workspace/deployment/panel/panel-tabs/hierarchy-tab/hierarchy-tab.component.ts
blob: 604b194283160088f208f92ec3c59f20e98e2e8f (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
import {Component, Input} from "@angular/core";
import {Component as TopologyTemplate, ComponentInstance, DisplayModule, Module, PropertyModel} from "app/models";
import {TranslateService} from "app/ng2/shared/translator/translate.service";
import {ComponentType} from "app/utils/constants";
import {WorkspaceService} from "../../../../workspace.service";
import {ModulesService} from "../../../../../../services/modules.service";
import * as _ from "lodash";
import {ModalsHandler} from "../../../../../../../utils/modals-handler";
import {ComponentFactory} from "../../../../../../../utils/component-factory";
import {Select, Store} from "@ngxs/store";
import { SdcUiServices } from "onap-ui-angular";
import { EditModuleName } from "../edit-module-name/edit-module-name.component";
import {GraphState} from "../../../../../composition/common/store/graph.state";
import {DeploymentGraphService} from "../../../../../composition/deployment/deployment-graph.service";
import {OnSidebarOpenOrCloseAction} from "../../../../../composition/common/store/graph.actions";

@   Component({
    selector: 'hierarchy-tab',
    templateUrl: './hierarchy-tab.component.html',
    styleUrls: ['./hierarchy-tab.component.less'],
})
export class HierarchyTabComponent {

    @Select(GraphState.withSidebar) withSidebar$: boolean;
    @Input() isViewOnly: boolean;
    public selectedIndex: number;
    public selectedModule: DisplayModule;
    public isLoading: boolean;
    public topologyTemplateName: string;
    public topologyTemplateType: string;
    public modules: Array<Module> = [];
    public componentInstances: Array<ComponentInstance> = [];
    private editPropertyModalTopologyTemplate: TopologyTemplate;

    constructor(private translateService: TranslateService,
                private workspaceService: WorkspaceService,
                private deploymentService: DeploymentGraphService,
                private modulesService: ModulesService,
                private ModalsHandler: ModalsHandler,
                private componentFactory: ComponentFactory,
                private store: Store,
                private popoverService: SdcUiServices.PopoverService) {
        this.isLoading = false;
        this.topologyTemplateName = this.workspaceService.metadata.name;
        this.topologyTemplateType = this.workspaceService.metadata.componentType;
    }

    ngOnInit() {
        this.modules = this.deploymentService.modules;
        this.componentInstances = this.deploymentService.componentInstances;
        this.editPropertyModalTopologyTemplate = this.componentFactory.createEmptyComponent(this.topologyTemplateType);
        this.editPropertyModalTopologyTemplate.componentInstances = this.deploymentService.componentInstances;
    }

    onModuleSelected(module: Module, componentInstanceId?: string): void {

        let onSuccess = (module: DisplayModule) => {
            console.log("Module Loaded: ", module);
            this.selectedModule = module;
            this.isLoading = false;
        };

        let onFailed = () => {
            this.isLoading = false;
        };

        if (!this.selectedModule || (this.selectedModule && this.selectedModule.uniqueId != module.uniqueId)) {
            this.isLoading = true;
            if (this.topologyTemplateType == ComponentType.SERVICE) {
                // this.selectedInstanceId = componentInstanceId;
                this.modulesService.getComponentInstanceModule(this.topologyTemplateType, this.workspaceService.metadata.uniqueId, componentInstanceId, module.uniqueId).subscribe((resultModule: DisplayModule) => {
                    onSuccess(resultModule);
                }, () => {
                    onFailed();
                });
            } else {
                this.modulesService.getModuleForDisplay(this.topologyTemplateType, this.workspaceService.metadata.uniqueId, module.uniqueId).subscribe((resultModule: DisplayModule) => {
                    onSuccess(resultModule);
                }, () => {
                    onFailed();
                });
            }
        }
    }

    updateHeatName(): void {
        this.isLoading = true;
        let originalName: string = this.selectedModule.name;
       
        this.selectedModule.updateName();
        let moduleIndex: number = _.indexOf(this.modules, _.find(this.modules, (module: Module) => {
            return module.uniqueId === this.selectedModule.uniqueId;
        }));
        
        if (moduleIndex !== -1) {
            this.modules[moduleIndex].name = this.selectedModule.name;
            this.modulesService.updateModuleMetadata(this.topologyTemplateType, this.workspaceService.metadata.uniqueId, this.modules[moduleIndex]).subscribe(() => {
                this.isLoading = false;
            }, () => {
                this.updateOriginalHeatName(originalName, moduleIndex);
                this.modules[moduleIndex].name = originalName;
            });
        } else {
                this.updateOriginalHeatName(originalName, moduleIndex);
        }
    };

    private updateOriginalHeatName(originalName: string, moduleIndex: number){
        this.isLoading = false;
        this.selectedModule.name = originalName;
        this.selectedModule.heatName = this.selectedModule.name.split('..')[1];
    }

    openEditPropertyModal(property: PropertyModel): void {
        this.editPropertyModalTopologyTemplate.setComponentMetadata(this.workspaceService.metadata);
        this.ModalsHandler.openEditModulePropertyModal(property, this.editPropertyModalTopologyTemplate, this.selectedModule, this.selectedModule.properties).then(() => {
        });
    }

    private getKeys(map: Map<any, any>) {
        return _.keys(map);
    }

    private toggleSidebarDisplay = () => {
        // this.withSidebar = !this.withSidebar;
        this.store.dispatch(new OnSidebarOpenOrCloseAction());
    }

    public openEditModuleNamePopup($event) {
        const editModuleNameInstance = this.popoverService.createPopOverWithInnerComponent('Edit Module Name', '', {x:$event.x , y:$event.y }, EditModuleName, {selectModule: _.cloneDeep(this.selectedModule)}, 'top');
        editModuleNameInstance.innerPopoverContent.instance.clickButtonEvent.subscribe((newHeatName) => {
            if(newHeatName != null){
                this.selectedModule.heatName = newHeatName;
                this.updateHeatName();
            }
            editModuleNameInstance.closePopover();
        })
    }
}