1 /// <reference path="../../../../references"/>
2 module Sdc.Graph.Utils {
4 import Dictionary = Sdc.Utils.Dictionary;
6 export class CompositionGraphGeneralUtils {
8 public componentRequirementsAndCapabilitiesCaching = new Dictionary<string, Models.Components.Component>();
9 protected static graphUtilsUpdateQueue: Sdc.Utils.Functions.QueueUtils;
11 constructor(private $q: ng.IQService,
12 private LoaderService: Services.LoaderService,
13 private commonGraphUtils: Sdc.Graph.Utils.CommonGraphUtils,
14 private matchCapabilitiesRequirementsUtils: Graph.Utils.MatchCapabilitiesRequirementsUtils) {
15 CompositionGraphGeneralUtils.graphUtilsUpdateQueue = new Sdc.Utils.Functions.QueueUtils(this.$q);
20 * Get the offset for the link creation Menu
22 * @returns {Cy.Position}
24 public calcMenuOffset: Function = (point: Cy.Position): Cy.Position => {
25 point.x = point.x + 60;
26 point.y = point.y + 105;
31 * return the top left position of the link menu
33 * @param targetNodePosition
34 * @returns {Cy.Position}
36 public getLinkMenuPosition = (cy: Cy.Instance, targetNodePosition: Cy.Position) => {
37 let menuPosition: Cy.Position = this.calcMenuOffset(targetNodePosition); //get the link mid point
38 if (document.body.scrollHeight < menuPosition.y + Sdc.Utils.Constants.GraphUIObjects.LINK_MENU_HEIGHT + $(document.getElementsByClassName('sdc-composition-graph-wrapper')).offset().top) { // if position menu is overflow bottom
39 menuPosition.y = document.body.scrollHeight - Sdc.Utils.Constants.GraphUIObjects.TOP_HEADER_HEIGHT - Sdc.Utils.Constants.GraphUIObjects.LINK_MENU_HEIGHT;
46 * will return true/false if two nodes overlapping
50 private isNodesOverlapping(node: Cy.CollectionFirstNode, draggedNode: Cy.CollectionFirstNode): boolean {
52 let nodeBoundingBox: Cy.BoundingBox = node.renderedBoundingBox();
53 let secondNodeBoundingBox: Cy.BoundingBox = draggedNode.renderedBoundingBox();
55 return this.isBBoxOverlapping(nodeBoundingBox, secondNodeBoundingBox);
59 * Checks whether the bounding boxes of two nodes are overlapping on any side
64 private isBBoxOverlapping(nodeOneBBox: Cy.BoundingBox, nodeTwoBBox: Cy.BoundingBox) {
65 return (((nodeOneBBox.x1 < nodeTwoBBox.x1 && nodeOneBBox.x2 > nodeTwoBBox.x1) ||
66 (nodeOneBBox.x1 < nodeTwoBBox.x2 && nodeOneBBox.x2 > nodeTwoBBox.x2) ||
67 (nodeTwoBBox.x1 < nodeOneBBox.x1 && nodeTwoBBox.x2 > nodeOneBBox.x2)) &&
68 ((nodeOneBBox.y1 < nodeTwoBBox.y1 && nodeOneBBox.y2 > nodeTwoBBox.y1) ||
69 (nodeOneBBox.y1 < nodeTwoBBox.y2 && nodeOneBBox.y2 > nodeTwoBBox.y2) ||
70 (nodeTwoBBox.y1 < nodeOneBBox.y1 && nodeTwoBBox.y2 > nodeOneBBox.y2)))
75 * Checks whether a specific component instance can be hosted on the UCPE instance
76 * @param cy - Cytoscape instance
77 * @param fromUcpeInstance
78 * @param toComponentInstance
79 * @returns {Models.MatchReqToCapability}
81 public canBeHostedOn(cy: Cy.Instance, fromUcpeInstance: Models.ComponentsInstances.ComponentInstance, toComponentInstance: Models.ComponentsInstances.ComponentInstance): Models.MatchReqToCapability {
83 let matches: Array<Models.MatchBase> = this.matchCapabilitiesRequirementsUtils.getMatchedRequirementsCapabilities(fromUcpeInstance, toComponentInstance, this.getAllCompositionCiLinks(cy));
84 let hostedOnMatch: Models.MatchBase = _.find(matches, (match: Models.MatchReqToCapability) => {
85 return match.requirement.capability.toLowerCase() === 'tosca.capabilities.container';
88 return <Models.MatchReqToCapability>hostedOnMatch;
93 * Checks whether node can be dropped into UCPE
99 private isValidDropInsideUCPE(cy: Cy.Instance, nodeToInsert: Models.ComponentsInstances.ComponentInstance, ucpeNode: Models.ComponentsInstances.ComponentInstance): boolean {
101 let hostedOnMatch: Models.MatchReqToCapability = this.canBeHostedOn(cy, ucpeNode, nodeToInsert);
102 let result: boolean = !angular.isUndefined(hostedOnMatch) || nodeToInsert.isVl(); //group validation
109 * For drops from palette, checks whether the node can be dropped. If node is being held over another node, check if capable of hosting
111 * @param pseudoNodeBBox
112 * @param paletteComponentInstance
115 public isPaletteDropValid(cy: Cy.Instance, pseudoNodeBBox: Cy.BoundingBox, paletteComponentInstance:Sdc.Models.ComponentsInstances.ComponentInstance) {
117 let componentIsUCPE:boolean = (paletteComponentInstance.capabilities && paletteComponentInstance.capabilities['tosca.capabilities.Container'] && paletteComponentInstance.name.toLowerCase().indexOf('ucpe') > -1);
119 if(componentIsUCPE && cy.nodes('[?isUcpe]').length > 0) { //second UCPE not allowed
123 let illegalOverlappingNodes = _.filter(cy.nodes("[isSdcElement]"), (graphNode: Cy.CollectionFirstNode) => {
125 if(this.isBBoxOverlapping(pseudoNodeBBox, graphNode.renderedBoundingBox())){
126 if (!componentIsUCPE && graphNode.data().isUcpe) {
127 return !this.isValidDropInsideUCPE(cy, paletteComponentInstance, graphNode.data().componentInstance); //if this is valid insert into ucpe, we return false - no illegal overlapping nodes
135 return illegalOverlappingNodes.length === 0;
139 * will return true/false if a drop of a single node is valid
143 public isValidDrop(cy: Cy.Instance, draggedNode: Cy.CollectionFirstNode): boolean {
145 let illegalOverlappingNodes = _.filter(cy.nodes("[isSdcElement]"), (graphNode: Cy.CollectionFirstNode) => { //all sdc nodes, removing child nodes (childe node allways collaps
147 if (draggedNode.data().isUcpe && (graphNode.isChild() || graphNode.data().isInsideGroup)) { //ucpe cps always inside ucpe, no overlapping
150 if(draggedNode.data().isInsideGroup && (!draggedNode.active() || graphNode.data().isUcpe)) {
154 if (!draggedNode.data().isUcpe && !(draggedNode.data() instanceof Sdc.Models.Graph.CompositionCiNodeUcpeCp) && graphNode.data().isUcpe) { //case we are dragging a node into UCPE
155 let isEntirelyInUCPE:boolean = this.commonGraphUtils.isFirstBoxContainsInSecondBox(draggedNode.renderedBoundingBox(), graphNode.renderedBoundingBox());
156 if (isEntirelyInUCPE){
157 if(this.isValidDropInsideUCPE(cy, draggedNode.data().componentInstance, graphNode.data().componentInstance)){ //if this is valid insert into ucpe, we return false - no illegal overlapping nodes
162 return graphNode.data().id !== draggedNode.data().id && this.isNodesOverlapping(draggedNode, graphNode);
166 return illegalOverlappingNodes.length === 0;
170 * will return true/false if the move of the nodes is valid (no node overlapping and verifying if insert into UCPE is valid)
172 * @param nodesArray - the selected drags nodes
174 public isGroupValidDrop(cy: Cy.Instance, nodesArray: Cy.CollectionNodes): boolean {
175 var filterDraggedNodes = nodesArray.filter('[?isDraggable]');
176 let isValidDrop = _.every(filterDraggedNodes, (node: Cy.CollectionFirstNode) => {
177 return this.isValidDrop(cy, node);
184 * get all links in diagram
186 * @returns {any[]|boolean[]}
188 public getAllCompositionCiLinks = (cy: Cy.Instance): Array<Models.CompositionCiLinkBase> => {
189 return _.map(cy.edges("[isSdcElement]"), (edge: Cy.CollectionEdges) => {
196 * Get Graph Utils server queue
197 * @returns {Sdc.Utils.Functions.QueueUtils}
199 public getGraphUtilsServerUpdateQueue(): Sdc.Utils.Functions.QueueUtils {
200 return CompositionGraphGeneralUtils.graphUtilsUpdateQueue;
206 * @param blockAction - true/false if this is a block action
210 public pushMultipleUpdateComponentInstancesRequestToQueue = (blockAction: boolean, instances: Array<Models.ComponentsInstances.ComponentInstance>, component: Models.Components.Component): void => {
212 this.getGraphUtilsServerUpdateQueue().addBlockingUIAction(
213 () => component.updateMultipleComponentInstances(instances)
216 this.getGraphUtilsServerUpdateQueue().addNonBlockingUIAction(
217 () => component.updateMultipleComponentInstances(instances),
218 () => this.LoaderService.hideLoader('composition-graph'));
223 * this function will update component instance data
224 * @param blockAction - true/false if this is a block action
225 * @param updatedInstance
227 public pushUpdateComponentInstanceActionToQueue = (component: Models.Components.Component, blockAction: boolean, updatedInstance: Models.ComponentsInstances.ComponentInstance): void => {
230 this.LoaderService.showLoader('composition-graph');
231 this.getGraphUtilsServerUpdateQueue().addBlockingUIAction(
232 () => component.updateComponentInstance(updatedInstance)
235 this.getGraphUtilsServerUpdateQueue().addNonBlockingUIAction(
236 () => component.updateComponentInstance(updatedInstance),
237 () => this.LoaderService.hideLoader('composition-graph'));
242 CompositionGraphGeneralUtils.$inject = ['$q', 'LoaderService', 'CommonGraphUtils', 'MatchCapabilitiesRequirementsUtils'];