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.
5 cr
.define('chrome.sync.types', function() {
6 var typeCountersMap
= {};
9 * Redraws the counters table taking advantage of the most recent
10 * available information.
12 * Makes use of typeCountersMap, which is defined in the containing scope.
14 var refreshTypeCountersDisplay = function() {
15 var typeCountersArray
= [];
17 // Transform our map into an array to make jstemplate happy.
18 Object
.keys(typeCountersMap
).sort().forEach(function(t
) {
19 typeCountersArray
.push({
21 counters
: typeCountersMap
[t
],
26 new JsEvalContext({ rows
: typeCountersArray
}),
27 $('type-counters-table'));
31 * Helps to initialize the table by picking up where initTypeCounters() left
32 * off. That function registers this listener and requests that this event
35 * @param {!Object} e An event containing the list of known sync types.
37 var onReceivedListOfTypes = function(e
) {
38 var types
= e
.details
.types
;
39 types
.map(function(type
) {
40 if (!typeCountersMap
.hasOwnProperty(type
)) {
41 typeCountersMap
[type
] = {};
44 chrome
.sync
.events
.removeEventListener(
45 'onReceivedListOfTypes',
46 onReceivedListOfTypes
);
47 refreshTypeCountersDisplay();
51 * Callback for receipt of updated per-type counters.
53 * @param {!Object} e An event containing an updated counter.
55 var onCountersUpdated = function(e
) {
56 var details
= e
.details
;
58 var modelType
= details
.modelType
;
59 var counters
= details
.counters
;
61 if (typeCountersMap
.hasOwnProperty(modelType
))
63 typeCountersMap
[modelType
][k
] = counters
[k
];
65 refreshTypeCountersDisplay();
69 * Initializes state and callbacks for the per-type counters and status UI.
71 var initTypeCounters = function() {
72 chrome
.sync
.events
.addEventListener(
75 chrome
.sync
.events
.addEventListener(
76 'onReceivedListOfTypes',
77 onReceivedListOfTypes
);
79 chrome
.sync
.requestListOfTypes();
80 chrome
.sync
.registerForPerTypeCounters();
83 var onLoad = function() {
92 document
.addEventListener('DOMContentLoaded', chrome
.sync
.types
.onLoad
, false);