| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 */ | 82 */ |
| 83 function hasChildFolders(id, nodes) { | 83 function hasChildFolders(id, nodes) { |
| 84 var children = nodes[id].children; | 84 var children = nodes[id].children; |
| 85 for (var i = 0; i < children.length; i++) { | 85 for (var i = 0; i < children.length; i++) { |
| 86 if (nodes[children[i]].children) | 86 if (nodes[children[i]].children) |
| 87 return true; | 87 return true; |
| 88 } | 88 } |
| 89 return false; | 89 return false; |
| 90 } | 90 } |
| 91 | 91 |
| 92 /** |
| 93 * Get all descendants of a node, including the node itself. |
| 94 * @param {NodeList} nodes |
| 95 * @param {string} baseId |
| 96 * @return {!Set<string>} |
| 97 */ |
| 98 function getDescendants(nodes, baseId) { |
| 99 var descendants = new Set(); |
| 100 var stack = []; |
| 101 stack.push(baseId); |
| 102 |
| 103 while (stack.length > 0) { |
| 104 var id = stack.pop(); |
| 105 var node = nodes[id]; |
| 106 |
| 107 if (!node) |
| 108 continue; |
| 109 |
| 110 descendants.add(id); |
| 111 |
| 112 if (!node.children) |
| 113 continue; |
| 114 |
| 115 node.children.forEach(function(childId) { |
| 116 stack.push(childId); |
| 117 }); |
| 118 } |
| 119 |
| 120 return descendants; |
| 121 } |
| 122 |
| 123 /** |
| 124 * @param {!Object<string, T>} map |
| 125 * @param {!Set<string>} ids |
| 126 * @return {!Object<string, T>} |
| 127 * @template T |
| 128 */ |
| 129 function removeIdsFromMap(map, ids) { |
| 130 var newMap = Object.assign({}, map); |
| 131 ids.forEach(function(id) { |
| 132 delete newMap[id]; |
| 133 }); |
| 134 return newMap; |
| 135 } |
| 136 |
| 137 /** |
| 138 * @param {!Set<string>} set |
| 139 * @param {!Set<string>} ids |
| 140 * @return {!Set<string>} |
| 141 */ |
| 142 function removeIdsFromSet(set, ids) { |
| 143 var difference = new Set(set); |
| 144 ids.forEach(function(id) { |
| 145 difference.delete(id); |
| 146 }); |
| 147 return difference; |
| 148 } |
| 149 |
| 92 return { | 150 return { |
| 93 createEmptyState: createEmptyState, | 151 createEmptyState: createEmptyState, |
| 152 getDescendants: getDescendants, |
| 94 getDisplayedList: getDisplayedList, | 153 getDisplayedList: getDisplayedList, |
| 95 hasChildFolders: hasChildFolders, | 154 hasChildFolders: hasChildFolders, |
| 96 isShowingSearch: isShowingSearch, | 155 isShowingSearch: isShowingSearch, |
| 97 normalizeNodes: normalizeNodes, | 156 normalizeNodes: normalizeNodes, |
| 157 removeIdsFromMap: removeIdsFromMap, |
| 158 removeIdsFromSet: removeIdsFromSet, |
| 98 }; | 159 }; |
| 99 }); | 160 }); |
| OLD | NEW |