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

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

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

Powered by Google App Engine
This is Rietveld 408576698