aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/components/ui/canvas-zone/zone-instance/zone-instance.component.ts
blob: 3c2dd45db540c01422a38691372d2cda3e3e07d2 (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
import { Component, Input, Output, EventEmitter, ViewEncapsulation, OnInit, SimpleChange, ElementRef, ViewChild, SimpleChanges } from '@angular/core';
import {
    ZoneInstance, ZoneInstanceMode, ZoneInstanceType,
    IZoneInstanceAssignment
} from 'app/models/graph/zones/zone-instance';
import { PoliciesService } from '../../../../services/policies.service';
import { GroupsService } from '../../../../services/groups.service';
import { IZoneService } from "../../../../../models/graph/zones/zone";
import { EventListenerService } from 'app/services';
import { GRAPH_EVENTS } from '../../../../../utils';
import { Subject, Observable } from 'rxjs';

@Component({
    selector: 'zone-instance',
    templateUrl: './zone-instance.component.html',
    styleUrls: ['./zone-instance.component.less'],
    encapsulation: ViewEncapsulation.None
})
export class ZoneInstanceComponent implements OnInit {

    @Input() zoneInstance:ZoneInstance;
    @Input() defaultIconText:string;
    @Input() isActive:boolean;
    @Input() isViewOnly:boolean;
    @Input() activeInstanceMode: ZoneInstanceMode;
    @Input() hidden:boolean;
    @Input() forceSave:Subject<Function>;
    @Output() modeChange: EventEmitter<any> = new EventEmitter<any>();
    @Output() assignmentSaveStart: EventEmitter<void> = new EventEmitter<void>();
    @Output() assignmentSaveComplete: EventEmitter<boolean> = new EventEmitter<boolean>();
    @Output() tagHandleClick: EventEmitter<ZoneInstance> = new EventEmitter<ZoneInstance>();
    @ViewChild('currentComponent') currentComponent: ElementRef;
    private MODE = ZoneInstanceMode;
    private zoneService:IZoneService;

    constructor(private policiesService:PoliciesService, private groupsService:GroupsService, private eventListenerService:EventListenerService){}

    ngOnInit(){
        if(this.zoneInstance.type == ZoneInstanceType.POLICY){
            this.zoneService = this.policiesService;
        } else {
            this.zoneService = this.groupsService;
        }
        this.forceSave.subscribe((afterSaveFunction:Function) => {
            this.setMode(ZoneInstanceMode.TAG, null, afterSaveFunction);
        })
    }

    ngOnChanges(changes:SimpleChanges) {
        if(changes.hidden){
            this.currentComponent.nativeElement.scrollIntoView({behavior: "smooth", block: "nearest", inline:"end"});
        }
    }

    ngOnDestroy() {
        this.forceSave.unsubscribe();
    }

    private setMode = (mode:ZoneInstanceMode, event?:any, afterSaveCallback?:Function):void => {
        
        if(event){ //prevent event from handle and then repeat event from zone instance
            event.stopPropagation();
        }

        if(!this.isActive && this.activeInstanceMode === ZoneInstanceMode.TAG) {
            return; //someone else is tagging. No events allowed
        }

        if(this.isActive && this.zoneInstance.mode === ZoneInstanceMode.TAG){
            if(mode !== ZoneInstanceMode.TAG) {
                return; //ignore all other events. The only valid option is saving changes.
            }

            let oldAssignments:Array<IZoneInstanceAssignment> = this.zoneInstance.instanceData.getSavedAssignments();
            if(this.zoneInstance.isZoneAssignmentChanged(oldAssignments, this.zoneInstance.assignments)) {

                this.assignmentSaveStart.emit();
                
                this.zoneService.updateZoneInstanceAssignments(this.zoneInstance.parentComponentType, this.zoneInstance.parentComponentID, this.zoneInstance.instanceData.uniqueId, this.zoneInstance.assignments).subscribe(
                    (success) => {
                        this.zoneInstance.instanceData.setSavedAssignments(this.zoneInstance.assignments);
                        if(this.zoneInstance.type === ZoneInstanceType.POLICY){
                            this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, this.zoneInstance.instanceData);
                        } else {
                            this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_GROUP_INSTANCE_UPDATE, this.zoneInstance.instanceData);
                        }
                        this.assignmentSaveComplete.emit(true);
                        if(afterSaveCallback) afterSaveCallback();
                    }, (error) => {
                        this.zoneInstance.assignments = oldAssignments;
                        this.assignmentSaveComplete.emit(false);
                });
            } else {
                if(afterSaveCallback) afterSaveCallback();
            }
            this.modeChange.emit({newMode: ZoneInstanceMode.NONE, instance: this.zoneInstance});

        } else {
            this.modeChange.emit({newMode: mode, instance: this.zoneInstance});
        }         
        

    } 

    private tagHandleClicked = (event:Event) => {
        this.tagHandleClick.emit(this.zoneInstance);
        event.stopPropagation();
    };

}