Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / webapp / js / winery-common-topologyrendering.js
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *    Oliver Kopp - initial API and implementation and/or initial documentation
11  *******************************************************************************/
12 /**
13  * This file contains supporting functions for the rendering a topology template
14  */
15 define(
16         ["jsplumb", "winery-support-common"],
17         function (globdefa, wsc) {
18                 var readOnly = false;
19
20                 var module = {
21                         initNodeTemplate: initNodeTemplate,
22                         handleConnectionCreated: handleConnectionCreated,
23                         imageError: imageError,
24                         setReadOnly: setReadOnly
25                 };
26
27                 return module;
28
29                 /**
30                  * @param nodeTemplateShape the set of node template shapes to initialize
31                  * @param makeDraggable true if the nodeTemplates should be made draggable
32                  */
33                 function initNodeTemplate(nodeTemplateShapeSet, makeDraggable) {
34                         if (makeDraggable) {
35                                 jsPlumb.draggable(nodeTemplateShapeSet);
36                         }
37                         jsPlumb.makeTarget(nodeTemplateShapeSet, {
38                                 anchor:"Continuous",
39                                 endpoint:"Blank"
40                         });
41
42                         // this function is defined in index.jsp via jsp functions
43                         // as it depends on the available relationship types
44                         createConnectorEndpoints(nodeTemplateShapeSet);
45
46                         nodeTemplateShapeSet.addClass("layoutableComponent");
47
48                         nodeTemplateShapeSet.each(function(idx, s) {
49                                 var shape = $(s);
50
51                                 var id = shape.attr("id");
52
53                                 // KV Properties
54                                 var props = shape.children(".propertiesContainer")
55                                         .children(".content")
56                                         .children("table")
57                                         .children("tbody");
58                                 if (!readOnly) {
59                                         props.find(".KVPropertyValue").editable();
60                                 }
61
62                                 // Deployment Artifacts
63                                 var fu = shape.children(".deploymentArtifactsContainer")
64                                         .children(".content")
65                                         .children(".addnewartifacttemplate")
66                                         .children(".fileupload");
67                                 fu.attr("data-url", "nodetemplates/" + wsc.encodeId(id) + "/deploymentartifacts/");
68                         });
69
70                 //      nodeTemplateShapeSet.children(".deploymentArtifactsContainer").children(".content").children(".deploymentArtifact").each(function(index, e) {
71                 //      addnewfileoverlay could be added here
72                 //              $(this).
73                 //      });
74                 }
75
76                 /**
77                  * Handles the creation of connections by jsPlumb
78                  *
79                  * Also called if connection is created during loading
80                  */
81                 function handleConnectionCreated(data) {
82                         // might be called directly from here or by the event
83                         // if called by jsPlumb infrastructure, we have to get rid of the surrounding element
84                         var conn;
85                         if (data.connection) {
86                                 conn = data.connection;
87                         } else {
88                                 conn = data;
89                         }
90                         winery.debugConnData = conn;
91
92                         var id = conn.id;
93                         winery.connections[id] = {
94                                 // we store the id to have a default for the id
95                                 id: id,
96                                 // and use it also as starting point of a name
97                                 name: id,
98                                 // we do NOT copy the plain type here
99                                 // type is stored in the connection
100                                 // type: .getType()[0]
101                                 // BUT: we copy the detailed ns and id
102                                 nsAndLocalName: wsc.getNamespaceAndLocalNameFromQName(conn.getType()[0])
103                         };
104                         putToolTipInfo(conn);
105
106                         // we have to manually show and hide the tooltips as Bootstrap's tooltip plugin does not work after a connection was highlighted.
107                         conn.bind("mouseenter", function(conn,e) {
108                                 putToolTipInfo(conn);
109                         });
110                         conn.bind("mouseexit", function(conn,e) {
111                                 $("div.tooltip").remove();
112                                 // we have to replace the tooltip as
113                                 putToolTipInfo(conn);
114                         });
115                 }
116
117                 function putToolTipInfo(conn) {
118                         // add tooltip showing the relationship type
119                         var svgElement = $(conn.canvas);
120                         // the title attribute is shown in the tooltip
121                         // set the relationship type as tooltip
122                         // we show the localname only
123                         var nsAndLocalName = winery.connections[conn.id].nsAndLocalName;
124                         // Vino4TOSCA: type in brackets
125                         var title = "(" + nsAndLocalName.localname + ")";
126                         svgElement.tooltip({title: title});
127                 }
128
129                 /**
130                  * Removes the image from the display. Used at images which could not be loaded
131                  *
132                  * Used via {@code <img onerror="imageError(this);" ... />}
133                  */
134                 function imageError(image) {
135                         image.onError="";
136                         image.style.visibility = "hidden";
137                 }
138
139                 function setReadOnly() {
140                         readOnly = true;
141                 }
142         }
143 );