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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* log message in the console
*/
$.log = (function(message) {
if ( !window.archivaJavascriptLog ){
return;
}
if (typeof window.console != 'undefined' && typeof window.console.log != 'undefined') {
console.log(message);
} else {
// do nothing no console
}
});
/**
* return value of a param in the url
* @param name
*/
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results) {
return results[1] || 0;
}
return null;
}
/**
* display a success message
* @param text the success text
* @param idToAppend the id to append the success box
*/
displaySuccessMessage=function(text,idToAppend){
var textId = idToAppend ? $("#"+idToAppend) : $("#user-messages");
$.tmpl($("#alert-message-success").html(), { "message" : text }).appendTo( textId );
$(textId).focus();
}
/**
* display an error message
* @param text the success text
* @param idToAppend the id to append the success box
*/
displayErrorMessage=function(text,idToAppend){
var textId = idToAppend ? $("#"+idToAppend) : $("#user-messages");
$.tmpl($("#alert-message-error").html(), { "message" : text }).appendTo( textId );
$(textId).focus();
}
/**
* display a warning message
* @param text the success text
* @param idToAppend the id to append the success box
*/
displayWarningMessage=function(text,idToAppend){
var textId = idToAppend ? $("#"+idToAppend) : $("#user-messages");
$.tmpl($("#alert-message-warning").html(), { "message" : text }).appendTo( textId );
$(textId).focus();
}
displayInfoMessage=function(text,idToAppend){
var textId = idToAppend ? $("#"+idToAppend) : $("#user-messages");
$.tmpl($("#alert-message-info").html(), { "message" : text }).appendTo( textId );
$(textId).focus();
}
/**
* clear #main-content and call clearUserMessages
*/
screenChange=function(){
$("#main-content").html("");
$("#main-content").removeAttr("data-bind");
clearUserMessages();
}
/**
* clear content of id if none clear content of #user-messages
* @param idToAppend
*/
clearUserMessages=function(idToAppend){
var textId = idToAppend ? $("#"+idToAppend) : $("#user-messages");
$(textId).html('');
}
/**
* clear all input text and password found in the the selector
* @param selectorStr
*/
clearForm=function(selectorStr){
$(selectorStr).find("input[type='text']").each(function(ele){
$(this).val("");
});
$(selectorStr).find("input[type='password']").each(function(ele){
$(this).val("");
});
}
/**
* open a confirm dialog based on bootstrap modal
* @param okFn callback function to call on ok confirm
* @param okMessage
* @param cancelMessage
* @param title
*/
openDialogConfirm=function(okFn, okMessage, cancelMessage, title,bodyText){
if (window.modalConfirmDialog==null) {
window.modalConfirmDialog = $("#dialog-confirm-modal").modal();//{backdrop:'static',show:false}
window.modalConfirmDialog.bind('hidden', function () {
$("#dialog-confirm-modal-header-title").html("");
$("#dialog-confirm-modal-body-text").html("");
})
$("#dialog-confirm-modal-cancel").on("click", function(){
window.modalConfirmDialog.modal('hide');
});
}
$("#dialog-confirm-modal-header-title").html(title);
$("#dialog-confirm-modal-body-text").html(bodyText);
if (okMessage){
$("#dialog-confirm-modal-ok").html(okMessage);
}
if (cancelMessage){
$("#dialog-confirm-modal-cancel").html(cancelMessage);
}
window.modalConfirmDialog.modal('show');
// unbind previous events !!
$("#dialog-confirm-modal-ok").off( );
$("#dialog-confirm-modal-ok").on("click", okFn);
}
/**
* return a small spinner html img element
*/
smallSpinnerImg=function(){
return "<img id=\"small-spinner\" src=\"images/small-spinner.gif\"/>";
};
removeSmallSpinnerImg=function(){
$("#small-spinner").remove();
}
mediumSpinnerImg=function(){
return "<img id=\"medium-spinner\" src=\"images/medium-spinner.gif\"/>";
};
removeMediumSpinnerImg=function(){
$("#medium-spinner").remove();
}
removeMediumSpinnerImg=function(selector){
$(selector+" #medium-spinner").remove();
}
closeDialogConfirm=function(){
window.modalConfirmDialog.modal('hide');
}
closeDialogConfirmui=function(){
$("#dialog-confirm" ).dialog("close");
}
/**
* open a confirm dialog with jqueryui
* @param okFn callback function to call on ok confirm
* @param okMessage
* @param cancelMessage
* @param title
*/
openDialogConfirmui=function(okFn, okMessage, cancelMessage, title){
$("#dialog-confirm" ).dialog({
resizable: false,
title: title,
modal: true,
show: 'slide',
buttons: [{
text: okMessage,
click: okFn},
{
text: cancelMessage,
click:function() {
$(this).dialog( "close" );
}
}]
});
}
mapStringArray=function(data){
if (data) {
if ($.isArray(data)){
return $.map(data,function(item){
return item;
});
} else {
return new Array(data);
}
}
return null;
}
/**
* display redback error from redback json error response
* {"redbackRestError":{"errorMessages":{"args":1,"errorKey":"user.password.violation.numeric"}}}
* @param obj
* @param idToAppend
*/
displayRedbackError=function(obj,idToAppend) {
if ($.isArray(obj.errorMessages)) {
$.log("displayRedbackError with array");
for(var i=0; i<obj.errorMessages.length; i++ ) {
if(obj.errorMessages[i].errorKey) {
$.log("displayRedbackError with array loop");
displayErrorMessage($.i18n.prop( obj.errorMessages[i].errorKey, obj.errorMessages[i].args ),idToAppend);
}
}
} else {
$.log("displayRedbackError no array");
displayErrorMessage($.i18n.prop( obj.errorMessages.errorKey, obj.errorMessages.args ),idToAppend);
}
}
/*
* generic function to display error return by rest service
* if fieldName is here the function will try to find a field with this name and add a span on it
* if not error is displayed in #user-messages div
*/
displayRestError=function(data,idToAppend){
if (data.redbackRestError){
displayRedbackError(archivaRestError,idToAppend)
}
// if we have the fieldName display error on it
if (data && data.fieldName){
var mainContent=$("#main-content");
if (mainContent.find("#"+data.fieldName)){
var message=null;
if (data.errorKey) {
message=$.i18n.prop('data.errorKey');
} else {
message=data.errorMessage;
}
mainContent.find("div.clearfix" ).removeClass( "error" );
mainContent.find("span.help-inline" ).remove();
mainContent.find("#"+data.fieldName).parents( "div.clearfix" ).addClass( "error" );
mainContent.find("#"+data.fieldName).parent().append( "<span class=\"help-inline\">" + message + "</span>" );
return;
}
// we don't have any id with this fieldName so continue
}
if (data.errorKey && data.errorKey.length>0){
displayErrorMessage($.i18n.prop( data.errorKey ),idToAppend);
} else {
$.log("data.errorMessage:"+data.errorMessage);
displayErrorMessage(data.errorMessage,idToAppend);
}
}
/**
* used by validation error to customize error display in the ui
* @param selector
* @param validator
* @param errorMap
* @param errorList
*/
customShowError=function(selector, validator, errorMap, errorList) {
$.isFunction(selector)? selector.find("div.control-group" ).removeClass( "error" ):$(selector).find("div.control-group" ).removeClass( "error" );
$.isFunction(selector)? selector.find("span.help-inline").remove():$(selector).find("span.help-inline").remove();
for ( var i = 0; errorList[i]; i++ ) {
var error = errorList[i];
var field = $("#"+error.element.id);
field.parents( "div.control-group" ).addClass( "error" );
field.parent().append( "<span class=\"help-inline\">" + error.message + "</span>" );
}
}
timestampNoCache=function(){
if (!window.archivaDevMode){
return "";
}
return "&_="+jQuery.now();
}
appendTemplateUrl=function(){
return "?"+appendArchivaVersion()+timestampNoCache();
}
/**
* mapping for a java Map entry
* @param key
* @param value
*/
Entry=function(key,value){
var self=this;
this.key=ko.observable(key);
//this.key.subscribe(function(newValue){self.modified(true)});
this.value=ko.observable(value);
//this.value.subscribe(function(newValue){self.modified(true)});
}
/**
* map {"strings":["snapshots","internal"]} to an array
* @param data
*/
mapStringList=function(data){
if (data && data.strings){
return $.isArray(data.strings) ?
$.map(data.strings,function(item){return item}): [data.strings];
}
return [];
}
/**
* return an array with removing duplicate strings
* @param strArray an array of string
* @param sorted to sort or not
*/
unifyArray=function(strArray,sorted){
var res = [];
$(strArray).each(function(idx,str){
if ( $.inArray(str,res)<0){
res.push(str);
}
});
return sorted?res.sort():res;
}
// utils
String.prototype.endsWith = function(str) {
return (this.match(str+"$")==str)
}
String.prototype.startsWith = function(str) {
return (this.match("^"+str)==str)
}
String.prototype.substringBeforeLast = function(str) {
return this.substring(0,this.lastIndexOf(str));
}
// extends jquery tmpl to support var def
$.extend($.tmpl.tag, {
"var": {
open: "var $1;"
}
});
|