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
10 * Oliver Kopp - initial API and implementation and/or initial documentation
11 *******************************************************************************/
13 function addResourceInstance() {
14 if (highlightRequiredFields()) {
15 vShowError("Please fill in all required fields");
19 var dataToSend = $('#createResourceForm').serialize();
20 var cr = $('#createResource');
25 "url": cr.data("url"),
27 error: function(jqXHR, textStatus, errorThrown) {
28 vShowAJAXError("Could not create resource", jqXHR, errorThrown);
31 success: function(resData, textStatus, jqXHR) {
32 cr.data("onSuccess")($('#createResourceForm').serializeArray(), resData, textStatus, jqXHR);
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.
42 * REQUIRES <script id="template-createresource" type="text/x-tmpl">
43 * Currently placed in header.jsp
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()
50 function createResource(nameOfResource, fields, url, onSuccess) {
51 var cr = $('#createResource');
53 if (cr.data("url") == url) {
54 // the same dialog has been created before. Reuse it
58 // remove the dialog and thus enable the creation of a new one
64 nameOfResource: nameOfResource,
67 require(["tmpl"], function(tmpl) {
68 var div = tmpl("template-createresource", data);
70 $("body").append(div);
71 cr = $('#createResource');
72 cr.on("shown.bs.modal", function() {
73 $("#createResourceForm > fieldset > div:first-child > input").focus();
78 cr.data("onSuccess", onSuccess);
84 * @param selection jQuery selection object (<selection>)
85 * @param value the value of the text to add
86 * @param text the text to add
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();
93 var toAppend = '<option value="' + value + '" selected="selected">' + text + '</option>';
94 if (option.length == 0) {
95 selection.append(toAppend);
97 option.before(toAppend);
101 /**** begin: for datatable ****/
104 * Uses selected row as information for deleting on server (and on success deleting in table)
106 * the id of the thing to delete is read from the first column of the table
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
116 function deleteOnServerAndInTable(tableInfo, nameOfThingToDelete, baseURL, idColumn, nameColumn, namespaceColumn, withoutConfirmation) {
117 if (tableInfo.selectedRow == null) {
118 vShowError("No row selected.");
120 idColumn = idColumn || 0; // default: first column indicates identifier
121 var id = tableInfo.table.fnGetData(tableInfo.selectedRow, idColumn);
123 if (typeof nameColumn === "undefined") {
126 name = tableInfo.table.fnGetData(tableInfo.selectedRow, nameColumn);
130 if (typeof namespaceColumn !== "undefined") {
131 var namespace = tableInfo.table.fnGetData(tableInfo.selectedRow, namespaceColumn);
132 namespace = encodeID(namespace);
133 url = url + namespace + '/';
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);
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
151 /**** end: for datatable ****/
154 * Uploads the content of given form to given url
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
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);
172 xhr.open('post', url, true);
177 * PUTs given value to the server in the BODY
179 * @param thing the thing to send. used as URL and in the error messages
181 function updateValue(thing, value) {
188 processData: false, // leads to a send in the body
189 error: function(jqXHR, textStatus, errorThrown) {
190 vShowAJAXError("Could not set " + thing, jqXHR, errorThrown);
192 success: function() {
193 vShowSuccess("Successfully updated " + thing);
199 * Puts the color to visualappearance/{id}
201 * Required by visualappearance.jsp (node type and relation ship type)
205 function putColor(id, hex) {
212 url : "visualappearance/" + id,
215 error : function(jqXHR, textStatus, errorThrown) {
216 vShowError("Could not set color " + errorThrown);
218 success: function(data, textStatus, jqXHR) {
219 vShowSuccess("Successfully updated color");