aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/webapp/js/winery-support-non-AMD.js
blob: f1f1cfaf6e0e907a3dcb4aaf88a5f7952bf4f5e4 (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
/*******************************************************************************
 * Copyright (c) 2012-2013 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
 *******************************************************************************/

function addResourceInstance() {
	if (highlightRequiredFields()) {
		vShowError("Please fill in all required fields");
		return;
	}

	var dataToSend = $('#createResourceForm').serialize();
	var cr = $('#createResource');
	$.ajax({
		type: "POST",
		async: false,
		"data": dataToSend,
		"url": cr.data("url"),
		dataType: "text",
		error: function(jqXHR, textStatus, errorThrown) {
			vShowAJAXError("Could not create resource", jqXHR, errorThrown);
			cr.modal("hide");
		},
		success: function(resData, textStatus, jqXHR) {
			cr.data("onSuccess")($('#createResourceForm').serializeArray(), resData, textStatus, jqXHR);
			cr.modal('hide');
		}
	});
}

/**
 * This function creates a dialog, where the user can add key/value pairs.
 * These pairs are then sent to the given URL via POST.
 *
 * REQUIRES <script id="template-createresource" type="text/x-tmpl">
 * Currently placed in header.jsp
 *
 * @param nameOfResource the name of the resource to add
 * @param fields array of label/name/type/hint/checked values to use for the field and to pass in the AJAX call. (optional) type is in "text"/"checkbox"/... -- the values allowed for "type" attributes of <input> fields. Currently, all fields are required.
 * @param url the URL to use. The URL is uses as unique ID. If a dialog is requested to be open with a URL and the previous dialog had the same URL, the previous dialog is opened
 * @param onSuccess: function(serializedArray, data, textStatus, jqXHR) to call if adding has been successful. "serializedArray" contains the value of $('#formid').serializeArray()
 */
function createResource(nameOfResource, fields, url, onSuccess) {
	var cr = $('#createResource');
	if (cr.length == 1) {
		if (cr.data("url") == url) {
			// the same dialog has been created before. Reuse it
			cr.modal("show");
			return;
		} else {
			// remove the dialog and thus enable the creation of a new one
			cr.remove();
		}
	}

	var data  = {
		nameOfResource: nameOfResource,
		fields: fields
	};
	require(["tmpl"], function(tmpl) {
		var div = tmpl("template-createresource", data);

		$("body").append(div);
		cr = $('#createResource');
		cr.on("shown.bs.modal", function() {
			$("#createResourceForm > fieldset > div:first-child > input").focus();
		});

		cr.modal('show');
		cr.data("url", url);
		cr.data("onSuccess", onSuccess);
	});
}

/**
 *
 * @param selection jQuery selection object (<selection>)
 * @param value the value of the text to add
 * @param text the text to add
 */
function addSortedSelectionItem(selection, value, text) {
	var option = selection.children("option:first-child");
	while ((option.length == 1) && (option.text() < text)) {
		option = option.next();
	}
	var toAppend = '<option value="' + value + '" selected="selected">' + text + '</option>';
	if (option.length == 0) {
		selection.append(toAppend);
	} else {
		option.before(toAppend);
	}
}

/**** begin: for datatable ****/

/**
 * Uses selected row as information for deleting on server (and on success deleting in table)
 *
 * the id of the thing to delete is read from the first column of the table
 *
 * @param tableInfo: info object about table
 * @param nameOfThingToDelete: used at messages
 * @param baseURL: used to form URL by baseURL+<name of thing>
 * @param idColumn: (optional) column to look for the id. If not provided, look in the first column
 * @param nameColumn: (optional) column to look for a name. If not provided, the id is used
 * @param namespaceColumn: (optional) column to look for a namespace. If not provided, do not use any nameespace information
 * @param withoutConfirmation (optional) if given, the resource is deleted without any confirmation
*/
function deleteOnServerAndInTable(tableInfo, nameOfThingToDelete, baseURL, idColumn, nameColumn, namespaceColumn, withoutConfirmation) {
	if (tableInfo.selectedRow == null) {
		vShowError("No row selected.");
	} else {
		idColumn = idColumn || 0; // default: first column indicates identifier
		var id = tableInfo.table.fnGetData(tableInfo.selectedRow, idColumn);
		var name;
		if (typeof nameColumn === "undefined") {
			name = id;
		} else  {
			name = tableInfo.table.fnGetData(tableInfo.selectedRow, nameColumn);
		}

		var url = baseURL;
		if (typeof namespaceColumn !== "undefined") {
			var namespace = tableInfo.table.fnGetData(tableInfo.selectedRow, namespaceColumn);
			namespace = encodeID(namespace);
			url = url + namespace + '/';
		}
		// append the id
		// we could add a "/" to be compatible with Jersey's URL rewriting
		// However, that prevents deleting a thing being a leaf in the URL (e.g. a namespace)
		url = url + encodeID(id);

		// defined in winery-common.js
		deleteResource(nameOfThingToDelete + " " + name, url,
			function(data, textSTatus, jqXHR) {
				tableInfo.table.fnDeleteRow(tableInfo.selectedRow);
				tableInfo.selectedRow = null;
				tableInfo.selectedTr = null;
			}, false, false, withoutConfirmation
		);
	}
}

/**** end: for datatable ****/

/**
 * Uploads the content of given form to given url
 *
 * @param form specifies the form to read data from
 * @param url specifies the URL to send the data to
 * @param onSuccess: function(XMLHttpRequest) to handle result
 */
function uploadFile(form, url, onSuccess) {
	var xhr = new XMLHttpRequest();
	var fd = new FormData(form);
	xhr.onreadystatechange = function(e) {
		if (this.readyState == 4) {
			if ((xhr.status != 200) && (xhr.status != 201)) {
				alert("Upload error occurred: " + xhr.status);
			} else {
				onSuccess(xhr);
			}
		}
	};
	xhr.open('post', url, true);
	xhr.send(fd);
}

/**
 * PUTs given value to the server in the BODY
 *
 * @param thing the thing to send. used as URL and in the error messages
 */
function updateValue(thing, value) {
	$.ajax({
		type: "PUT",
		async: false,
		url: thing,
		"data": value,
		dataType: "text",
		processData: false, // leads to a send in the body
		error: function(jqXHR, textStatus, errorThrown) {
			vShowAJAXError("Could not set " + thing, jqXHR, errorThrown);
		},
		success: function() {
			vShowSuccess("Successfully updated " + thing);
		}
	});
}

/**
 * Puts the color to visualappearance/{id}
 *
 * Required by visualappearance.jsp (node type and relation ship type)
 *
 * @param id
 */
function putColor(id, hex) {
	var dataToSend = {
		"color" : hex
	};
	$.ajax({
		type : "PUT",
		async : false,
		url : "visualappearance/" + id,
		"data" : dataToSend,
		dataType : "text",
		error : function(jqXHR, textStatus, errorThrown) {
			vShowError("Could not set color " + errorThrown);
		},
		success: function(data, textStatus, jqXHR) {
			vShowSuccess("Successfully updated color");
		}
	});
}