Pass table entry to rename and delete callbacks,

since closures don't work the way I thought they did.


BUG=106421
TEST=Manual


Review URL: http://codereview.chromium.org/8830001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113917 0039d316-1c4b-4281-b951-d872f2087c98
parent d984f4e4
...@@ -117,16 +117,25 @@ remoting.HostList.prototype.setHosts_ = function(hosts) { ...@@ -117,16 +117,25 @@ remoting.HostList.prototype.setHosts_ = function(hosts) {
this.showError_(null); this.showError_(null);
this.hostTableEntries_ = []; this.hostTableEntries_ = [];
/** @type {remoting.HostList} */ /**
* @type {remoting.HostList}
*/
var that = this; var that = this;
/**
* @param {remoting.HostTableEntry} hostTableEntry The entry being renamed.
*/
var onRename = function(hostTableEntry) { that.renameHost_(hostTableEntry); }
/**
* @param {remoting.HostTableEntry} hostTableEntry The entry beign deleted.
*/
var onDelete = function(hostTableEntry) { that.deleteHost_(hostTableEntry); }
for (var i = 0; i < hosts.length; ++i) { for (var i = 0; i < hosts.length; ++i) {
/** @type {remoting.Host} */ /** @type {remoting.Host} */
var host = hosts[i]; var host = hosts[i];
// Validate the entry to make sure it has all the fields we expect. // Validate the entry to make sure it has all the fields we expect.
if (host.hostName && host.hostId && host.status && host.jabberId && if (host.hostName && host.hostId && host.status && host.jabberId &&
host.publicKey) { host.publicKey) {
var onRename = function() { that.renameHost_(host.hostId); }
var onDelete = function() { that.deleteHost_(host.hostId); }
var hostTableEntry = new remoting.HostTableEntry(); var hostTableEntry = new remoting.HostTableEntry();
hostTableEntry.init(host, onRename, onDelete); hostTableEntry.init(host, onRename, onDelete);
this.hostTableEntries_[i] = hostTableEntry; this.hostTableEntries_[i] = hostTableEntry;
...@@ -173,21 +182,14 @@ remoting.HostList.prototype.showOrHide_ = function(show) { ...@@ -173,21 +182,14 @@ remoting.HostList.prototype.showOrHide_ = function(show) {
/** /**
* Remove a host from the list, and deregister it. * Remove a host from the list, and deregister it.
* @param {string} hostId The id of the host to be removed. * @param {remoting.HostTableEntry} hostTableEntry The host to be removed.
* @return {void} Nothing. * @return {void} Nothing.
* @private * @private
*/ */
remoting.HostList.prototype.deleteHost_ = function(hostId) { remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) {
/** @type {remoting.HostTableEntry} */
var hostTableEntry = this.getHostForId(hostId);
if (!hostTableEntry) {
console.error('No host registered for id ' + hostId);
return;
}
this.table_.removeChild(hostTableEntry.tableRow); this.table_.removeChild(hostTableEntry.tableRow);
var index = this.hostTableEntries_.indexOf(hostTableEntry); var index = this.hostTableEntries_.indexOf(hostTableEntry);
if (index != -1) { // Since we've just found it, index must be >= 0 if (index != -1) {
this.hostTableEntries_.splice(index, 1); this.hostTableEntries_.splice(index, 1);
} }
...@@ -195,7 +197,8 @@ remoting.HostList.prototype.deleteHost_ = function(hostId) { ...@@ -195,7 +197,8 @@ remoting.HostList.prototype.deleteHost_ = function(hostId) {
var deleteHost = function(token) { var deleteHost = function(token) {
var headers = { 'Authorization': 'OAuth ' + token }; var headers = { 'Authorization': 'OAuth ' + token };
remoting.xhr.remove( remoting.xhr.remove(
'https://www.googleapis.com/chromoting/v1/@me/hosts/' + hostId, 'https://www.googleapis.com/chromoting/v1/@me/hosts/' +
hostTableEntry.host.hostId,
function() {}, '', headers); function() {}, '', headers);
} }
remoting.oauth2.callWithToken(deleteHost); remoting.oauth2.callWithToken(deleteHost);
...@@ -205,17 +208,11 @@ remoting.HostList.prototype.deleteHost_ = function(hostId) { ...@@ -205,17 +208,11 @@ remoting.HostList.prototype.deleteHost_ = function(hostId) {
/** /**
* Prepare a host for renaming by replacing its name with an edit box. * Prepare a host for renaming by replacing its name with an edit box.
* @param {string} hostId The id of the host to be renamed. * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed.
* @return {void} Nothing. * @return {void} Nothing.
* @private * @private
*/ */
remoting.HostList.prototype.renameHost_ = function(hostId) { remoting.HostList.prototype.renameHost_ = function(hostTableEntry) {
/** @type {remoting.HostTableEntry} */
var hostTableEntry = this.getHostForId(hostId);
if (!hostTableEntry) {
console.error('No host registered for id ' + hostId);
return;
}
/** @param {string} token */ /** @param {string} token */
var renameHost = function(token) { var renameHost = function(token) {
var headers = { var headers = {
...@@ -228,7 +225,8 @@ remoting.HostList.prototype.renameHost_ = function(hostId) { ...@@ -228,7 +225,8 @@ remoting.HostList.prototype.renameHost_ = function(hostId) {
publicKey: hostTableEntry.host.publicKey publicKey: hostTableEntry.host.publicKey
} }; } };
remoting.xhr.put( remoting.xhr.put(
'https://www.googleapis.com/chromoting/v1/@me/hosts/' + hostId, 'https://www.googleapis.com/chromoting/v1/@me/hosts/' +
hostTableEntry.host.hostId,
function(xhr) {}, function(xhr) {},
JSON.stringify(newHostDetails), JSON.stringify(newHostDetails),
headers); headers);
......
...@@ -42,15 +42,17 @@ remoting.HostTableEntry = function() { ...@@ -42,15 +42,17 @@ remoting.HostTableEntry = function() {
this.tableRow = null; this.tableRow = null;
/** @type {Element} @private */ /** @type {Element} @private */
this.hostNameCell_ = null; this.hostNameCell_ = null;
/** @type {function():void} @private */ /** @type {function(remoting.HostTableEntry):void} @private */
this.onRename_ = function() {}; this.onRename_ = function(hostId) {};
}; };
/** /**
* Create the HTML elements for this entry. * Create the HTML elements for this entry.
* @param {remoting.Host} host The host, as obtained from Apiary. * @param {remoting.Host} host The host, as obtained from Apiary.
* @param {function():void} onRename Callback for rename operations. * @param {function(remoting.HostTableEntry):void} onRename Callback for
* @param {function():void} onDelete Callback for delete operations. * rename operations.
* @param {function(remoting.HostTableEntry):void} onDelete Callback for
* delete operations.
*/ */
remoting.HostTableEntry.prototype.init = function(host, onRename, onDelete) { remoting.HostTableEntry.prototype.init = function(host, onRename, onDelete) {
this.host = host; this.host = host;
...@@ -114,15 +116,15 @@ remoting.HostTableEntry.prototype.init = function(host, onRename, onDelete) { ...@@ -114,15 +116,15 @@ remoting.HostTableEntry.prototype.init = function(host, onRename, onDelete) {
this.tableRow.appendChild(editButton); this.tableRow.appendChild(editButton);
// Create the host delete cell. // Create the host delete cell.
var removeButton = document.createElement('td'); var deleteButton = document.createElement('td');
removeButton.onclick = onDelete; deleteButton.onclick = function() { onDelete(that); }
addClass(removeButton, 'clickable'); addClass(deleteButton, 'clickable');
addClass(removeButton, 'host-list-edit'); addClass(deleteButton, 'host-list-edit');
var crossImage = document.createElement('img'); var crossImage = document.createElement('img');
crossImage.src = 'icon_cross.png'; crossImage.src = 'icon_cross.png';
addClass(crossImage, 'host-list-remove-icon'); addClass(crossImage, 'host-list-remove-icon');
removeButton.appendChild(crossImage); deleteButton.appendChild(crossImage);
this.tableRow.appendChild(removeButton); this.tableRow.appendChild(deleteButton);
}; };
/** /**
...@@ -157,7 +159,7 @@ remoting.HostTableEntry.prototype.commitRename_ = function() { ...@@ -157,7 +159,7 @@ remoting.HostTableEntry.prototype.commitRename_ = function() {
if (editBox) { if (editBox) {
if (this.host.hostName != editBox.value) { if (this.host.hostName != editBox.value) {
this.host.hostName = editBox.value; this.host.hostName = editBox.value;
this.onRename_(); this.onRename_(this);
} }
this.removeEditBox_(); this.removeEditBox_();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment