re base code
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / panel / panel-header / panel-header.component.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import { Component,  Input,  AfterViewInit, SimpleChanges, OnInit, OnChanges } from "@angular/core";
22 import {  SdcUiComponents } from "sdc-ui/lib/angular";
23 import { IModalConfig } from 'sdc-ui/lib/angular/modals/models/modal-config';
24 import { ZoneInstanceType } from 'app/models/graph/zones/zone-instance';
25 import { ValueEditComponent } from './../../../../components/ui/forms/value-edit/value-edit.component';
26 import { Component as TopologyTemplate, ComponentInstance, IAppMenu } from "app/models";
27 import { PoliciesService } from '../../../../services/policies.service';
28 import { GroupsService } from '../../../../services/groups.service';
29 import {IZoneService} from "../../../../../models/graph/zones/zone";
30 import { EventListenerService, LoaderService } from "../../../../../services";
31 import { GRAPH_EVENTS, EVENTS } from "../../../../../utils";
32 import { UIZoneInstanceObject } from "../../../../../models/ui-models/ui-zone-instance-object";
33 import { ModalButtonComponent } from "sdc-ui/lib/angular/components";
34
35 @Component({
36     selector: 'ng2-composition-panel-header',
37     templateUrl: './panel-header.component.html',
38     styleUrls: ['./panel-header.component.less']
39 })
40 export class CompositionPanelHeaderComponent implements OnInit, OnChanges {
41
42     @Input() topologyTemplate: TopologyTemplate;
43     @Input() selectedZoneInstanceType: ZoneInstanceType;
44     @Input() selectedZoneInstanceId: string;
45     @Input() name: string;
46     @Input() nonCertified: boolean;
47     @Input() isViewOnly: boolean;
48     @Input() isLoading: boolean;
49
50     constructor(private groupsService:GroupsService, private policiesService: PoliciesService, 
51                 private modalService:SdcUiComponents.ModalService, private eventListenerService:EventListenerService) { }
52
53     private service:IZoneService;
54     private iconClassName: string;
55
56     ngOnInit(): void {
57         this.init();
58     }
59
60     ngOnChanges (changes:SimpleChanges):void {
61         if(changes.selectedZoneInstanceId){
62             this.init();
63         }
64     }
65
66     ngOnDestroy() {
67         
68         
69     }
70     private init = (): void => {
71         if (this.selectedZoneInstanceType === ZoneInstanceType.POLICY) {
72             this.iconClassName = "sprite-policy-icons policy";
73             this.service = this.policiesService;
74         } else if (this.selectedZoneInstanceType === ZoneInstanceType.GROUP) {
75             this.iconClassName = "sprite-group-icons group";
76             this.service = this.groupsService;
77         } else {
78             this.iconClassName = "sprite-resource-icons defaulticon";
79         }
80     }
81
82     private renameInstance = (): void => {
83         const modalConfig = {
84             title: "Edit Name",
85             size: "sm",
86             type: "custom",
87             testId: "renameInstanceModal",
88             buttons: [
89                 {id: 'saveButton', text: 'OK', size: 'xsm', callback: this.saveInstanceName, closeModal: false},
90                 {id: 'cancelButton', text: 'Cancel', size: 'sm', closeModal: true} 
91             ] as ModalButtonComponent[]
92         } as IModalConfig;
93         this.modalService.openCustomModal(modalConfig, ValueEditComponent, {name: this.name, validityChangedCallback: this.enableOrDisableSaveButton});
94     };
95
96     private enableOrDisableSaveButton = (shouldEnable: boolean): void => {
97         let saveButton: ModalButtonComponent = this.modalService.getCurrentInstance().getButtonById('saveButton');
98         saveButton.disabled = !shouldEnable;
99     }
100
101     private saveInstanceName = ():void => {
102         let currentModal = this.modalService.getCurrentInstance();
103         let nameFromModal:string = currentModal.innerModalContent.instance.name;
104
105         if(nameFromModal != this.name){
106             currentModal.buttons[0].disabled = true;
107             this.service.updateName(this.topologyTemplate.componentType, this.topologyTemplate.uniqueId, this.selectedZoneInstanceId, nameFromModal).subscribe((success)=>{
108                 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_ZONE_INSTANCE_NAME_CHANGED, nameFromModal);
109                 this.modalService.closeModal();
110             }, (error)=> {
111                 currentModal.buttons[0].disabled = false;
112             });
113         } else {
114             this.modalService.closeModal();
115         }
116     };
117     
118     private deleteInstance = (): void => {
119         let title:string = "Delete Confirmation";
120         let message:string = "Are you sure you would like to delete "+ this.name + "?";
121         this.modalService.openAlertModal(title, message, "OK", this.deleteInstanceConfirmed, "deleteInstanceModal");
122     };
123
124     private deleteInstanceConfirmed = () => {
125         this.eventListenerService.notifyObservers(EVENTS.SHOW_LOADER_EVENT + 'composition-graph');
126         this.service.deleteZoneInstance(this.topologyTemplate.componentType, this.topologyTemplate.uniqueId, this.selectedZoneInstanceId).finally(()=> {
127             this.eventListenerService.notifyObservers(EVENTS.HIDE_LOADER_EVENT + 'composition-graph');
128         }).subscribe(()=> {
129             let deletedItem:UIZoneInstanceObject = new UIZoneInstanceObject(this.selectedZoneInstanceId, this.selectedZoneInstanceType, this.name);
130             this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_DELETE_ZONE_INSTANCE, deletedItem);
131         });
132     };
133
134 }
135