blob: c77a3e2b37dc791aa8d765b967911ddfc8bd7b20 [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 */
124function renderVars(data) {
125 serviceDataVars.empty();
126 for(i in data.variables) {
127 var _var = data.variables[i];
128 var _tr = tr(null, null, [
129 td(null, null, [ text(_var.name) ]),
130 td(null, null, [ text(_var.value) ]),
131 td(null, null, [ text(_var.sendsEvents) ])
132 ]);
133 serviceDataVars.append(_tr);
134 }
135 initStaticWidgets();
136}
137
138/* BEGIN ACTION HANDLERS */
139
140var selectedDevice = false; // the LI element of the selected device, reset on load
141function renderDevice(device) {
142 // generate content
143 var table = '';
144 for(var key in device.props) {
145 table += '<tr><td class="ui-priority-primary">' + key + '</td><td>' + _val(device.props[key]) + '</td></tr>';
146 }
147
148 // update the UI
149 deviceTableBody.html(table);
150 reloadVars.addClass('ui-state-disabled');
151 deviceData.removeClass('ui-helper-hidden');
152 serviceData.addClass('ui-helper-hidden')
153
154 // reset selected items
155 if (selectedDevice) selectedDevice.css('font-weight', 'normal');
156 selectedDevice = $('#' + _id(device.props['UPnP.device.UDN']) + ' span').css('font-weight', 'bold');
157}
158
159var selectedUdn = false;
160var selectedUrn = false;
161var selectServiceTime = false;
162function renderService(udn, urn, data) {
163 // save selection
164 selectedUdn = udn;
165 selectedUrn = urn;
166
167 // append service info
168 serviceDataInfoID.text(data.id);
169 serviceDataInfoType.text(data.type);
170
171 // append state variables
172 renderVars(data);
173
174 // append actions
175 if (data.actions) {
176 var html = '';
177 var x = data.actions;
178 for (var a in x) html += '<option value="' + a + '">' + x[a].name + '</option>';
179 actionsSelect.html(html).unbind('change').change(function() {
180 var index = $(this).val();
181 actionSelected(udn, urn, x[index]);
182 }).trigger('change');
183 actionsContainer.removeClass('ui-helper-hidden');
184 } else {
185 actionsContainer.addClass('ui-helper-hidden');
186 }
187
188 // update UI
189 deviceData.addClass('ui-helper-hidden');
190 serviceData.removeClass('ui-helper-hidden');
191 reloadVars.removeClass('ui-state-disabled');
192 initStaticWidgets();
193
194 // refresh once - to get updates asynchronously
195 selectServiceTime = setTimeout('reloadVars.click()', 3000);
196}
197
198function actionSelected(udn, urn, action) {
199 // add input arguments
200 if (action.inVars) {
201 actionsTableBody.empty();
202 for (var i in action.inVars) {
203 var _arg = action.inVars[i];
204 var _tr = actionsTableRow.clone().appendTo(actionsTableBody);
205 _tr.find('td:eq(0)').text(_arg.name);
206 _tr.find('td:eq(1)').text(_arg.type);
207 var _el = _tr.find('input').attr('id', 'arg'+i);
208 _arg['element'] = _el;
209 }
210 actionsTable.removeClass('ui-helper-hidden');
211 } else {
212 actionsTable.addClass('ui-helper-hidden');
213 }
214
215 actionsInvoke.unbind('click').click(function() {
216 invokeAction(udn, urn, action);
217 });
218
219 initStaticWidgets(actionsTableBody);
220}
221
222function invokeAction(udn, urn, action) {
223 // prepare arguments
224 var names = new Array();
225 var vals = new Array();
226 for (var i in action.inVars) {
227 var x = action.inVars[i];
228 names.push(x['name']);
229 vals.push(x['element'].val());
230 }
231 // invoke action
232 $.post(pluginRoot, {
233 'udn' : udn,
234 'urn' : urn,
235 'action': 'invokeAction',
236 'actionID' : action.name,
237 'names' : names,
238 'vals' : vals
239 }, function(data) {
240 var html = i18n.no_params_out;
241 if (data.output) {
242 html = '<table class="nicetable"><tr><th>'+i18n.args_name+'</th><th>'+i18n.args_type+'</th><th>' + i18n.args_value + '</th></tr>';
243 for(var i in data.output) {
244 var arg = data.output[i];
245 html += '<tr><td>' + arg['name'] + '</td><td>' + arg['type'] + '</td><td>' + arg['value'] + '</td></tr>';
246 }
247 html += '</table>';
248 }
249 Xalert(html, i18n.dl_title_ok);
250 }, 'json');
251}
252
Valentin Valchevae38b5b2012-04-03 16:16:05 +0000253function sortNm(a) {
254 if (typeof a != 'undefined' && typeof a.props != undefined) {
255 var nm = a.props['UPnP.device.friendlyName'];
256 if (typeof nm != 'undefined') return nm.toLowerCase();
257 }
258 return '';
259}
260
261function sortFn(a,b) {
262 return sortNm(a) > sortNm(b) ? 1 : -1;
263}
264
Valentin Valchevd350cc72010-03-22 06:52:06 +0000265function listDevices() {
266 browser.empty().addClass('ui-helper-hidden');
267 searching.removeClass('ui-helper-hidden');
Valentin Valchevae38b5b2012-04-03 16:16:05 +0000268
Valentin Valchevd350cc72010-03-22 06:52:06 +0000269 $.post(pluginRoot, { 'action': 'listDevices' }, function(data) {
270 if (data && data.devices) {
Valentin Valchevae38b5b2012-04-03 16:16:05 +0000271 data.devices.sort(sortFn);
Valentin Valchevd350cc72010-03-22 06:52:06 +0000272 $.each(data.devices, function(index) {
273 var html = addDevice(this);
274 browser.treeview( { add: html.appendTo(browser) } );
275 });
Valentin Valchev1c7be6f2012-04-04 11:52:14 +0000276 $('.statline').text(i18n.status_devs.msgFormat(data.devices.length));
Valentin Valchevd350cc72010-03-22 06:52:06 +0000277 } else {
Valentin Valchev1c7be6f2012-04-04 11:52:14 +0000278 $('.statline').text(i18n.status_none);
279 browser.append('', i18n.status_none, '');
Valentin Valchevd350cc72010-03-22 06:52:06 +0000280 }
281
282 // update selected items
283 selectedDevice = false;
284 selectedUdn = false;
285 selectedUrn = false;
286
287 // update IU elements
288 browser.removeClass('ui-helper-hidden');
289 searching.addClass('ui-helper-hidden');
290 deviceData.addClass('ui-helper-hidden');
291 serviceData.addClass('ui-helper-hidden');
292 }, 'json');
293
294 return false;
295}
296
297
298
299$(document).ready( function() {
300 // init elements of style
301 searching = $('#searching');
302 deviceData = $('#deviceData');
303 deviceTableBody = $('#deviceTable tbody');
304
305 // services
306 serviceData = $('#serviceData');
307 serviceDataInfoID = $('#serviceDataInfoID');
308 serviceDataInfoType= $('#serviceDataInfoType');
309 serviceDataVars = $('#serviceDataVars tbody');
310
311 // actions
312 actionsContainer = $('#actionsContainer');
313 actionsSelect = actionsContainer.find('select');
314 actionsInvoke = actionsContainer.find('button');
315 actionsTable = actionsContainer.find('table');
316 actionsTableBody = actionsTable.find('tbody');
317 actionsTableRow = actionsTableBody.find('tr').clone();
318 actionsTableBody.empty();
319
320 // init navigation tree
321 browser = $('#browser').treeview({
322 animated: 'fast',
323 collapsed: true,
324 unique: true
325 });
326
327 // reload button
328 reloadVars = $('#reloadVars').click(function() {
329 if (selectedUdn && selectedUrn) {
330 $.post(pluginRoot, {
331 'action': 'serviceDetails',
332 'udn' : selectedUdn,
333 'urn' : selectedUrn
334 }, renderVars, 'json');
335 }
336 })
337
338 $('#reloadDevices').click(listDevices);
339
340 listDevices();
341});
342