blob: 581fb5ff13757f1ae692329d44f82682a16c6dbd [file] [log] [blame]
Felix Meschbergerb812b2e2010-02-18 15:36:53 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
Carsten Ziegeler149faaa2010-03-10 16:41:53 +000017// tables container - will get hidden, when no config service available
18var configContent = false;
19
20// config table list
21var configTable = false;
22var configBody = false;
23var configRow = false;
24
25// factories table list
Carsten Ziegeler149faaa2010-03-10 16:41:53 +000026var factoryBody = false;
27var factoryRow = false;
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000028
29
Carsten Ziegeler149faaa2010-03-10 16:41:53 +000030// editor dialog
31var editor = false;
Valentin Valchevde160882010-03-22 09:30:38 +000032var editorMessage = false;
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000033
Carsten Ziegeler149faaa2010-03-10 16:41:53 +000034function configure(pid, create) {
35 var uri = pluginRoot + '/' + pid;
Carsten Ziegelerd2637c32012-12-11 17:46:47 +000036 // we have to add a cache killer for IE8
37 var postUri = uri + '?';
38 if ( create ) {
Felix Meschberger27870e82012-12-28 15:19:38 +000039 postUri += param.create + '=1&';
Carsten Ziegelerd2637c32012-12-11 17:46:47 +000040 }
41 postUri = postUri + 'ts='+new Date().getMilliseconds();
42 $.post(postUri, null, displayConfigForm, 'json');
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000043}
44
45function displayConfigForm(obj) {
Carsten Ziegeler149faaa2010-03-10 16:41:53 +000046 var parent = document.getElementById('editorTable');
47 clearChildren( parent )
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000048
Carsten Ziegeler149faaa2010-03-10 16:41:53 +000049 var trEl = tr( );
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000050 parent.appendChild( trEl );
51
Carsten Ziegeler149faaa2010-03-10 16:41:53 +000052 var tdEl = td( null, { colSpan: "2" } );
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000053 trEl.appendChild( tdEl );
54
55 var formEl = createElement( "form", null, {
Carsten Ziegeler149faaa2010-03-10 16:41:53 +000056 id : "editorForm",
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000057 method: "POST",
58 action: pluginRoot + "/" + obj.pid
59 });
60 tdEl.appendChild( formEl );
61
62 var inputEl = createElement( "input", null, {
63 type: "hidden",
Felix Meschberger27870e82012-12-28 15:19:38 +000064 name: param.apply,
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000065 value: "true"
66 });
67 formEl.appendChild( inputEl );
68
69 // add the factory PID as a hidden form field if present
70 if (obj.factoryPid)
71 {
72 inputEl = createElement( "input", null, {
73 type: "hidden",
74 name: "factoryPid",
75 value: obj.factoryPid
76 });
77 formEl.appendChild( inputEl );
78 }
79
80 // add the PID filter as a hidden form field if present
Felix Meschberger27870e82012-12-28 15:19:38 +000081 if (obj[ param.pidFilter ])
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000082 {
83 inputEl = createElement( "input", null, {
84 type: "hidden",
Felix Meschberger27870e82012-12-28 15:19:38 +000085 name: param.pidFilter,
Felix Meschbergerb812b2e2010-02-18 15:36:53 +000086 value: obj.pidFilter
87 });
88 formEl.appendChild( inputEl );
89 }
90
91 inputEl = createElement( "input", null, {
92 type: "hidden",
93 name: "action",
94 value: "ajaxConfigManager"
95 });
96 formEl.appendChild( inputEl );
97
98 var tableEl = createElement( "table", null, {
99 border: 0,
100 width: "100%"
101 });
102 formEl.appendChild( tableEl );
103
104 var bodyEl = createElement( "tbody" );
105 tableEl.appendChild( bodyEl );
106
107 if (obj.description)
108 {
109 trEl = tr( );
110 tdEl = td( null, { colSpan: "2" } );
111 addText( tdEl, obj.description );
112 trEl.appendChild( tdEl );
113 bodyEl.appendChild( trEl );
114 }
115
Felix Meschbergera87e8592011-12-17 16:19:41 +0000116 if (obj.properties)
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000117 {
Felix Meschbergera87e8592011-12-17 16:19:41 +0000118 printForm(bodyEl, obj.properties);
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000119 }
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000120
121 printConfigurationInfo(parent, obj);
Valentin Valchevde160882010-03-22 09:30:38 +0000122 if ( obj.service_location && obj.bundle_location && obj.service_location != obj.bundle_location) {
123 editorMessage.removeClass('ui-helper-hidden').text(i18n.err_bind.msgFormat(obj.pid, obj.bundle_location, obj.service_location));
124 } else editorMessage.addClass('ui-helper-hidden');
Valentin Valchev0fc8b7b2010-04-15 12:33:58 +0000125
126 // ugly workaround for IE6 and IE7 - these browsers don't show correctly the dialog
127 var ua = navigator.userAgent;
128 if (ua.indexOf('MSIE 6') != -1 || ua.indexOf('MSIE 7') != -1) $(parent).html(parent.innerHTML);
129
Felix Meschberger07741202011-07-07 11:55:53 +0000130 initStaticWidgets(editor
131 .attr('__pid', obj.pid)
132 .attr('__location', obj.bundleLocation?obj.bundleLocation:'')
133 .dialog('option', 'title', obj.title)
134 .dialog('open'));
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000135}
136
Felix Meschbergera87e8592011-12-17 16:19:41 +0000137function printForm( /* Element */ parent, /* Object */ properties ) {
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000138 var propList;
Felix Meschbergera87e8592011-12-17 16:19:41 +0000139 for (var prop in properties)
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000140 {
Felix Meschbergera87e8592011-12-17 16:19:41 +0000141 var attr = properties[prop];
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000142
143 var trEl = tr( null, null, [
144 td( null, null, [ text( attr.name ) ] )
145 ]);
146 parent.appendChild( trEl );
147
148 var tdEl = td( null, { style: { width: "99%" } } );
149 trEl.appendChild( tdEl );
150
151 if (attr.value != undefined)
152 {
153 // check is required to also handle empty strings, 0 and false
154 tdEl.appendChild( createInput( prop, attr.value, attr.type, '99%' ) );
155 tdEl.appendChild( createElement( "br" ) );
156 }
157 else if (typeof(attr.type) == 'object')
158 {
159 // assume attr.values and multiselect
160 createMultiSelect( tdEl, prop, attr.values, attr.type, '99%' );
161 tdEl.appendChild( createElement( "br" ) );
162 }
163 else if (attr.values.length == 0)
164 {
165 tdEl.appendChild( createSpan( prop, "", attr.type ) );
166 }
167 else
168 {
169 for (var i=0;i<attr.values.length;i++)
170 {
171 tdEl.appendChild( createSpan( prop, attr.values[i], attr.type ) );
172 }
173 }
174
175 if (attr.description)
176 {
177 addText( tdEl, attr.description );
178 }
179
180 if (propList) {
181 propList += ',' + prop;
182 } else {
183 propList = prop;
184 }
185 }
186
187 parent.appendChild( createElement( "input", null, {
188 type: "hidden",
Felix Meschberger27870e82012-12-28 15:19:38 +0000189 name: param.propertylist,
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000190 value: propList
191 })
192 );
Felix Meschbergera87e8592011-12-17 16:19:41 +0000193
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000194 // FIX for IE6 and above: checkbox can only be checked after it is in the DOM
195 $(".checked_box").attr("checked", true).removeClass("checked_box");
196}
197
198function printConfigurationInfo( /* Element */ parent, obj )
199{
200 parent.appendChild( tr( null, null, [
201 createElement( "th", null, { colSpan: "2" }, [
202 text( i18n.cfg_title )
203 ])
204 ])
205 );
206
207 parent.appendChild( tr( null, null, [
208 td( null, null, [
209 text( i18n.pid )
210 ]),
211 td( null, null, [
212 text( obj.pid )
213 ])
214 ])
215 );
216
217 if (obj.factoryPid)
218 {
219 parent.appendChild( tr( null, null, [
220 td( null, null, [
221 text( i18n.fpid )
222 ]),
223 td( null, null, [
224 text( obj.factoryPid )
225 ])
226 ])
227 );
228 }
229
230 var binding = obj.bundleLocation;
231 if (!binding)
232 {
233 binding = i18n.unbound;
234 }
235
236 parent.appendChild( tr( null, null, [
237 td( null, null, [
238 text( i18n.binding )
239 ]),
240 td( null, null, [
241 text( binding )
242 ])
243 ])
244 );
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000245
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000246}
247
248
249var spanCounter = 0;
250/* Element */ function createSpan(prop, value, type) {
251 spanCounter++;
252 var newId = prop + spanCounter;
253
254 var addButton = createElement("input", null,
255 { type: "button",
256 style: {width : "5%"},
257 value: "+"
258 }
259 );
260 $(addButton).click(function() {addValue(prop, newId)});
261 var remButton = createElement("input", null,
262 { type: "button",
263 style: {width : "5%"},
264 value: "-"
265 }
266 );
267 $(remButton).click(function() {removeValue(newId)});
268 var spanEl = createElement( "span", null, { id: newId }, [
269 createInput( prop, value, type, '89%' ), addButton, remButton,
270 createElement("br")
271 ]);
272
273 return spanEl;
274}
275
276/* Element */ function createInput(prop, value, type, width) {
277 if (type == 11) { // AttributeDefinition.BOOLEAN
278
279 var inputEl = createElement( "input", null, {
280 type: "checkbox",
281 name: prop,
282 value: "true"
283 });
284
285 if (value && typeof(value) != "boolean")
286 {
287 value = value.toString().toLowerCase() == "true";
288 }
289 if (value)
290 {
291 $(inputEl).addClass("checked_box");
292 }
293 var hiddenEl = createElement( "input", null, {
294 type: "hidden",
295 name: prop,
296 value: "false"
297 });
298 var divEl = createElement("div");
299 divEl.appendChild(inputEl);
300 divEl.appendChild(hiddenEl);
301 return divEl;
302
303 } else if (typeof(type) == "object") { // predefined values
304
305 var selectEl = createElement( "select", null, {
306 name: prop,
307 style: { width: width }
308 });
309
310 var labels = type.labels;
311 var values = type.values;
312 for (var idx in labels) {
313 var optionEl = createElement( "option", null, {
314 value: values[idx]
315 }, [ text( labels[idx] ) ]);
316
317 if (value == values[idx])
318 {
319 optionEl.setAttribute( "selected", true );
320 }
321 selectEl.appendChild( optionEl );
322 }
323
324 return selectEl;
325
326 } else { // Simple
Felix Meschbergerd5fe6b62011-12-17 15:26:02 +0000327 // Metatype 1.2: Attr type 12 is PASSWORD
328 var elType = (type == 12) ? "password" : "text";
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000329 return createElement( "input", null, {
Felix Meschbergerd5fe6b62011-12-17 15:26:02 +0000330 type: elType,
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000331 name: prop,
332 value: value,
333 style: { width: width }
334 });
335 }
336}
337
338function createMultiSelect(/* Element */ parent, prop, values, options, width) {
339 // convert value list into 'set'
340 var valueSet = new Object();
341 for (var idx in values) {
342 valueSet[ values[idx] ] = true;
343 }
344
345 var labels = options.labels;
346 var values = options.values;
347 for (var idx in labels) {
348
349 var inputEl = createElement( "input", null, {
350 type: "checkbox",
351 name: prop,
352 value: values[idx]
353 });
354
355 if (valueSet[ values[idx] ])
356 {
357 inputEl.setAttribute( "checked", true );
358 }
359
360 var labelEl = createElement( "label", "multiselect" );
361 labelEl.appendChild( inputEl );
362 addText( labelEl, labels[idx] );
363
364 parent.appendChild( labelEl );
365 }
366}
367
368
369function addValue(prop, vidx)
370{
371 var span = document.getElementById(vidx);
372 if (!span)
373 {
374 return;
375 }
376 var newSpan = createSpan(prop, '');
377 span.parentNode.insertBefore(newSpan, span.nextSibling);
378 // FIX for IE6 and above: checkbox can only be checked after it is in the DOM
379 $(".checked_box").attr("checked", true).removeClass("checked_box");
380 //$(span).ready(initStaticWidgets);
381}
382
383function removeValue(vidx)
384{
385 var span = document.getElementById(vidx);
386 if (!span)
387 {
388 return;
389 }
390 span.parentNode.removeChild(span);
391}
392
393function configConfirm(/* String */ message, /* String */ title, /* String */ location)
394{
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000395 if (title) {
396 message += "\r\n" + i18n.del_config + title;
397 }
398 if (location) {
399 message += "\r\n" + i18n.del_bundle + location;
400 }
401
402 return confirm(message);
403}
404
Felix Meschberger07741202011-07-07 11:55:53 +0000405function deleteConfig(/* String */ configId, /* String */ bundleLocation)
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000406{
Felix Meschberger07741202011-07-07 11:55:53 +0000407 if ( configConfirm(i18n.del_ask, configId, bundleLocation) ) {
Felix Meschberger27870e82012-12-28 15:19:38 +0000408 $.post(pluginRoot + '/' + configId + '?' + param.apply + '=1&' + param.dele + '=1', null, function() {
Felix Meschberger07741202011-07-07 11:55:53 +0000409 document.location.href = pluginRoot;
410 }, 'json');
411 return true;
412 }
413 return false;
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000414}
415
Felix Meschberger07741202011-07-07 11:55:53 +0000416function unbindConfig(/* String */ configId, /* String */ bundleLocation)
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000417{
Felix Meschberger07741202011-07-07 11:55:53 +0000418 if ( configConfirm(i18n.unbind_ask, configId, bundleLocation) ) {
Felix Meschberger27870e82012-12-28 15:19:38 +0000419 $.post(pluginRoot + '/' + configId + '?' + param.unbind + '=1', null, function() {
Felix Meschberger07741202011-07-07 11:55:53 +0000420 document.location.href = pluginRoot + '/' + configId;
421 }, 'json');
422 return true;
423 }
424 return false;
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000425}
426
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000427function addConfig(conf) {
428 var tr = configRow.clone().appendTo(configBody);
Felix Meschberger67726fe2010-03-17 06:55:00 +0000429
430 // rendering name - indented if factory pid is set
Valentin Valchev0fc8b7b2010-04-15 12:33:58 +0000431 var nms = tr.find('td:eq(0) div');
Felix Meschberger67726fe2010-03-17 06:55:00 +0000432 if (conf.fpid) {
433 nms.after(conf.id);
434 tr.attr('fpid', conf.name);
435 } else {
436 nms.addClass('ui-helper-hidden').parent().text(conf.name);
437 }
438
439 tr.find('td:eq(0)').click(function() { // name & edit
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000440 configure(conf.id);
441 });
Felix Meschberger67726fe2010-03-17 06:55:00 +0000442 tr.find('td:eq(1)').html(conf.bundle ? '<a href="' + pluginRoot + '/../bundles/' + conf.bundle + '">' + conf.bundle_name + '</a>' : '-'); // binding
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000443
444 // buttons
445 tr.find('li:eq(0)').click(function() { // edit
446 configure(conf.id);
447 });
448 tr.find('li:eq(2)').click(function() { // delete
Felix Meschberger07741202011-07-07 11:55:53 +0000449 deleteConfig(conf.id, conf.bundle_name);
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000450 });
Felix Meschberger07741202011-07-07 11:55:53 +0000451 if (conf.bundle) {
452 tr.find('li:eq(1)').click(function() { // unbind
453 unbindConfig(conf.id, conf.bundle_name);
454 }).removeClass('ui-state-disabled');
455 }
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000456}
457
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000458function addFactoryConfig(conf) {
Felix Meschberger67726fe2010-03-17 06:55:00 +0000459 var tr = factoryRow.clone().appendTo(configTable).attr('fpid', conf.name);
460 //tr.find('td:eq(1)').text(conf.id); // fpid
Felix Meschberger1e671e12010-03-13 15:09:51 +0000461 tr.find('td:eq(0)').text(conf.name).click(function() { // name & edit
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000462 configure(conf.id, true);
463 });
464 // buttons
465 tr.find('li:eq(0)').click(function() { // edit
466 configure(conf.id, true);
467 });
468}
469
Felix Meschberger67726fe2010-03-17 06:55:00 +0000470function treetableExtraction(node) {
471 var td = $(node);
472 var text = td.text();
473 if (!text) return text;
474
475 // current sort order
476 var desc = $(this)[0].sortList[0][1];
477
478 var row = td.parent();
479 var fpid = row.attr('fpid');
480
481 // factory row
482 if ( row.hasClass('fpid') && fpid) return fpid + (desc==0?1:0) + text;
483
484 // bundle or name row
485 if ( fpid ) return fpid + desc + text;
486
487 return mixedLinksExtraction(node);
488};
489
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000490$(document).ready(function() {
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000491 configContent = $('#configContent');
492 // config table list
Felix Meschberger67726fe2010-03-17 06:55:00 +0000493 configTable = $('#configTable');
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000494 configBody = configTable.find('tbody');
Felix Meschberger67726fe2010-03-17 06:55:00 +0000495 configRow = configBody.find('tr:eq(0)').clone();
496 factoryRow = configBody.find('tr:eq(1)').clone();
Felix Meschberger1e671e12010-03-13 15:09:51 +0000497
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000498 // setup button - cannot inline in dialog option because of i18n
499 var _buttons = {};
500 _buttons[i18n.abort] = function() {
501 $(this).dialog('close');
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000502 }
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000503 _buttons[i18n.reset] = function() {
504 var form = document.getElementById('editorForm');
505 if (form) form.reset();
506 }
Felix Meschberger07741202011-07-07 11:55:53 +0000507 _buttons[i18n.del] = function() {
508 if (deleteConfig($(this).attr('__pid'), $(this).attr('__location'))) {
509 $(this).dialog('close');
510 }
511 }
512 _buttons[i18n.unbind_btn] = function() {
513 unbindConfig($(this).attr('__pid'), $(this).attr('__location'));
514 }
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000515 _buttons[i18n.save] = function() {
Valentin Valchev4b25d4f2011-09-15 07:06:36 +0000516 $.post(pluginRoot + '/' + $(this).attr('__pid') + '?' + $(this).find('form').serialize(), function() {
517 // reload on success - prevents AJAX errors - see FELIX-3116
518 document.location.href = pluginRoot;
519 });
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000520 $(this).dialog('close');
521 }
522 // prepare editor, but don't open yet!
523 editor = $('#editor').dialog({
524 autoOpen : false,
525 modal : true,
526 width : '90%',
527 closeText: i18n.abort,
528 buttons : _buttons
529 });
Valentin Valchevde160882010-03-22 09:30:38 +0000530 editorMessage = editor.find('p');
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000531
532 // display the configuration data
533 $(".statline").html(configData.status ? i18n.stat_ok : i18n.stat_missing);
534 if (configData.status) {
Felix Meschberger67726fe2010-03-17 06:55:00 +0000535 configBody.empty();
536 var factories = {};
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000537
Felix Meschberger67726fe2010-03-17 06:55:00 +0000538 for(var i in configData.pids) {
539 var c = configData.pids[i];
540 if (c.fpid) {
541 if (!factories[c.fpid]) factories[c.fpid] = new Array();
542 factories[c.fpid].push(c);
543 } else {
544 addConfig(c);
545 }
546 }
547 for(var i in configData.fpids) {
548 addFactoryConfig(configData.fpids[i]);
Felix Meschberger1e671e12010-03-13 15:09:51 +0000549
Felix Meschbergerb58af942011-12-19 12:54:03 +0000550 var fpid = configData.fpids[i].id;
551 var confs = factories[ fpid ];
552 if (confs) {
553 for (var j in confs) {
554 addConfig(confs[j]);
555 }
556 delete factories[ fpid ];
Felix Meschberger67726fe2010-03-17 06:55:00 +0000557 }
558 }
Felix Meschbergerb58af942011-12-19 12:54:03 +0000559 for(var fpid in factories) {
560 var flist = factories[fpid];
561 for(var i in flist) {
562 delete flist[i].fpid; // render as regular config
563 addConfig(flist[i]);
564 }
565 }
Felix Meschberger67726fe2010-03-17 06:55:00 +0000566 initStaticWidgets(configTable);
567
568 // init tablesorte
569 configTable.tablesorter({
570 headers: { 2: { sorter: false } },
571 sortList: [[0,1]],
572 textExtraction: treetableExtraction
573 }).bind('sortStart', function() { // clear cache, otherwse extraction will not work
574 var table = $(this).trigger('update');
575 }).find('th:eq(0)').click();
Carsten Ziegeler149faaa2010-03-10 16:41:53 +0000576 } else {
577 configContent.addClass('ui-helper-hidden');
578 }
579 if (selectedPid) configure(selectedPid);
Felix Meschbergerb812b2e2010-02-18 15:36:53 +0000580});