OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview |
| 7 * Class representing an entry in the host-list portion of the home screen. |
| 8 */ |
| 9 |
| 10 'use strict'; |
| 11 |
| 12 /** @suppress {duplicate} */ |
| 13 var remoting = remoting || {}; |
| 14 |
| 15 /** |
| 16 * The deserialized form of the chromoting host as returned by Apiary. |
| 17 * Note that the object has more fields than are detailed below--these |
| 18 * are just the ones that we refer to directly. |
| 19 * @constructor |
| 20 */ |
| 21 remoting.Host = function() { |
| 22 /** @type {string} */ |
| 23 this.hostName = ''; |
| 24 /** @type {string} */ |
| 25 this.hostId = ''; |
| 26 /** @type {string} */ |
| 27 this.status = ''; |
| 28 /** @type {string} */ |
| 29 this.jabberId = ''; |
| 30 /** @type {string} */ |
| 31 this.publicKey = ''; |
| 32 }; |
| 33 |
| 34 /** |
| 35 * An entry in the host table. |
| 36 * @constructor |
| 37 */ |
| 38 remoting.HostTableEntry = function() { |
| 39 /** @type {remoting.Host} */ |
| 40 this.host = null; |
| 41 /** @type {Element} */ |
| 42 this.tableRow = null; |
| 43 /** @type {Element} @private */ |
| 44 this.hostNameCell_ = null; |
| 45 /** @type {function():void} @private */ |
| 46 this.onRename_ = function() {}; |
| 47 }; |
| 48 |
| 49 /** |
| 50 * Create the HTML elements for this entry. |
| 51 * @param {remoting.Host} host The host, as obtained from Apiary. |
| 52 * @param {function():void} onRename Callback for rename operations. |
| 53 * @param {function():void} onDelete Callback for delete operations. |
| 54 */ |
| 55 remoting.HostTableEntry.prototype.init = function(host, onRename, onDelete) { |
| 56 this.host = host; |
| 57 this.onRename_ = onRename; |
| 58 |
| 59 /** @type {remoting.HostTableEntry} */ |
| 60 var that = this; |
| 61 |
| 62 this.tableRow = document.createElement('tr'); |
| 63 addClass(this.tableRow, 'host-list-row'); |
| 64 |
| 65 // Create the host icon cell. |
| 66 var hostIcon = document.createElement('td'); |
| 67 addClass(hostIcon, 'host-list-row-start'); |
| 68 var hostIconImage = document.createElement('img'); |
| 69 hostIconImage.src = 'icon_host.png'; |
| 70 addClass(hostIconImage, 'host-list-main-icon'); |
| 71 hostIcon.appendChild(hostIconImage); |
| 72 this.tableRow.appendChild(hostIcon); |
| 73 |
| 74 // Create the host name cell. |
| 75 this.hostNameCell_ = document.createElement('td'); |
| 76 addClass(this.hostNameCell_, 'mode-select-label'); |
| 77 this.hostNameCell_.appendChild( |
| 78 document.createTextNode(host.hostName)); |
| 79 this.hostNameCell_.ondblclick = function() { that.beginRename_(); }; |
| 80 this.tableRow.appendChild(this.hostNameCell_); |
| 81 |
| 82 // Create the host status cell. |
| 83 var hostStatus = document.createElement('td'); |
| 84 if (host.status == 'ONLINE') { |
| 85 var connectButton = document.createElement('button'); |
| 86 connectButton.setAttribute('class', 'mode-select-button'); |
| 87 connectButton.setAttribute('type', 'button'); |
| 88 connectButton.setAttribute('onclick', |
| 89 'remoting.connectHost("' + host.hostId + '")'); |
| 90 connectButton.innerHTML = |
| 91 chrome.i18n.getMessage(/*i18n-content*/'CONNECT_BUTTON'); |
| 92 hostStatus.appendChild(connectButton); |
| 93 } else { |
| 94 addClass(this.tableRow, 'host-offline'); |
| 95 hostStatus.innerHTML = chrome.i18n.getMessage(/*i18n-content*/'OFFLINE'); |
| 96 } |
| 97 hostStatus.className = 'host-list-row-end'; |
| 98 this.tableRow.appendChild(hostStatus); |
| 99 |
| 100 // Create the host rename cell. |
| 101 var editButton = document.createElement('td'); |
| 102 editButton.onclick = function() { that.beginRename_(); }; |
| 103 addClass(editButton, 'clickable'); |
| 104 addClass(editButton, 'host-list-edit'); |
| 105 var penImage = document.createElement('img'); |
| 106 penImage.src = 'icon_pencil.png'; |
| 107 addClass(penImage, 'host-list-rename-icon'); |
| 108 editButton.appendChild(penImage); |
| 109 this.tableRow.appendChild(editButton); |
| 110 |
| 111 // Create the host delete cell. |
| 112 var removeButton = document.createElement('td'); |
| 113 removeButton.onclick = onDelete; |
| 114 addClass(removeButton, 'clickable'); |
| 115 addClass(removeButton, 'host-list-edit'); |
| 116 var crossImage = document.createElement('img'); |
| 117 crossImage.src = 'icon_cross.png'; |
| 118 addClass(crossImage, 'host-list-remove-icon'); |
| 119 removeButton.appendChild(crossImage); |
| 120 this.tableRow.appendChild(removeButton); |
| 121 }; |
| 122 |
| 123 /** |
| 124 * Prepare the host for renaming by replacing its name with an edit box. |
| 125 * @return {void} Nothing. |
| 126 * @private |
| 127 */ |
| 128 remoting.HostTableEntry.prototype.beginRename_ = function() { |
| 129 var editBox = /** @type {HTMLInputElement} */ document.createElement('input'); |
| 130 editBox.type = 'text'; |
| 131 editBox.value = this.host.hostName; |
| 132 this.hostNameCell_.innerHTML = ''; |
| 133 this.hostNameCell_.appendChild(editBox); |
| 134 editBox.select(); |
| 135 |
| 136 /** @type {remoting.HostTableEntry} */ |
| 137 var that = this; |
| 138 editBox.onblur = function() { that.commitRename_(); }; |
| 139 |
| 140 /** @param {Event} event */ |
| 141 var onKeydown = function(event) { that.onKeydown_(event); } |
| 142 editBox.onkeydown = onKeydown; |
| 143 }; |
| 144 |
| 145 /** |
| 146 * Accept the hostname entered by the user. |
| 147 * @return {void} Nothing. |
| 148 * @private |
| 149 */ |
| 150 remoting.HostTableEntry.prototype.commitRename_ = function() { |
| 151 var editBox = this.hostNameCell_.querySelector('input'); |
| 152 if (editBox) { |
| 153 if (this.host.hostName != editBox.value) { |
| 154 this.host.hostName = editBox.value; |
| 155 this.removeEditBox_(); |
| 156 this.onRename_(); |
| 157 return; |
| 158 } |
| 159 } |
| 160 }; |
| 161 |
| 162 /** |
| 163 * Remove the edit box corresponding to the specified host, and reset its name. |
| 164 * @return {void} Nothing. |
| 165 * @private |
| 166 */ |
| 167 remoting.HostTableEntry.prototype.removeEditBox_ = function() { |
| 168 var editBox = this.hostNameCell_.querySelector('input'); |
| 169 if (editBox) { |
| 170 // onblur will fire when the edit box is removed, so remove the hook. |
| 171 editBox.onblur = null; |
| 172 } |
| 173 this.hostNameCell_.innerHTML = ''; |
| 174 this.hostNameCell_.appendChild(document.createTextNode(this.host.hostName)); |
| 175 }; |
| 176 |
| 177 /** |
| 178 * Handle a key event while the user is typing a host name |
| 179 * @param {Event} event The keyboard event. |
| 180 * @return {void} Nothing. |
| 181 * @private |
| 182 */ |
| 183 remoting.HostTableEntry.prototype.onKeydown_ = function(event) { |
| 184 if (event.which == 27) { // Escape |
| 185 this.removeEditBox_(); |
| 186 } else if (event.which == 13) { // Enter |
| 187 this.commitRename_(); |
| 188 } |
| 189 }; |
OLD | NEW |