OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 to track of connections to and from this computer and display them. | |
8 */ | |
9 | |
10 'use strict'; | |
11 | |
12 /** @suppress {duplicate} */ | |
13 var remoting = remoting || {}; | |
14 | |
15 /** @constructor */ | |
16 remoting.ConnectionHistory = function() { | |
17 /** @type {HTMLElement} @private */ | |
18 this.viewAll_ = document.getElementById('history-view-all'); | |
19 /** @type {HTMLElement} @private */ | |
20 this.viewOutgoing_ = document.getElementById('history-view-outgoing'); | |
21 /** @type {HTMLElement} @private */ | |
22 this.viewIncoming_ = document.getElementById('history-view-incoming'); | |
23 /** @type {HTMLElement} @private */ | |
24 this.clear_ = document.getElementById('clear-connection-history'); | |
25 /** @type {HTMLElement} @private */ | |
26 this.historyEntries_ = document.getElementById('connection-history-entries'); | |
27 /** @type {remoting.ConnectionHistory.Filter} @private */ | |
28 this.filter_ = remoting.ConnectionHistory.Filter.VIEW_ALL; | |
29 | |
30 /** @type {remoting.ConnectionHistory} */ | |
31 var that = this; | |
32 var closeButton = document.getElementById('close-connection-history'); | |
33 closeButton.addEventListener('click', function() { that.hide(); }, false); | |
34 /** @param {Event} event Event identifying which button was clicked. */ | |
35 var setFilter = function(event) { that.setFilter_(event.target); }; | |
36 var clearHistory = function() { that.clearHistory_(); }; | |
37 this.viewAll_.addEventListener('click', setFilter, false); | |
38 this.viewOutgoing_.addEventListener('click', setFilter, false); | |
39 this.viewIncoming_.addEventListener('click', setFilter, false); | |
40 this.clear_.addEventListener('click', clearHistory, false); | |
41 }; | |
42 | |
43 /** @enum {string} */ | |
44 remoting.ConnectionHistory.Filter = { | |
45 VIEW_ALL: 'history-view-all', | |
46 VIEW_OUTGOING: 'history-view-outgoing', | |
47 VIEW_INCOMING: 'history-view-incoming' | |
48 }; | |
49 | |
50 /** Show the dialog and refresh its contents */ | |
51 remoting.ConnectionHistory.prototype.show = function() { | |
52 this.load(); | |
53 remoting.setMode(remoting.AppMode.HISTORY); | |
54 }; | |
55 | |
56 /** Hide the dialog */ | |
57 remoting.ConnectionHistory.prototype.hide = function() { | |
58 remoting.setMode(remoting.AppMode.HOME); | |
59 }; | |
60 | |
61 /** | |
62 * A saved entry in the connection history. | |
63 * @param {Object} object A Javascript object, which may or may not be of the | |
64 * correct type. | |
65 * @constructor | |
66 */ | |
67 remoting.ConnectionHistory.Entry = function(object) { | |
68 this.valid = | |
69 'date' in object && typeof(object['date']) == 'number' && | |
70 'from' in object && typeof(object['from']) == 'string' && | |
71 'to' in object && typeof(object['to']) == 'string' && | |
72 'duration' in object && typeof(object['duration']) == 'number'; | |
73 if (this.valid) { | |
74 /** @type {Date} */ | |
75 this.date = new Date(object['date']); | |
76 /** @type {string} */ | |
77 this.from = object['from']; | |
78 /** @type {string} */ | |
79 this.to = object['to']; | |
80 /** @type {number} */ | |
81 this.duration = object['duration']; | |
82 } | |
83 }; | |
84 | |
85 /** | |
86 * @return {string} The connection duration, formatted as a string, or 'Not | |
87 * available' if there is no duration stored. | |
88 * @private | |
89 */ | |
90 remoting.ConnectionHistory.Entry.prototype.durationString_ = function() { | |
91 var secs = this.duration % 60; | |
92 var mins = ((this.duration - secs) / 60) % 60; | |
93 var hours = (this.duration - secs - 60 * mins) / 3600; | |
94 if (secs < 10) { | |
95 secs = '0' + secs; | |
96 } | |
97 var result = mins + ':' + secs; | |
98 if (hours > 0) { | |
99 if (mins < 10) { | |
100 result = '0' + result; | |
101 } | |
102 result = hours + ':' + result; | |
103 } | |
104 return result; | |
105 }; | |
106 | |
107 /** | |
108 * @return {{summary: Element, detail: Element}} Two table rows containing the | |
109 * summary and detail information, respectively, for the connection. | |
110 */ | |
111 remoting.ConnectionHistory.Entry.prototype.createTableRows = function() { | |
112 var summary = document.createElement('tr'); | |
113 addClass(summary, 'connection-history-summary'); | |
114 var zippy = document.createElement('td'); | |
115 addClass(zippy, 'zippy'); | |
116 summary.appendChild(zippy); | |
117 // TODO(jamiewalch): Find a way of combining date and time such both align | |
118 // vertically without being considered separate columns, which puts too much | |
119 // space between them. | |
garykac
2012/03/02 00:53:56
For the 2-column case, shouldn't the space between
Jamie
2012/03/02 01:11:05
<td> has no margin IIRC. I tried your suggestion o
| |
120 var date = document.createElement('td'); | |
121 // TODO(jamiewalch): Use a shorter localized version of the date. | |
122 date.innerText = this.date.toLocaleDateString(); | |
123 summary.appendChild(date); | |
124 var time = document.createElement('td'); | |
125 time.innerText = this.date.toLocaleTimeString(); | |
126 summary.appendChild(time); | |
127 var from = document.createElement('td'); | |
128 from.innerText = this.from; | |
129 summary.appendChild(from); | |
130 var to = document.createElement('td'); | |
131 to.innerText = this.to; | |
132 summary.appendChild(to); | |
133 var duration = document.createElement('td'); | |
134 duration.innerText = this.durationString_(); | |
135 summary.appendChild(duration); | |
136 // TODO(jamiewalch): Fill out the detail row correctly. | |
137 var detail = document.createElement('tr'); | |
138 addClass(detail, 'connection-history-detail'); | |
139 for (var i = 0; i < summary.childElementCount; ++i) { | |
140 var td = document.createElement('td'); | |
141 if (i != 0) { | |
142 // The inner div allows the details rows to be hidden without changing | |
143 // the column widths. | |
144 var div = document.createElement('div'); | |
145 div.innerText = 'Nothing to see here'; | |
146 td.appendChild(div); | |
147 } | |
148 detail.appendChild(td); | |
149 } | |
150 /** @param {Element} node The summary row. */ | |
151 var toggleDetail = function(node) { | |
152 if (hasClass(node.className, 'expanded')) { | |
153 removeClass(node, 'expanded'); | |
154 } else { | |
155 addClass(node, 'expanded'); | |
156 } | |
157 }; | |
158 summary.addEventListener('click', | |
159 function() { toggleDetail(summary); }, | |
160 false); | |
161 detail.addEventListener('click', | |
162 function() { toggleDetail(summary); }, | |
163 false); | |
164 return { 'summary': summary, 'detail': detail }; | |
165 }; | |
166 | |
167 /** Refresh the contents of the connection history table */ | |
168 remoting.ConnectionHistory.prototype.load = function() { | |
169 // TODO(jamiewalch): Load connection history data when it's available. | |
170 var history = []; | |
171 // Remove existing entries from the DOM and repopulate. | |
172 // TODO(jamiewalch): Enforce the filter. | |
173 this.historyEntries_.innerHTML = ''; | |
174 for (var i in history) { | |
175 var connection = new remoting.ConnectionHistory.Entry(history[i]); | |
176 if (connection.valid) { | |
177 var rows = connection.createTableRows(); | |
178 this.historyEntries_.appendChild(rows.summary); | |
179 this.historyEntries_.appendChild(rows.detail); | |
180 } | |
181 } | |
182 }; | |
183 | |
184 /** | |
185 * @param {EventTarget} element The element that was clicked. | |
186 * @private | |
187 */ | |
188 remoting.ConnectionHistory.prototype.setFilter_ = function(element) { | |
189 for (var i in remoting.ConnectionHistory.Filter) { | |
190 var link = document.getElementById(remoting.ConnectionHistory.Filter[i]); | |
191 if (element == link) { | |
192 addClass(link, 'no-link'); | |
193 this.filter_ = /** @type {remoting.ConnectionHistory.Filter} */ (i); | |
194 } else { | |
195 removeClass(link, 'no-link'); | |
196 } | |
197 } | |
198 }; | |
199 | |
200 /** | |
201 * @private | |
202 */ | |
203 remoting.ConnectionHistory.prototype.clearHistory_ = function() { | |
204 // TODO(jamiewalch): Implement once we store users' connection histories. | |
205 }; | |
206 | |
207 /** @type {remoting.ConnectionHistory} */ | |
208 remoting.ConnectionHistory.connectionHistory = null; | |
OLD | NEW |