summaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.topologymodeler/src/main/webapp/js/winery-support-common.js
blob: 4e87f22ef79caac19fed0965b0924dd06c141acf (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*******************************************************************************
 * Copyright (c) 2012-2014 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
 *******************************************************************************/

/**
 * Functions copied from winery-common.js to replace it in the long term
 *
 * Shared between topology modeler and repository
 */
define([], function() {
		var xmlParser = new DOMParser();
		var VALUE_OF_NONE_CHOOSEN = "(none)"; // constant to indicate that nothing is chosen in a select2

		return {
			encodeId: encodeId,
			getNamespaceAndLocalNameFromQName: getNamespaceAndLocalNameFromQName,
			replaceDialogShownHookForOrionUpdate: replaceDialogShownHookForOrionUpdate,
			writeCollectionDefinedByATextArea: writeCollectionDefinedByATextArea,
			getDocument: getDocument,

			getURLFragmentOutOfFullQName: getURLFragmentOutOfFullQName,
			makeArtifactTypeURLFromQName: makeArtifactTypeURLFromQName,
			makeNodeTypeURLFromQName: makeNodeTypeURLFromQName,
			makeRelationshipTypeURLFromQName: makeRelationshipTypeURLFromQName,
			makeRelationshipTypeURLFromNSAndLocalName: makeRelationshipTypeURLFromNSAndLocalName,

			qname2href: qname2href,

			fetchSelect2DataAndInitSelect2: fetchSelect2DataAndInitSelect2,
			removeItemFromSelect2Field: removeItemFromSelect2Field,

			checkXMLValidityAndShowErrorIfInvalid: checkXMLValidityAndShowErrorIfInvalid,
			synchronizeNameAndType: synchronizeNameAndType,

			VALUE_OF_NONE_CHOOSEN: VALUE_OF_NONE_CHOOSEN
		};

		/**
		 * OriginalName: encodeID
		 */
		function encodeId(id) {
			// the URL sent to the server should be the encoded id
			id = encodeURIComponent(id);
			// therefore, we have to encode it twice
			id = encodeURIComponent(id);
			return id;
		}

		/**
		 * @param qname a QName in the form {namespace}localname
		 * @return { namespace: namespace, localname: localname }
		 */
		function getNamespaceAndLocalNameFromQName(qname) {
			var i = qname.indexOf("}");
			var res = {
				namespace : qname.substr(1,i-1),
				localname : qname.substr(i+1)
			};
			return res;
		}

		/**
		 * Orion does not update content if field not fully shown
		 * therefore, we hook in into the "shown" event
		 */
		function replaceDialogShownHookForOrionUpdate(diag, orionAreaId, content) {
			diag.off("shown.bs.modal");
			diag.on("shown.bs.modal", function() {
				var area = window.winery.orionareas[orionAreaId];
				area.editor.setText(content);
				area.fixEditorHeight();
			});
		}

		function getURLFragmentOutOfNSAndLocalName(nsAndLocalName) {
			var res;
			res = encodeID(nsAndLocalName.namespace);
			res = res + "/";
			res = res + encodeID(nsAndLocalName.localname);
			return res;
		}

		/**
		 * Extracts an URL fragment of the form <encoded namespace>/<encoded id> out of a full QName
		 *
		 * @param qname a QName in the form {namespace}localname
		 */
		function getURLFragmentOutOfFullQName(qname) {
			var d = getNamespaceAndLocalNameFromQName(qname);
			return getURLFragmentOutOfNSAndLocalName(d);
		}

		/**
		 * @param w the XMLwriter
		 * @param elementSet the set of HTML elements to write
		 * @param elementName the name of the wrapper element (e.g., "Requirements", "Policies")
		 */
		function writeCollectionDefinedByATextArea(w, elementSet, elementName) {
			if (elementSet.length !== 0) {
				w.writeStartElement(elementName);
				elementSet.each(function(i, element) {
					// XML contains element completely
					// we do not have to parse reqorcap.children("div.id").children("span.id").text() or the span.name
					var text = $(element).children("textarea").val();
					w.writeXML(text);
				});
				w.writeEndElement();
			}
		}

		function makeArtifactTypeURLFromQName(repoURL, qname) {
			return repoURL + "/artifacttypes/" + getURLFragmentOutOfFullQName(qname) + "/";
		}

		function makeNodeTypeURLFromQName(repoURL, qname) {
			return repoURL + "/nodetypes/" + getURLFragmentOutOfFullQName(qname) + "/";
		}

		function makeRelationshipTypeURLFromQName(repoURL, qname) {
			return repoURL + "/relationshiptypes/" + getURLFragmentOutOfFullQName(qname) + "/";
		}

		function makeRelationshipTypeURLFromNSAndLocalName(repoURL, nsAndLocalName) {
			return repoURL + "/relationshiptypes/" + getURLFragmentOutOfNSAndLocalName(nsAndLocalName) + "/";
		}

		/**
		 * functionality similar to org.eclipse.winery.common.Util.qname2href(String, Class<? extends TExtensibleElements>, QName)
		 */
		function qname2href(repositoryUrl, componentPathFragment, qname) {
			var nsAndId = getNamespaceAndLocalNameFromQName(qname);
			var absoluteURL = repositoryUrl + "/" + componentPathFragment + "/" + getURLFragmentOutOfNSAndLocalName(nsAndId);
			var res = "<a target=\"_blank\" data-qname=\"" + qname + "\" href=\"" + absoluteURL + "\">" + nsAndId.localname + "</a>";
			return res;
		}

		/**
		 * Inspired by
		 *
		 * @param field is the jquery field
		 * @param id_to_remove the id to remove
		 */
		function removeItemFromSelect2Field(field, id_to_remove) {
			// nothing can be done currently
			// see https://github.com/ivaynberg/select2/issues/535#issuecomment-30210641 for a disucssion
			vShowNotification("The select field shows stale data. Refresh the page to get rid of that.")
		}

		/**
		 * Fetches select2 data from the given URL and initializes the field provided by the fieldId
		 *
		 * Calls vShowError if something went wrong
		 *
		 * @param onSuccess (optional)
		 * @param allowAdditions (optional) if set to true, select2 is initalized with the functionality to allow additions during the search
		 */
		function fetchSelect2DataAndInitSelect2(fieldId, url, onSuccess, allowAdditions) {
			$.ajax({
				url: url,
				dataType: "json"
			}).done(function (result) {
				var params = {"data": result};
				if (typeof allowAdditions === "boolean") {
					params.createSearchChoice = function(term) {
						// enables creation of new namespaces
						return {id:term, text:term};
					}
				}

				// init select2 and select first item
				$("#" + fieldId).select2(params);
				if (result.length === 0) {
					$("#" + fieldId).select2("val", null);
				} else {
					$("#" + fieldId).select2("val", result[0].id);
				}

				if (typeof onSuccess === "function") {
					onSuccess();
				}
			}).fail(function(jqXHR, textStatus, errorThrown) {
				vShowAJAXError("Could not fetch select2 data from " + url, jqXHR, errorThrown);
			});

		}


		function getDocument(xmlString) {
			var xmlDoc = xmlParser.parseFromString(xmlString, "text/xml");
			return xmlDoc;
		}

		/**
		 * Checks given XML string for validity. If it's invalid, an error message is shown
		 * Relies on the current browser's XML handling returning a HTML document if something went wrong during parsing
		 *
		 * @return XMLdocument if XML is valid, false otherwise
		 */
		function checkXMLValidityAndShowErrorIfInvalid(xmlString) {
			var doc = getDocument(xmlString);
			var errorMsg = "";
			if (doc.firstChild.localName == "html") {
				errorMsg = new XMLSerializer().serializeToString(doc);
			} else {
				// at Chrome, the error may be nested in the XML

				// quick hack, only "xhtml" is quered
				function nsResolover(x) {
					return "http://www.w3.org/1999/xhtml";
				}

				var element = doc.evaluate( '//x:parsererror', doc, nsResolover, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
				if (element !== null) {
					errorMsg = new XMLSerializer().serializeToString(element);
				}
			}

			if (errorMsg !== "") {
				vShowError(errorMsg);
				return false;
			} else {
				return doc;
			}
		}

				/**
		 * Updates the XML in the orion editor based on the values given in the input fields.
		 * Shows error if XML is invalid
		 *
		 * Works only with SELECT2 fields
		 *
		 * @param idPrefix: (new|edit)${shortName}, derived names: [idPrefix]Name, [idPrefix]Id, Orion[idPrefix]XML
		 * @param hasIdField: whether the Id should be read and written
		 * @param selectFields: array of {attribute, fieldSuffix}, where attribute is a name of an attribute having a QName.
		 * 		Each select box is determined by #[idPrefix][fieldSuffix].
		 * 		The select box content is converted to a QName and the result is written to the attribute [name]
		 * 		Default: {attribute: "type", fieldSuffix: "Type"}
		 *
		 * @return false if XML is invalid, true: an object if id/name/attribute1/attribute2/... (qname + attribute1FullQName: qname object)/xml (to be used in tmpl-${cssClassPrefix})
		 * 	{id:id, name:name, type: "ns5:type", typeFullQName: "{http://www.example.org}type"}
		 */
		function synchronizeNameAndType(idPrefix, hasIdField, selectFields) {
			if (typeof hasIdField === undefined) {
				hasIdField = true;
			}
			if (typeof selectFields === undefined) {
				selectFields = [{attribute: "type", fieldSuffix: "Type"}];
			}


			var val = window.winery.orionareas["Orion" + idPrefix + "XML"].editor.getText();
			var xmlDoc = checkXMLValidityAndShowErrorIfInvalid(val);
			if (xmlDoc) {
				// handle name
				var name = $("#" + idPrefix + "Name").val();
				// initialize result object
				var res = {
					name: name
				};
				xmlDoc.firstChild.setAttribute("name", name);

				// write id and name to XML
				if (hasIdField) {
					var id = $("#" + idPrefix + "Id").val();
					if (!id) {
						// TODO a checking should be done if the id exists
						// probably not here, but at caller's side
						id = name;
					}
					xmlDoc.firstChild.setAttribute("id", id);
					res.id = id;
				}

				// write each selectField to xml
				// for that, we have to determine the QName
				$(selectFields).each(function(i, m) {
					var content = $("#" + idPrefix + m.fieldSuffix).select2("val");

					if (content == VALUE_OF_NONE_CHOOSEN) {
						// if nothing is chosen do not put it into the result
						return;
					}

					// determine qname of type
					//getQNameOutOfFullQName(type, xmlDoc.firstChild) does not always work as xmlDoc.firstChild does not have ALL *available* namespace prefixes
					var typeNSAndId = getNamespaceAndLocalNameFromQName(content);
					var prefix = xmlDoc.firstChild.lookupPrefix(typeNSAndId.namespace);
					if (!prefix) {
						// we have to ask the repo for a prefix
						$.ajax({
							type: "GET",
							async: false,
							"url": winery.repositoryURL + "/admin/namespaces/" + encodeID(typeNSAndId.namespace),
							dataType: "text",
							error: function(jqXHR, textStatus, errorThrown) {
								vShowAJAXError("Could not determine prefix", jqXHR, errorThrown);
							},
							success: function(resData, textStatus, jqXHR) {
								prefix = resData;
							}
						});
						// new prefix fetched, xmlns attribute has to be written
						xmlDoc.firstChild.setAttribute("xmlns:" + prefix, typeNSAndId.namespace);
					}
					var qname = prefix + ":" + typeNSAndId.localname;
					res[m.attribute] = qname;
					res[m.attribute + "FullQName"] = typeNSAndId;
					xmlDoc.firstChild.setAttribute(m.attribute, qname);
				});

				var xml = new XMLSerializer().serializeToString(xmlDoc);
				res.xml = xml;

				return res;
			} else {
				return false;
			}
		}


	}
);