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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 return nodeMap; | 64 return nodeMap; |
| 65 } | 65 } |
| 66 | 66 |
| 67 /** @return {!BookmarksPageState} */ | 67 /** @return {!BookmarksPageState} */ |
| 68 function createEmptyState() { | 68 function createEmptyState() { |
| 69 return { | 69 return { |
| 70 nodes: {}, | 70 nodes: {}, |
| 71 selectedFolder: '0', | 71 selectedFolder: '0', |
| 72 closedFolders: new Set(), | 72 closedFolders: new Set(), |
| 73 prefs: { | 73 prefs: { |
| 74 canEdit: true, | |
| 74 incognitoAvailability: IncognitoAvailability.ENABLED, | 75 incognitoAvailability: IncognitoAvailability.ENABLED, |
| 75 }, | 76 }, |
| 76 search: { | 77 search: { |
| 77 term: '', | 78 term: '', |
| 78 inProgress: false, | 79 inProgress: false, |
| 79 results: [], | 80 results: [], |
| 80 }, | 81 }, |
| 81 selection: { | 82 selection: { |
| 82 items: new Set(), | 83 items: new Set(), |
| 83 anchor: null, | 84 anchor: null, |
| 84 }, | 85 }, |
| 85 }; | 86 }; |
| 86 } | 87 } |
| 87 | 88 |
| 88 /** | 89 /** |
| 89 * @param {BookmarksPageState} state | 90 * @param {BookmarksPageState} state |
| 90 * @return {boolean} | 91 * @return {boolean} |
| 91 */ | 92 */ |
| 92 function isShowingSearch(state) { | 93 function isShowingSearch(state) { |
| 93 return !!state.search.term && !state.search.inProgress; | 94 return !!state.search.term && !state.search.inProgress; |
| 94 } | 95 } |
| 95 | 96 |
| 96 /** | 97 /** |
| 98 * Returns true if the node with ID |itemId| is unmodifiable. This should only | |
| 99 * be used to disable operations which act on the node itself (eg, rename), | |
| 100 * as it might still be possible to modify this node's children. | |
| 101 * @param {BookmarksPageState} state | |
| 102 * @param {string} itemId | |
| 103 * @return {boolean} | |
| 104 */ | |
|
calamity
2017/06/09 06:47:31
It's definitely worth mentioning here how unmodifi
tsergeant
2017/06/13 03:13:10
Done.
| |
| 105 function isNodeUnmodifiable(state, itemId) { | |
| 106 return areChildrenUnmodifiable(state, itemId) || | |
| 107 state.nodes[itemId].parentId == ROOT_NODE_ID; | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * Returns true if it is not possible to modify the children of the node with | |
| 112 * ID |itemId|. This includes rearranging the children or adding new ones. | |
| 113 * @param {BookmarksPageState} state | |
| 114 * @param {string} itemId | |
| 115 * @return {boolean} | |
| 116 */ | |
| 117 function areChildrenUnmodifiable(state, itemId) { | |
|
calamity
2017/06/09 06:47:31
As discussed, it would be good to invert these 2 f
tsergeant
2017/06/13 03:13:10
Done.
| |
| 118 return !state.prefs.canEdit || !!state.nodes[itemId].unmodifiable || | |
| 119 itemId == ROOT_NODE_ID; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 97 * @param {string} id | 123 * @param {string} id |
| 98 * @param {NodeMap} nodes | 124 * @param {NodeMap} nodes |
| 99 * @return {boolean} | 125 * @return {boolean} |
| 100 */ | 126 */ |
| 101 function hasChildFolders(id, nodes) { | 127 function hasChildFolders(id, nodes) { |
| 102 var children = nodes[id].children; | 128 var children = nodes[id].children; |
| 103 for (var i = 0; i < children.length; i++) { | 129 for (var i = 0; i < children.length; i++) { |
| 104 if (nodes[children[i]].children) | 130 if (nodes[children[i]].children) |
| 105 return true; | 131 return true; |
| 106 } | 132 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 */ | 185 */ |
| 160 function removeIdsFromSet(set, ids) { | 186 function removeIdsFromSet(set, ids) { |
| 161 var difference = new Set(set); | 187 var difference = new Set(set); |
| 162 ids.forEach(function(id) { | 188 ids.forEach(function(id) { |
| 163 difference.delete(id); | 189 difference.delete(id); |
| 164 }); | 190 }); |
| 165 return difference; | 191 return difference; |
| 166 } | 192 } |
| 167 | 193 |
| 168 return { | 194 return { |
| 195 areChildrenUnmodifiable: areChildrenUnmodifiable, | |
| 169 createEmptyState: createEmptyState, | 196 createEmptyState: createEmptyState, |
| 170 getDescendants: getDescendants, | 197 getDescendants: getDescendants, |
| 171 getDisplayedList: getDisplayedList, | 198 getDisplayedList: getDisplayedList, |
| 172 hasChildFolders: hasChildFolders, | 199 hasChildFolders: hasChildFolders, |
| 200 isNodeUnmodifiable: isNodeUnmodifiable, | |
| 173 isShowingSearch: isShowingSearch, | 201 isShowingSearch: isShowingSearch, |
| 174 normalizeNode: normalizeNode, | 202 normalizeNode: normalizeNode, |
| 175 normalizeNodes: normalizeNodes, | 203 normalizeNodes: normalizeNodes, |
| 176 removeIdsFromMap: removeIdsFromMap, | 204 removeIdsFromMap: removeIdsFromMap, |
| 177 removeIdsFromSet: removeIdsFromSet, | 205 removeIdsFromSet: removeIdsFromSet, |
| 178 }; | 206 }; |
| 179 }); | 207 }); |
| OLD | NEW |