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 /** |
| 11 * @param {!BookmarksPageState} state | 11 * @param {!BookmarksPageState} state |
| 12 * @return {!Array<string>} | 12 * @return {!Array<string>} |
| 13 */ | 13 */ |
| 14 function getDisplayedList(state) { | 14 function getDisplayedList(state) { |
| 15 if (!isShowingSearch(state)) | 15 if (!isShowingSearch(state)) |
| 16 return assert(state.nodes[assert(state.selectedFolder)].children); | 16 return assert(state.nodes[assert(state.selectedFolder)].children); |
| 17 | 17 |
| 18 return state.search.results; | 18 return state.search.results; |
| 19 } | 19 } |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * @param {BookmarkTreeNode} treeNode | |
| 23 * @return {BookmarkNode} | |
| 24 */ | |
| 25 function normalizeNode(treeNode) { | |
| 26 var node = Object.assign({}, treeNode); | |
| 27 // Node index is not necessary and not kept up-to-date. Remove it from the | |
| 28 // data structure so we don't accidentally depend on the incorrect | |
| 29 // information. | |
| 30 delete node.index; | |
| 31 | |
| 32 if (!('url' in node)) { | |
| 33 // The onCreated API listener returns folders without |children| defined. | |
|
calamity
2017/04/04 08:14:51
K.
| |
| 34 node.children = (node.children || []).map(function(child) { | |
| 35 return child.id; | |
| 36 }); | |
| 37 } | |
| 38 return /** @type {BookmarkNode} */ (node); | |
| 39 } | |
| 40 | |
| 41 /** | |
| 22 * @param {BookmarkTreeNode} rootNode | 42 * @param {BookmarkTreeNode} rootNode |
| 23 * @return {NodeList} | 43 * @return {NodeList} |
| 24 */ | 44 */ |
| 25 function normalizeNodes(rootNode) { | 45 function normalizeNodes(rootNode) { |
| 26 /** @type {NodeList} */ | 46 /** @type {NodeList} */ |
| 27 var nodeList = {}; | 47 var nodeList = {}; |
| 28 var stack = []; | 48 var stack = []; |
| 29 stack.push(rootNode); | 49 stack.push(rootNode); |
| 30 | 50 |
| 31 while (stack.length > 0) { | 51 while (stack.length > 0) { |
| 32 var node = stack.pop(); | 52 var node = stack.pop(); |
| 33 // Node index is not necessary and not kept up-to-date. Remove it from the | 53 nodeList[node.id] = normalizeNode(node); |
| 34 // data structure so we don't accidentally depend on the incorrect | |
| 35 // information. | |
| 36 delete node.index; | |
| 37 nodeList[node.id] = node; | |
| 38 if (!node.children) | 54 if (!node.children) |
| 39 continue; | 55 continue; |
| 40 | 56 |
| 41 var childIds = []; | |
| 42 node.children.forEach(function(child) { | 57 node.children.forEach(function(child) { |
| 43 childIds.push(child.id); | |
| 44 stack.push(child); | 58 stack.push(child); |
| 45 }); | 59 }); |
| 46 node.children = childIds; | |
| 47 } | 60 } |
| 48 | 61 |
| 49 return nodeList; | 62 return nodeList; |
| 50 } | 63 } |
| 51 | 64 |
| 52 /** @return {!BookmarksPageState} */ | 65 /** @return {!BookmarksPageState} */ |
| 53 function createEmptyState() { | 66 function createEmptyState() { |
| 54 return { | 67 return { |
| 55 nodes: {}, | 68 nodes: {}, |
| 56 selectedFolder: '0', | 69 selectedFolder: '0', |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 87 return true; | 100 return true; |
| 88 } | 101 } |
| 89 return false; | 102 return false; |
| 90 } | 103 } |
| 91 | 104 |
| 92 return { | 105 return { |
| 93 createEmptyState: createEmptyState, | 106 createEmptyState: createEmptyState, |
| 94 getDisplayedList: getDisplayedList, | 107 getDisplayedList: getDisplayedList, |
| 95 hasChildFolders: hasChildFolders, | 108 hasChildFolders: hasChildFolders, |
| 96 isShowingSearch: isShowingSearch, | 109 isShowingSearch: isShowingSearch, |
| 110 normalizeNode: normalizeNode, | |
| 97 normalizeNodes: normalizeNodes, | 111 normalizeNodes: normalizeNodes, |
| 98 ROOT_NODE_ID: '0', | 112 ROOT_NODE_ID: '0', |
| 99 }; | 113 }; |
| 100 }); | 114 }); |
| OLD | NEW |