aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/models/graph/zones/zone-instance.ts
blob: ee6473a20c37070ca22abeb8778e7e1261bb6261 (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
import {PolicyInstance} from "app/models/graph/zones/policy-instance";
import {GroupInstance} from "./group-instance";
import {Component as TopologyTemplate} from "app/models";
import {IUiBaseObject} from "../../ui-models/ui-base-object";
import { Subject } from "rxjs";

export enum ZoneInstanceMode {
    NONE,
    HOVER,
    SELECTED,
    TAG
}

export enum ZoneInstanceType {
    GROUP,
    POLICY
}

export enum ZoneInstanceAssignmentType {
    COMPONENT_INSTANCES,
    GROUPS
}

export interface IZoneInstanceMethod {

    getSavedAssignments():Array<IZoneInstanceAssignment>;
    setSavedAssignments(newAssignments:Array<IZoneInstanceAssignment>):void;
}

export interface IZoneInstanceAssignment extends IUiBaseObject{
    type: ZoneInstanceAssignmentType
}

export class ZoneInstance {

    parentComponentType:string;
    parentComponentID:string;
    instanceData: PolicyInstance | GroupInstance;
    mode:ZoneInstanceMode;
    type:ZoneInstanceType;
    handle:string;
    assignments:Array<IZoneInstanceAssignment>; //temp assignments visible on the UI; not the saved values on the BE
    hidden:boolean;
    forceSave:Subject<Function>;

    constructor(instance: PolicyInstance | GroupInstance, topologyTemplateType: string, topologyTemplateId: string) {

        this.instanceData = instance;
        this.parentComponentType = topologyTemplateType;
        this.parentComponentID = topologyTemplateId;

        if (instance instanceof PolicyInstance) {
            this.type = ZoneInstanceType.POLICY;
        } else {
            this.type = ZoneInstanceType.GROUP;
        }

        this.assignments = this.instanceData.getSavedAssignments();
        this.mode = ZoneInstanceMode.NONE;
        this.hidden = false;
        this.forceSave = new Subject();
    }

    public isAlreadyAssigned = (nodeId:string):boolean => {
        let matchingAssignments = this.assignments.filter((assignment) => {
            return assignment.uniqueId == nodeId;
        });
        return matchingAssignments && matchingAssignments.length > 0;
    }

    public addOrRemoveAssignment = (nodeId:string, nodeType:ZoneInstanceAssignmentType)=> { //change temp assignments, unsaved but visible in UI.

        if (!this.isAlreadyAssigned(nodeId)) {
            this.assignments.push(<IZoneInstanceAssignment>{uniqueId: nodeId, type: nodeType});
        } else {
            this.assignments = this.assignments.filter(assignment => assignment.uniqueId != nodeId);
        }
    }
    
    public isZoneAssignmentChanged(oldAssignments:Array<IZoneInstanceAssignment>, newAssignments:Array<IZoneInstanceAssignment>):boolean {
        if (oldAssignments.length != newAssignments.length) {
            return true;
        }
        let difference:Array<IZoneInstanceAssignment> = oldAssignments.filter((oldAssignment) => {
            return !newAssignments.find(newAssignment => newAssignment.uniqueId == oldAssignment.uniqueId);
        });
        if (difference.length) {
            return true;
        }

        return false;
    }

    public updateInstanceData (instanceData:  PolicyInstance | GroupInstance):void {
        this.instanceData = instanceData;
        this.assignments = this.instanceData.getSavedAssignments();
    }

    public showHandle = (handleId:string) => {
        this.handle = handleId;
    }

    public hideHandle = ():void => {
        this.handle = null;
    }
}