1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// 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.
import {assert} from 'chrome://resources/js/assert.m.js';
import {BOOKMARKS_BAR_ID, IncognitoAvailability, ROOT_NODE_ID} from './constants.js';
import {BookmarkNode, BookmarksPageState, NodeMap} from './types.js';
/**
* @fileoverview Utility functions for the Bookmarks page.
*/
/**
* Returns the list of bookmark IDs to be displayed in the UI, taking into
* account search and the currently selected folder.
* @param {!BookmarksPageState} state
* @return {!Array<string>}
*/
export function getDisplayedList(state) {
if (isShowingSearch(state)) {
return assert(state.search.results);
}
return assert(state.nodes[state.selectedFolder].children);
}
/**
* @param {chrome.bookmarks.BookmarkTreeNode} treeNode
* @return {!BookmarkNode}
*/
export function normalizeNode(treeNode) {
const node = Object.assign({}, treeNode);
// 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;
if (!('url' in node)) {
// The onCreated API listener returns folders without |children| defined.
node.children = (node.children || []).map(function(child) {
return child.id;
});
}
return /** @type {BookmarkNode} */ (node);
}
/**
* @param {chrome.bookmarks.BookmarkTreeNode} rootNode
* @return {NodeMap}
*/
export function normalizeNodes(rootNode) {
/** @type {NodeMap} */
const nodeMap = {};
const stack = [];
stack.push(rootNode);
while (stack.length > 0) {
const node = stack.pop();
nodeMap[node.id] = normalizeNode(node);
if (!node.children) {
continue;
}
node.children.forEach(function(child) {
stack.push(child);
});
}
return nodeMap;
}
/** @return {!BookmarksPageState} */
export function createEmptyState() {
return {
nodes: {},
selectedFolder: BOOKMARKS_BAR_ID,
folderOpenState: new Map(),
prefs: {
canEdit: true,
incognitoAvailability: IncognitoAvailability.ENABLED,
},
search: {
term: '',
inProgress: false,
results: null,
},
selection: {
items: new Set(),
anchor: null,
},
};
}
/**
* @param {BookmarksPageState} state
* @return {boolean}
*/
export function isShowingSearch(state) {
return state.search.results != null;
}
/**
* Returns true if the node with ID |itemId| is modifiable, allowing
* the node to be renamed, moved or deleted. Note that if a node is
* uneditable, it may still have editable children (for example, the top-level
* folders).
* @param {BookmarksPageState} state
* @param {string} itemId
* @return {boolean}
*/
export function canEditNode(state, itemId) {
return itemId !== ROOT_NODE_ID &&
state.nodes[itemId].parentId !== ROOT_NODE_ID &&
!state.nodes[itemId].unmodifiable && state.prefs.canEdit;
}
/**
* Returns true if it is possible to modify the children list of the node with
* ID |itemId|. This includes rearranging the children or adding new ones.
* @param {BookmarksPageState} state
* @param {string} itemId
* @return {boolean}
*/
export function canReorderChildren(state, itemId) {
return itemId !== ROOT_NODE_ID && !state.nodes[itemId].unmodifiable &&
state.prefs.canEdit;
}
/**
* @param {string} id
* @param {NodeMap} nodes
* @return {boolean}
*/
export function hasChildFolders(id, nodes) {
const children = nodes[id].children;
for (let i = 0; i < children.length; i++) {
if (nodes[children[i]].children) {
return true;
}
}
return false;
}
/**
* Get all descendants of a node, including the node itself.
* @param {NodeMap} nodes
* @param {string} baseId
* @return {!Set<string>}
*/
export function getDescendants(nodes, baseId) {
const descendants = new Set();
const stack = [];
stack.push(baseId);
while (stack.length > 0) {
const id = stack.pop();
const node = nodes[id];
if (!node) {
continue;
}
descendants.add(id);
if (!node.children) {
continue;
}
node.children.forEach(function(childId) {
stack.push(childId);
});
}
return descendants;
}
/**
* @param {!Object<string, T>} map
* @param {!Set<string>} ids
* @return {!Object<string, T>}
* @template T
*/
export function removeIdsFromObject(map, ids) {
const newObject = Object.assign({}, map);
ids.forEach(function(id) {
delete newObject[id];
});
return newObject;
}
/**
* @param {!Map<string, T>} map
* @param {!Set<string>} ids
* @return {!Map<string, T>}
* @template T
*/
export function removeIdsFromMap(map, ids) {
const newMap = new Map(map);
ids.forEach(function(id) {
newMap.delete(id);
});
return newMap;
}
/**
* @param {!Set<string>} set
* @param {!Set<string>} ids
* @return {!Set<string>}
*/
export function removeIdsFromSet(set, ids) {
const difference = new Set(set);
ids.forEach(function(id) {
difference.delete(id);
});
return difference;
}
|