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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
// 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 {BookmarkNode, BookmarksPageState, FolderOpenState, NodeMap, PreferencesState, SearchState, SelectionState} from './types.js';
import {removeIdsFromMap, removeIdsFromObject, removeIdsFromSet} from './util.js';
/**
* @fileoverview Module of functions which produce a new page state in response
* to an action. Reducers (in the same sense as Array.prototype.reduce) must be
* pure functions: they must not modify existing state objects, or make any API
* calls.
*/
/**
* @param {SelectionState} selectionState
* @param {Object} action
* @return {SelectionState}
*/
function selectItems(selectionState, action) {
let newItems = new Set();
if (!action.clear) {
newItems = new Set(selectionState.items);
}
action.items.forEach(function(id) {
let add = true;
if (action.toggle) {
add = !newItems.has(id);
}
if (add) {
newItems.add(id);
} else {
newItems.delete(id);
}
});
return /** @type {SelectionState} */ (Object.assign({}, selectionState, {
items: newItems,
anchor: action.anchor,
}));
}
/**
* @param {SelectionState} selectionState
* @return {SelectionState}
*/
function deselectAll(selectionState) {
return {
items: new Set(),
anchor: null,
};
}
/**
* @param {SelectionState} selectionState
* @param {!Set<string>} deleted
* @return SelectionState
*/
function deselectItems(selectionState, deleted) {
return /** @type {SelectionState} */ (Object.assign({}, selectionState, {
items: removeIdsFromSet(selectionState.items, deleted),
anchor: !selectionState.anchor || deleted.has(selectionState.anchor) ?
null :
selectionState.anchor,
}));
}
/**
* @param {SelectionState} selectionState
* @param {Object} action
* @return {SelectionState}
*/
function updateAnchor(selectionState, action) {
return /** @type {SelectionState} */ (Object.assign({}, selectionState, {
anchor: action.anchor,
}));
}
/**
* Exported for tests.
* @param {SelectionState} selection
* @param {Object} action
* @return {SelectionState}
*/
export function updateSelection(selection, action) {
switch (action.name) {
case 'clear-search':
case 'finish-search':
case 'select-folder':
case 'deselect-items':
return deselectAll(selection);
case 'select-items':
return selectItems(selection, action);
case 'remove-bookmark':
return deselectItems(selection, action.descendants);
case 'move-bookmark':
// Deselect items when they are moved to another folder, since they will
// no longer be visible on screen (for simplicity, ignores items visible
// in search results).
if (action.parentId !== action.oldParentId &&
selection.items.has(action.id)) {
return deselectItems(selection, new Set([action.id]));
}
return selection;
case 'update-anchor':
return updateAnchor(selection, action);
default:
return selection;
}
}
/**
* @param {SearchState} search
* @param {Object} action
* @return {SearchState}
*/
function startSearch(search, action) {
return {
term: action.term,
inProgress: true,
results: search.results,
};
}
/**
* @param {SearchState} search
* @param {Object} action
* @return {SearchState}
*/
function finishSearch(search, action) {
return /** @type {SearchState} */ (Object.assign({}, search, {
inProgress: false,
results: action.results,
}));
}
/** @return {SearchState} */
function clearSearch() {
return {
term: '',
inProgress: false,
results: null,
};
}
/**
* @param {SearchState} search
* @param {!Set<string>} deletedIds
* @return {SearchState}
*/
function removeDeletedResults(search, deletedIds) {
if (!search.results) {
return search;
}
const newResults = [];
search.results.forEach(function(id) {
if (!deletedIds.has(id)) {
newResults.push(id);
}
});
return /** @type {SearchState} */ (Object.assign({}, search, {
results: newResults,
}));
}
/**
* @param {SearchState} search
* @param {Object} action
* @return {SearchState}
*/
function updateSearch(search, action) {
switch (action.name) {
case 'start-search':
return startSearch(search, action);
case 'select-folder':
case 'clear-search':
return clearSearch();
case 'finish-search':
return finishSearch(search, action);
case 'remove-bookmark':
return removeDeletedResults(search, action.descendants);
default:
return search;
}
}
/**
* @param {NodeMap} nodes
* @param {string} id
* @param {function(BookmarkNode):BookmarkNode} callback
* @return {NodeMap}
*/
function modifyNode(nodes, id, callback) {
const nodeModification = {};
nodeModification[id] = callback(nodes[id]);
return Object.assign({}, nodes, nodeModification);
}
/**
* @param {NodeMap} nodes
* @param {Object} action
* @return {NodeMap}
*/
function createBookmark(nodes, action) {
const nodeModifications = {};
nodeModifications[action.id] = action.node;
const parentNode = nodes[action.parentId];
const newChildren = parentNode.children.slice();
newChildren.splice(action.parentIndex, 0, action.id);
nodeModifications[action.parentId] = Object.assign({}, parentNode, {
children: newChildren,
});
return Object.assign({}, nodes, nodeModifications);
}
/**
* @param {NodeMap} nodes
* @param {Object} action
* @return {NodeMap}
*/
function editBookmark(nodes, action) {
// Do not allow folders to change URL (making them no longer folders).
if (!nodes[action.id].url && action.changeInfo.url) {
delete action.changeInfo.url;
}
return modifyNode(nodes, action.id, function(node) {
return /** @type {BookmarkNode} */ (
Object.assign({}, node, action.changeInfo));
});
}
/**
* @param {NodeMap} nodes
* @param {Object} action
* @return {NodeMap}
*/
function moveBookmark(nodes, action) {
const nodeModifications = {};
const id = action.id;
// Change node's parent.
nodeModifications[id] =
Object.assign({}, nodes[id], {parentId: action.parentId});
// Remove from old parent.
const oldParentId = action.oldParentId;
const oldParentChildren = nodes[oldParentId].children.slice();
oldParentChildren.splice(action.oldIndex, 1);
nodeModifications[oldParentId] =
Object.assign({}, nodes[oldParentId], {children: oldParentChildren});
// Add to new parent.
const parentId = action.parentId;
const parentChildren = oldParentId === parentId ?
oldParentChildren :
nodes[parentId].children.slice();
parentChildren.splice(action.index, 0, action.id);
nodeModifications[parentId] =
Object.assign({}, nodes[parentId], {children: parentChildren});
return Object.assign({}, nodes, nodeModifications);
}
/**
* @param {NodeMap} nodes
* @param {Object} action
* @return {NodeMap}
*/
function removeBookmark(nodes, action) {
const newState = modifyNode(nodes, action.parentId, function(node) {
const newChildren = node.children.slice();
newChildren.splice(action.index, 1);
return /** @type {BookmarkNode} */ (
Object.assign({}, node, {children: newChildren}));
});
return removeIdsFromObject(newState, action.descendants);
}
/**
* @param {NodeMap} nodes
* @param {Object} action
* @return {NodeMap}
*/
function reorderChildren(nodes, action) {
return modifyNode(nodes, action.id, function(node) {
return /** @type {BookmarkNode} */ (
Object.assign({}, node, {children: action.children}));
});
}
/**
* Exported for tests.
* @param {NodeMap} nodes
* @param {Object} action
* @return {NodeMap}
*/
export function updateNodes(nodes, action) {
switch (action.name) {
case 'create-bookmark':
return createBookmark(nodes, action);
case 'edit-bookmark':
return editBookmark(nodes, action);
case 'move-bookmark':
return moveBookmark(nodes, action);
case 'remove-bookmark':
return removeBookmark(nodes, action);
case 'reorder-children':
return reorderChildren(nodes, action);
case 'refresh-nodes':
return action.nodes;
default:
return nodes;
}
}
/**
* @param {NodeMap} nodes
* @param {string} ancestorId
* @param {string} childId
* @return {boolean}
*/
function isAncestorOf(nodes, ancestorId, childId) {
let currentId = childId;
// Work upwards through the tree from child.
while (currentId) {
if (currentId === ancestorId) {
return true;
}
currentId = nodes[currentId].parentId;
}
return false;
}
/**
* Exported for tests.
* @param {string} selectedFolder
* @param {Object} action
* @param {NodeMap} nodes
* @return {string}
*/
export function updateSelectedFolder(selectedFolder, action, nodes) {
switch (action.name) {
case 'select-folder':
return action.id;
case 'change-folder-open':
// When hiding the selected folder by closing its ancestor, select
// that ancestor instead.
if (!action.open && selectedFolder &&
isAncestorOf(nodes, action.id, selectedFolder)) {
return action.id;
}
return selectedFolder;
case 'remove-bookmark':
// When deleting the selected folder (or its ancestor), select the
// parent of the deleted node.
if (selectedFolder && isAncestorOf(nodes, action.id, selectedFolder)) {
return assert(nodes[action.id].parentId);
}
return selectedFolder;
default:
return selectedFolder;
}
}
/**
* @param {FolderOpenState} folderOpenState
* @param {string|undefined} id
* @param {NodeMap} nodes
* @return {FolderOpenState}
*/
function openFolderAndAncestors(folderOpenState, id, nodes) {
const newFolderOpenState =
/** @type {FolderOpenState} */ (new Map(folderOpenState));
for (let currentId = id; currentId; currentId = nodes[currentId].parentId) {
newFolderOpenState.set(currentId, true);
}
return newFolderOpenState;
}
/**
* @param {FolderOpenState} folderOpenState
* @param {Object} action
* @return {FolderOpenState}
*/
function changeFolderOpen(folderOpenState, action) {
const newFolderOpenState =
/** @type {FolderOpenState} */ (new Map(folderOpenState));
newFolderOpenState.set(action.id, action.open);
return newFolderOpenState;
}
/**
* Exported for tests.
* @param {FolderOpenState} folderOpenState
* @param {Object} action
* @param {NodeMap} nodes
* @return {FolderOpenState}
*/
export function updateFolderOpenState(folderOpenState, action, nodes) {
switch (action.name) {
case 'change-folder-open':
return changeFolderOpen(folderOpenState, action);
case 'select-folder':
return openFolderAndAncestors(
folderOpenState, nodes[action.id].parentId, nodes);
case 'move-bookmark':
if (!nodes[action.id].children) {
return folderOpenState;
}
return openFolderAndAncestors(folderOpenState, action.parentId, nodes);
case 'remove-bookmark':
return removeIdsFromMap(folderOpenState, action.descendants);
default:
return folderOpenState;
}
}
/**
* @param {PreferencesState} prefs
* @param {Object} action
* @return {PreferencesState}
*/
function updatePrefs(prefs, action) {
switch (action.name) {
case 'set-incognito-availability':
return /** @type {PreferencesState} */ (Object.assign({}, prefs, {
incognitoAvailability: action.value,
}));
case 'set-can-edit':
return /** @type {PreferencesState} */ (Object.assign({}, prefs, {
canEdit: action.value,
}));
default:
return prefs;
}
}
/**
* Root reducer for the Bookmarks page. This is called by the store in
* response to an action, and the return value is used to update the UI.
* @param {!BookmarksPageState} state
* @param {Object} action
* @return {!BookmarksPageState}
*/
export function reduceAction(state, action) {
return {
nodes: updateNodes(state.nodes, action),
selectedFolder:
updateSelectedFolder(state.selectedFolder, action, state.nodes),
folderOpenState:
updateFolderOpenState(state.folderOpenState, action, state.nodes),
prefs: updatePrefs(state.prefs, action),
search: updateSearch(state.search, action),
selection: updateSelection(state.selection, action),
};
}
|