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 Module of functions which produce a new page state in response | 6 * @fileoverview Module of functions which produce a new page state in response |
7 * to an action. Reducers (in the same sense as Array.prototype.reduce) must be | 7 * to an action. Reducers (in the same sense as Array.prototype.reduce) must be |
8 * pure functions: they must not modify existing state objects, or make any API | 8 * pure functions: they must not modify existing state objects, or make any API |
9 * calls. | 9 * calls. |
10 */ | 10 */ |
11 | 11 |
12 cr.define('bookmarks', function() { | 12 cr.define('bookmarks', function() { |
13 var SelectionState = {}; | 13 var SelectionState = {}; |
14 | 14 |
15 /** | 15 /** |
16 * @param {SelectionState} selectionState | 16 * @param {SelectionState} selectionState |
17 * @param {Action} action | 17 * @param {Action} action |
18 * @return {SelectionState} | 18 * @return {SelectionState} |
19 */ | 19 */ |
20 SelectionState.selectItems = function(selectionState, action) { | 20 SelectionState.selectItems = function(selectionState, action) { |
21 var newItems = {}; | 21 var newItems = new Set(); |
22 if (action.add) | 22 if (action.add) |
23 Object.assign(newItems, selectionState.items); | 23 newItems = new Set(selectionState.items); |
24 | 24 |
25 action.items.forEach(function(id) { | 25 action.items.forEach(function(id) { |
26 newItems[id] = true; | 26 newItems.add(id); |
27 }); | 27 }); |
28 | 28 |
29 return /** @type {SelectionState} */ (Object.assign({}, selectionState, { | 29 return /** @type {SelectionState} */ (Object.assign({}, selectionState, { |
30 items: newItems, | 30 items: newItems, |
31 anchor: action.anchor, | 31 anchor: action.anchor, |
32 })); | 32 })); |
33 }; | 33 }; |
34 | 34 |
35 /** | 35 /** |
36 * @param {SelectionState} selectionState | 36 * @param {SelectionState} selectionState |
37 * @return {SelectionState} | 37 * @return {SelectionState} |
38 */ | 38 */ |
39 SelectionState.deselectAll = function(selectionState) { | 39 SelectionState.deselectAll = function(selectionState) { |
40 return { | 40 return { |
41 items: {}, | 41 items: new Set(), |
42 anchor: null, | 42 anchor: null, |
43 }; | 43 }; |
44 }; | 44 }; |
45 | 45 |
46 /** | 46 /** |
47 * @param {SelectionState} selection | 47 * @param {SelectionState} selection |
48 * @param {Action} action | 48 * @param {Action} action |
49 * @return {SelectionState} | 49 * @return {SelectionState} |
50 */ | 50 */ |
51 SelectionState.updateSelection = function(selection, action) { | 51 SelectionState.updateSelection = function(selection, action) { |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 var ClosedFolderState = {}; | 267 var ClosedFolderState = {}; |
268 | 268 |
269 /** | 269 /** |
270 * @param {ClosedFolderState} closedFolders | 270 * @param {ClosedFolderState} closedFolders |
271 * @param {string|undefined} id | 271 * @param {string|undefined} id |
272 * @param {NodeList} nodes | 272 * @param {NodeList} nodes |
273 * @return {ClosedFolderState} | 273 * @return {ClosedFolderState} |
274 */ | 274 */ |
275 ClosedFolderState.openFolderAndAncestors = function( | 275 ClosedFolderState.openFolderAndAncestors = function( |
276 closedFolders, id, nodes) { | 276 closedFolders, id, nodes) { |
277 var modifications = {}; | 277 var newClosedFolders = new Set(closedFolders); |
278 var currentId = id; | 278 var currentId = id; |
279 while (currentId) { | 279 while (currentId) { |
280 if (closedFolders[currentId]) | 280 if (closedFolders.has(currentId)) |
281 modifications[currentId] = false; | 281 newClosedFolders.delete(currentId); |
282 | 282 |
283 currentId = nodes[currentId].parentId; | 283 currentId = nodes[currentId].parentId; |
284 } | 284 } |
285 | 285 |
286 return Object.assign({}, closedFolders, modifications); | 286 return newClosedFolders; |
287 }; | 287 }; |
288 | 288 |
289 /** | 289 /** |
290 * @param {ClosedFolderState} closedFolders | 290 * @param {ClosedFolderState} closedFolders |
291 * @param {Action} action | 291 * @param {Action} action |
292 * @return {ClosedFolderState} | 292 * @return {ClosedFolderState} |
293 */ | 293 */ |
294 ClosedFolderState.changeFolderOpen = function(closedFolders, action) { | 294 ClosedFolderState.changeFolderOpen = function(closedFolders, action) { |
295 var closed = !action.open; | 295 var closed = !action.open; |
296 var modification = {}; | 296 var newClosedFolders = new Set(closedFolders); |
297 modification[action.id] = closed; | 297 if (closed) |
| 298 newClosedFolders.add(action.id); |
| 299 else |
| 300 newClosedFolders.delete(action.id); |
298 | 301 |
299 return Object.assign({}, closedFolders, modification); | 302 return newClosedFolders; |
300 }; | 303 }; |
301 | 304 |
302 /** | 305 /** |
303 * @param {ClosedFolderState} closedFolders | 306 * @param {ClosedFolderState} closedFolders |
304 * @param {Action} action | 307 * @param {Action} action |
305 * @param {NodeList} nodes | 308 * @param {NodeList} nodes |
306 * @return {ClosedFolderState} | 309 * @return {ClosedFolderState} |
307 */ | 310 */ |
308 ClosedFolderState.updateClosedFolders = function( | 311 ClosedFolderState.updateClosedFolders = function( |
309 closedFolders, action, nodes) { | 312 closedFolders, action, nodes) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 348 |
346 return { | 349 return { |
347 reduceAction: reduceAction, | 350 reduceAction: reduceAction, |
348 ClosedFolderState: ClosedFolderState, | 351 ClosedFolderState: ClosedFolderState, |
349 NodeState: NodeState, | 352 NodeState: NodeState, |
350 SearchState: SearchState, | 353 SearchState: SearchState, |
351 SelectedFolderState: SelectedFolderState, | 354 SelectedFolderState: SelectedFolderState, |
352 SelectionState: SelectionState, | 355 SelectionState: SelectionState, |
353 }; | 356 }; |
354 }); | 357 }); |
OLD | NEW |