5 LeftPaletteMetadataTypes,
8 ZoneInstanceAssignmentType
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";
23 export class CompositionGraphZoneUtils {
25 constructor(private dynamicComponentService: DynamicComponentService,
26 private policiesService: PoliciesService,
27 private groupsService: GroupsService,
28 private workspaceService: WorkspaceService,
29 private compositionService: CompositionService) {
33 public createCompositionZones = (): Array<Zone> => {
34 let zones: Array<Zone> = [];
36 zones[ZoneInstanceType.POLICY] = new Zone('Policies', 'P', ZoneInstanceType.POLICY);
37 zones[ZoneInstanceType.GROUP] = new Zone('Groups', 'G', ZoneInstanceType.GROUP);
42 public showZone = (zone: Zone): void => {
44 zone.minimized = false;
47 public getZoneTypeForPaletteComponent = (componentCategory: LeftPaletteMetadataTypes) => {
48 if (componentCategory == LeftPaletteMetadataTypes.Group) {
49 return ZoneInstanceType.GROUP;
50 } else if (componentCategory == LeftPaletteMetadataTypes.Policy) {
51 return ZoneInstanceType.POLICY;
55 public initZoneInstances(zones: Array<Zone>) {
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);
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);
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);
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);
99 public createZoneInstanceFromLeftPalette = (zoneType: ZoneInstanceType, paletteComponentType: string): Observable<ZoneInstance> => {
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);
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);
116 public addInstanceToZone(zone: Zone, instance: ZoneInstance, hide?: boolean) {
118 instance.hidden = true;
120 zone.instances.push(instance);
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');
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();
144 public startCyTagMode = (cy: Cy.Instance) => {
146 cy.nodes().unselectify();
147 cy.emit('tagstart'); //dont need to show handles because they're already visible bcz of hover event
151 public endCyTagMode = (cy: Cy.Instance) => {
153 cy.nodes().selectify();
157 public handleTagClick = (cy: Cy.Instance, zoneInstance: ZoneInstance, nodeId: string) => {
158 zoneInstance.addOrRemoveAssignment(nodeId, ZoneInstanceAssignmentType.COMPONENT_INSTANCES);
159 this.showZoneTagIndicationForNode(nodeId, zoneInstance, cy);
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);
169 public hideGroupZoneIndications = (instances: Array<ZoneInstance>) => {
170 instances.forEach((instance) => {
171 instance.hideHandle();
175 public showZoneTagIndications = (cy: Cy.Instance, zoneInstance: ZoneInstance) => {
177 cy.nodes().forEach(node => {
178 let handleType: string = this.getCorrectHandleForNode(node.id(), zoneInstance);
179 cy.emit('showhandle', [node, handleType]);
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]);
189 public hideZoneTagIndications = (cy: Cy.Instance) => {
190 cy.emit('hidehandles');
193 public getCorrectHandleForNode = (nodeId: string, zoneInstance: ZoneInstance): string => {
194 if (zoneInstance.isAlreadyAssigned(nodeId)) {
195 if (zoneInstance.type == ZoneInstanceType.POLICY) {
196 return CanvasHandleTypes.TAGGED_POLICY;
198 return CanvasHandleTypes.TAGGED_GROUP;
201 return CanvasHandleTypes.TAG_AVAILABLE;