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 = new Set(); | 21 var newItems = new Set(); |
22 if (action.add) | 22 if (action.add) |
23 newItems = new Set(selectionState.items); | 23 newItems = new Set(selectionState.items); |
24 | 24 |
25 action.items.forEach(function(id) { | 25 action.items.forEach(function(id) { |
26 newItems.add(id); | 26 var add = true; |
| 27 if (action.toggle) |
| 28 add = !newItems.has(id); |
| 29 |
| 30 if (add) |
| 31 newItems.add(id); |
| 32 else |
| 33 newItems.delete(id); |
27 }); | 34 }); |
28 | 35 |
29 return /** @type {SelectionState} */ (Object.assign({}, selectionState, { | 36 return /** @type {SelectionState} */ (Object.assign({}, selectionState, { |
30 items: newItems, | 37 items: newItems, |
31 anchor: action.anchor, | 38 anchor: action.anchor, |
32 })); | 39 })); |
33 }; | 40 }; |
34 | 41 |
35 /** | 42 /** |
36 * @param {SelectionState} selectionState | 43 * @param {SelectionState} selectionState |
37 * @return {SelectionState} | 44 * @return {SelectionState} |
38 */ | 45 */ |
39 SelectionState.deselectAll = function(selectionState) { | 46 SelectionState.deselectAll = function(selectionState) { |
40 return { | 47 return { |
41 items: new Set(), | 48 items: new Set(), |
42 anchor: null, | 49 anchor: null, |
43 }; | 50 }; |
44 }; | 51 }; |
45 | 52 |
46 /** | 53 /** |
47 * @param {SelectionState} selectionState | 54 * @param {SelectionState} selectionState |
48 * @param {!Set<string>} deleted | 55 * @param {!Set<string>} deleted |
49 * @return SelectionState | 56 * @return SelectionState |
50 */ | 57 */ |
51 SelectionState.deselectDeletedItems = function(selectionState, deleted) { | 58 SelectionState.deselectDeletedItems = function(selectionState, deleted) { |
52 return /** @type {SelectionState} */ Object.assign({}, selectionState, { | 59 return /** @type {SelectionState} */ Object.assign({}, selectionState, { |
53 items: bookmarks.util.removeIdsFromSet(selectionState.items, deleted), | 60 items: bookmarks.util.removeIdsFromSet(selectionState.items, deleted), |
54 anchor: null, | 61 anchor: !selectionState.anchor || deleted.has(selectionState.anchor) ? |
| 62 null : |
| 63 selectionState.anchor, |
55 }); | 64 }); |
56 }; | 65 }; |
57 | 66 |
58 /** | 67 /** |
59 * @param {SelectionState} selection | 68 * @param {SelectionState} selection |
60 * @param {Action} action | 69 * @param {Action} action |
61 * @return {SelectionState} | 70 * @return {SelectionState} |
62 */ | 71 */ |
63 SelectionState.updateSelection = function(selection, action) { | 72 SelectionState.updateSelection = function(selection, action) { |
64 switch (action.name) { | 73 switch (action.name) { |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 | 431 |
423 return { | 432 return { |
424 reduceAction: reduceAction, | 433 reduceAction: reduceAction, |
425 ClosedFolderState: ClosedFolderState, | 434 ClosedFolderState: ClosedFolderState, |
426 NodeState: NodeState, | 435 NodeState: NodeState, |
427 SearchState: SearchState, | 436 SearchState: SearchState, |
428 SelectedFolderState: SelectedFolderState, | 437 SelectedFolderState: SelectedFolderState, |
429 SelectionState: SelectionState, | 438 SelectionState: SelectionState, |
430 }; | 439 }; |
431 }); | 440 }); |
OLD | NEW |