Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / graph / utils / composition-graph-zone-utils.ts
1 import {
2     Point,
3     PolicyInstance,
4     Zone,
5     LeftPaletteMetadataTypes,
6     ZoneInstance,
7     ZoneInstanceType,
8     ZoneInstanceAssignmentType
9 } from "app/models";
10 import {CanvasHandleTypes} from "app/utils";
11 import {Observable} from "rxjs";
12 import {GroupInstance} from "app/models/graph/zones/group-instance";
13 import {Injectable} from "@angular/core";
14 import {DynamicComponentService} from "app/ng2/services/dynamic-component.service";
15 import {PoliciesService} from "app/ng2/services/policies.service";
16 import {GroupsService} from "app/ng2/services/groups.service";
17 import {Store} from "@ngxs/store";
18 import {CompositionService} from "../../composition.service";
19 import {WorkspaceService} from "app/ng2/pages/workspace/workspace.service";
20 import { PaletteAnimationComponent } from "app/ng2/pages/composition/palette/palette-animation/palette-animation.component";
21
22 @Injectable()
23 export class CompositionGraphZoneUtils {
24
25     constructor(private dynamicComponentService: DynamicComponentService,
26                 private policiesService: PoliciesService,
27                 private groupsService: GroupsService,
28                 private workspaceService: WorkspaceService,
29                 private compositionService: CompositionService) {
30     }
31
32
33     public createCompositionZones = (): Array<Zone> => {
34         let zones: Array<Zone> = [];
35
36         zones[ZoneInstanceType.POLICY] = new Zone('Policies', 'P', ZoneInstanceType.POLICY);
37         zones[ZoneInstanceType.GROUP] = new Zone('Groups', 'G', ZoneInstanceType.GROUP);
38
39         return zones;
40     }
41
42     public showZone = (zone: Zone): void => {
43         zone.visible = true;
44         zone.minimized = false;
45     }
46
47     public getZoneTypeForPaletteComponent = (componentCategory: LeftPaletteMetadataTypes) => {
48         if (componentCategory == LeftPaletteMetadataTypes.Group) {
49             return ZoneInstanceType.GROUP;
50         } else if (componentCategory == LeftPaletteMetadataTypes.Policy) {
51             return ZoneInstanceType.POLICY;
52         }
53     };
54
55     public initZoneInstances(zones: Array<Zone>) {
56
57         if (this.compositionService.groupInstances && this.compositionService.groupInstances.length) {
58             this.showZone(zones[ZoneInstanceType.GROUP]);
59             zones[ZoneInstanceType.GROUP].instances = [];
60             _.forEach(this.compositionService.groupInstances, (group: GroupInstance) => {
61                 let newInstance = new ZoneInstance(group, this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId);
62                 this.addInstanceToZone(zones[ZoneInstanceType.GROUP], newInstance);
63             });
64         }
65
66         if (this.compositionService.policies && this.compositionService.policies.length) {
67             this.showZone(zones[ZoneInstanceType.POLICY]);
68             zones[ZoneInstanceType.POLICY].instances = [];
69             _.forEach(this.compositionService.policies, (policy: PolicyInstance) => {
70                 let newInstance = new ZoneInstance(policy, this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId);
71                 this.addInstanceToZone(zones[ZoneInstanceType.POLICY], newInstance);
72
73             });
74         }
75     }
76
77     public findAndUpdateZoneInstanceData(zones: Array<Zone>, instanceData: PolicyInstance | GroupInstance) {
78         _.forEach(zones, (zone: Zone) => {
79             _.forEach(zone.instances, (zoneInstance: ZoneInstance) => {
80                 if (zoneInstance.instanceData.uniqueId === instanceData.uniqueId) {
81                     zoneInstance.updateInstanceData(instanceData);
82                 }
83             });
84         });
85     }
86
87     public updateTargetsOrMembersOnCanvasDelete = (canvasNodeID: string, zones: Array<Zone>, type: ZoneInstanceAssignmentType): void => {
88         _.forEach(zones, (zone) => {
89             _.forEach(zone.instances, (zoneInstance: ZoneInstance) => {
90                 if (zoneInstance.isAlreadyAssigned(canvasNodeID)) {
91                     zoneInstance.addOrRemoveAssignment(canvasNodeID, type);
92                     //remove it from our list of BE targets and members as well (so that it will not be sent in future calls to BE).
93                     zoneInstance.instanceData.setSavedAssignments(zoneInstance.assignments);
94                 }
95             });
96         });
97     };
98
99     public createZoneInstanceFromLeftPalette = (zoneType: ZoneInstanceType, paletteComponentType: string): Observable<ZoneInstance> => {
100
101         if (zoneType === ZoneInstanceType.POLICY) {
102             return this.policiesService.createPolicyInstance(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, paletteComponentType).map(response => {
103                 let newInstance = new PolicyInstance(response);
104                 this.compositionService.addPolicyInstance(newInstance);
105                 return new ZoneInstance(newInstance, this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId);
106             });
107         } else if (zoneType === ZoneInstanceType.GROUP) {
108             return this.groupsService.createGroupInstance(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, paletteComponentType).map(response => {
109                 let newInstance = new GroupInstance(response);
110                 this.compositionService.addGroupInstance(newInstance);
111                 return new ZoneInstance(newInstance, this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId);
112             });
113         }
114     }
115
116     public addInstanceToZone(zone: Zone, instance: ZoneInstance, hide?: boolean) {
117         if (hide) {
118             instance.hidden = true;
119         }
120         zone.instances.push(instance);
121
122     };
123
124     private findZoneCoordinates(zoneType): Point {
125         let point: Point = new Point(0, 0);
126         let zone = angular.element(document.querySelector('.' + zoneType + '-zone'));
127         let wrapperZone = zone.offsetParent();
128         point.x = zone.prop('offsetLeft') + wrapperZone.prop('offsetLeft');
129         point.y = zone.prop('offsetTop') + wrapperZone.prop('offsetTop');
130         return point;
131     }
132
133     public createPaletteToZoneAnimation = (startPoint: Point, zoneType: ZoneInstanceType, newInstance: ZoneInstance) => {
134         let zoneTypeName = ZoneInstanceType[zoneType].toLowerCase();
135         let paletteToZoneAnimation = this.dynamicComponentService.createDynamicComponent(PaletteAnimationComponent);
136         paletteToZoneAnimation.instance.from = startPoint;
137         paletteToZoneAnimation.instance.type = zoneType;
138         paletteToZoneAnimation.instance.to = this.findZoneCoordinates(zoneTypeName);
139         paletteToZoneAnimation.instance.zoneInstance = newInstance;
140         paletteToZoneAnimation.instance.iconName = zoneTypeName;
141         paletteToZoneAnimation.instance.runAnimation();
142     }
143
144     public startCyTagMode = (cy: Cy.Instance) => {
145         cy.autolock(true);
146         cy.nodes().unselectify();
147         cy.emit('tagstart'); //dont need to show handles because they're already visible bcz of hover event
148
149     };
150
151     public endCyTagMode = (cy: Cy.Instance) => {
152         cy.emit('tagend');
153         cy.nodes().selectify();
154         cy.autolock(false);
155     };
156
157     public handleTagClick = (cy: Cy.Instance, zoneInstance: ZoneInstance, nodeId: string) => {
158         zoneInstance.addOrRemoveAssignment(nodeId, ZoneInstanceAssignmentType.COMPONENT_INSTANCES);
159         this.showZoneTagIndicationForNode(nodeId, zoneInstance, cy);
160     };
161
162     public showGroupZoneIndications = (groupInstances: Array<ZoneInstance>, policyInstance: ZoneInstance) => {
163         groupInstances.forEach((groupInstance: ZoneInstance) => {
164             let handle: string = this.getCorrectHandleForNode(groupInstance.instanceData.uniqueId, policyInstance);
165             groupInstance.showHandle(handle);
166         })
167     };
168
169     public hideGroupZoneIndications = (instances: Array<ZoneInstance>) => {
170         instances.forEach((instance) => {
171             instance.hideHandle();
172         })
173     }
174
175     public showZoneTagIndications = (cy: Cy.Instance, zoneInstance: ZoneInstance) => {
176
177         cy.nodes().forEach(node => {
178             let handleType: string = this.getCorrectHandleForNode(node.id(), zoneInstance);
179             cy.emit('showhandle', [node, handleType]);
180         });
181     };
182
183     public showZoneTagIndicationForNode = (nodeId: string, zoneInstance: ZoneInstance, cy: Cy.Instance) => {
184         let node = cy.getElementById(nodeId);
185         let handleType: string = this.getCorrectHandleForNode(nodeId, zoneInstance);
186         cy.emit('showhandle', [node, handleType]);
187     }
188
189     public hideZoneTagIndications = (cy: Cy.Instance) => {
190         cy.emit('hidehandles');
191     };
192
193     public getCorrectHandleForNode = (nodeId: string, zoneInstance: ZoneInstance): string => {
194         if (zoneInstance.isAlreadyAssigned(nodeId)) {
195             if (zoneInstance.type == ZoneInstanceType.POLICY) {
196                 return CanvasHandleTypes.TAGGED_POLICY;
197             } else {
198                 return CanvasHandleTypes.TAGGED_GROUP;
199             }
200         } else {
201             return CanvasHandleTypes.TAG_AVAILABLE;
202         }
203     };
204 }