Valentin Pavlov Valchev | ec44842 | 2010-03-22 06:52:06 +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 | // device selection
|
| 18 | var deviceData = false;
|
| 19 | var deviceTableBody = false;
|
| 20 |
|
| 21 | // service selection
|
| 22 | var serviceData = false;
|
| 23 | var serviceDataVars = false;
|
| 24 | var serviceDataInfoID = false;
|
| 25 | var serviceDataInfoType = false;
|
| 26 |
|
| 27 | // actions
|
| 28 | var actionsContainer = false;
|
| 29 | var actionsSelect = false;
|
| 30 | var actionsInvoke = false;
|
| 31 | var actionsTable = false;
|
| 32 | var actionsTableBody = false;
|
| 33 | var actionsTableRow = false;
|
| 34 |
|
| 35 | // tree browser, buttons, error dialog
|
| 36 | var browser = false;
|
| 37 | var searching = false;
|
| 38 | var reloadVars = false;
|
| 39 |
|
| 40 | /* BEGIN HELPERS */
|
| 41 |
|
| 42 | /* helper functions for tree node */
|
| 43 | function _id(dn) { return dn.replace(/[:-\\.\\$]/g,'_') }
|
| 44 | /* creates a node in the device tree */
|
| 45 | function 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 */
|
| 57 | function 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 */
|
| 75 | function _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 */
|
| 90 | function 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 Pavlov Valchev | 2e894ad | 2010-03-22 14:42:02 +0000 | [diff] [blame] | 98 | return false;
|
Valentin Pavlov Valchev | ec44842 | 2010-03-22 06:52:06 +0000 | [diff] [blame] | 99 | });
|
| 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 Pavlov Valchev | 2e894ad | 2010-03-22 14:42:02 +0000 | [diff] [blame] | 117 | ul.append( addDevice(device.children[i]) );
|
Valentin Pavlov Valchev | ec44842 | 2010-03-22 06:52:06 +0000 | [diff] [blame] | 118 | }
|
| 119 | if (hasChildren) node.append(ul);
|
| 120 |
|
| 121 | return node;
|
| 122 | }
|
| 123 | /* fills in the list of state variables */
|
| 124 | function 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 |
|
| 140 | var selectedDevice = false; // the LI element of the selected device, reset on load
|
| 141 | function 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 |
|
| 159 | var selectedUdn = false;
|
| 160 | var selectedUrn = false;
|
| 161 | var selectServiceTime = false;
|
| 162 | function 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 |
|
| 198 | function 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 |
|
| 222 | function 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 |
|
| 253 | function listDevices() {
|
| 254 | browser.empty().addClass('ui-helper-hidden');
|
| 255 | searching.removeClass('ui-helper-hidden');
|
| 256 |
|
| 257 | $.post(pluginRoot, { 'action': 'listDevices' }, function(data) {
|
| 258 | if (data && data.devices) {
|
| 259 | $.each(data.devices, function(index) {
|
| 260 | var html = addDevice(this);
|
| 261 | browser.treeview( { add: html.appendTo(browser) } );
|
| 262 | });
|
| 263 | } else {
|
| 264 | browser.append('','No devices available', '');
|
| 265 | }
|
| 266 |
|
| 267 | // update selected items
|
| 268 | selectedDevice = false;
|
| 269 | selectedUdn = false;
|
| 270 | selectedUrn = false;
|
| 271 |
|
| 272 | // update IU elements
|
| 273 | browser.removeClass('ui-helper-hidden');
|
| 274 | searching.addClass('ui-helper-hidden');
|
| 275 | deviceData.addClass('ui-helper-hidden');
|
| 276 | serviceData.addClass('ui-helper-hidden');
|
| 277 | }, 'json');
|
| 278 |
|
| 279 | return false;
|
| 280 | }
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 | $(document).ready( function() {
|
| 285 | // init elements of style
|
| 286 | searching = $('#searching');
|
| 287 | deviceData = $('#deviceData');
|
| 288 | deviceTableBody = $('#deviceTable tbody');
|
| 289 |
|
| 290 | // services
|
| 291 | serviceData = $('#serviceData');
|
| 292 | serviceDataInfoID = $('#serviceDataInfoID');
|
| 293 | serviceDataInfoType= $('#serviceDataInfoType');
|
| 294 | serviceDataVars = $('#serviceDataVars tbody');
|
| 295 |
|
| 296 | // actions
|
| 297 | actionsContainer = $('#actionsContainer');
|
| 298 | actionsSelect = actionsContainer.find('select');
|
| 299 | actionsInvoke = actionsContainer.find('button');
|
| 300 | actionsTable = actionsContainer.find('table');
|
| 301 | actionsTableBody = actionsTable.find('tbody');
|
| 302 | actionsTableRow = actionsTableBody.find('tr').clone();
|
| 303 | actionsTableBody.empty();
|
| 304 |
|
| 305 | // init navigation tree
|
| 306 | browser = $('#browser').treeview({
|
| 307 | animated: 'fast',
|
| 308 | collapsed: true,
|
| 309 | unique: true
|
| 310 | });
|
| 311 |
|
| 312 | // reload button
|
| 313 | reloadVars = $('#reloadVars').click(function() {
|
| 314 | if (selectedUdn && selectedUrn) {
|
| 315 | $.post(pluginRoot, {
|
| 316 | 'action': 'serviceDetails',
|
| 317 | 'udn' : selectedUdn,
|
| 318 | 'urn' : selectedUrn
|
| 319 | }, renderVars, 'json');
|
| 320 | }
|
| 321 | })
|
| 322 |
|
| 323 | $('#reloadDevices').click(listDevices);
|
| 324 |
|
| 325 | listDevices();
|
| 326 | });
|
| 327 |
|