Chromium Code Reviews| Index: chrome/browser/resources/md_bookmarks/util.js |
| diff --git a/chrome/browser/resources/md_bookmarks/util.js b/chrome/browser/resources/md_bookmarks/util.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f280b74d1e5f58238e9c3555cf86d2a43a537b72 |
| --- /dev/null |
| +++ b/chrome/browser/resources/md_bookmarks/util.js |
| @@ -0,0 +1,62 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview Utility functions for the Bookmarks page. |
| + */ |
| + |
| +cr.define('bookmarks.util', function() { |
| + /** |
| + * @param {!BookmarksPageState} state |
| + * @return {!Array<string>} |
| + */ |
| + function getDisplayedList(state) { |
| + return assert(state.nodes[assert(state.selectedFolder)].children); |
|
calamity
2017/03/07 07:27:35
Does this remove the 'unselected sidebar during se
tsergeant
2017/03/08 02:47:10
The plan is that the sidebar will be unselected du
|
| + } |
| + |
| + /** |
| + * @param {BookmarkTreeNode} rootNode |
| + * @return {NodeList} |
| + */ |
| + function normalizeNodes(rootNode) { |
| + /** @type {NodeList} */ |
| + var nodeMap = {}; |
|
calamity
2017/03/07 07:27:35
nit: nodeList?
tsergeant
2017/03/08 02:47:11
Done.
|
| + var queue = []; |
|
calamity
2017/03/07 07:27:35
Looks more like a stack to me. Jus sayin'.
tsergeant
2017/03/08 02:47:10
Done.
|
| + queue.push(rootNode); |
| + |
| + while (queue.length > 0) { |
| + var node = queue.pop(); |
| + // Node index is not necessary and not kept up-to-date. Remove it from the |
| + // data structure so we don't accidentally depend on the incorrect |
| + // information. |
| + delete node.index; |
| + nodeMap[node.id] = node; |
| + if (node.children) { |
|
calamity
2017/03/07 07:27:35
nit: invert and early continue.
tsergeant
2017/03/08 02:47:10
Done.
|
| + var childIds = []; |
| + node.children.forEach(function(child) { |
| + childIds.push(child.id); |
| + queue.push(child); |
| + }); |
| + node.children = childIds; |
| + } |
| + } |
| + |
| + return nodeMap; |
| + } |
| + |
| + /** @return {!BookmarksPageState} */ |
| + function createEmptyState() { |
| + return { |
| + nodes: {}, |
| + selectedFolder: '0', |
| + closedFolders: {}, |
| + }; |
| + } |
| + |
| + return { |
| + createEmptyState: createEmptyState, |
| + getDisplayedList: getDisplayedList, |
| + normalizeNodes: normalizeNodes, |
| + }; |
| +}); |