blob: d15aa171a7a238e7befa84b0f831c62a19736c68 [file] [log] [blame]
Felix Meschbergerbb30b0c2008-05-16 11:59:32 +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 */
17
18
19function configure() {
20 var span = document.getElementById('configField');
21 if (!span) {
22 return;
23 }
24 var select = document.configSelection.pid;
25 var pid = select.options[select.selectedIndex].value;
26 var parm = '?action=ajaxConfigManager&' + pid;
27 sendRequest('GET', parm, displayConfigForm);
28}
29
30function displayConfigForm(obj) {
31 var span = document.getElementById('configField');
32 if (!span) {
33 return;
34 }
35 var innerHtml = '<tr class="content" id="configField">' + span.innerHTML + '</tr>';
36 innerHtml += '<tr class="content">';
37 innerHtml += '<th colspan="2" class="content" >' + obj.title + '</th></tr>';
38 innerHtml += '<tr class="content">';
39 innerHtml += '<td class="content">&nbsp;</td>';
40 innerHtml += '<td class="content">';
41 innerHtml += '<form method="post">';
42 innerHtml += '<input type="hidden" name="apply" value="true" />';
43 innerHtml += '<input type="hidden" name="pid" value="' + obj.pid + '" />';
44 innerHtml += '<input type="hidden" name="action" value="ajaxConfigManager" />';
45 innerHtml += '<table border="0" width="100%">';
46 if (obj.description) {
47 innerHtml += '<tr class="content">';
48 innerHtml += '<td class="content" colspan="2">' + obj.description + '</td></tr>';
49 }
50 if (obj.propertylist == 'properties') {
51 innerHtml += printTextArea(obj.properties);
52 } else {
53 innerHtml += printForm(obj);
54 }
55 innerHtml += '<tr class="content">';
56 innerHtml += '<td class="content">&nbsp;</td>';
57 innerHtml += '<td class="content">';
58 innerHtml += '<input type="submit" class="submit" name="submit" value="Save" />';
59 innerHtml += '&nbsp;&nbsp;&nbsp;';
60 innerHtml += '<input type="reset" class="submit" name="reset" value="Reset" />';
61 innerHtml += '&nbsp;&nbsp;&nbsp;';
62 innerHtml += '<input type="submit" class="submit" name="delete" value="Delete" onClick="return confirmDelete();"/>';
63 if (obj.isFactory) {
64 innerHtml += '&nbsp;&nbsp;&nbsp;';
65 innerHtml += '<input type="submit" class="submit" name="create" value="Create Configuration" onClick="return promptContext(this);" />';
66 innerHtml += '<input type="hidden" name="sling.context" value="" />';
67 }
68 innerHtml += '</td></tr>';
69 innerHtml += '</table>';
70 innerHtml += '</form>';
71 innerHtml += '</td></tr>';
72 innerHtml += printConfigurationInfo(obj);
73 span.parentNode.innerHTML = innerHtml;
74}
75
76function printTextArea(props) {
77 var innerHtml = '<tr class="content">';
78 innerHtml += '<td class="content" style="vertical-align: top">Properties</td>';
79 innerHtml += '<td class="content" style="width: 99%">';
80 innerHtml += '<textarea name="properties" style="height: 50%; width: 99%">';
81 for (var key in props) {
82 innerHtml += key + ' = ' + props[key] + '\r\n';
83 }
84 innerHtml += '</textarea>';
85 innerHtml += 'Enter Name-Value pairs of configuration properties.</td>';
86 return innerHtml;
87}
88
89function printForm(obj) {
90 var innerHtml = '';
91 var propList;
92 for (var idx in obj.propertylist) {
93 var prop = obj.propertylist[idx];
94 var attr = obj[prop];
95 innerHtml += '<tr class="content">';
96 innerHtml += '<td class="content" style="vertical-align: top">' + attr.name + '</td>';
97 innerHtml += '<td class="content" style="width: 99%">';
98 if (attr.value != undefined) { // check is required to also handle empty strings, 0 and false
99 innerHtml += createInput(prop, attr.value, attr.type, '99%');
100 innerHtml += '<br />';
101 } else if (typeof(attr.type) == 'object') {
102 // assume attr.values and multiselect
103 innerHtml += createMultiSelect(prop, attr.values, attr.type, '99%');
104 innerHtml += '<br />';
105 } else {
106 for (var vidx in attr.values) {
107 var spanElement = createSpan(prop, attr.values[vidx], attr.type);
108 innerHtml += '<span id="' + spanElement.id + '">';
109 innerHtml += spanElement.innerHTML;
110 innerHtml += '</span>';
111 }
112 }
113 if (attr.description) {
114 innerHtml += attr.description;
115 }
116 innerHtml += '</td>';
117 if (propList) {
118 propList += ',' + prop;
119 } else {
120 propList = prop;
121 }
122 }
123 innerHtml += '<input type="hidden" name="propertylist" value="' + propList + '"/>';
124 return innerHtml;
125}
126
127function printConfigurationInfo(obj) {
128 var innerHtml = '<tr class="content">';
129 innerHtml += '<th colspan="2" class="content" >Configuration Information</th></tr>';
130 innerHtml += '<tr class="content">';
131 innerHtml += '<td class="content">Persistent Identity (PID)</td>';
132 innerHtml += '<td class="content">' + obj.pid + '</td></tr>';
133 if (obj.factoryPID) {
134 innerHtml += '<tr class="content">';
135 innerHtml += '<td class="content">Factory Peristent Identifier (Factory PID)</td>';
136 innerHtml += '<td class="content">' + obj.factoryPID + '</td></tr>';
137 }
138 innerHtml += '<tr class="content">';
139 innerHtml += '<td class="content">Configuration Binding</td>';
140 innerHtml += '<td class="content">' + obj.bundleLocation + '</td></tr>';
141 return innerHtml;
142}
143
144function addValue(prop, vidx) {
145 var span = document.getElementById(vidx);
146 if (!span) {
147 return;
148 }
149 var newSpan = createSpan(prop, '');
150 span.parentNode.insertBefore(newSpan, span.nextSibling);
151}
152
153var spanCounter = 0;
154function createSpan(prop, value, type) {
155 spanCounter++;
156 var newId = prop + spanCounter;
157 var innerHtml = createInput(prop, value, type, '89%');
158 innerHtml += '<input class="input" type="button" value="+" onClick="addValue(\'' + prop + '\',\'' + newId + '\');" style="width: 5%" />';
159 innerHtml += '<input class="input" type="button" value="-" onClick="removeValue(\'' + newId + '\');" style="width: 5%" />';
160 innerHtml += '<br />';
161 var newSpan = document.createElement('span');
162 newSpan.id = newId;
163 newSpan.innerHTML = innerHtml;
164 return newSpan;
165}
166
167function createInput(prop, value, type, width) {
168 if (type == 11) { // AttributeDefinition.BOOLEAN
169 if (value && typeof(value) != "boolean") {
170 value = value.toString().toLowerCase() == "true";
171 }
172 var checked = value ? 'checked' : '';
173 return '<input class="input" type="checkbox" name="' + prop + '" value="true" ' + checked + '/>';
174 } else if (typeof(type) == "object") { // predefined values
175 var labels = type.labels;
176 var values = type.values;
177 var innerHtml = '<select class="select" name="' + prop + '" style="width: ' + width + '">';
178 for (var idx in labels) {
179 var selected = (value == values[idx]) ? ' selected' : '';
180 innerHtml += '<option value="' + values[idx] + '"' + selected + '>' + labels[idx] + '</option>';
181 }
182 innerHtml += '</select>';
183 return innerHtml;
184 } else { // Simple
185 return '<input class="input" type="text" name="' + prop + '" value="' + value + '" style="width: ' + width + '"/>';
186 }
187}
188
189function createMultiSelect(prop, values, options, width) {
190 // convert value list into 'set'
191 var valueSet = new Object();
192 for (var idx in values) {
193 valueSet[ values[idx] ] = true;
194 }
195
196 var labels = options.labels;
197 var values = options.values;
198 var innerHtml = '';
199 for (var idx in labels) {
200 var checked = valueSet[ values[idx] ] ? ' checked' : '';
201 innerHtml += '<label><input type="checkbox" name="' + prop + '" value="' + values[idx] + '"' + checked + '>' + labels[idx] + '</label>';
202 }
203 return innerHtml;
204}
205
206function removeValue(vidx) {
207 var span = document.getElementById(vidx);
208 if (!span) {
209 return;
210 }
211 span.parentNode.removeChild(span);
212}
213
214function confirmDelete() {
215 return confirm("Are you sure to delete this configuration ?");
216}
217
218function promptContext(form) {
219 var result = prompt("Please give a Sling Context for the new configuration");
220 // alert("You entered: [" + result + "] for form [" + form + "/" + form.form + "]");
221
222 // set the hidden sling.context input field with the value
223 if (result != null) {
224 form.form["sling.context"].value = result;
225 return true;
226 }
227
228 return false;
229}