app.js
9.7 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Set up global error handling
$().ready(function(){
$.ajaxSetup({
error:function(x,e){
if(x.status==0){
alert('You are offline!!\n Please Check Your Network.');
} else if(x.status==404){
alert('Requested URL not found.');
}else if(x.status==500){
alert('Internal Server Error.');
}else if(e=='parsererror'){
alert('Error.\nParsing JSON Request failed.');
}else if(e=='timeout'){
alert('Request Time out.');
}else {
alert('Unknown Error.\n'+x.responseText);
}
}
});
});
// Global index that is incremented at each appAddRow operation
var addRowIndex = 0;
var uispinServlet = 'uispin';
/**
* Called when the user clicks Add to add a new row to a widget.
* This takes the prototype with the given id and clones it,
* replacing the system Ids.
* @param id the id of the div containing the editor prototype
* @param single to hide the
*/
function appAddRow(id, single) {
var prototype = $('#' + id);
var clone = prototype.clone();
var uid = clone.find('.appEditorHiddenField').attr('name').substring(8);
var newUid = 'addedRowUid' + addRowIndex;
var initializer = null;
clone.attr('id', clone.attr('id') + '-' + addRowIndex);
clone.find('*').each(function(index, element) {
if($(element).hasClass('appDeleteRowButton')) {
$(element).removeAttr('onclick');
$(element).click(function() {
appDeleteRow(newUid);
});
}
if($(element).hasClass('appWidgetInitializer')) {
initializer = $(element);
}
$.each(this.attributes, function(i, attrib) {
var name = attrib.name;
var oldValue = attrib.value;
if(oldValue.indexOf(uid) != -1) {
var newValue = oldValue.replace(uid, newUid);
$(element).attr(name, newValue);
}
});
});
addRowIndex++;
clone.children().insertBefore(prototype.parent());
if(initializer) {
eval('(' + initializer.attr('content') + ')');
initializer.remove();
}
if(single) {
prototype.parent().parent().find('.appAddButtonDiv').each(function(i,item) {
$(item).css('display', 'none');
});
}
}
/**
* Called when the user clicks delete to delete a row from a widget.
* This completely deletes the element with the given id.
* The hidden fields to instruct the server that the given object
* shall be deleted are kept, and moved under the parent of the row.
* The function also makes sure that the add button is visible.
* @param id the id of the element to delete
*/
function appDeleteRow(id) {
var row = $('#' + id);
row.parent().parent().find('.appAddButtonDiv').each(function(index, item) {
$(item).css('display', '');
});
row.find('.appEditorHiddenField').appendTo(row.parent());
row.remove();
}
/**
* Turns an input element with a given id into a jQuery auto-complete.
* The actually selected resource will be returned to the server with
* a hidden field that needs to be passed into this function as well.
* @param id the id of the visible input element
* @param hiddenId the id of the hidden field
* @param link the link for the server callback
*/
function appInitAutoComplete(id, hiddenId, link) {
$('#' + id).autocomplete({
dataType : 'json',
source : link,
formatItem : function(item) {
return item.label;
},
select : function(event, ui) {
$('#' + hiddenId).val(ui.item.resource);
}
});
}
/**
* Turns an input element with a given widget id into a jQuery datepicker.
* @param id the uid of the widget
*/
function appInitDatePicker(id) {
$('#dateEditor' + id).datepicker({
altField: '#new-' + id,
altFormat: 'yy-mm-dd',
dateFormat: 'yy-mm-dd',
});
}
/**
* Replaces a form with another mode.
* This is for example called as handler of the Edit and Cancel buttons.
* @param formId the id of the form to replace
* @param mode the name of the new mode
* @param resourceURI the URI of the resource
* @param resourceTypeURI the URI of the resource type (optional)
* @param queryGraphURI the URI of the query graph
* @returns false to stop further processing
*/
function appLoadForm(formId, mode, resourceURI, resourceTypeURI, queryGraphURI) {
var data = {
formId: formId,
modeName: mode,
_viewClass: 'app:FormSelector',
_snippet: true
};
if(resourceURI) {
data.resource = '<' + resourceURI + '>';
}
if(resourceTypeURI) {
data.resourceType = '<' + resourceTypeURI + '>';
}
if(queryGraphURI) {
data._base = '<' + queryGraphURI + '>';
}
$.get(uispinServlet, data, function(data) {
$('#' + formId).parent().html(data);
});
return false;
}
/**
* Loads an edit form into a given element.
* @param parentId the id of the element to load into
* @param resourceType the class to edit
*/
function appLoadEditForm(parentId, resourceType) {
var data = {
modeName: 'edit',
resourceType: resourceType,
_viewClass: 'app:FormSelector',
_snippet: true
};
$.get(uispinServlet, data, function(data) {
$('#' + parentId).html(data);
});
return false;
}
/**
* Replaces a given element (specified by oldElementId) with a search form
* for a given resource type, using a specified query graph.
* @param oldElementId the id of the element to replace - this will be the id of the new form.
* @param resultsId the id of the element to display the search results
* @param resourceTypeURI the type of resources to search for
* @param queryGraphURI the (instance) query graph URI
* @param schemaGraphURI the URI of the schema graph
* @param noBorder (optional) to suppress the border
*/
function appLoadSearchForm(oldElementId, resultsId, resourceTypeURI, queryGraphURI, schemaGraphURI, noBorder) {
var data = {
formId : oldElementId,
resourceType : '<' + resourceTypeURI + '>',
resultsId : resultsId,
queryGraph : '<' + queryGraphURI + '>',
_base : '<' + schemaGraphURI + '>',
_viewClass : 'app:SearchForm',
_snippet : true
};
if(noBorder) {
data.noBorder = true;
}
$.get(uispinServlet, data, function(data) {
$('#' + oldElementId).parent().html(data);
});
}
/**
* Loads a SearchResultsGrid based on the selections in a form with
* a given id. Will replace the content of a given target element.
* @param formId the form id
* @param targetId the target id
* @returns false
*/
function appLoadSearchResultsGrid(formId, targetId) {
var params = $('#' + formId).serialize();
var escaped = '¶ms=' + escape(params);
$.get(uispinServlet + '?_viewClass=app:SearchResultsGrid&_snippet=true&' +
params + escaped, function(data) {
$('#' + targetId).html(data);
});
return false;
}
/**
* Changes the location (URL) of the current browser window to the
* URL of the uispin servlet with the default view of a given resource
* and a given context graph
* @param resourceURI the URI of the resource to navigate to
* @param queryGraphURI the URI of the current query graph
* @return false
*/
function appNavigateTo(resourceURI, queryGraphURI) {
window.location = uispinServlet + '?_base=' + escape(queryGraphURI) +
'&_resource=' + escape(resourceURI);
}
/**
* Opens a new tab or browser window with the URL of the uispin servlet
* with the default view of a given resource and a given context graph
* @param resourceURI the URI of the resource to navigate to
* @param queryGraphURI the URI of the current query graph
*/
function appNavigateToInTab(resourceURI, queryGraphURI) {
window.open('uispinServlet' + '?_base=' + escape(queryGraphURI) +
'&_resource=' + escape(resourceURI));
}
/**
* Walks up the parents of a 'link' element with a given id until it
* finds a form. Then it replaces that form with a new form displaying
* a given resource.
* @param resourceURI the URI of the resource to load
* @param queryGraphURI the current query graph
* @param linkElement an element that is contained within the form
*/
function appReloadForm(resourceURI, queryGraphURI, linkElementId) {
var form = $('#' + linkElementId).closest('.appForm');
if(!form) {
alert('appReloadForm can only be called within an .appForm');
return;
}
var id = form.attr('id');
appLoadForm(id, 'view', resourceURI, null, queryGraphURI);
}
/**
* Submits a form and switches it to viewing mode when done.
* @param form the id of the form
* @param servlet the optional name of the servlet
* @returns false to stop further processing
*/
function appSubmitForm(form, servlet) {
if(!servlet) {
servlet = 'swpEdit';
}
var params = $(form).serialize();
$.getJSON(servlet + '?' + params, function(data) {
if(data.errors) {
var msg = data.errors.length + ' Constraint violations:';
$.each(data.errors, function(index, item) {
msg += '\n - ' + item.message;
});
appUpdateFormErrors(form, data.errors);
alert(msg);
}
else {
var root = data.rootResource;
appLoadForm(form, 'view', root);
}
});
return false;
}
/**
* Displays or clears the error indicators on a given form, based
* on an array of errors (as produced by the SPIN constraint validation)
* @param form the id of the form containing the indicators
* @param errors the errors to display
*/
function appUpdateFormErrors(form, errors) {
$(form).find('.appErrorIndicator').each(function(index, itemElement) {
var item = $(itemElement);
var e = false;
$.each(errors, function(i, error) {
if(item.attr('id') == 'error-' + error.path) {
e = true;
item.addClass('ui-icon');
item.addClass('ui-icon-alert');
item.attr('title', error.message);
}
});
if(!e) {
item.removeClass('ui-icon');
item.removeClass('ui-icon-alert');
item.removeAttr('title');
}
});
}
/**
* Validates a form and updates the errors.
* @param form the id of the form
* @returns false to stop further processing
*/
function appValidateForm(form) {
var params = $(form).serialize();
$.getJSON('swpEdit?validateOnly=true&' + params, function(data) {
if(data.errors) {
appUpdateFormErrors(form, data.errors);
}
else {
appUpdateFormErrors(form, []);
}
});
return false;
}