OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 cr.define('chrome.sync.types', function() { | |
6 var typeCountersMap = {}; | |
7 | |
8 /** | |
9 * Redraws the counters table taking advantage of the most recent | |
10 * available information. | |
11 * | |
12 * Makes use of typeCountersMap, which is defined in the containing scope. | |
13 */ | |
14 var refreshTypeCountersDisplay = function() { | |
15 var typeCountersArray = []; | |
16 | |
17 // Transform our map into an array to make jstemplate happy. | |
18 Object.keys(typeCountersMap).sort().forEach(function(t) { | |
19 typeCountersArray.push({ | |
20 type: t, | |
21 counters: typeCountersMap[t], | |
22 }); | |
23 }); | |
24 | |
25 jstProcess( | |
26 new JsEvalContext({ rows: typeCountersArray }), | |
27 $('type-counters-table')); | |
28 }; | |
29 | |
30 /** | |
31 * Helps to initialize the table by picking up where initTypeCounters() left | |
32 * off. That function registers this listener and requests that this event | |
33 * be emitted. | |
34 * | |
35 * @param {!Object} e An event containing the list of known sync types. | |
36 */ | |
37 var onReceivedListOfTypes = function(e) { | |
38 var types = e.details.types; | |
39 types.map(function(type) { | |
40 if (!typeCountersMap.hasOwnProperty(type)) { | |
41 typeCountersMap[type] = {}; | |
42 } | |
43 }); | |
44 chrome.sync.events.removeEventListener( | |
45 'onReceivedListOfTypes', | |
46 onReceivedListOfTypes); | |
47 refreshTypeCountersDisplay(); | |
48 }; | |
49 | |
50 /** | |
51 * Callback for receipt of updated per-type counters. | |
52 * | |
53 * @param {!Object} e An event containing an updated counter. | |
54 */ | |
55 var onCountersUpdated = function(e) { | |
56 var details = e.details; | |
57 | |
58 var modelType = details.modelType; | |
59 var counters = details.counters; | |
60 | |
61 if (typeCountersMap.hasOwnProperty(modelType)) | |
62 for (k in counters) { | |
63 typeCountersMap[modelType][k] = counters[k]; | |
64 } | |
65 refreshTypeCountersDisplay(); | |
66 }; | |
67 | |
68 /** | |
69 * Initializes state and callbacks for the per-type counters and status UI. | |
70 */ | |
71 var initTypeCounters = function() { | |
72 chrome.sync.events.addEventListener( | |
73 'onCountersUpdated', | |
74 onCountersUpdated); | |
75 chrome.sync.events.addEventListener( | |
76 'onReceivedListOfTypes', | |
77 onReceivedListOfTypes); | |
78 | |
79 chrome.sync.requestListOfTypes(); | |
80 chrome.sync.registerForPerTypeCounters(); | |
81 }; | |
82 | |
83 var onLoad = function() { | |
84 initTypeCounters(); | |
85 }; | |
86 | |
87 return { | |
88 onLoad: onLoad | |
89 }; | |
90 }); | |
91 | |
92 document.addEventListener('DOMContentLoaded', chrome.sync.types.onLoad, false); | |
OLD | NEW |