Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / webapp / js / winery-support-non-AMD.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 function addResourceInstance() {
14         if (highlightRequiredFields()) {
15                 vShowError("Please fill in all required fields");
16                 return;
17         }
18
19         var dataToSend = $('#createResourceForm').serialize();
20         var cr = $('#createResource');
21         $.ajax({
22                 type: "POST",
23                 async: false,
24                 "data": dataToSend,
25                 "url": cr.data("url"),
26                 dataType: "text",
27                 error: function(jqXHR, textStatus, errorThrown) {
28                         vShowAJAXError("Could not create resource", jqXHR, errorThrown);
29                         cr.modal("hide");
30                 },
31                 success: function(resData, textStatus, jqXHR) {
32                         cr.data("onSuccess")($('#createResourceForm').serializeArray(), resData, textStatus, jqXHR);
33                         cr.modal('hide');
34                 }
35         });
36 }
37
38 /**
39  * This function creates a dialog, where the user can add key/value pairs.
40  * These pairs are then sent to the given URL via POST.
41  *
42  * REQUIRES <script id="template-createresource" type="text/x-tmpl">
43  * Currently placed in header.jsp
44  *
45  * @param nameOfResource the name of the resource to add
46  * @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.
47  * @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
48  * @param onSuccess: function(serializedArray, data, textStatus, jqXHR) to call if adding has been successful. "serializedArray" contains the value of $('#formid').serializeArray()
49  */
50 function createResource(nameOfResource, fields, url, onSuccess) {
51         var cr = $('#createResource');
52         if (cr.length == 1) {
53                 if (cr.data("url") == url) {
54                         // the same dialog has been created before. Reuse it
55                         cr.modal("show");
56                         return;
57                 } else {
58                         // remove the dialog and thus enable the creation of a new one
59                         cr.remove();
60                 }
61         }
62
63         var data  = {
64                 nameOfResource: nameOfResource,
65                 fields: fields
66         };
67         require(["tmpl"], function(tmpl) {
68                 var div = tmpl("template-createresource", data);
69
70                 $("body").append(div);
71                 cr = $('#createResource');
72                 cr.on("shown.bs.modal", function() {
73                         $("#createResourceForm > fieldset > div:first-child > input").focus();
74                 });
75
76                 cr.modal('show');
77                 cr.data("url", url);
78                 cr.data("onSuccess", onSuccess);
79         });
80 }
81
82 /**
83  *
84  * @param selection jQuery selection object (<selection>)
85  * @param value the value of the text to add
86  * @param text the text to add
87  */
88 function addSortedSelectionItem(selection, value, text) {
89         var option = selection.children("option:first-child");
90         while ((option.length == 1) && (option.text() < text)) {
91                 option = option.next();
92         }
93         var toAppend = '<option value="' + value + '" selected="selected">' + text + '</option>';
94         if (option.length == 0) {
95                 selection.append(toAppend);
96         } else {
97                 option.before(toAppend);
98         }
99 }
100
101 /**** begin: for datatable ****/
102
103 /**
104  * Uses selected row as information for deleting on server (and on success deleting in table)
105  *
106  * the id of the thing to delete is read from the first column of the table
107  *
108  * @param tableInfo: info object about table
109  * @param nameOfThingToDelete: used at messages
110  * @param baseURL: used to form URL by baseURL+<name of thing>
111  * @param idColumn: (optional) column to look for the id. If not provided, look in the first column
112  * @param nameColumn: (optional) column to look for a name. If not provided, the id is used
113  * @param namespaceColumn: (optional) column to look for a namespace. If not provided, do not use any nameespace information
114  * @param withoutConfirmation (optional) if given, the resource is deleted without any confirmation
115 */
116 function deleteOnServerAndInTable(tableInfo, nameOfThingToDelete, baseURL, idColumn, nameColumn, namespaceColumn, withoutConfirmation) {
117         if (tableInfo.selectedRow == null) {
118                 vShowError("No row selected.");
119         } else {
120                 idColumn = idColumn || 0; // default: first column indicates identifier
121                 var id = tableInfo.table.fnGetData(tableInfo.selectedRow, idColumn);
122                 var name;
123                 if (typeof nameColumn === "undefined") {
124                         name = id;
125                 } else  {
126                         name = tableInfo.table.fnGetData(tableInfo.selectedRow, nameColumn);
127                 }
128
129                 var url = baseURL;
130                 if (typeof namespaceColumn !== "undefined") {
131                         var namespace = tableInfo.table.fnGetData(tableInfo.selectedRow, namespaceColumn);
132                         namespace = encodeID(namespace);
133                         url = url + namespace + '/';
134                 }
135                 // append the id
136                 // we could add a "/" to be compatible with Jersey's URL rewriting
137                 // However, that prevents deleting a thing being a leaf in the URL (e.g. a namespace)
138                 url = url + encodeID(id);
139
140                 // defined in winery-common.js
141                 deleteResource(nameOfThingToDelete + " " + name, url,
142                         function(data, textSTatus, jqXHR) {
143                                 tableInfo.table.fnDeleteRow(tableInfo.selectedRow);
144                                 tableInfo.selectedRow = null;
145                                 tableInfo.selectedTr = null;
146                         }, false, false, withoutConfirmation
147                 );
148         }
149 }
150
151 /**** end: for datatable ****/
152
153 /**
154  * Uploads the content of given form to given url
155  *
156  * @param form specifies the form to read data from
157  * @param url specifies the URL to send the data to
158  * @param onSuccess: function(XMLHttpRequest) to handle result
159  */
160 function uploadFile(form, url, onSuccess) {
161         var xhr = new XMLHttpRequest();
162         var fd = new FormData(form);
163         xhr.onreadystatechange = function(e) {
164                 if (this.readyState == 4) {
165                         if ((xhr.status != 200) && (xhr.status != 201)) {
166                                 alert("Upload error occurred: " + xhr.status);
167                         } else {
168                                 onSuccess(xhr);
169                         }
170                 }
171         };
172         xhr.open('post', url, true);
173         xhr.send(fd);
174 }
175
176 /**
177  * PUTs given value to the server in the BODY
178  *
179  * @param thing the thing to send. used as URL and in the error messages
180  */
181 function updateValue(thing, value) {
182         $.ajax({
183                 type: "PUT",
184                 async: false,
185                 url: thing,
186                 "data": value,
187                 dataType: "text",
188                 processData: false, // leads to a send in the body
189                 error: function(jqXHR, textStatus, errorThrown) {
190                         vShowAJAXError("Could not set " + thing, jqXHR, errorThrown);
191                 },
192                 success: function() {
193                         vShowSuccess("Successfully updated " + thing);
194                 }
195         });
196 }
197
198 /**
199  * Puts the color to visualappearance/{id}
200  *
201  * Required by visualappearance.jsp (node type and relation ship type)
202  *
203  * @param id
204  */
205 function putColor(id, hex) {
206         var dataToSend = {
207                 "color" : hex
208         };
209         $.ajax({
210                 type : "PUT",
211                 async : false,
212                 url : "visualappearance/" + id,
213                 "data" : dataToSend,
214                 dataType : "text",
215                 error : function(jqXHR, textStatus, errorThrown) {
216                         vShowError("Could not set color " + errorThrown);
217                 },
218                 success: function(data, textStatus, jqXHR) {
219                         vShowSuccess("Successfully updated color");
220                 }
221         });
222 }
223