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

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: Remove selection from this CL 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 20 matching lines...) Expand all
43 /** 98 /**
44 * @param {NodeList} nodes 99 * @param {NodeList} nodes
45 * @param {Action} action 100 * @param {Action} action
46 * @return {NodeList} 101 * @return {NodeList}
47 */ 102 */
48 NodeState.removeBookmark = function(nodes, action) { 103 NodeState.removeBookmark = function(nodes, action) {
49 return NodeState.modifyNode_(nodes, action.parentId, function(node) { 104 return NodeState.modifyNode_(nodes, action.parentId, function(node) {
50 var newChildren = node.children.slice(); 105 var newChildren = node.children.slice();
51 newChildren.splice(action.index, 1); 106 newChildren.splice(action.index, 1);
52 return /** @type {BookmarkNode} */ ( 107 return /** @type {BookmarkNode} */ (
53 Object.assign({}, node, {children: newChildren})); 108 Object.assign({}, node, {children: newChildren}));
calamity 2017/03/13 04:50:36 Was this supposed to have a TODO to remove the nod
tsergeant 2017/03/14 02:40:36 That TODO lives in the test file (and as above, I'
54 }); 109 });
55 }; 110 };
56 111
57 /** 112 /**
58 * @param {NodeList} nodes 113 * @param {NodeList} nodes
59 * @param {Action} action 114 * @param {Action} action
60 * @return {NodeList} 115 * @return {NodeList}
61 */ 116 */
62 NodeState.updateNodes = function(nodes, action) { 117 NodeState.updateNodes = function(nodes, action) {
63 switch (action.name) { 118 switch (action.name) {
(...skipping 41 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 return nodes['0'].children[0];
calamity 2017/03/13 04:50:36 Add a TODO to make this return to the previously s
tsergeant 2017/03/14 02:40:36 Done.
115 default: 174 default:
116 return selectedFolder; 175 return selectedFolder;
117 } 176 }
118 }; 177 };
119 178
120 var ClosedFolderState = {}; 179 var ClosedFolderState = {};
121 180
122 /** 181 /**
123 * @param {ClosedFolderState} state 182 * @param {ClosedFolderState} state
124 * @param {Action} action 183 * @param {Action} action
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 * @param {Action} action 235 * @param {Action} action
177 * @return {!BookmarksPageState} 236 * @return {!BookmarksPageState}
178 */ 237 */
179 function reduceAction(state, action) { 238 function reduceAction(state, action) {
180 return { 239 return {
181 nodes: NodeState.updateNodes(state.nodes, action), 240 nodes: NodeState.updateNodes(state.nodes, action),
182 selectedFolder: SelectedFolderState.updateSelectedFolder( 241 selectedFolder: SelectedFolderState.updateSelectedFolder(
183 state.selectedFolder, action, state.nodes), 242 state.selectedFolder, action, state.nodes),
184 closedFolders: ClosedFolderState.updateClosedFolders( 243 closedFolders: ClosedFolderState.updateClosedFolders(
185 state.closedFolders, action, state.nodes), 244 state.closedFolders, action, state.nodes),
245 search: SearchState.updateSearch(state.search, action),
186 }; 246 };
187 } 247 }
188 248
189 return { 249 return {
190 reduceAction: reduceAction, 250 reduceAction: reduceAction,
191 ClosedFolderState: ClosedFolderState, 251 ClosedFolderState: ClosedFolderState,
192 NodeState: NodeState, 252 NodeState: NodeState,
253 SearchState: SearchState,
193 SelectedFolderState: SelectedFolderState, 254 SelectedFolderState: SelectedFolderState,
194 }; 255 };
195 }); 256 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698