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

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

Issue 2750463010: [MD Bookmarks] Handle bookmark moves. (Closed)
Patch Set: remove log 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 */
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 return /** @type {BookmarkNode} */ ( 157 return /** @type {BookmarkNode} */ (
158 Object.assign({}, node, {children: newChildren})); 158 Object.assign({}, node, {children: newChildren}));
159 }); 159 });
160 }; 160 };
161 161
162 /** 162 /**
163 * @param {NodeList} nodes 163 * @param {NodeList} nodes
164 * @param {Action} action 164 * @param {Action} action
165 * @return {NodeList} 165 * @return {NodeList}
166 */ 166 */
167 NodeState.moveBookmark = function(nodes, action) {
168 var nodeModifications = {};
169 var id = action.id;
170
171 // Change node's parent.
172 nodeModifications[id] =
173 Object.assign({}, nodes[id], {parentId: action.parentId});
174
175 // Remove from old parent.
176 var oldParentId = action.oldParentId;
177 var oldParentChildren = nodes[oldParentId].children.slice();
178 oldParentChildren.splice(action.oldIndex, 1);
179 nodeModifications[oldParentId] =
180 Object.assign({}, nodes[oldParentId], {children: oldParentChildren});
181
182 // Add to new parent.
183 var parentId = action.parentId;
184 var parentChildren = oldParentId == parentId ?
185 oldParentChildren :
186 nodes[parentId].children.slice();
187 parentChildren.splice(action.index, 0, action.id);
188 nodeModifications[parentId] =
189 Object.assign({}, nodes[parentId], {children: parentChildren});
190
191 return Object.assign({}, nodes, nodeModifications);
192 };
193
194 /**
195 * @param {NodeList} nodes
196 * @param {Action} action
197 * @return {NodeList}
198 */
167 NodeState.updateNodes = function(nodes, action) { 199 NodeState.updateNodes = function(nodes, action) {
168 switch (action.name) { 200 switch (action.name) {
169 case 'edit-bookmark': 201 case 'edit-bookmark':
170 return NodeState.editBookmark(nodes, action); 202 return NodeState.editBookmark(nodes, action);
171 case 'remove-bookmark': 203 case 'remove-bookmark':
172 return NodeState.removeBookmark(nodes, action); 204 return NodeState.removeBookmark(nodes, action);
205 case 'move-bookmark':
206 return NodeState.moveBookmark(nodes, action);
173 case 'refresh-nodes': 207 case 'refresh-nodes':
174 return action.nodes; 208 return action.nodes;
175 default: 209 default:
176 return nodes; 210 return nodes;
177 } 211 }
178 }; 212 };
179 213
180 var SelectedFolderState = {}; 214 var SelectedFolderState = {};
181 215
182 /** 216 /**
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 return nodes['0'].children[0]; 259 return nodes['0'].children[0];
226 default: 260 default:
227 return selectedFolder; 261 return selectedFolder;
228 } 262 }
229 }; 263 };
230 264
231 var ClosedFolderState = {}; 265 var ClosedFolderState = {};
232 266
233 /** 267 /**
234 * @param {ClosedFolderState} closedFolders 268 * @param {ClosedFolderState} closedFolders
235 * @param {Action} action 269 * @param {string|undefined} id
236 * @param {NodeList} nodes 270 * @param {NodeList} nodes
237 * @return {ClosedFolderState} 271 * @return {ClosedFolderState}
238 */ 272 */
239 ClosedFolderState.openAncestorsOf = function(closedFolders, action, nodes) { 273 ClosedFolderState.openFolderAndAncestors = function(
240 var id = action.id; 274 closedFolders, id, nodes) {
241 var modifications = {}; 275 var modifications = {};
242 var parentId = nodes[id].parentId; 276 var currentId = id;
243 while (parentId) { 277 while (currentId) {
244 if (closedFolders[parentId]) { 278 if (closedFolders[currentId]) {
245 modifications[parentId] = false; 279 modifications[currentId] = false;
246 } 280 }
247 parentId = nodes[parentId].parentId; 281 currentId = nodes[currentId].parentId;
248 } 282 }
249 283
250 return Object.assign({}, closedFolders, modifications); 284 return Object.assign({}, closedFolders, modifications);
251 }; 285 };
252 286
253 /** 287 /**
254 * @param {ClosedFolderState} closedFolders 288 * @param {ClosedFolderState} closedFolders
255 * @param {Action} action 289 * @param {Action} action
256 * @return {ClosedFolderState} 290 * @return {ClosedFolderState}
257 */ 291 */
(...skipping 10 matching lines...) Expand all
268 * @param {Action} action 302 * @param {Action} action
269 * @param {NodeList} nodes 303 * @param {NodeList} nodes
270 * @return {ClosedFolderState} 304 * @return {ClosedFolderState}
271 */ 305 */
272 ClosedFolderState.updateClosedFolders = function( 306 ClosedFolderState.updateClosedFolders = function(
273 closedFolders, action, nodes) { 307 closedFolders, action, nodes) {
274 switch (action.name) { 308 switch (action.name) {
275 case 'change-folder-open': 309 case 'change-folder-open':
276 return ClosedFolderState.changeFolderOpen(closedFolders, action); 310 return ClosedFolderState.changeFolderOpen(closedFolders, action);
277 case 'select-folder': 311 case 'select-folder':
278 return ClosedFolderState.openAncestorsOf(closedFolders, action, nodes); 312 return ClosedFolderState.openFolderAndAncestors(
313 closedFolders, nodes[action.id].parentId, nodes);
314 case 'move-bookmark':
315 if (!nodes[action.id].children)
316 return closedFolders;
317
318 return ClosedFolderState.openFolderAndAncestors(
319 closedFolders, action.parentId, nodes);
279 default: 320 default:
280 return closedFolders; 321 return closedFolders;
281 }; 322 };
282 }; 323 };
283 324
284 /** 325 /**
285 * Root reducer for the Bookmarks page. This is called by the store in 326 * Root reducer for the Bookmarks page. This is called by the store in
286 * response to an action, and the return value is used to update the UI. 327 * response to an action, and the return value is used to update the UI.
287 * @param {!BookmarksPageState} state 328 * @param {!BookmarksPageState} state
288 * @param {Action} action 329 * @param {Action} action
(...skipping 13 matching lines...) Expand all
302 343
303 return { 344 return {
304 reduceAction: reduceAction, 345 reduceAction: reduceAction,
305 ClosedFolderState: ClosedFolderState, 346 ClosedFolderState: ClosedFolderState,
306 NodeState: NodeState, 347 NodeState: NodeState,
307 SearchState: SearchState, 348 SearchState: SearchState,
308 SelectedFolderState: SelectedFolderState, 349 SelectedFolderState: SelectedFolderState,
309 SelectionState: SelectionState, 350 SelectionState: SelectionState,
310 }; 351 };
311 }); 352 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_bookmarks/api_listener.js ('k') | chrome/test/data/webui/md_bookmarks/reducers_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698