| OLD | NEW |
| 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/ui.js | 6 // require: cr/ui.js |
| 7 // require: cr/ui/tree.js | 7 // require: cr/ui/tree.js |
| 8 | 8 |
| 9 cr.define('chrome.sync', function() { | 9 cr.define('chrome.sync', function() { |
| 10 // Allow platform specific CSS rules. | |
| 11 // | |
| 12 // TODO(akalin): BMM and options page does something similar, too. | |
| 13 // Move this to util.js. | |
| 14 if (cr.isWindows) | |
| 15 document.documentElement.setAttribute('os', 'win'); | |
| 16 | |
| 17 // TODO(akalin): Create SyncNodeTree/SyncNodeTreeItem classes that | |
| 18 // hide all these details. | |
| 19 | |
| 20 /** | |
| 21 * Returns an object which measures elapsed time. | |
| 22 */ | |
| 23 var makeTimer = function() { | |
| 24 var start = new Date(); | |
| 25 | |
| 26 return { | |
| 27 /** | |
| 28 * @return {number} The number of seconds since the timer was | |
| 29 * created. | |
| 30 */ | |
| 31 get elapsedSeconds() { | |
| 32 return ((new Date()).getTime() - start.getTime()) / 1000.0; | |
| 33 } | |
| 34 }; | |
| 35 }; | |
| 36 | |
| 37 /** | 10 /** |
| 38 * Gets all children of the given node and passes it to the given | 11 * Gets all children of the given node and passes it to the given |
| 39 * callback. | 12 * callback. |
| 40 * @param {Object} nodeInfo The info for the node whose children we | 13 * @param {Object} nodeInfo The info for the node whose children we |
| 41 * want. | 14 * want. |
| 42 * @param {Function} callback The callback to call with the list of | 15 * @param {Function} callback The callback to call with the list of |
| 43 * children. | 16 * children. |
| 44 */ | 17 */ |
| 45 function getSyncNodeChildren(nodeInfo, callback) { | 18 function getSyncNodeChildren(nodeInfo, callback) { |
| 46 var timer = makeTimer(); | 19 var timer = chrome.sync.makeTimer(); |
| 47 chrome.sync.getChildNodeIds(nodeInfo.id, function(childNodeIds) { | 20 chrome.sync.getChildNodeIds(nodeInfo.id, function(childNodeIds) { |
| 48 console.debug('getChildNodeIds took ' + | 21 console.debug('getChildNodeIds took ' + |
| 49 timer.elapsedSeconds + 's to retrieve ' + | 22 timer.elapsedSeconds + 's to retrieve ' + |
| 50 childNodeIds.length + ' ids'); | 23 childNodeIds.length + ' ids'); |
| 51 timer = makeTimer(); | 24 timer = chrome.sync.makeTimer(); |
| 52 chrome.sync.getNodesById(childNodeIds, function(children) { | 25 chrome.sync.getNodesById(childNodeIds, function(children) { |
| 53 console.debug('getNodesById took ' + | 26 console.debug('getNodesById took ' + |
| 54 timer.elapsedSeconds + 's to retrieve ' + | 27 timer.elapsedSeconds + 's to retrieve ' + |
| 55 children.length + ' nodes'); | 28 children.length + ' nodes'); |
| 56 callback(children); | 29 callback(children); |
| 57 }); | 30 }); |
| 58 }); | 31 }); |
| 59 } | 32 } |
| 60 | 33 |
| 61 /** | 34 /** |
| (...skipping 10 matching lines...) Expand all Loading... |
| 72 | 45 |
| 73 if (nodeInfo.isFolder) { | 46 if (nodeInfo.isFolder) { |
| 74 treeItem.mayHaveChildren_ = true; | 47 treeItem.mayHaveChildren_ = true; |
| 75 | 48 |
| 76 // Load children asynchronously on expand. | 49 // Load children asynchronously on expand. |
| 77 // TODO(akalin): Add a throbber while loading? | 50 // TODO(akalin): Add a throbber while loading? |
| 78 treeItem.triggeredLoad_ = false; | 51 treeItem.triggeredLoad_ = false; |
| 79 treeItem.addEventListener('expand', function(event) { | 52 treeItem.addEventListener('expand', function(event) { |
| 80 if (!treeItem.triggeredLoad_) { | 53 if (!treeItem.triggeredLoad_) { |
| 81 getSyncNodeChildren(nodeInfo, function(children) { | 54 getSyncNodeChildren(nodeInfo, function(children) { |
| 82 var timer = makeTimer(); | 55 var timer = chrome.sync.makeTimer(); |
| 83 for (var i = 0; i < children.length; ++i) { | 56 for (var i = 0; i < children.length; ++i) { |
| 84 var childTreeItem = makeNodeTreeItem(children[i]); | 57 var childTreeItem = makeNodeTreeItem(children[i]); |
| 85 treeItem.add(childTreeItem); | 58 treeItem.add(childTreeItem); |
| 86 } | 59 } |
| 87 console.debug('adding ' + children.length + ' children took ' + | 60 console.debug('adding ' + children.length + ' children took ' + |
| 88 timer.elapsedSeconds + 's'); | 61 timer.elapsedSeconds + 's'); |
| 89 }); | 62 }); |
| 90 treeItem.triggeredLoad_ = true; | 63 treeItem.triggeredLoad_ = true; |
| 91 } | 64 } |
| 92 }); | 65 }); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 for (var i = 0; i < pendingSyncNodeBrowsers.length; ++i) { | 118 for (var i = 0; i < pendingSyncNodeBrowsers.length; ++i) { |
| 146 decorateSyncNodeBrowser(pendingSyncNodeBrowsers[i]); | 119 decorateSyncNodeBrowser(pendingSyncNodeBrowsers[i]); |
| 147 } | 120 } |
| 148 domLoaded = true; | 121 domLoaded = true; |
| 149 }); | 122 }); |
| 150 | 123 |
| 151 return { | 124 return { |
| 152 decorateSyncNodeBrowser: decorateSyncNodeBrowserAfterDOMLoad | 125 decorateSyncNodeBrowser: decorateSyncNodeBrowserAfterDOMLoad |
| 153 }; | 126 }; |
| 154 }); | 127 }); |
| OLD | NEW |