1 import { Component, Input, Output, EventEmitter, ViewEncapsulation, OnInit, SimpleChange, ElementRef, ViewChild, SimpleChanges } from '@angular/core';
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';
14 selector: 'zone-instance',
15 templateUrl: './zone-instance.component.html',
16 styleUrls: ['./zone-instance.component.less'],
17 encapsulation: ViewEncapsulation.None
19 export class ZoneInstanceComponent implements OnInit {
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;
36 constructor(private policiesService:PoliciesService, private groupsService:GroupsService, private eventListenerService:EventListenerService){}
39 if(this.zoneInstance.type == ZoneInstanceType.POLICY){
40 this.zoneService = this.policiesService;
42 this.zoneService = this.groupsService;
44 this.forceSave.subscribe((afterSaveFunction:Function) => {
45 this.setMode(ZoneInstanceMode.TAG, null, afterSaveFunction);
49 ngOnChanges(changes:SimpleChanges) {
51 this.currentComponent.nativeElement.scrollIntoView({behavior: "smooth", block: "nearest", inline:"end"});
56 this.forceSave.unsubscribe();
59 private setMode = (mode:ZoneInstanceMode, event?:any, afterSaveCallback?:Function):void => {
61 if(event){ //prevent event from handle and then repeat event from zone instance
62 event.stopPropagation();
65 if(!this.isActive && this.activeInstanceMode === ZoneInstanceMode.TAG) {
66 return; //someone else is tagging. No events allowed
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.
74 let oldAssignments:Array<IZoneInstanceAssignment> = this.zoneInstance.instanceData.getSavedAssignments();
75 if(this.zoneInstance.isZoneAssignmentChanged(oldAssignments, this.zoneInstance.assignments)) {
77 this.assignmentSaveStart.emit();
79 this.zoneService.updateZoneInstanceAssignments(this.zoneInstance.parentComponentType, this.zoneInstance.parentComponentID, this.zoneInstance.instanceData.uniqueId, this.zoneInstance.assignments).subscribe(
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);
85 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_GROUP_INSTANCE_UPDATE, this.zoneInstance.instanceData);
87 this.assignmentSaveComplete.emit(true);
88 if(afterSaveCallback) afterSaveCallback();
90 this.zoneInstance.assignments = oldAssignments;
91 this.assignmentSaveComplete.emit(false);
94 if(afterSaveCallback) afterSaveCallback();
96 this.modeChange.emit({newMode: ZoneInstanceMode.NONE, instance: this.zoneInstance});
99 this.modeChange.emit({newMode: mode, instance: this.zoneInstance});
105 private tagHandleClicked = (event:Event) => {
106 this.tagHandleClick.emit(this.zoneInstance);
107 event.stopPropagation();