Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: chrome/browser/resources/md_bookmarks/reducers.js

Issue 2741393002: MD Bookmarks: Implement item selection in new data flow system (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 = {};
14
15 /**
16 * @param {SelectionState} selectionState
17 * @param {Action} action
18 * @return {SelectionState}
19 */
20 SelectionState.selectItems = function(selectionState, action) {
21 var newState = {};
calamity 2017/03/14 03:01:45 nit: newItems
tsergeant 2017/03/14 05:42:02 Done.
22 if (action.add)
23 Object.assign(newState, selectionState.items);
24
25 action.items.forEach(function(id) {
26 newState[id] = true;
27 });
28
29 return /** @type {SelectionState} */ (Object.assign({}, selectionState, {
30 items: newState,
31 anchor: action.anchor,
32 }));
33 };
34
35 /**
36 * @param {SelectionState} selectionState
37 * @return {SelectionState}
38 */
39 SelectionState.deselectAll = function(selectionState) {
40 return {
41 count: 0,
calamity 2017/03/14 03:01:45 Is count used anywhere? Or is it just like, a #def
tsergeant 2017/03/14 05:42:02 I kept meaning to either implement it or remove it
42 items: {},
43 anchor: null,
44 };
45 };
46
47 /**
48 * @param {SelectionState} selection
49 * @param {Action} action
50 * @return {SelectionState}
51 */
52 SelectionState.updateSelection = function(selection, action) {
53 switch (action.name) {
54 case 'select-folder':
55 case 'finish-search':
calamity 2017/03/14 03:01:45 Also clear-search. +Test
tsergeant 2017/03/14 05:42:02 Done.
56 return SelectionState.deselectAll(selection);
57 case 'select-items':
58 return SelectionState.selectItems(selection, action);
calamity 2017/03/14 03:01:45 What happens to removed anchor indexes? They'll ex
tsergeant 2017/03/14 05:42:02 Yup, again, that's something that's was fixed in t
59 }
60 return selection;
61 };
62
13 var SearchState = {}; 63 var SearchState = {};
14 64
15 /** 65 /**
16 * @param {SearchState} search 66 * @param {SearchState} search
17 * @param {Action} action 67 * @param {Action} action
18 * @return {SearchState} 68 * @return {SearchState}
19 */ 69 */
20 SearchState.startSearch = function(search, action) { 70 SearchState.startSearch = function(search, action) {
21 return { 71 return {
22 term: action.term, 72 term: action.term,
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 260
211 return Object.assign({}, state, modification); 261 return Object.assign({}, state, modification);
212 }; 262 };
213 263
214 /** 264 /**
215 * @param {ClosedFolderState} state 265 * @param {ClosedFolderState} state
216 * @param {Action} action 266 * @param {Action} action
217 * @param {NodeList} nodes 267 * @param {NodeList} nodes
218 * @return {ClosedFolderState} 268 * @return {ClosedFolderState}
219 */ 269 */
220 ClosedFolderState.updateClosedFolders = function(state, action, nodes) { 270 ClosedFolderState.updateClosedFolders = function(state, action, nodes) {
calamity 2017/03/14 03:01:45 Change state to closedFolders to be consistent wit
tsergeant 2017/03/14 05:42:02 Done.
221 switch (action.name) { 271 switch (action.name) {
222 case 'change-folder-open': 272 case 'change-folder-open':
223 return ClosedFolderState.changeFolderOpen(state, action); 273 return ClosedFolderState.changeFolderOpen(state, action);
224 case 'select-folder': 274 case 'select-folder':
225 return ClosedFolderState.openAncestorsOf(state, action, nodes); 275 return ClosedFolderState.openAncestorsOf(state, action, nodes);
226 default: 276 default:
227 return state; 277 return state;
228 }; 278 };
229 }; 279 };
230 280
231 /** 281 /**
232 * Root reducer for the Bookmarks page. This is called by the store in 282 * Root reducer for the Bookmarks page. This is called by the store in
233 * response to an action, and the return value is used to update the UI. 283 * response to an action, and the return value is used to update the UI.
234 * @param {!BookmarksPageState} state 284 * @param {!BookmarksPageState} state
235 * @param {Action} action 285 * @param {Action} action
236 * @return {!BookmarksPageState} 286 * @return {!BookmarksPageState}
237 */ 287 */
238 function reduceAction(state, action) { 288 function reduceAction(state, action) {
239 return { 289 return {
240 nodes: NodeState.updateNodes(state.nodes, action), 290 nodes: NodeState.updateNodes(state.nodes, action),
241 selectedFolder: SelectedFolderState.updateSelectedFolder( 291 selectedFolder: SelectedFolderState.updateSelectedFolder(
242 state.selectedFolder, action, state.nodes), 292 state.selectedFolder, action, state.nodes),
243 closedFolders: ClosedFolderState.updateClosedFolders( 293 closedFolders: ClosedFolderState.updateClosedFolders(
244 state.closedFolders, action, state.nodes), 294 state.closedFolders, action, state.nodes),
245 search: SearchState.updateSearch(state.search, action), 295 search: SearchState.updateSearch(state.search, action),
296 selection: SelectionState.updateSelection(state.selection, action),
246 }; 297 };
247 } 298 }
248 299
249 return { 300 return {
250 reduceAction: reduceAction, 301 reduceAction: reduceAction,
251 ClosedFolderState: ClosedFolderState, 302 ClosedFolderState: ClosedFolderState,
252 NodeState: NodeState, 303 NodeState: NodeState,
253 SearchState: SearchState, 304 SearchState: SearchState,
254 SelectedFolderState: SelectedFolderState, 305 SelectedFolderState: SelectedFolderState,
306 SelectionState: SelectionState,
255 }; 307 };
256 }); 308 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698