Felix Meschberger | 62e1a4f | 2010-02-18 15:36:53 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 17 | |
| 18 | |
| 19 | function configure() { |
| 20 | var select = document.getElementById('configSelection_pid'); |
| 21 | var pid = select.options[select.selectedIndex].value; |
| 22 | var parm = pluginRoot + '/' + pid; |
| 23 | $.post(parm, null, displayConfigForm, "json"); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | function create() { |
| 28 | var select = document.getElementById('configSelection_factory'); |
| 29 | var pid = select.options[select.selectedIndex].value; |
| 30 | var parm = pluginRoot + '/' + pid + '?create=true'; |
| 31 | $.post(parm, null, displayConfigForm, "json"); |
| 32 | } |
| 33 | |
| 34 | function displayConfigForm(obj) { |
| 35 | var span1 = document.getElementById('configField'); |
| 36 | var span2 = document.getElementById('factoryField'); |
| 37 | if (!span1 && !span2) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | var parent = span1 ? span1.parentNode : span2.parentNode; |
| 42 | |
| 43 | clearChildren( parent ); |
| 44 | |
| 45 | if (span1) { |
| 46 | parent.appendChild( span1 ); |
| 47 | } |
| 48 | if (span2) { |
| 49 | parent.appendChild( span2 ); |
| 50 | } |
| 51 | |
| 52 | var trEl = tr( null ); |
| 53 | var tdEl = createElement( "th", null, { colSpan: "2" } ); |
| 54 | addText( tdEl, obj.title ); |
| 55 | trEl.appendChild( tdEl ); |
| 56 | parent.appendChild( trEl ); |
| 57 | |
| 58 | trEl = tr( ); |
| 59 | parent.appendChild( trEl ); |
| 60 | |
| 61 | tdEl = td( ); |
| 62 | addText( tdEl, "\u00a0" ); |
| 63 | trEl.appendChild( tdEl ); |
| 64 | |
| 65 | tdEl = td( ); |
| 66 | trEl.appendChild( tdEl ); |
| 67 | |
| 68 | var formEl = createElement( "form", null, { |
| 69 | method: "POST", |
| 70 | action: pluginRoot + "/" + obj.pid |
| 71 | }); |
| 72 | tdEl.appendChild( formEl ); |
| 73 | |
| 74 | var inputEl = createElement( "input", null, { |
| 75 | type: "hidden", |
| 76 | name: "apply", |
| 77 | value: "true" |
| 78 | }); |
| 79 | formEl.appendChild( inputEl ); |
| 80 | |
| 81 | // add the factory PID as a hidden form field if present |
| 82 | if (obj.factoryPid) |
| 83 | { |
| 84 | inputEl = createElement( "input", null, { |
| 85 | type: "hidden", |
| 86 | name: "factoryPid", |
| 87 | value: obj.factoryPid |
| 88 | }); |
| 89 | formEl.appendChild( inputEl ); |
| 90 | } |
| 91 | |
| 92 | // add the PID filter as a hidden form field if present |
| 93 | if (obj.pidFilter) |
| 94 | { |
| 95 | inputEl = createElement( "input", null, { |
| 96 | type: "hidden", |
| 97 | name: "pidFilter", |
| 98 | value: obj.pidFilter |
| 99 | }); |
| 100 | formEl.appendChild( inputEl ); |
| 101 | } |
| 102 | |
| 103 | inputEl = createElement( "input", null, { |
| 104 | type: "hidden", |
| 105 | name: "action", |
| 106 | value: "ajaxConfigManager" |
| 107 | }); |
| 108 | formEl.appendChild( inputEl ); |
| 109 | |
| 110 | var tableEl = createElement( "table", null, { |
| 111 | border: 0, |
| 112 | width: "100%" |
| 113 | }); |
| 114 | formEl.appendChild( tableEl ); |
| 115 | |
| 116 | var bodyEl = createElement( "tbody" ); |
| 117 | tableEl.appendChild( bodyEl ); |
| 118 | |
| 119 | if (obj.description) |
| 120 | { |
| 121 | trEl = tr( ); |
| 122 | tdEl = td( null, { colSpan: "2" } ); |
| 123 | addText( tdEl, obj.description ); |
| 124 | trEl.appendChild( tdEl ); |
| 125 | bodyEl.appendChild( trEl ); |
| 126 | } |
| 127 | |
| 128 | if (obj.propertylist == 'properties') |
| 129 | { |
| 130 | printTextArea(bodyEl, obj.properties); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | printForm(bodyEl, obj); |
| 135 | } |
| 136 | |
| 137 | trEl = tr( ); |
| 138 | bodyEl.appendChild( trEl ); |
| 139 | |
| 140 | tdEl = td( ); |
| 141 | addText( tdEl, "\u00a0" ); |
| 142 | trEl.appendChild( tdEl ); |
| 143 | |
| 144 | tdEl = td( ); |
| 145 | trEl.appendChild( tdEl ); |
| 146 | |
| 147 | // define this TD as innerHTML otherwise the onClick event handler |
| 148 | // of the Delete button is not accepted by IE6 (!)... |
| 149 | var innerHTML = '<input type="submit" name="submit" value="'+i18n.save+'" />'; |
| 150 | innerHTML += ' '; |
| 151 | innerHTML += '<input type="reset" name="reset" value="'+i18n.reset+'" />'; |
| 152 | innerHTML += ' '; |
| 153 | innerHTML += '<input type="submit" name="delete" value="'+i18n.del+'" onClick="return confirmDelete(\'' + obj.pid + '\', \'' + obj.bundleLocation + '\');"/>'; |
| 154 | tdEl.innerHTML = innerHTML; |
| 155 | |
| 156 | printConfigurationInfo(parent, obj); |
| 157 | initStaticWidgets($(parent)); |
| 158 | } |
| 159 | |
| 160 | function printTextArea(/* Element */ parent, props ) |
| 161 | { |
| 162 | |
| 163 | var propsValue = ""; |
| 164 | for (var key in props) |
| 165 | { |
| 166 | propsValue += key + ' = ' + props[key] + '\r\n'; |
| 167 | } |
| 168 | |
| 169 | parent.appendChild( |
| 170 | tr( null, null, [ |
| 171 | td( null, null, [ |
| 172 | text( i18n.props_title ) |
| 173 | ]), |
| 174 | td( null, { style: { width: "99%" } }, [ |
| 175 | createElement( "textarea", null, { |
| 176 | name: "properties", |
Guillaume Nodet | b47d660 | 2010-03-04 20:54:39 +0000 | [diff] [blame^] | 177 | style: { height: "20em", width: "99%" } |
Felix Meschberger | 62e1a4f | 2010-02-18 15:36:53 +0000 | [diff] [blame] | 178 | }, [ text( propsValue ) ] ), |
Guillaume Nodet | b47d660 | 2010-03-04 20:54:39 +0000 | [diff] [blame^] | 179 | createElement( "br" ), |
Felix Meschberger | 62e1a4f | 2010-02-18 15:36:53 +0000 | [diff] [blame] | 180 | text( i18n.props_enter ) |
| 181 | ]) |
| 182 | ]) |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | function printForm( /* Element */ parent, obj ) { |
| 187 | var propList; |
| 188 | for (var idx in obj.propertylist) |
| 189 | { |
| 190 | var prop = obj.propertylist[idx]; |
| 191 | var attr = obj[prop]; |
| 192 | |
| 193 | var trEl = tr( null, null, [ |
| 194 | td( null, null, [ text( attr.name ) ] ) |
| 195 | ]); |
| 196 | parent.appendChild( trEl ); |
| 197 | |
| 198 | var tdEl = td( null, { style: { width: "99%" } } ); |
| 199 | trEl.appendChild( tdEl ); |
| 200 | |
| 201 | if (attr.value != undefined) |
| 202 | { |
| 203 | // check is required to also handle empty strings, 0 and false |
| 204 | tdEl.appendChild( createInput( prop, attr.value, attr.type, '99%' ) ); |
| 205 | tdEl.appendChild( createElement( "br" ) ); |
| 206 | } |
| 207 | else if (typeof(attr.type) == 'object') |
| 208 | { |
| 209 | // assume attr.values and multiselect |
| 210 | createMultiSelect( tdEl, prop, attr.values, attr.type, '99%' ); |
| 211 | tdEl.appendChild( createElement( "br" ) ); |
| 212 | } |
| 213 | else if (attr.values.length == 0) |
| 214 | { |
| 215 | tdEl.appendChild( createSpan( prop, "", attr.type ) ); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | for (var i=0;i<attr.values.length;i++) |
| 220 | { |
| 221 | tdEl.appendChild( createSpan( prop, attr.values[i], attr.type ) ); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (attr.description) |
| 226 | { |
| 227 | addText( tdEl, attr.description ); |
| 228 | } |
| 229 | |
| 230 | if (propList) { |
| 231 | propList += ',' + prop; |
| 232 | } else { |
| 233 | propList = prop; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | parent.appendChild( createElement( "input", null, { |
| 238 | type: "hidden", |
| 239 | name: "propertylist", |
| 240 | value: propList |
| 241 | }) |
| 242 | ); |
| 243 | // FIX for IE6 and above: checkbox can only be checked after it is in the DOM |
| 244 | $(".checked_box").attr("checked", true).removeClass("checked_box"); |
| 245 | } |
| 246 | |
| 247 | function printConfigurationInfo( /* Element */ parent, obj ) |
| 248 | { |
| 249 | parent.appendChild( tr( null, null, [ |
| 250 | createElement( "th", null, { colSpan: "2" }, [ |
| 251 | text( i18n.cfg_title ) |
| 252 | ]) |
| 253 | ]) |
| 254 | ); |
| 255 | |
| 256 | parent.appendChild( tr( null, null, [ |
| 257 | td( null, null, [ |
| 258 | text( i18n.pid ) |
| 259 | ]), |
| 260 | td( null, null, [ |
| 261 | text( obj.pid ) |
| 262 | ]) |
| 263 | ]) |
| 264 | ); |
| 265 | |
| 266 | if (obj.factoryPid) |
| 267 | { |
| 268 | parent.appendChild( tr( null, null, [ |
| 269 | td( null, null, [ |
| 270 | text( i18n.fpid ) |
| 271 | ]), |
| 272 | td( null, null, [ |
| 273 | text( obj.factoryPid ) |
| 274 | ]) |
| 275 | ]) |
| 276 | ); |
| 277 | } |
| 278 | |
| 279 | var binding = obj.bundleLocation; |
| 280 | if (!binding) |
| 281 | { |
| 282 | binding = i18n.unbound; |
| 283 | } |
| 284 | |
| 285 | parent.appendChild( tr( null, null, [ |
| 286 | td( null, null, [ |
| 287 | text( i18n.binding ) |
| 288 | ]), |
| 289 | td( null, null, [ |
| 290 | text( binding ) |
| 291 | ]) |
| 292 | ]) |
| 293 | ); |
| 294 | |
| 295 | if (obj.bundleLocation) |
| 296 | { |
| 297 | var form = createElement( "form", null, { |
| 298 | method: "POST", |
| 299 | action: pluginRoot + "/" + obj.pid |
| 300 | }); |
| 301 | |
| 302 | // define this form contents as innerHTML otherwise the onClick |
| 303 | // event handler of the Unbind button is not accepted by IE6 (!)... |
| 304 | var formInner = '<input type="hidden" name="unbind" value="true"/>'; |
| 305 | formInner += '<input type="submit" name="submit" value="'+i18n.unbind_btn+'" class="ui-state-default ui-corner-all" title="'+i18n.unbind_tip+'" onClick="return confirmUnbind(\'' + obj.pid + '\', \'' + obj.bundleLocation + '\');"/>'; |
| 306 | form.innerHTML = formInner; |
| 307 | |
| 308 | parent.appendChild( tr( null, null, [ |
| 309 | td( null, null, [ |
| 310 | text( " " ) |
| 311 | ]), |
| 312 | td( null, null, [ form ] ) |
| 313 | ]) |
| 314 | ); |
| 315 | } |
| 316 | //$(form).ready(initStaticWidgets); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | var spanCounter = 0; |
| 321 | /* Element */ function createSpan(prop, value, type) { |
| 322 | spanCounter++; |
| 323 | var newId = prop + spanCounter; |
| 324 | |
| 325 | var addButton = createElement("input", null, |
| 326 | { type: "button", |
| 327 | style: {width : "5%"}, |
| 328 | value: "+" |
| 329 | } |
| 330 | ); |
| 331 | $(addButton).click(function() {addValue(prop, newId)}); |
| 332 | var remButton = createElement("input", null, |
| 333 | { type: "button", |
| 334 | style: {width : "5%"}, |
| 335 | value: "-" |
| 336 | } |
| 337 | ); |
| 338 | $(remButton).click(function() {removeValue(newId)}); |
| 339 | var spanEl = createElement( "span", null, { id: newId }, [ |
| 340 | createInput( prop, value, type, '89%' ), addButton, remButton, |
| 341 | createElement("br") |
| 342 | ]); |
| 343 | |
| 344 | return spanEl; |
| 345 | } |
| 346 | |
| 347 | /* Element */ function createInput(prop, value, type, width) { |
| 348 | if (type == 11) { // AttributeDefinition.BOOLEAN |
| 349 | |
| 350 | var inputEl = createElement( "input", null, { |
| 351 | type: "checkbox", |
| 352 | name: prop, |
| 353 | value: "true" |
| 354 | }); |
| 355 | |
| 356 | if (value && typeof(value) != "boolean") |
| 357 | { |
| 358 | value = value.toString().toLowerCase() == "true"; |
| 359 | } |
| 360 | if (value) |
| 361 | { |
| 362 | $(inputEl).addClass("checked_box"); |
| 363 | } |
| 364 | var hiddenEl = createElement( "input", null, { |
| 365 | type: "hidden", |
| 366 | name: prop, |
| 367 | value: "false" |
| 368 | }); |
| 369 | var divEl = createElement("div"); |
| 370 | divEl.appendChild(inputEl); |
| 371 | divEl.appendChild(hiddenEl); |
| 372 | return divEl; |
| 373 | |
| 374 | } else if (typeof(type) == "object") { // predefined values |
| 375 | |
| 376 | var selectEl = createElement( "select", null, { |
| 377 | name: prop, |
| 378 | style: { width: width } |
| 379 | }); |
| 380 | |
| 381 | var labels = type.labels; |
| 382 | var values = type.values; |
| 383 | for (var idx in labels) { |
| 384 | var optionEl = createElement( "option", null, { |
| 385 | value: values[idx] |
| 386 | }, [ text( labels[idx] ) ]); |
| 387 | |
| 388 | if (value == values[idx]) |
| 389 | { |
| 390 | optionEl.setAttribute( "selected", true ); |
| 391 | } |
| 392 | selectEl.appendChild( optionEl ); |
| 393 | } |
| 394 | |
| 395 | return selectEl; |
| 396 | |
| 397 | } else { // Simple |
| 398 | return createElement( "input", null, { |
| 399 | type: "text", |
| 400 | name: prop, |
| 401 | value: value, |
| 402 | style: { width: width } |
| 403 | }); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | function createMultiSelect(/* Element */ parent, prop, values, options, width) { |
| 408 | // convert value list into 'set' |
| 409 | var valueSet = new Object(); |
| 410 | for (var idx in values) { |
| 411 | valueSet[ values[idx] ] = true; |
| 412 | } |
| 413 | |
| 414 | var labels = options.labels; |
| 415 | var values = options.values; |
| 416 | for (var idx in labels) { |
| 417 | |
| 418 | var inputEl = createElement( "input", null, { |
| 419 | type: "checkbox", |
| 420 | name: prop, |
| 421 | value: values[idx] |
| 422 | }); |
| 423 | |
| 424 | if (valueSet[ values[idx] ]) |
| 425 | { |
| 426 | inputEl.setAttribute( "checked", true ); |
| 427 | } |
| 428 | |
| 429 | var labelEl = createElement( "label", "multiselect" ); |
| 430 | labelEl.appendChild( inputEl ); |
| 431 | addText( labelEl, labels[idx] ); |
| 432 | |
| 433 | parent.appendChild( labelEl ); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | |
| 438 | function addValue(prop, vidx) |
| 439 | { |
| 440 | var span = document.getElementById(vidx); |
| 441 | if (!span) |
| 442 | { |
| 443 | return; |
| 444 | } |
| 445 | var newSpan = createSpan(prop, ''); |
| 446 | span.parentNode.insertBefore(newSpan, span.nextSibling); |
| 447 | // FIX for IE6 and above: checkbox can only be checked after it is in the DOM |
| 448 | $(".checked_box").attr("checked", true).removeClass("checked_box"); |
| 449 | //$(span).ready(initStaticWidgets); |
| 450 | } |
| 451 | |
| 452 | function removeValue(vidx) |
| 453 | { |
| 454 | var span = document.getElementById(vidx); |
| 455 | if (!span) |
| 456 | { |
| 457 | return; |
| 458 | } |
| 459 | span.parentNode.removeChild(span); |
| 460 | } |
| 461 | |
| 462 | function configConfirm(/* String */ message, /* String */ title, /* String */ location) |
| 463 | { |
| 464 | var message = i18n.del_ask; |
| 465 | |
| 466 | if (title) { |
| 467 | message += "\r\n" + i18n.del_config + title; |
| 468 | } |
| 469 | if (location) { |
| 470 | message += "\r\n" + i18n.del_bundle + location; |
| 471 | } |
| 472 | |
| 473 | return confirm(message); |
| 474 | } |
| 475 | |
| 476 | function confirmDelete(/* String */ title, /* String */ location) |
| 477 | { |
| 478 | return configConfirm(i18n.unbind_ask, title, location); |
| 479 | } |
| 480 | |
| 481 | function confirmUnbind(/* String */ title, /* String */ location) |
| 482 | { |
| 483 | return configConfirm(i18n.unbind_ask, title, location); |
| 484 | } |
| 485 | |
| 486 | function addOption(list, target) |
| 487 | { |
| 488 | var html = ""; |
| 489 | for (i in list) { |
| 490 | var sel = list[i].id == selectedPid ? '" selected="selected' : ''; |
| 491 | html += '<option value="' + list[i].id + sel + '">' + list[i].name + '</option>'; |
| 492 | } |
| 493 | if (html) target.html(html); |
| 494 | } |
| 495 | |
| 496 | var configSelection_pid = false; |
| 497 | var configSelection_factory = false; |
| 498 | $(document).ready(function() { |
| 499 | configSelection_pid = $('#configSelection_pid'); |
| 500 | configSelection_factory = $('#configSelection_factory'); |
| 501 | $(".statline").html(configData.status ? i18n.stat_ok : i18n.stat_missing); |
| 502 | $("#config_content").css("display", configData.status ? "block" : "none"); |
| 503 | if (configData.status) { |
| 504 | addOption(configData.pids, $("#configSelection_pid")); |
| 505 | addOption(configData.fpids, $("#configSelection_factory")); |
| 506 | } |
| 507 | if (selectedPid) configure(); |
| 508 | }); |