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

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

Issue 2740863003: MD Bookmarks: Implement search and selection in new data flow system (Closed)
Patch Set: Review round 2 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 SearchState = {};
14
15 /**
16 * @param {SearchState} search
17 * @param {Action} action
18 * @return {SearchState}
19 */
20 SearchState.startSearch = function(search, action) {
21 return {
22 term: action.term,
23 inProgress: true,
24 results: [],
25 };
26 };
27
28 /**
29 * @param {SearchState} search
30 * @param {Action} action
31 * @return {SearchState}
32 */
33 SearchState.finishSearch = function(search, action) {
34 return /** @type {SearchState} */ (Object.assign({}, search, {
35 inProgress: false,
36 results: action.results,
37 }));
38 };
39
40 /** @return {SearchState} */
41 SearchState.clearSearch = function() {
42 return {
43 term: '',
44 inProgress: false,
45 results: [],
46 };
47 };
48
49 /**
50 * @param {SearchState} search
51 * @param {Action} action
52 * @return {SearchState}
53 */
54 SearchState.updateSearch = function(search, action) {
55 switch (action.name) {
56 case 'start-search':
57 return SearchState.startSearch(search, action);
58 case 'select-folder':
59 case 'clear-search':
60 return SearchState.clearSearch();
61 case 'finish-search':
62 return SearchState.finishSearch(search, action);
63 default:
64 return search;
65 }
66 };
67
13 var NodeState = {}; 68 var NodeState = {};
14 69
15 /** 70 /**
16 * @param {NodeList} nodes 71 * @param {NodeList} nodes
17 * @param {string} id 72 * @param {string} id
18 * @param {function(BookmarkNode):BookmarkNode} callback 73 * @param {function(BookmarkNode):BookmarkNode} callback
19 * @return {NodeList} 74 * @return {NodeList}
20 */ 75 */
21 NodeState.modifyNode_ = function(nodes, id, callback) { 76 NodeState.modifyNode_ = function(nodes, id, callback) {
22 var nodeModification = {}; 77 var nodeModification = {};
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 return action.id; 160 return action.id;
106 case 'change-folder-open': 161 case 'change-folder-open':
107 // When hiding the selected folder by closing its ancestor, select 162 // When hiding the selected folder by closing its ancestor, select
108 // that ancestor instead. 163 // that ancestor instead.
109 if (!action.open && selectedFolder && 164 if (!action.open && selectedFolder &&
110 SelectedFolderState.isAncestorOf( 165 SelectedFolderState.isAncestorOf(
111 nodes, action.id, selectedFolder)) { 166 nodes, action.id, selectedFolder)) {
112 return action.id; 167 return action.id;
113 } 168 }
114 return selectedFolder; 169 return selectedFolder;
170 case 'finish-search':
171 return null;
172 case 'clear-search':
173 // TODO(tsergeant): Return to the folder that was selected before the
174 // search.
175 return nodes['0'].children[0];
115 default: 176 default:
116 return selectedFolder; 177 return selectedFolder;
117 } 178 }
118 }; 179 };
119 180
120 var ClosedFolderState = {}; 181 var ClosedFolderState = {};
121 182
122 /** 183 /**
123 * @param {ClosedFolderState} state 184 * @param {ClosedFolderState} state
124 * @param {Action} action 185 * @param {Action} action
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 * @param {Action} action 237 * @param {Action} action
177 * @return {!BookmarksPageState} 238 * @return {!BookmarksPageState}
178 */ 239 */
179 function reduceAction(state, action) { 240 function reduceAction(state, action) {
180 return { 241 return {
181 nodes: NodeState.updateNodes(state.nodes, action), 242 nodes: NodeState.updateNodes(state.nodes, action),
182 selectedFolder: SelectedFolderState.updateSelectedFolder( 243 selectedFolder: SelectedFolderState.updateSelectedFolder(
183 state.selectedFolder, action, state.nodes), 244 state.selectedFolder, action, state.nodes),
184 closedFolders: ClosedFolderState.updateClosedFolders( 245 closedFolders: ClosedFolderState.updateClosedFolders(
185 state.closedFolders, action, state.nodes), 246 state.closedFolders, action, state.nodes),
247 search: SearchState.updateSearch(state.search, action),
186 }; 248 };
187 } 249 }
188 250
189 return { 251 return {
190 reduceAction: reduceAction, 252 reduceAction: reduceAction,
191 ClosedFolderState: ClosedFolderState, 253 ClosedFolderState: ClosedFolderState,
192 NodeState: NodeState, 254 NodeState: NodeState,
255 SearchState: SearchState,
193 SelectedFolderState: SelectedFolderState, 256 SelectedFolderState: SelectedFolderState,
194 }; 257 };
195 }); 258 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_bookmarks/list.js ('k') | chrome/browser/resources/md_bookmarks/toolbar.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698