Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 /** | 5 /** |
| 6 * @fileoverview Utility functions for the Bookmarks page. | 6 * @fileoverview Utility functions for the Bookmarks page. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 cr.define('bookmarks.util', function() { | 9 cr.define('bookmarks.util', function() { |
| 10 /** | 10 /** |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 inProgress: false, | 60 inProgress: false, |
| 61 results: [], | 61 results: [], |
| 62 }, | 62 }, |
| 63 selection: { | 63 selection: { |
| 64 items: {}, | 64 items: {}, |
| 65 anchor: null, | 65 anchor: null, |
| 66 }, | 66 }, |
| 67 }; | 67 }; |
| 68 } | 68 } |
| 69 | 69 |
| 70 /** | |
| 71 * Get all descendants of a node, including the node itself. | |
| 72 * @param {NodeList} nodes | |
| 73 * @param {string} baseId | |
| 74 * @return {!Set<string>} | |
| 75 */ | |
| 76 function getDescendants(nodes, baseId) { | |
| 77 var descendants = new Set(); | |
| 78 var stack = []; | |
| 79 stack.push(baseId); | |
| 80 | |
| 81 while (stack.length > 0) { | |
| 82 var id = stack.pop(); | |
| 83 var node = nodes[id]; | |
| 84 | |
| 85 if (!node) | |
| 86 continue; | |
| 87 | |
| 88 descendants.add(id); | |
| 89 | |
| 90 if (!node.children) | |
| 91 continue; | |
| 92 | |
| 93 node.children.forEach(function(childId) { | |
| 94 stack.push(childId); | |
| 95 }); | |
| 96 } | |
| 97 | |
| 98 return descendants; | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * @param {!Object<string, T>} map | |
| 103 * @param {!Set<string>} nodes | |
| 104 * @return {!Object<string, T>} | |
| 105 * @template T | |
| 106 */ | |
| 107 function removeNodesFromMap(map, nodes) { | |
|
calamity
2017/04/03 06:54:24
Maybe genericize further into removeKeysFromMap? A
tsergeant
2017/04/04 04:42:28
Done, with removeIdsFromMap (and removeIdsFromSet)
| |
| 108 var newMap = Object.assign({}, map); | |
| 109 nodes.forEach(function(id) { | |
| 110 delete newMap[id]; | |
| 111 }); | |
| 112 return newMap; | |
| 113 }; | |
| 114 | |
| 70 return { | 115 return { |
| 71 createEmptyState: createEmptyState, | 116 createEmptyState: createEmptyState, |
| 117 getDescendants: getDescendants, | |
| 72 getDisplayedList: getDisplayedList, | 118 getDisplayedList: getDisplayedList, |
| 73 normalizeNodes: normalizeNodes, | 119 normalizeNodes: normalizeNodes, |
| 120 removeNodesFromMap: removeNodesFromMap, | |
| 74 }; | 121 }; |
| 75 }); | 122 }); |
| OLD | NEW |