Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: chrome/browser/resources/quota_internals/event_handler.js

Issue 2600683002: Run tools/clang-format-js on some of chrome/browser/resources/ (Closed)
Patch Set: hackhackhack Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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 // require cr.js 5 // require cr.js
6 // require cr/event_target.js 6 // require cr/event_target.js
7 // require cr/ui.js 7 // require cr/ui.js
8 // require cr/ui/tabs.js 8 // require cr/ui/tabs.js
9 // require cr/ui/tree.js 9 // require cr/ui/tree.js
10 // require cr/util.js 10 // require cr/util.js
11 11
12 (function() { 12 (function() {
13 'use strict'; 13 'use strict';
14 14
15 /** 15 /**
16 * @param {Object} object Object to be checked. 16 * @param {Object} object Object to be checked.
17 * @return {boolean} true if |object| is {}. 17 * @return {boolean} true if |object| is {}.
18 * @private 18 * @private
19 */ 19 */
20 function isEmptyObject_(object) { 20 function isEmptyObject_(object) {
21 for (var i in object) 21 for (var i in object)
22 return false; 22 return false;
23 return true; 23 return true;
24 } 24 }
25 25
26 /** 26 /**
27 * Copy properties from |source| to |destination|. 27 * Copy properties from |source| to |destination|.
28 * @param {Object} source Source of the copy. 28 * @param {Object} source Source of the copy.
29 * @param {Object} destination Destination of the copy. 29 * @param {Object} destination Destination of the copy.
30 * @return {Object} |destination|. 30 * @return {Object} |destination|.
31 * @private 31 * @private
32 */ 32 */
33 function copyAttributes_(source, destination) { 33 function copyAttributes_(source, destination) {
34 for (var i in source) 34 for (var i in source)
35 destination[i] = source[i]; 35 destination[i] = source[i];
36 return destination; 36 return destination;
37 } 37 }
38 38
39 /** 39 /**
40 * Apply localization to |element| with i18n_template.js if available. 40 * Apply localization to |element| with i18n_template.js if available.
41 * @param {Element} element Element to be localized. 41 * @param {Element} element Element to be localized.
42 * @private 42 * @private
43 */ 43 */
44 function localize_(element) { 44 function localize_(element) {
45 if (window.i18nTemplate && window.loadTimeData) 45 if (window.i18nTemplate && window.loadTimeData)
46 i18nTemplate.process(element, loadTimeData); 46 i18nTemplate.process(element, loadTimeData);
47 } 47 }
48 48
49 /** 49 /**
50 * Returns 'N/A' (Not Available) text if |value| is undefined. 50 * Returns 'N/A' (Not Available) text if |value| is undefined.
51 * @param {Object} value Object to print. 51 * @param {Object} value Object to print.
52 * @return {string} 'N/A' or ''. 52 * @return {string} 'N/A' or ''.
53 * @private 53 * @private
54 */ 54 */
55 function checkIfAvailable_(value) { 55 function checkIfAvailable_(value) {
56 return value === undefined ? 'N/A' : ''; 56 return value === undefined ? 'N/A' : '';
57 } 57 }
58 58
59 /** 59 /**
60 * Returns |value| itself if |value| is not undefined, 60 * Returns |value| itself if |value| is not undefined,
61 * else returns 'N/A' text. 61 * else returns 'N/A' text.
62 * @param {?string} value String to print. 62 * @param {?string} value String to print.
63 * @return {string} 'N/A' or |value|. 63 * @return {string} 'N/A' or |value|.
64 * @private 64 * @private
65 */ 65 */
66 function stringToText_(value) { 66 function stringToText_(value) {
67 return checkIfAvailable_(value) || value; 67 return checkIfAvailable_(value) || value;
68 } 68 }
69 69
70 /** 70 /**
71 * Separates |value| into segments. 71 * Separates |value| into segments.
72 * The length of first segment is at most |maxLength|. 72 * The length of first segment is at most |maxLength|.
73 * Length of other following segments are just |maxLength|. 73 * Length of other following segments are just |maxLength|.
74 * e.g. separateBackward_('abcdefghijk', 4) == ['abc','defg','hijk']; 74 * e.g. separateBackward_('abcdefghijk', 4) == ['abc','defg','hijk'];
75 * @param {string} value String to be separated. 75 * @param {string} value String to be separated.
76 * @param {number} maxLength Max length of segments. 76 * @param {number} maxLength Max length of segments.
77 * @return {Array<string>} Array of segments. 77 * @return {Array<string>} Array of segments.
78 * @private 78 * @private
79 */ 79 */
80 function separateBackward_(value, maxLength) { 80 function separateBackward_(value, maxLength) {
81 var result = []; 81 var result = [];
82 while (value.length > maxLength) { 82 while (value.length > maxLength) {
83 result.unshift(value.slice(-3)); 83 result.unshift(value.slice(-3));
84 value = value.slice(0, -3); 84 value = value.slice(0, -3);
85 }
86 result.unshift(value);
87 return result;
85 } 88 }
86 result.unshift(value);
87 return result;
88 }
89 89
90 /** 90 /**
91 * Returns formatted string from number as number of bytes. 91 * Returns formatted string from number as number of bytes.
92 * e.g. numBytesToText(123456789) = '123.45 MB (123,456,789 B)'. 92 * e.g. numBytesToText(123456789) = '123.45 MB (123,456,789 B)'.
93 * If |value| is undefined, this function returns 'N/A'. 93 * If |value| is undefined, this function returns 'N/A'.
94 * @param {?number} value Number to print. 94 * @param {?number} value Number to print.
95 * @return {string} 'N/A' or formatted |value|. 95 * @return {string} 'N/A' or formatted |value|.
96 * @private 96 * @private
97 */ 97 */
98 function numBytesToText_(value) { 98 function numBytesToText_(value) {
99 var result = checkIfAvailable_(value); 99 var result = checkIfAvailable_(value);
100 if (result) 100 if (result)
101 return result;
102
103 var segments = separateBackward_(value.toString(), 3);
104 result = segments.join(',') + ' B';
105
106 if (segments.length > 1) {
107 var UNIT = [' B', ' KB', ' MB', ' GB', ' TB', ' PB'];
108 result = segments[0] + '.' + segments[1].slice(0, 2) +
109 UNIT[Math.min(segments.length, UNIT.length) - 1] + ' (' + result +
110 ')';
111 }
112
101 return result; 113 return result;
102
103 var segments = separateBackward_(value.toString(), 3);
104 result = segments.join(',') + ' B';
105
106 if (segments.length > 1) {
107 var UNIT = [' B', ' KB', ' MB', ' GB', ' TB', ' PB'];
108 result = segments[0] + '.' + segments[1].slice(0, 2) +
109 UNIT[Math.min(segments.length, UNIT.length) - 1] +
110 ' (' + result + ')';
111 } 114 }
112 115
113 return result; 116 /**
114 }
115
116 /**
117 * Return formatted date |value| if |value| is not undefined. 117 * Return formatted date |value| if |value| is not undefined.
118 * If |value| is undefined, this function returns 'N/A'. 118 * If |value| is undefined, this function returns 'N/A'.
119 * @param {?number} value Number of milliseconds since 119 * @param {?number} value Number of milliseconds since
120 * UNIX epoch time (0:00, Jan 1, 1970, UTC). 120 * UNIX epoch time (0:00, Jan 1, 1970, UTC).
121 * @return {string} Formatted text of date or 'N/A'. 121 * @return {string} Formatted text of date or 'N/A'.
122 * @private 122 * @private
123 */ 123 */
124 function dateToText(value) { 124 function dateToText(value) {
125 var result = checkIfAvailable_(value); 125 var result = checkIfAvailable_(value);
126 if (result) 126 if (result)
127 return result;
128
129 var time = new Date(value);
130 var now = new Date();
131 var delta = Date.now() - value;
132
133 var SECOND = 1000;
134 var MINUTE = 60 * SECOND;
135 var HOUR = 60 * MINUTE;
136 var DAY = 23 * HOUR;
137 var WEEK = 7 * DAY;
138
139 var SHOW_SECOND = 5 * MINUTE;
140 var SHOW_MINUTE = 5 * HOUR;
141 var SHOW_HOUR = 3 * DAY;
142 var SHOW_DAY = 2 * WEEK;
143 var SHOW_WEEK = 3 * 30 * DAY;
144
145 if (delta < 0) {
146 result = 'access from future ';
147 } else if (delta < SHOW_SECOND) {
148 result = Math.ceil(delta / SECOND) + ' sec ago ';
149 } else if (delta < SHOW_MINUTE) {
150 result = Math.ceil(delta / MINUTE) + ' min ago ';
151 } else if (delta < SHOW_HOUR) {
152 result = Math.ceil(delta / HOUR) + ' hr ago ';
153 } else if (delta < SHOW_WEEK) {
154 result = Math.ceil(delta / DAY) + ' day ago ';
155 }
156
157 result += '(' + time.toString() + ')';
127 return result; 158 return result;
128
129 var time = new Date(value);
130 var now = new Date();
131 var delta = Date.now() - value;
132
133 var SECOND = 1000;
134 var MINUTE = 60 * SECOND;
135 var HOUR = 60 * MINUTE;
136 var DAY = 23 * HOUR;
137 var WEEK = 7 * DAY;
138
139 var SHOW_SECOND = 5 * MINUTE;
140 var SHOW_MINUTE = 5 * HOUR;
141 var SHOW_HOUR = 3 * DAY;
142 var SHOW_DAY = 2 * WEEK;
143 var SHOW_WEEK = 3 * 30 * DAY;
144
145 if (delta < 0) {
146 result = 'access from future ';
147 } else if (delta < SHOW_SECOND) {
148 result = Math.ceil(delta / SECOND) + ' sec ago ';
149 } else if (delta < SHOW_MINUTE) {
150 result = Math.ceil(delta / MINUTE) + ' min ago ';
151 } else if (delta < SHOW_HOUR) {
152 result = Math.ceil(delta / HOUR) + ' hr ago ';
153 } else if (delta < SHOW_WEEK) {
154 result = Math.ceil(delta / DAY) + ' day ago ';
155 } 159 }
156 160
157 result += '(' + time.toString() + ')'; 161 /**
158 return result; 162 * Available disk space.
159 } 163 * @type {number|undefined}
164 */
165 var availableSpace = undefined;
160 166
161 /** 167 /**
162 * Available disk space. 168 * Root of the quota data tree,
163 * @type {number|undefined} 169 * holding userdata as |treeViewObject.detail|.
164 */ 170 * @type {cr.ui.Tree}
165 var availableSpace = undefined; 171 */
172 var treeViewObject = undefined;
166 173
167 /** 174 /**
168 * Root of the quota data tree, 175 * Key-value styled statistics data.
169 * holding userdata as |treeViewObject.detail|. 176 * This WebUI does not touch contents, just show.
170 * @type {cr.ui.Tree} 177 * The value is hold as |statistics[key].detail|.
171 */ 178 * @type {Object<string,Element>}
172 var treeViewObject = undefined; 179 */
180 var statistics = {};
173 181
174 /** 182 /**
175 * Key-value styled statistics data.
176 * This WebUI does not touch contents, just show.
177 * The value is hold as |statistics[key].detail|.
178 * @type {Object<string,Element>}
179 */
180 var statistics = {};
181
182 /**
183 * Initialize and return |treeViewObject|. 183 * Initialize and return |treeViewObject|.
184 * @return {cr.ui.Tree} Initialized |treeViewObject|. 184 * @return {cr.ui.Tree} Initialized |treeViewObject|.
185 */ 185 */
186 function getTreeViewObject() { 186 function getTreeViewObject() {
187 if (!treeViewObject) { 187 if (!treeViewObject) {
188 treeViewObject = $('tree-view'); 188 treeViewObject = $('tree-view');
189 cr.ui.decorate(treeViewObject, cr.ui.Tree); 189 cr.ui.decorate(treeViewObject, cr.ui.Tree);
190 treeViewObject.detail = {payload: {}, children: {}}; 190 treeViewObject.detail = {payload: {}, children: {}};
191 treeViewObject.addEventListener('change', updateDescription); 191 treeViewObject.addEventListener('change', updateDescription);
192 }
193 return treeViewObject;
192 } 194 }
193 return treeViewObject;
194 }
195 195
196 /** 196 /**
197 * Initialize and return a tree item, that represents specified storage type. 197 * Initialize and return a tree item, that represents specified storage type.
198 * @param {!string} type Storage type. 198 * @param {!string} type Storage type.
199 * @return {cr.ui.TreeItem} Initialized |storageObject|. 199 * @return {cr.ui.TreeItem} Initialized |storageObject|.
200 */ 200 */
201 function getStorageObject(type) { 201 function getStorageObject(type) {
202 var treeViewObject = getTreeViewObject(); 202 var treeViewObject = getTreeViewObject();
203 var storageObject = treeViewObject.detail.children[type]; 203 var storageObject = treeViewObject.detail.children[type];
204 if (!storageObject) { 204 if (!storageObject) {
205 storageObject = new cr.ui.TreeItem({ 205 storageObject = new cr.ui.TreeItem(
206 label: type, 206 {label: type, detail: {payload: {}, children: {}}});
207 detail: {payload: {}, children: {}} 207 storageObject.mayHaveChildren_ = true;
208 }); 208 treeViewObject.detail.children[type] = storageObject;
209 storageObject.mayHaveChildren_ = true; 209 treeViewObject.add(storageObject);
210 treeViewObject.detail.children[type] = storageObject; 210 }
211 treeViewObject.add(storageObject); 211 return storageObject;
212 } 212 }
213 return storageObject;
214 }
215 213
216 /** 214 /**
217 * Initialize and return a tree item, that represents specified 215 * Initialize and return a tree item, that represents specified
218 * storage type and hostname. 216 * storage type and hostname.
219 * @param {!string} type Storage type. 217 * @param {!string} type Storage type.
220 * @param {!string} host Hostname. 218 * @param {!string} host Hostname.
221 * @return {cr.ui.TreeItem} Initialized |hostObject|. 219 * @return {cr.ui.TreeItem} Initialized |hostObject|.
222 */ 220 */
223 function getHostObject(type, host) { 221 function getHostObject(type, host) {
224 var storageObject = getStorageObject(type); 222 var storageObject = getStorageObject(type);
225 var hostObject = storageObject.detail.children[host]; 223 var hostObject = storageObject.detail.children[host];
226 if (!hostObject) { 224 if (!hostObject) {
227 hostObject = new cr.ui.TreeItem({ 225 hostObject = new cr.ui.TreeItem(
228 label: host, 226 {label: host, detail: {payload: {}, children: {}}});
229 detail: {payload: {}, children: {}} 227 hostObject.mayHaveChildren_ = true;
230 }); 228 storageObject.detail.children[host] = hostObject;
231 hostObject.mayHaveChildren_ = true; 229 storageObject.add(hostObject);
232 storageObject.detail.children[host] = hostObject; 230 }
233 storageObject.add(hostObject); 231 return hostObject;
234 } 232 }
235 return hostObject;
236 }
237 233
238 /** 234 /**
239 * Initialize and return a tree item, that represents specified 235 * Initialize and return a tree item, that represents specified
240 * storage type, hostname and origin url. 236 * storage type, hostname and origin url.
241 * @param {!string} type Storage type. 237 * @param {!string} type Storage type.
242 * @param {!string} host Hostname. 238 * @param {!string} host Hostname.
243 * @param {!string} origin Origin URL. 239 * @param {!string} origin Origin URL.
244 * @return {cr.ui.TreeItem} Initialized |originObject|. 240 * @return {cr.ui.TreeItem} Initialized |originObject|.
245 */ 241 */
246 function getOriginObject(type, host, origin) { 242 function getOriginObject(type, host, origin) {
247 var hostObject = getHostObject(type, host); 243 var hostObject = getHostObject(type, host);
248 var originObject = hostObject.detail.children[origin]; 244 var originObject = hostObject.detail.children[origin];
249 if (!originObject) { 245 if (!originObject) {
250 originObject = new cr.ui.TreeItem({ 246 originObject = new cr.ui.TreeItem(
251 label: origin, 247 {label: origin, detail: {payload: {}, children: {}}});
252 detail: {payload: {}, children: {}} 248 originObject.mayHaveChildren_ = false;
253 }); 249 hostObject.detail.children[origin] = originObject;
254 originObject.mayHaveChildren_ = false; 250 hostObject.add(originObject);
255 hostObject.detail.children[origin] = originObject; 251 }
256 hostObject.add(originObject); 252 return originObject;
257 } 253 }
258 return originObject; 254
259 } 255 /**
260 256 * Event Handler for |cr.quota.onAvailableSpaceUpdated|.
261 /** 257 * |event.detail| contains |availableSpace|.
262 * Event Handler for |cr.quota.onAvailableSpaceUpdated|. 258 * |availableSpace| represents total available disk space.
263 * |event.detail| contains |availableSpace|. 259 * @param {CustomEvent} event AvailableSpaceUpdated event.
264 * |availableSpace| represents total available disk space. 260 */
265 * @param {CustomEvent} event AvailableSpaceUpdated event. 261 function handleAvailableSpace(event) {
266 */ 262 /**
267 function handleAvailableSpace(event) { 263 * @type {string}
268 /** 264 */
269 * @type {string} 265 availableSpace = event.detail;
270 */ 266 $('diskspace-entry').innerHTML = numBytesToText_(availableSpace);
271 availableSpace = event.detail; 267 };
272 $('diskspace-entry').innerHTML = numBytesToText_(availableSpace); 268
273 }; 269 /**
274 270 * Event Handler for |cr.quota.onGlobalInfoUpdated|.
275 /** 271 * |event.detail| contains a record which has:
276 * Event Handler for |cr.quota.onGlobalInfoUpdated|. 272 * |type|:
277 * |event.detail| contains a record which has: 273 * Storage type, that is either 'temporary' or 'persistent'.
278 * |type|: 274 * |usage|:
279 * Storage type, that is either 'temporary' or 'persistent'. 275 * Total storage usage of all hosts.
280 * |usage|: 276 * |unlimitedUsage|:
281 * Total storage usage of all hosts. 277 * Total storage usage of unlimited-quota origins.
282 * |unlimitedUsage|: 278 * |quota|:
283 * Total storage usage of unlimited-quota origins. 279 * Total quota of the storage.
284 * |quota|: 280 *
285 * Total quota of the storage. 281 * |usage|, |unlimitedUsage| and |quota| can be missing,
286 * 282 * and some additional fields can be included.
287 * |usage|, |unlimitedUsage| and |quota| can be missing, 283 * @param {CustomEvent} event GlobalInfoUpdated event.
288 * and some additional fields can be included. 284 */
289 * @param {CustomEvent} event GlobalInfoUpdated event. 285 function handleGlobalInfo(event) {
290 */ 286 /**
291 function handleGlobalInfo(event) { 287 * @type {{
292 /** 288 * type: {!string},
293 * @type {{ 289 * usage: {?number},
294 * type: {!string}, 290 * unlimitedUsage: {?number}
295 * usage: {?number}, 291 * quota: {?string}
296 * unlimitedUsage: {?number} 292 * }}
297 * quota: {?string} 293 */
298 * }} 294 var data = event.detail;
299 */ 295 var storageObject = getStorageObject(data.type);
300 var data = event.detail; 296 copyAttributes_(data, storageObject.detail.payload);
301 var storageObject = getStorageObject(data.type); 297 storageObject.reveal();
302 copyAttributes_(data, storageObject.detail.payload); 298 if (getTreeViewObject().selectedItem == storageObject)
303 storageObject.reveal();
304 if (getTreeViewObject().selectedItem == storageObject)
305 updateDescription();
306
307 };
308
309 /**
310 * Event Handler for |cr.quota.onPerHostInfoUpdated|.
311 * |event.detail| contains records which have:
312 * |host|:
313 * Hostname of the entry. (e.g. 'example.com')
314 * |type|:
315 * Storage type. 'temporary' or 'persistent'
316 * |usage|:
317 * Total storage usage of the host.
318 * |quota|:
319 * Per-host quota.
320 *
321 * |usage| and |quota| can be missing,
322 * and some additional fields can be included.
323 * @param {CustomEvent} event PerHostInfoUpdated event.
324 */
325 function handlePerHostInfo(event) {
326 /**
327 * @type {Array<{
328 * host: {!string},
329 * type: {!string},
330 * usage: {?number},
331 * quota: {?number}
332 * }}
333 */
334 var dataArray = event.detail;
335
336 for (var i = 0; i < dataArray.length; ++i) {
337 var data = dataArray[i];
338 var hostObject = getHostObject(data.type, data.host);
339 copyAttributes_(data, hostObject.detail.payload);
340 hostObject.reveal();
341 if (getTreeViewObject().selectedItem == hostObject)
342 updateDescription(); 299 updateDescription();
343 300 };
344 } 301
345 } 302 /**
346 303 * Event Handler for |cr.quota.onPerHostInfoUpdated|.
347 /** 304 * |event.detail| contains records which have:
348 * Event Handler for |cr.quota.onPerOriginInfoUpdated|. 305 * |host|:
349 * |event.detail| contains records which have: 306 * Hostname of the entry. (e.g. 'example.com')
350 * |origin|: 307 * |type|:
351 * Origin URL of the entry. 308 * Storage type. 'temporary' or 'persistent'
352 * |type|: 309 * |usage|:
353 * Storage type of the entry. 'temporary' or 'persistent'. 310 * Total storage usage of the host.
354 * |host|: 311 * |quota|:
355 * Hostname of the entry. 312 * Per-host quota.
356 * |inUse|: 313 *
357 * true if the origin is in use. 314 * |usage| and |quota| can be missing,
358 * |usedCount|: 315 * and some additional fields can be included.
359 * Used count of the storage from the origin. 316 * @param {CustomEvent} event PerHostInfoUpdated event.
360 * |lastAccessTime|: 317 */
361 * Last storage access time from the origin. 318 function handlePerHostInfo(event) {
362 * Number of milliseconds since UNIX epoch (Jan 1, 1970, 0:00:00 UTC). 319 /**
363 * |lastModifiedTime|: 320 * @type {Array<{
364 * Last modified time of the storage from the origin. 321 * host: {!string},
365 * Number of milliseconds since UNIX epoch. 322 * type: {!string},
366 * 323 * usage: {?number},
367 * |inUse|, |usedCount|, |lastAccessTime| and |lastModifiedTime| can be missing, 324 * quota: {?number}
368 * and some additional fields can be included. 325 * }}
369 * @param {CustomEvent} event PerOriginInfoUpdated event. 326 */
370 */ 327 var dataArray = event.detail;
371 function handlePerOriginInfo(event) { 328
372 /** 329 for (var i = 0; i < dataArray.length; ++i) {
373 * @type {Array<{ 330 var data = dataArray[i];
374 * origin: {!string}, 331 var hostObject = getHostObject(data.type, data.host);
375 * type: {!string}, 332 copyAttributes_(data, hostObject.detail.payload);
376 * host: {!string}, 333 hostObject.reveal();
377 * inUse: {?boolean}, 334 if (getTreeViewObject().selectedItem == hostObject)
378 * usedCount: {?number}, 335 updateDescription();
379 * lastAccessTime: {?number} 336 }
380 * lastModifiedTime: {?number} 337 }
381 * }>} 338
382 */ 339 /**
383 var dataArray = event.detail; 340 * Event Handler for |cr.quota.onPerOriginInfoUpdated|.
384 341 * |event.detail| contains records which have:
385 for (var i = 0; i < dataArray.length; ++i) { 342 * |origin|:
386 var data = dataArray[i]; 343 * Origin URL of the entry.
387 var originObject = getOriginObject(data.type, data.host, data.origin); 344 * |type|:
388 copyAttributes_(data, originObject.detail.payload); 345 * Storage type of the entry. 'temporary' or 'persistent'.
389 originObject.reveal(); 346 * |host|:
390 if (getTreeViewObject().selectedItem == originObject) 347 * Hostname of the entry.
391 updateDescription(); 348 * |inUse|:
392 } 349 * true if the origin is in use.
393 } 350 * |usedCount|:
394 351 * Used count of the storage from the origin.
395 /** 352 * |lastAccessTime|:
396 * Event Handler for |cr.quota.onStatisticsUpdated|. 353 * Last storage access time from the origin.
397 * |event.detail| contains misc statistics data as dictionary. 354 * Number of milliseconds since UNIX epoch (Jan 1, 1970, 0:00:00 UTC).
398 * @param {CustomEvent} event StatisticsUpdated event. 355 * |lastModifiedTime|:
399 */ 356 * Last modified time of the storage from the origin.
400 function handleStatistics(event) { 357 * Number of milliseconds since UNIX epoch.
401 /** 358 *
402 * @type {Object<string>} 359 * |inUse|, |usedCount|, |lastAccessTime| and |lastModifiedTime| can be
403 */ 360 * missing,
404 var data = event.detail; 361 * and some additional fields can be included.
405 for (var key in data) { 362 * @param {CustomEvent} event PerOriginInfoUpdated event.
406 var entry = statistics[key]; 363 */
407 if (!entry) { 364 function handlePerOriginInfo(event) {
408 entry = cr.doc.createElement('tr'); 365 /**
409 $('stat-entries').appendChild(entry); 366 * @type {Array<{
410 statistics[key] = entry; 367 * origin: {!string},
411 } 368 * type: {!string},
412 entry.detail = data[key]; 369 * host: {!string},
413 entry.innerHTML = 370 * inUse: {?boolean},
414 '<td>' + stringToText_(key) + '</td>' + 371 * usedCount: {?number},
415 '<td>' + stringToText_(entry.detail) + '</td>'; 372 * lastAccessTime: {?number}
416 localize_(entry); 373 * lastModifiedTime: {?number}
417 } 374 * }>}
418 } 375 */
419 376 var dataArray = event.detail;
420 /** 377
421 * Update description on 'tree-item-description' field with 378 for (var i = 0; i < dataArray.length; ++i) {
422 * selected item in tree view. 379 var data = dataArray[i];
423 */ 380 var originObject = getOriginObject(data.type, data.host, data.origin);
424 function updateDescription() { 381 copyAttributes_(data, originObject.detail.payload);
425 var item = getTreeViewObject().selectedItem; 382 originObject.reveal();
426 var tbody = $('tree-item-description'); 383 if (getTreeViewObject().selectedItem == originObject)
427 tbody.innerHTML = ''; 384 updateDescription();
428 385 }
429 if (item) { 386 }
430 var keyAndLabel = [['type', 'Storage Type'], 387
431 ['host', 'Host Name'], 388 /**
432 ['origin', 'Origin URL'], 389 * Event Handler for |cr.quota.onStatisticsUpdated|.
433 ['usage', 'Total Storage Usage', numBytesToText_], 390 * |event.detail| contains misc statistics data as dictionary.
434 ['unlimitedUsage', 'Usage of Unlimited Origins', 391 * @param {CustomEvent} event StatisticsUpdated event.
435 numBytesToText_], 392 */
436 ['quota', 'Quota', numBytesToText_], 393 function handleStatistics(event) {
437 ['inUse', 'Origin is in use?'], 394 /**
438 ['usedCount', 'Used count'], 395 * @type {Object<string>}
439 ['lastAccessTime', 'Last Access Time', 396 */
440 dateToText], 397 var data = event.detail;
441 ['lastModifiedTime', 'Last Modified Time', 398 for (var key in data) {
442 dateToText] 399 var entry = statistics[key];
443 ]; 400 if (!entry) {
444 for (var i = 0; i < keyAndLabel.length; ++i) { 401 entry = cr.doc.createElement('tr');
445 var key = keyAndLabel[i][0]; 402 $('stat-entries').appendChild(entry);
446 var label = keyAndLabel[i][1]; 403 statistics[key] = entry;
447 var entry = item.detail.payload[key]; 404 }
448 if (entry === undefined) 405 entry.detail = data[key];
449 continue; 406 entry.innerHTML = '<td>' + stringToText_(key) + '</td>' +
450 407 '<td>' + stringToText_(entry.detail) + '</td>';
451 var normalize = keyAndLabel[i][2] || stringToText_; 408 localize_(entry);
452 409 }
453 var row = cr.doc.createElement('tr'); 410 }
454 row.innerHTML = 411
455 '<td>' + label + '</td>' + 412 /**
456 '<td>' + normalize(entry) + '</td>'; 413 * Update description on 'tree-item-description' field with
457 localize_(row); 414 * selected item in tree view.
458 tbody.appendChild(row); 415 */
459 } 416 function updateDescription() {
460 } 417 var item = getTreeViewObject().selectedItem;
461 } 418 var tbody = $('tree-item-description');
462 419 tbody.innerHTML = '';
463 /** 420
421 if (item) {
422 var keyAndLabel = [
423 ['type', 'Storage Type'], ['host', 'Host Name'],
424 ['origin', 'Origin URL'],
425 ['usage', 'Total Storage Usage', numBytesToText_],
426 ['unlimitedUsage', 'Usage of Unlimited Origins', numBytesToText_],
427 ['quota', 'Quota', numBytesToText_], ['inUse', 'Origin is in use?'],
428 ['usedCount', 'Used count'],
429 ['lastAccessTime', 'Last Access Time', dateToText],
430 ['lastModifiedTime', 'Last Modified Time', dateToText]
431 ];
432 for (var i = 0; i < keyAndLabel.length; ++i) {
433 var key = keyAndLabel[i][0];
434 var label = keyAndLabel[i][1];
435 var entry = item.detail.payload[key];
436 if (entry === undefined)
437 continue;
438
439 var normalize = keyAndLabel[i][2] || stringToText_;
440
441 var row = cr.doc.createElement('tr');
442 row.innerHTML = '<td>' + label + '</td>' +
443 '<td>' + normalize(entry) + '</td>';
444 localize_(row);
445 tbody.appendChild(row);
446 }
447 }
448 }
449
450 /**
464 * Dump |treeViewObject| or subtree to a object. 451 * Dump |treeViewObject| or subtree to a object.
465 * @param {?{cr.ui.Tree|cr.ui.TreeItem}} opt_treeitem 452 * @param {?{cr.ui.Tree|cr.ui.TreeItem}} opt_treeitem
466 * @return {Object} Dump result object from |treeViewObject|. 453 * @return {Object} Dump result object from |treeViewObject|.
467 */ 454 */
468 function dumpTreeToObj(opt_treeitem) { 455 function dumpTreeToObj(opt_treeitem) {
469 var treeitem = opt_treeitem || getTreeViewObject(); 456 var treeitem = opt_treeitem || getTreeViewObject();
470 var res = {}; 457 var res = {};
471 res.payload = treeitem.detail.payload; 458 res.payload = treeitem.detail.payload;
472 res.children = []; 459 res.children = [];
473 for (var i in treeitem.detail.children) { 460 for (var i in treeitem.detail.children) {
474 var child = treeitem.detail.children[i]; 461 var child = treeitem.detail.children[i];
475 res.children.push(dumpTreeToObj(child)); 462 res.children.push(dumpTreeToObj(child));
476 } 463 }
477 464
478 if (isEmptyObject_(res.payload)) 465 if (isEmptyObject_(res.payload))
479 delete res.payload; 466 delete res.payload;
480 467
481 if (res.children.length == 0) 468 if (res.children.length == 0)
482 delete res.children; 469 delete res.children;
483 return res; 470 return res;
484 } 471 }
485 472
486 /** 473 /**
487 * Dump |statistics| to a object. 474 * Dump |statistics| to a object.
488 * @return {Object} Dump result object from |statistics|. 475 * @return {Object} Dump result object from |statistics|.
489 */ 476 */
490 function dumpStatisticsToObj() { 477 function dumpStatisticsToObj() {
491 var result = {}; 478 var result = {};
492 for (var key in statistics) 479 for (var key in statistics)
493 result[key] = statistics[key].detail; 480 result[key] = statistics[key].detail;
494 return result; 481 return result;
495 } 482 }
496 483
497 /** 484 /**
498 * Event handler for 'dump-button' 'click'ed. 485 * Event handler for 'dump-button' 'click'ed.
499 * Dump and show all data from WebUI page to 'dump-field' element. 486 * Dump and show all data from WebUI page to 'dump-field' element.
500 */ 487 */
501 function dump() { 488 function dump() {
502 var separator = '========\n'; 489 var separator = '========\n';
503 490
504 $('dump-field').textContent = 491 $('dump-field').textContent = separator + 'Summary\n' + separator +
505 separator + 492 JSON.stringify({availableSpace: availableSpace}, null, 2) + '\n' +
506 'Summary\n' + 493 separator + 'Usage And Quota\n' + separator +
507 separator + 494 JSON.stringify(dumpTreeToObj(), null, 2) + '\n' + separator +
508 JSON.stringify({availableSpace: availableSpace}, null, 2) + '\n' + 495 'Misc Statistics\n' + separator +
509 separator + 496 JSON.stringify(dumpStatisticsToObj(), null, 2);
510 'Usage And Quota\n' + 497 }
511 separator + 498
512 JSON.stringify(dumpTreeToObj(), null, 2) + '\n' + 499 function onLoad() {
513 separator + 500 cr.ui.decorate('tabbox', cr.ui.TabBox);
514 'Misc Statistics\n' + 501
515 separator + 502 cr.quota.onAvailableSpaceUpdated.addEventListener(
516 JSON.stringify(dumpStatisticsToObj(), null, 2); 503 'update', handleAvailableSpace);
517 } 504 cr.quota.onGlobalInfoUpdated.addEventListener('update', handleGlobalInfo);
518 505 cr.quota.onPerHostInfoUpdated.addEventListener('update', handlePerHostInfo);
519 function onLoad() { 506 cr.quota.onPerOriginInfoUpdated.addEventListener(
520 cr.ui.decorate('tabbox', cr.ui.TabBox); 507 'update', handlePerOriginInfo);
521 508 cr.quota.onStatisticsUpdated.addEventListener('update', handleStatistics);
522 cr.quota.onAvailableSpaceUpdated.addEventListener('update', 509 cr.quota.requestInfo();
523 handleAvailableSpace); 510
524 cr.quota.onGlobalInfoUpdated.addEventListener('update', handleGlobalInfo); 511 $('refresh-button').addEventListener('click', cr.quota.requestInfo, false);
525 cr.quota.onPerHostInfoUpdated.addEventListener('update', handlePerHostInfo); 512 $('dump-button').addEventListener('click', dump, false);
526 cr.quota.onPerOriginInfoUpdated.addEventListener('update', 513 }
527 handlePerOriginInfo); 514
528 cr.quota.onStatisticsUpdated.addEventListener('update', handleStatistics); 515 cr.doc.addEventListener('DOMContentLoaded', onLoad, false);
529 cr.quota.requestInfo();
530
531 $('refresh-button').addEventListener('click', cr.quota.requestInfo, false);
532 $('dump-button').addEventListener('click', dump, false);
533 }
534
535 cr.doc.addEventListener('DOMContentLoaded', onLoad, false);
536 })(); 516 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698