| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Javascript for local_discovery.html, served from chrome://devices/ | 6 * Javascript for local_discovery.html, served from chrome://devices/ |
| 7 * This is used to show discoverable devices near the user. | 7 * This is used to show discoverable devices near the user. |
| 8 * | 8 * |
| 9 * The simple object defined in this javascript file listens for | 9 * The simple object defined in this javascript file listens for |
| 10 * callbacks from the C++ code saying that a new device is available. | 10 * callbacks from the C++ code saying that a new device is available. |
| 11 */ | 11 */ |
| 12 | 12 |
| 13 cr.define('local_discovery', function() { | 13 cr.define('local_discovery', function() { |
| 14 'use strict'; | 14 'use strict'; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Add a text TD to a TR. |
| 18 * @param {HTMLTableRowElement} row Row in table to be filled. |
| 19 * @param {string} text Text of TD. |
| 20 */ |
| 21 function textTD(row, text) { |
| 22 var td = document.createElement('td'); |
| 23 td.textContent = text; |
| 24 row.appendChild(td); |
| 25 } |
| 26 |
| 27 /** |
| 28 * Add a button TD to a TR. |
| 29 * @param {HTMLTableRowElement} row Row in table to be filled. |
| 30 * @param {string} text Text of button. |
| 31 * @param {function()} action Action to be taken when button is pressed. |
| 32 */ |
| 33 function buttonTD(row, text, action) { |
| 34 var td = document.createElement('td'); |
| 35 var button = document.createElement('button'); |
| 36 button.textContent = text; |
| 37 button.addEventListener('click', action); |
| 38 td.appendChild(button); |
| 39 row.appendChild(td); |
| 40 } |
| 41 |
| 42 /** |
| 43 * Fill a table row from the provided information. |
| 44 * @param {HTMLTableRowElement} row Row in table to be filled. |
| 45 * @param {string} name Name of the device. |
| 46 * @param {Object} info Information about the device. |
| 47 */ |
| 48 function fillRow(row, name, info) { |
| 49 textTD(row, name); |
| 50 textTD(row, info.domain); |
| 51 textTD(row, info.port); |
| 52 textTD(row, info.ip); |
| 53 textTD(row, info.lastSeen); |
| 54 |
| 55 if (!info.registered) { |
| 56 buttonTD(row, loadTimeData.getString('serviceRegister'), |
| 57 sendRegisterDevice.bind(null, name)); |
| 58 } else { |
| 59 textTD(row, loadTimeData.getString('registered')); |
| 60 } |
| 61 |
| 62 buttonTD(row, loadTimeData.getString('serviceInfo'), |
| 63 sendInfoRequest.bind(null, name)); |
| 64 } |
| 65 |
| 66 /** |
| 17 * Appends a row to the output table listing the new device. | 67 * Appends a row to the output table listing the new device. |
| 18 * @param {string} name Name of the device. | 68 * @param {string} name Name of the device. |
| 19 * @param {string} info Additional info of the device, if empty device need to | 69 * @param {string} info Additional info of the device, if empty device need to |
| 20 * be deleted. | 70 * be deleted. |
| 21 */ | 71 */ |
| 22 function onServiceUpdate(name, info) { | 72 function onServiceUpdate(name, info) { |
| 23 name = name.replace(/[\r\n]/g, ''); | 73 name = name.replace(/[\r\n]/g, ''); |
| 24 var table = $('devices-table'); | 74 var table = $('devices-table'); |
| 25 | 75 |
| 26 var params = []; | |
| 27 if (info) { | |
| 28 params[0] = info.domain; | |
| 29 params[1] = info.port; | |
| 30 params[2] = info.ip; | |
| 31 params[3] = info.lastSeen; | |
| 32 } | |
| 33 | |
| 34 for (var i = 0, row; row = table.rows[i]; i++) { | 76 for (var i = 0, row; row = table.rows[i]; i++) { |
| 35 if (row.cells[0].textContent == name) { | 77 if (row.cells[0].textContent == name) { |
| 36 if (!info) { | 78 if (!info) { |
| 37 // Delete service from the row. | 79 // Delete service from the row. |
| 38 table.removeChild(row); | 80 table.removeChild(row); |
| 39 } else { | 81 } else { |
| 40 // Replace existing service. | 82 // Replace existing service. |
| 41 for (var j = 0; j < params.length; j++) { | 83 while (row.firstChild) { |
| 42 row.cells[j + 1].textContent = params[j]; | 84 row.removeChild(row.firstChild); |
| 43 } | 85 } |
| 86 |
| 87 fillRow(row, name, info); |
| 88 return; |
| 44 } | 89 } |
| 45 return; | |
| 46 } | 90 } |
| 47 } | 91 } |
| 48 | 92 |
| 49 if (!info) { | 93 if (!info) { |
| 50 // Service could not be found in the table. | 94 // Service could not be found in the table. |
| 51 return; | 95 return; |
| 52 } | 96 } |
| 53 | 97 |
| 98 // Row does not exist. Create it. |
| 54 var tr = document.createElement('tr'); | 99 var tr = document.createElement('tr'); |
| 55 var td = document.createElement('td'); | |
| 56 td.textContent = name; | |
| 57 tr.appendChild(td); | |
| 58 | 100 |
| 59 for (var j = 0; j < params.length; j++) { | 101 fillRow(tr, name, info); |
| 60 td = document.createElement('td'); | |
| 61 td.textContent = params[j]; | |
| 62 tr.appendChild(td); | |
| 63 } | |
| 64 | |
| 65 td = document.createElement('td'); | |
| 66 if (!info.registered) { | |
| 67 var button = document.createElement('button'); | |
| 68 button.textContent = loadTimeData.getString('serviceRegister'); | |
| 69 button.addEventListener('click', sendRegisterDevice.bind(null, name)); | |
| 70 td.appendChild(button); | |
| 71 } else { | |
| 72 td.textContent = loadTimeData.getString('registered'); | |
| 73 } | |
| 74 tr.appendChild(td); | |
| 75 | |
| 76 td = document.createElement('td'); | |
| 77 button = document.createElement('button'); | |
| 78 button.textContent = loadTimeData.getString('serviceInfo'); | |
| 79 button.addEventListener('click', sendInfoRequest.bind(null, name)); | |
| 80 td.appendChild(button); | |
| 81 | |
| 82 tr.appendChild(td); | |
| 83 table.appendChild(tr); | 102 table.appendChild(tr); |
| 84 } | 103 } |
| 85 | 104 |
| 86 | |
| 87 /** | 105 /** |
| 88 * Adds a row to the logging console. | 106 * Adds a row to the logging console. |
| 89 * @param {string} msg The message to log. | 107 * @param {string} msg The message to log. |
| 90 */ | 108 */ |
| 91 function logToInfoConsole(msg) { | 109 function logToInfoConsole(msg) { |
| 92 var div = document.createElement('div'); | 110 var div = document.createElement('div'); |
| 93 div.textContent = msg; | 111 div.textContent = msg; |
| 94 $('info-console').appendChild(div); | 112 $('info-console').appendChild(div); |
| 95 } | 113 } |
| 96 | 114 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 }); | 270 }); |
| 253 | 271 |
| 254 return { | 272 return { |
| 255 registrationSuccess: registrationSuccess, | 273 registrationSuccess: registrationSuccess, |
| 256 registrationFailed: registrationFailed, | 274 registrationFailed: registrationFailed, |
| 257 onServiceUpdate: onServiceUpdate, | 275 onServiceUpdate: onServiceUpdate, |
| 258 infoFailed: infoFailed, | 276 infoFailed: infoFailed, |
| 259 renderInfo: renderInfo | 277 renderInfo: renderInfo |
| 260 }; | 278 }; |
| 261 }); | 279 }); |
| OLD | NEW |