summaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.topologymodeler/src/main/webapp/js/winery-topologymodeler.js
blob: cc5c564f60f5a8db4a3966ec98efdc40c7b10b5b (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*******************************************************************************
 * Copyright (c) 2012-2013,2015 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *    Oliver Kopp - initial API and implementation and/or initial documentation
 *    Yves Schubert - switch to bootstrap 3
 *******************************************************************************/

function getQName(qnameStr) {
	var pos = qnameStr.indexOf("}");
	var namespace = qnameStr.substring(1, pos);
	var localName = qnameStr.substring(pos+1);
	var res = {
			"namespace": namespace,
			"localName": localName
	};
	return res;
}

/**
 * @param attributeName an attribute with a value in the form prefix:localname
 * @param xmlElement a DOM element (offering the method lookupNamespaceURI)
 * @return { ns: namespace, id: id }
 */
function getNSAndId(attributeName, xmlElement) {
	var attributeValue = xmlElement.getAttribute(attributeName);
	var i = attributeValue.indexOf(":");
	var prefix = attributeValue.substring(0, i);
	var localName = attributeValue.substring(i+1);
	var ns = xmlElement.lookupNamespaceURI(prefix);
	var res = {
		ns : ns,
		id: localName
	};
	return res;
}

/**
 * @param el a href element
 * @param pathComponent the path element "artifacttemplates" or "artifacttypes"
 * @param attributeName the name of the attribute to read from the given xmlElement
 * @param xmlElement used to resolve a namespace prefix to a full namespace URI
 */
function addHref(el, pathComponent, attributeName, xmlElement) {
	var nsAndId = getNSAndId(attributeName, xmlElement);
	var loc = winery.repositoryURL + "/" + pathComponent + "/" + encodeID(nsAndId.ns) + "/" + encodeID(nsAndId.id);
	el.attr("href", loc);
}

var currentlySelectedDeploymentArtifactDiv;

/**
 * Sets global variables currentlySelectedNodeTemplate and currentlySelectedDeploymentArtifactDiv
 */
function showDeploymentArtifactInformation(nodeTemplateId, deploymentArtifactName) {
	currentlySelectedNodeTemplate = nodeTemplateId;
	var daDiv = $("#" + nodeTemplateId).children("div.deploymentArtifactsContainer").children("div.content").children("div.deploymentArtifact").children("div.name:contains(" + deploymentArtifactName + ")").parent();
	currentlySelectedDeploymentArtifactDiv = daDiv;
	var xml = daDiv.children("textarea").val();

	// get values to display directly from the "UI" instead of parsing the XML and asking the server for appropriate names
	var daArtifactTemplateName = daDiv.children("div.artifactTemplate").text();
	var daArtifactTypeName = daDiv.children("div.artifactType").text();

	// determine URLs
	require(["winery-support-common"], function(wsc) {
		xmlDoc = wsc.getDocument(xml);
		da = xmlDoc.firstChild;

		$("#DAname").text(deploymentArtifactName);

		$("#DAArtifactType").text(daArtifactTypeName);
		addHref($("#DAArtifactType"), "artifacttypes", "artifactType", da);

		var at = $("#DAArtifactTemplate");
		if (daArtifactTemplateName != "") {
			at.text(daArtifactTemplateName);
			addHref(at, "artifacttemplates", "artifactRef", da);
		} else {
			at.text("No template associated");
			at.removeAttr("href");
		}

		$("#DAXML").val(xml);

		$("#DeploymentArtifactInfo").modal("show");
	});
}

/**
 * Adds the given data to the deployment artifacts table of the currently active node template
 *
 * @param xmlAsDOM XML DOM document, TDeploymentArtifact. Produced by org.eclipse.winery.resources.artifacts.GenericArtifactsResource.onPost(String, String, String, String, String, String, String, String)
 * @param xmlAsString
 */
function addDeploymentArtifact(xmlAsDOM, xmlAsString) {
	var da = xmlAsDOM.firstChild;
	var daName = da.getAttribute("name");

	// we do NOT extract artifactType / artifactTemplate from the XML, but use the user input
	// showDeploymentArtifactInformation will extract its data directly from the XML without querying some input at the other HTML elements
	var daArtifactTemplateName = $("#artifactTemplateName").val();
	var daArtifactTypeName = $("#artifactType option:selected").text();

	// add information to node template shape
	var daData = {
		nodeTemplateId : currentlySelectedNodeTemplate,
		name : daName,
		xml : xmlAsString,
		artifactTypeName: daArtifactTypeName
	};
	if (daArtifactTemplateName != "") {
		daData.artifactTemplateName = daArtifactTemplateName;
	}
	addDeploymentArtifactInfoToNodeTemplate(daData);
}

function addDeploymentArtifactInfoToNodeTemplate(daData) {
	require(["tmpl"], function(tmpl){
		var data = tmpl("tmpl-deploymentArtifact", daData);
		var element = $("#" + currentlySelectedNodeTemplate).children(".deploymentArtifactsContainer").children(".content").children(".addDA:first");
		element.before(data);
	});
}

/**
 * This function directly accesses the fields of the dialog, because the return value of the server is XML and we do not want to parse XML
 *
 * @param artifactInfo = {name, interfaceName (may be undefined), operationName  (may be undefined), artifactTemplate (QName, may be undefined), artifactType}
 */
function artifactAddedSuccessfully(artifactInfo) {
	var typeNsAndId = getNamespaceAndLocalNameFromQName(artifactInfo.artifactType);
	var artifactTemplateNSAndId;
	if (artifactInfo.artifactTemplate) {
		artifactTemplateNSAndId = getNamespaceAndLocalNameFromQName(artifactInfo.artifactTemplate);
	} else {
		artifactTemplateNSAndId = undefined;
	}

	var daData = {
		nodeTemplateId : currentlySelectedNodeTemplate,
		name : artifactInfo.name,
		artifactTypeName: typeNsAndId.localname,
		artifactTypeNSAndId: typeNsAndId,
		artifactTemplateName: artifactInfo.artifactTemplateName,
		artifactTemplateNSAndId: artifactTemplateNSAndId
	};
	require(["tmpl"], function(tmpl){
		daData.xml = tmpl("tmpl-deploymentArtifactXML", daData);
		addDeploymentArtifactInfoToNodeTemplate(daData);
	});
}

// variables used for creation of deployment artifacts
var artifactTemplateAutoCreationEnabled = true;
var syncDAnameWithATname;

// introduced by the handling of deployment and implementation artifacts
// holds the ID only (!)
var currentlySelectedNodeTemplate;

/**
 * FIXME: this function is not updated to the the new dialog design and not included any more
 *
 * It should be used if the checkbox for at creation changes its checked status or if the at name is not valid
 *
 */
function updateArtifactTemplateCreationEnablement(value) {
	// remove field highlights
	// (currently, no intelligent removal and addition is made)
	$("#artifactName").removeClass("highlight");
	$("#artifactTemplateName").removeClass("highlight");

	if (value) {
		// enable it
		artifactTemplateAutoCreationEnabled = true;
		$("#artifactTemplateName").removeAttr("disabled");
		$("#artifactTemplateNS").removeAttr("disabled");
		$("#createWithoutFilesBtn").attr("disabled", "disabled");
		$("#createWithFilesBtn").removeAttr("disabled");
	} else {
		// disable it
		artifactTemplateAutoCreationEnabled = false;
		$("#artifactTemplateName").attr("disabled", "disabled");
		$("#artifactTemplateNS").attr("disabled", "disabled");
		$("#createWithoutFilesBtn").removeAttr("disabled");
		$("#createWithFilesBtn").attr("disabled", "disabled");
	}
}

function isShownNodeTemplateShapeChangeBoxes(shape) {
	return (shape.find(".endpointContainer").is(":visible"));
}

/**
 * @param shape jQuery object
 */
function showNodeTemplateShapeChangeBoxes(shape) {
	shape.find(".addDA").show();
	shape.children(".endpointContainer").show();
	shape.find(".addnewreqorcap").show();
	shape.find(".addnewpolicy").show();
}

/**
 * @param shape jQuery object
 */
function hideNodeTemplateShapeChangeBoxes(shape) {
	shape.find(".addDA").hide();
	shape.children(".endpointContainer").hide();
	shape.find(".addnewreqorcap").hide();
	shape.find(".addnewpolicy").hide();
}

// indicates if a connection is currently drawn
// used to decide whether the node template boxes should be displayed
var isInConnectionMode = false;

function wineryMoveSelectedNodeTemplateShapes(dX, dY) {
	var shapes = $("div.NodeTemplateShape.selected");
	hideNodeTemplateShapeChangeBoxes(shapes);
	shapes.each(function(i, nodeTemplate) {
		nodeTemplate = $(nodeTemplate);
		var offset = nodeTemplate.offset();
		offset.left += dX;
		offset.top += dY;
		nodeTemplate.offset(offset);
	});
	jsPlumb.repaint(shapes);
}


/**
 * Simple eventing framework
 *
 * use
 * winery.events.register(name, function) to register on an event
 * and
 * winery.events.fire(name) to fire all registered functions
 */

winery = {};
winery.events = {
	_events : {},

	/**
	 * Registers a function
	 *
	 * @return the registered function
	 */
	register : function(eventName, f) {
		if (!winery.events._events[eventName]) {
			winery.events._events[eventName] = {};
		}
		winery.events._events[eventName][f] = f;
		return f;
	},

	/**
	 * Fires all functions associated with the given event name
	 */
	fire : function(eventName) {
		if (winery.events._events[eventName]) {
			$.each(winery.events._events[eventName], function(index, value) {
				value();
			});
		}
		return true;
	}
};

/**
 * Determines whether a key combo is allowed.
 *
 * For instance, when a modal dialog is opened or a input is selected, DEL should not delete node template shapes
 */
function keyComboAllowed() {
	return ((!$(document.activeElement).is("input")) && ($("div.modal:visible").size() == 0));
}

function keyComboAllowedAndNodeTemplatesSelected() {
	return (keyComboAllowed() && ($("div.NodeTemplateShape.selected").size() != 0));
}




/* list of event names */
winery.events.name = {};
winery.events.name.command = {};

winery.events.name.SELECTION_CHANGED = "selectionchanged";
winery.events.name.command.SELECT_ALL_NODETEMPLATES = "selectAllNodeTemplates";
winery.events.name.command.UNSELECT_ALL_NODETEMPLATES = "unselectAllNodeTemplates";
winery.events.name.command.DELETE_SELECTION = "deleteSelection";

winery.events.name.command.MOVE_DOWN = "moveDown";
winery.events.name.command.MOVE_UP = "moveUp";
winery.events.name.command.MOVE_LEFT = "moveLeft";
winery.events.name.command.MOVE_RIGHT = "moveRight";

winery.events.name.command.SAVE = "save";