re base code
[sdc.git] / catalog-ui / src / app / ng2 / components / ui / canvas-zone / zone-instance / zone-instance.component.ts
1 import { Component, Input, Output, EventEmitter, ViewEncapsulation, OnInit, SimpleChange, ElementRef, ViewChild, SimpleChanges } from '@angular/core';
2 import {
3     ZoneInstance, ZoneInstanceMode, ZoneInstanceType,
4     IZoneInstanceAssignment
5 } from 'app/models/graph/zones/zone-instance';
6 import { PoliciesService } from '../../../../services/policies.service';
7 import { GroupsService } from '../../../../services/groups.service';
8 import { IZoneService } from "../../../../../models/graph/zones/zone";
9 import { EventListenerService } from 'app/services';
10 import { GRAPH_EVENTS } from '../../../../../utils';
11 import { Subject, Observable } from 'rxjs';
12
13 @Component({
14     selector: 'zone-instance',
15     templateUrl: './zone-instance.component.html',
16     styleUrls: ['./zone-instance.component.less'],
17     encapsulation: ViewEncapsulation.None
18 })
19 export class ZoneInstanceComponent implements OnInit {
20
21     @Input() zoneInstance:ZoneInstance;
22     @Input() defaultIconText:string;
23     @Input() isActive:boolean;
24     @Input() isViewOnly:boolean;
25     @Input() activeInstanceMode: ZoneInstanceMode;
26     @Input() hidden:boolean;
27     @Input() forceSave:Subject<Function>;
28     @Output() modeChange: EventEmitter<any> = new EventEmitter<any>();
29     @Output() assignmentSaveStart: EventEmitter<void> = new EventEmitter<void>();
30     @Output() assignmentSaveComplete: EventEmitter<boolean> = new EventEmitter<boolean>();
31     @Output() tagHandleClick: EventEmitter<ZoneInstance> = new EventEmitter<ZoneInstance>();
32     @ViewChild('currentComponent') currentComponent: ElementRef;
33     private MODE = ZoneInstanceMode;
34     private zoneService:IZoneService;
35
36     constructor(private policiesService:PoliciesService, private groupsService:GroupsService, private eventListenerService:EventListenerService){}
37
38     ngOnInit(){
39         if(this.zoneInstance.type == ZoneInstanceType.POLICY){
40             this.zoneService = this.policiesService;
41         } else {
42             this.zoneService = this.groupsService;
43         }
44         this.forceSave.subscribe((afterSaveFunction:Function) => {
45             this.setMode(ZoneInstanceMode.TAG, null, afterSaveFunction);
46         })
47     }
48
49     ngOnChanges(changes:SimpleChanges) {
50         if(changes.hidden){
51             this.currentComponent.nativeElement.scrollIntoView({behavior: "smooth", block: "nearest", inline:"end"});
52         }
53     }
54
55     ngOnDestroy() {
56         this.forceSave.unsubscribe();
57     }
58
59     private setMode = (mode:ZoneInstanceMode, event?:any, afterSaveCallback?:Function):void => {
60         
61         if(event){ //prevent event from handle and then repeat event from zone instance
62             event.stopPropagation();
63         }
64
65         if(!this.isActive && this.activeInstanceMode === ZoneInstanceMode.TAG) {
66             return; //someone else is tagging. No events allowed
67         }
68
69         if(this.isActive && this.zoneInstance.mode === ZoneInstanceMode.TAG){
70             if(mode !== ZoneInstanceMode.TAG) {
71                 return; //ignore all other events. The only valid option is saving changes.
72             }
73
74             let oldAssignments:Array<IZoneInstanceAssignment> = this.zoneInstance.instanceData.getSavedAssignments();
75             if(this.zoneInstance.isZoneAssignmentChanged(oldAssignments, this.zoneInstance.assignments)) {
76
77                 this.assignmentSaveStart.emit();
78                 
79                 this.zoneService.updateZoneInstanceAssignments(this.zoneInstance.parentComponentType, this.zoneInstance.parentComponentID, this.zoneInstance.instanceData.uniqueId, this.zoneInstance.assignments).subscribe(
80                     (success) => {
81                         this.zoneInstance.instanceData.setSavedAssignments(this.zoneInstance.assignments);
82                         if(this.zoneInstance.type === ZoneInstanceType.POLICY){
83                             this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, this.zoneInstance.instanceData);
84                         } else {
85                             this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_GROUP_INSTANCE_UPDATE, this.zoneInstance.instanceData);
86                         }
87                         this.assignmentSaveComplete.emit(true);
88                         if(afterSaveCallback) afterSaveCallback();
89                     }, (error) => {
90                         this.zoneInstance.assignments = oldAssignments;
91                         this.assignmentSaveComplete.emit(false);
92                 });
93             } else {
94                 if(afterSaveCallback) afterSaveCallback();
95             }
96             this.modeChange.emit({newMode: ZoneInstanceMode.NONE, instance: this.zoneInstance});
97
98         } else {
99             this.modeChange.emit({newMode: mode, instance: this.zoneInstance});
100         }         
101         
102
103     } 
104
105     private tagHandleClicked = (event:Event) => {
106         this.tagHandleClick.emit(this.zoneInstance);
107         event.stopPropagation();
108     };
109
110 }