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

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: Review comments 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 newItems = {};
22 if (action.add)
23 Object.assign(newItems, selectionState.items);
24
25 action.items.forEach(function(id) {
26 newItems[id] = true;
27 });
28
29 return /** @type {SelectionState} */ (Object.assign({}, selectionState, {
30 items: newItems,
31 anchor: action.anchor,
32 }));
33 };
34
35 /**
36 * @param {SelectionState} selectionState
37 * @return {SelectionState}
38 */
39 SelectionState.deselectAll = function(selectionState) {
40 return {
41 items: {},
42 anchor: null,
43 };
44 };
45
46 /**
47 * @param {SelectionState} selection
48 * @param {Action} action
49 * @return {SelectionState}
50 */
51 SelectionState.updateSelection = function(selection, action) {
52 switch (action.name) {
53 case 'clear-search':
54 case 'finish-search':
55 case 'select-folder':
56 return SelectionState.deselectAll(selection);
57 case 'select-items':
58 return SelectionState.selectItems(selection, action);
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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 // search. 224 // search.
175 return nodes['0'].children[0]; 225 return nodes['0'].children[0];
176 default: 226 default:
177 return selectedFolder; 227 return selectedFolder;
178 } 228 }
179 }; 229 };
180 230
181 var ClosedFolderState = {}; 231 var ClosedFolderState = {};
182 232
183 /** 233 /**
184 * @param {ClosedFolderState} state 234 * @param {ClosedFolderState} closedFolders
185 * @param {Action} action 235 * @param {Action} action
186 * @param {NodeList} nodes 236 * @param {NodeList} nodes
187 * @return {ClosedFolderState} 237 * @return {ClosedFolderState}
188 */ 238 */
189 ClosedFolderState.openAncestorsOf = function(state, action, nodes) { 239 ClosedFolderState.openAncestorsOf = function(closedFolders, action, nodes) {
190 var id = action.id; 240 var id = action.id;
191 var modifications = {}; 241 var modifications = {};
192 var parentId = nodes[id].parentId; 242 var parentId = nodes[id].parentId;
193 while (parentId) { 243 while (parentId) {
194 if (state[parentId]) { 244 if (closedFolders[parentId]) {
195 modifications[parentId] = false; 245 modifications[parentId] = false;
196 } 246 }
197 parentId = nodes[parentId].parentId; 247 parentId = nodes[parentId].parentId;
198 } 248 }
199 249
200 return Object.assign({}, state, modifications); 250 return Object.assign({}, closedFolders, modifications);
201 }; 251 };
202 252
203 /** 253 /**
204 * @param {ClosedFolderState} state 254 * @param {ClosedFolderState} closedFolders
205 * @param {Action} action 255 * @param {Action} action
206 * @return {ClosedFolderState} 256 * @return {ClosedFolderState}
207 */ 257 */
208 ClosedFolderState.changeFolderOpen = function(state, action) { 258 ClosedFolderState.changeFolderOpen = function(closedFolders, action) {
209 var closed = !action.open; 259 var closed = !action.open;
210 var modification = {}; 260 var modification = {};
211 modification[action.id] = closed; 261 modification[action.id] = closed;
212 262
213 return Object.assign({}, state, modification); 263 return Object.assign({}, closedFolders, modification);
214 }; 264 };
215 265
216 /** 266 /**
217 * @param {ClosedFolderState} state 267 * @param {ClosedFolderState} closedFolders
218 * @param {Action} action 268 * @param {Action} action
219 * @param {NodeList} nodes 269 * @param {NodeList} nodes
220 * @return {ClosedFolderState} 270 * @return {ClosedFolderState}
221 */ 271 */
222 ClosedFolderState.updateClosedFolders = function(state, action, nodes) { 272 ClosedFolderState.updateClosedFolders = function(
273 closedFolders, action, nodes) {
223 switch (action.name) { 274 switch (action.name) {
224 case 'change-folder-open': 275 case 'change-folder-open':
225 return ClosedFolderState.changeFolderOpen(state, action); 276 return ClosedFolderState.changeFolderOpen(closedFolders, action);
226 case 'select-folder': 277 case 'select-folder':
227 return ClosedFolderState.openAncestorsOf(state, action, nodes); 278 return ClosedFolderState.openAncestorsOf(closedFolders, action, nodes);
228 default: 279 default:
229 return state; 280 return closedFolders;
230 }; 281 };
231 }; 282 };
232 283
233 /** 284 /**
234 * Root reducer for the Bookmarks page. This is called by the store in 285 * Root reducer for the Bookmarks page. This is called by the store in
235 * response to an action, and the return value is used to update the UI. 286 * response to an action, and the return value is used to update the UI.
236 * @param {!BookmarksPageState} state 287 * @param {!BookmarksPageState} state
237 * @param {Action} action 288 * @param {Action} action
238 * @return {!BookmarksPageState} 289 * @return {!BookmarksPageState}
239 */ 290 */
240 function reduceAction(state, action) { 291 function reduceAction(state, action) {
241 return { 292 return {
242 nodes: NodeState.updateNodes(state.nodes, action), 293 nodes: NodeState.updateNodes(state.nodes, action),
243 selectedFolder: SelectedFolderState.updateSelectedFolder( 294 selectedFolder: SelectedFolderState.updateSelectedFolder(
244 state.selectedFolder, action, state.nodes), 295 state.selectedFolder, action, state.nodes),
245 closedFolders: ClosedFolderState.updateClosedFolders( 296 closedFolders: ClosedFolderState.updateClosedFolders(
246 state.closedFolders, action, state.nodes), 297 state.closedFolders, action, state.nodes),
247 search: SearchState.updateSearch(state.search, action), 298 search: SearchState.updateSearch(state.search, action),
299 selection: SelectionState.updateSelection(state.selection, action),
248 }; 300 };
249 } 301 }
250 302
251 return { 303 return {
252 reduceAction: reduceAction, 304 reduceAction: reduceAction,
253 ClosedFolderState: ClosedFolderState, 305 ClosedFolderState: ClosedFolderState,
254 NodeState: NodeState, 306 NodeState: NodeState,
255 SearchState: SearchState, 307 SearchState: SearchState,
256 SelectedFolderState: SelectedFolderState, 308 SelectedFolderState: SelectedFolderState,
309 SelectionState: SelectionState,
257 }; 310 };
258 }); 311 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_bookmarks/item.js ('k') | chrome/browser/resources/md_bookmarks/types.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698