blob: a4e294f083dd87bffb6cb83ac7cb87543dd7c147 [file] [log] [blame]
Valentin Valchevd350cc72010-03-22 06:52:06 +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// device selection
18var deviceData = false;
19var deviceTableBody = false;
20
21// service selection
22var serviceData = false;
23var serviceDataVars = false;
24var serviceDataInfoID = false;
25var serviceDataInfoType = false;
26
27// actions
28var actionsContainer = false;
29var actionsSelect = false;
30var actionsInvoke = false;
31var actionsTable = false;
32var actionsTableBody = false;
33var actionsTableRow = false;
34
35// tree browser, buttons, error dialog
36var browser = false;
37var searching = false;
38var reloadVars = false;
39
40/* BEGIN HELPERS */
41
42/* helper functions for tree node */
43function _id(dn) { return dn.replace(/[:-\\.\\$]/g,'_') }
44/* creates a node in the device tree */
45function treeNode(id, name, icon, span) {
46 var li = createElement('li', null, { 'id' : _id(id) }, [
47 createElement('span', null, null, [
48 icon ?
49 createElement('img', 'icon', { 'src' : icon }) :
50 createElement('span', 'ui-icon ui-icon-' + span) ,
51 text(name)
52 ])
53 ]);
54 return $(li);
55}
56/* creates a service node in the devices tree, and associates the click action */
57function servNode(udn, urn) {
58 return treeNode(udn+urn, urn, null, 'extlink').click(function() {
59 if (selectServiceTime) {
60 clearTimeout(selectServiceTime);
61 selectServiceTime = false;
62 }
63 $.post(pluginRoot, {
64 'action': 'serviceDetails',
65 'udn' : udn,
66 'urn' : urn
67 }, function(data) {
68 renderService(udn, urn, data)
69 }, 'json');
70 return false;
71 });
72}
73/* a helper function to format device properties values - specially
74 * converts arrays and strings, if the last are links */
75function _val(val) {
76 var ret = '';
77 if ($.isArray(val)) {
78 for (i in val) ret += _val(val[i]) + '<br/>';
79 } else {
80 ret = (typeof val == 'string' && val.indexOf('http://') != -1) ?
81 '<a target="blank" href="' + val + '">' + val + '</a>' : val;
82 }
83 return ret;
84}
85
86
87/* BEGIN UI-ELEMENTS CREATION */
88
89/* add element to the tree, just creates the node */
90function addDevice(device) {
91 var udn = device.props['UPnP.device.UDN'];
92 var name = device.props['UPnP.device.friendlyName'];
93 var icon = null;
94 if (device.icon) icon = pluginRoot + '?icon=' + udn;
95
96 var node = treeNode(udn, name, icon, 'lightbulb').click(function() {
97 renderDevice(device);
Valentin Valchevb8468aa2010-03-22 14:42:02 +000098 return false;
Valentin Valchevd350cc72010-03-22 06:52:06 +000099 });
100
101 var ul, hasChildren;
102
103 // services
104 hasChildren = false;
105 ul = $(createElement('ul', 'ui-helper-clearfix'));
106 for(var i in device.services) {
107 hasChildren = true;
108 ul.append( servNode(udn, device.services[i]) );
109 }
110 if (hasChildren) node.append(ul);
111
112 // child devices
113 hasChildren = false;
114 ul = $(createElement('ul'));
115 for(var i in device.children) {
116 hasChildren = true;
Valentin Valchevb8468aa2010-03-22 14:42:02 +0000117 ul.append( addDevice(device.children[i]) );
Valentin Valchevd350cc72010-03-22 06:52:06 +0000118 }
119 if (hasChildren) node.append(ul);
120
121 return node;
122}
123/* fills in the list of state variables */
Valentin Valchevf5bf0952012-04-04 14:18:29 +0000124function sortVarNm(a) { return typeof a != 'undefined' && typeof a.name != undefined ? a.name.toLowerCase() : '' }
125function namedObjectSorter(a,b) { return sortVarNm(a) > sortVarNm(b) ? 1 : -1 }
Valentin Valchevd350cc72010-03-22 06:52:06 +0000126function renderVars(data) {
127 serviceDataVars.empty();
Valentin Valchevae0dbd72012-04-05 11:07:18 +0000128 if (typeof data.variables == 'undefined') return;
129
Valentin Valchevf5bf0952012-04-04 14:18:29 +0000130 data.variables.sort(namedObjectSorter);
Valentin Valchevd350cc72010-03-22 06:52:06 +0000131 for(i in data.variables) {
132 var _var = data.variables[i];
133 var _tr = tr(null, null, [
134 td(null, null, [ text(_var.name) ]),
135 td(null, null, [ text(_var.value) ]),
136 td(null, null, [ text(_var.sendsEvents) ])
137 ]);
138 serviceDataVars.append(_tr);
139 }
140 initStaticWidgets();
141}
142
143/* BEGIN ACTION HANDLERS */
144
145var selectedDevice = false; // the LI element of the selected device, reset on load
146function renderDevice(device) {
147 // generate content
148 var table = '';
Valentin Valchevf5bf0952012-04-04 14:18:29 +0000149 var sortedKeys = [];
150 for(var key in device.props) sortedKeys.push(key);
151 sortedKeys.sort();
152 for(var x in sortedKeys) {
153 var key = sortedKeys[x];
154 var xvalue = _val(device.props[key]);
155 if ('objectClass' == key) continue;
156 if ('service.id' == key) {
Valentin Valchev3ccd2482012-07-11 08:12:14 +0000157 xvalue = '<a href="' + appRoot + '/services/' + xvalue + '">' + xvalue + '</a>';
Valentin Valchevf5bf0952012-04-04 14:18:29 +0000158 }
159 table += '<tr><td class="ui-priority-primary">' + key + '</td><td>' + xvalue + '</td></tr>';
Valentin Valchevd350cc72010-03-22 06:52:06 +0000160 }
161
162 // update the UI
163 deviceTableBody.html(table);
164 reloadVars.addClass('ui-state-disabled');
165 deviceData.removeClass('ui-helper-hidden');
166 serviceData.addClass('ui-helper-hidden')
167
168 // reset selected items
169 if (selectedDevice) selectedDevice.css('font-weight', 'normal');
170 selectedDevice = $('#' + _id(device.props['UPnP.device.UDN']) + ' span').css('font-weight', 'bold');
171}
172
173var selectedUdn = false;
174var selectedUrn = false;
175var selectServiceTime = false;
176function renderService(udn, urn, data) {
177 // save selection
178 selectedUdn = udn;
179 selectedUrn = urn;
180
181 // append service info
182 serviceDataInfoID.text(data.id);
183 serviceDataInfoType.text(data.type);
184
185 // append state variables
186 renderVars(data);
187
188 // append actions
189 if (data.actions) {
190 var html = '';
191 var x = data.actions;
Valentin Valchevf5bf0952012-04-04 14:18:29 +0000192 x.sort(namedObjectSorter);
Valentin Valchevd350cc72010-03-22 06:52:06 +0000193 for (var a in x) html += '<option value="' + a + '">' + x[a].name + '</option>';
194 actionsSelect.html(html).unbind('change').change(function() {
195 var index = $(this).val();
196 actionSelected(udn, urn, x[index]);
197 }).trigger('change');
198 actionsContainer.removeClass('ui-helper-hidden');
199 } else {
200 actionsContainer.addClass('ui-helper-hidden');
201 }
202
203 // update UI
204 deviceData.addClass('ui-helper-hidden');
205 serviceData.removeClass('ui-helper-hidden');
206 reloadVars.removeClass('ui-state-disabled');
207 initStaticWidgets();
208
209 // refresh once - to get updates asynchronously
210 selectServiceTime = setTimeout('reloadVars.click()', 3000);
211}
212
213function actionSelected(udn, urn, action) {
214 // add input arguments
215 if (action.inVars) {
216 actionsTableBody.empty();
217 for (var i in action.inVars) {
218 var _arg = action.inVars[i];
219 var _tr = actionsTableRow.clone().appendTo(actionsTableBody);
220 _tr.find('td:eq(0)').text(_arg.name);
221 _tr.find('td:eq(1)').text(_arg.type);
222 var _el = _tr.find('input').attr('id', 'arg'+i);
223 _arg['element'] = _el;
224 }
225 actionsTable.removeClass('ui-helper-hidden');
226 } else {
227 actionsTable.addClass('ui-helper-hidden');
228 }
229
230 actionsInvoke.unbind('click').click(function() {
231 invokeAction(udn, urn, action);
232 });
233
234 initStaticWidgets(actionsTableBody);
235}
236
237function invokeAction(udn, urn, action) {
238 // prepare arguments
239 var names = new Array();
240 var vals = new Array();
241 for (var i in action.inVars) {
242 var x = action.inVars[i];
243 names.push(x['name']);
244 vals.push(x['element'].val());
245 }
246 // invoke action
247 $.post(pluginRoot, {
248 'udn' : udn,
249 'urn' : urn,
250 'action': 'invokeAction',
251 'actionID' : action.name,
252 'names' : names,
253 'vals' : vals
254 }, function(data) {
255 var html = i18n.no_params_out;
256 if (data.output) {
257 html = '<table class="nicetable"><tr><th>'+i18n.args_name+'</th><th>'+i18n.args_type+'</th><th>' + i18n.args_value + '</th></tr>';
258 for(var i in data.output) {
259 var arg = data.output[i];
260 html += '<tr><td>' + arg['name'] + '</td><td>' + arg['type'] + '</td><td>' + arg['value'] + '</td></tr>';
261 }
262 html += '</table>';
263 }
264 Xalert(html, i18n.dl_title_ok);
265 }, 'json');
266}
267
Valentin Valchevae38b5b2012-04-03 16:16:05 +0000268function sortNm(a) {
269 if (typeof a != 'undefined' && typeof a.props != undefined) {
270 var nm = a.props['UPnP.device.friendlyName'];
271 if (typeof nm != 'undefined') return nm.toLowerCase();
272 }
273 return '';
274}
275
276function sortFn(a,b) {
277 return sortNm(a) > sortNm(b) ? 1 : -1;
278}
279
Valentin Valchevd350cc72010-03-22 06:52:06 +0000280function listDevices() {
281 browser.empty().addClass('ui-helper-hidden');
282 searching.removeClass('ui-helper-hidden');
Valentin Valchevae38b5b2012-04-03 16:16:05 +0000283
Valentin Valchevd350cc72010-03-22 06:52:06 +0000284 $.post(pluginRoot, { 'action': 'listDevices' }, function(data) {
285 if (data && data.devices) {
Valentin Valchevae38b5b2012-04-03 16:16:05 +0000286 data.devices.sort(sortFn);
Valentin Valchevd350cc72010-03-22 06:52:06 +0000287 $.each(data.devices, function(index) {
288 var html = addDevice(this);
289 browser.treeview( { add: html.appendTo(browser) } );
290 });
Valentin Valchev1c7be6f2012-04-04 11:52:14 +0000291 $('.statline').text(i18n.status_devs.msgFormat(data.devices.length));
Valentin Valchevd350cc72010-03-22 06:52:06 +0000292 } else {
Valentin Valchev1c7be6f2012-04-04 11:52:14 +0000293 $('.statline').text(i18n.status_none);
294 browser.append('', i18n.status_none, '');
Valentin Valchevd350cc72010-03-22 06:52:06 +0000295 }
296
297 // update selected items
298 selectedDevice = false;
299 selectedUdn = false;
300 selectedUrn = false;
301
302 // update IU elements
303 browser.removeClass('ui-helper-hidden');
304 searching.addClass('ui-helper-hidden');
305 deviceData.addClass('ui-helper-hidden');
306 serviceData.addClass('ui-helper-hidden');
307 }, 'json');
308
309 return false;
310}
311
312
313
314$(document).ready( function() {
315 // init elements of style
316 searching = $('#searching');
317 deviceData = $('#deviceData');
318 deviceTableBody = $('#deviceTable tbody');
319
320 // services
321 serviceData = $('#serviceData');
322 serviceDataInfoID = $('#serviceDataInfoID');
323 serviceDataInfoType= $('#serviceDataInfoType');
324 serviceDataVars = $('#serviceDataVars tbody');
325
326 // actions
327 actionsContainer = $('#actionsContainer');
328 actionsSelect = actionsContainer.find('select');
329 actionsInvoke = actionsContainer.find('button');
330 actionsTable = actionsContainer.find('table');
331 actionsTableBody = actionsTable.find('tbody');
332 actionsTableRow = actionsTableBody.find('tr').clone();
333 actionsTableBody.empty();
334
335 // init navigation tree
336 browser = $('#browser').treeview({
337 animated: 'fast',
338 collapsed: true,
339 unique: true
340 });
341
342 // reload button
343 reloadVars = $('#reloadVars').click(function() {
344 if (selectedUdn && selectedUrn) {
345 $.post(pluginRoot, {
346 'action': 'serviceDetails',
347 'udn' : selectedUdn,
348 'urn' : selectedUrn
349 }, renderVars, 'json');
350 }
351 })
352
353 $('#reloadDevices').click(listDevices);
354
355 listDevices();
356});
357