| OLD | NEW |
| 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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 return /** @type {BookmarkNode} */ ( | 197 return /** @type {BookmarkNode} */ ( |
| 198 Object.assign({}, node, action.changeInfo)); | 198 Object.assign({}, node, action.changeInfo)); |
| 199 }); | 199 }); |
| 200 }; | 200 }; |
| 201 | 201 |
| 202 /** | 202 /** |
| 203 * @param {NodeList} nodes | 203 * @param {NodeList} nodes |
| 204 * @param {Action} action | 204 * @param {Action} action |
| 205 * @return {NodeList} | 205 * @return {NodeList} |
| 206 */ | 206 */ |
| 207 NodeState.removeBookmark = function(nodes, action) { | |
| 208 var newState = | |
| 209 NodeState.modifyNode_(nodes, action.parentId, function(node) { | |
| 210 var newChildren = node.children.slice(); | |
| 211 newChildren.splice(action.index, 1); | |
| 212 return /** @type {BookmarkNode} */ ( | |
| 213 Object.assign({}, node, {children: newChildren})); | |
| 214 }); | |
| 215 | |
| 216 return bookmarks.util.removeIdsFromMap(newState, action.descendants); | |
| 217 }; | |
| 218 | |
| 219 /** | |
| 220 * @param {NodeList} nodes | |
| 221 * @param {Action} action | |
| 222 * @return {NodeList} | |
| 223 */ | |
| 224 NodeState.moveBookmark = function(nodes, action) { | 207 NodeState.moveBookmark = function(nodes, action) { |
| 225 var nodeModifications = {}; | 208 var nodeModifications = {}; |
| 226 var id = action.id; | 209 var id = action.id; |
| 227 | 210 |
| 228 // Change node's parent. | 211 // Change node's parent. |
| 229 nodeModifications[id] = | 212 nodeModifications[id] = |
| 230 Object.assign({}, nodes[id], {parentId: action.parentId}); | 213 Object.assign({}, nodes[id], {parentId: action.parentId}); |
| 231 | 214 |
| 232 // Remove from old parent. | 215 // Remove from old parent. |
| 233 var oldParentId = action.oldParentId; | 216 var oldParentId = action.oldParentId; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 246 Object.assign({}, nodes[parentId], {children: parentChildren}); | 229 Object.assign({}, nodes[parentId], {children: parentChildren}); |
| 247 | 230 |
| 248 return Object.assign({}, nodes, nodeModifications); | 231 return Object.assign({}, nodes, nodeModifications); |
| 249 }; | 232 }; |
| 250 | 233 |
| 251 /** | 234 /** |
| 252 * @param {NodeList} nodes | 235 * @param {NodeList} nodes |
| 253 * @param {Action} action | 236 * @param {Action} action |
| 254 * @return {NodeList} | 237 * @return {NodeList} |
| 255 */ | 238 */ |
| 239 NodeState.removeBookmark = function(nodes, action) { |
| 240 var newState = |
| 241 NodeState.modifyNode_(nodes, action.parentId, function(node) { |
| 242 var newChildren = node.children.slice(); |
| 243 newChildren.splice(action.index, 1); |
| 244 return /** @type {BookmarkNode} */ ( |
| 245 Object.assign({}, node, {children: newChildren})); |
| 246 }); |
| 247 |
| 248 return bookmarks.util.removeIdsFromMap(newState, action.descendants); |
| 249 }; |
| 250 |
| 251 /** |
| 252 * @param {NodeList} nodes |
| 253 * @param {Action} action |
| 254 * @return {NodeList} |
| 255 */ |
| 256 NodeState.reorderChildren = function(nodes, action) { |
| 257 return NodeState.modifyNode_(nodes, action.id, function(node) { |
| 258 return /** @type {BookmarkNode} */ ( |
| 259 Object.assign({}, node, {children: action.children})); |
| 260 }); |
| 261 }; |
| 262 |
| 263 /** |
| 264 * @param {NodeList} nodes |
| 265 * @param {Action} action |
| 266 * @return {NodeList} |
| 267 */ |
| 256 NodeState.updateNodes = function(nodes, action) { | 268 NodeState.updateNodes = function(nodes, action) { |
| 257 switch (action.name) { | 269 switch (action.name) { |
| 258 case 'create-bookmark': | 270 case 'create-bookmark': |
| 259 return NodeState.createBookmark(nodes, action); | 271 return NodeState.createBookmark(nodes, action); |
| 260 case 'edit-bookmark': | 272 case 'edit-bookmark': |
| 261 return NodeState.editBookmark(nodes, action); | 273 return NodeState.editBookmark(nodes, action); |
| 274 case 'move-bookmark': |
| 275 return NodeState.moveBookmark(nodes, action); |
| 262 case 'remove-bookmark': | 276 case 'remove-bookmark': |
| 263 return NodeState.removeBookmark(nodes, action); | 277 return NodeState.removeBookmark(nodes, action); |
| 264 case 'move-bookmark': | 278 case 'reorder-children': |
| 265 return NodeState.moveBookmark(nodes, action); | 279 return NodeState.reorderChildren(nodes, action); |
| 266 case 'refresh-nodes': | 280 case 'refresh-nodes': |
| 267 return action.nodes; | 281 return action.nodes; |
| 268 default: | 282 default: |
| 269 return nodes; | 283 return nodes; |
| 270 } | 284 } |
| 271 }; | 285 }; |
| 272 | 286 |
| 273 var SelectedFolderState = {}; | 287 var SelectedFolderState = {}; |
| 274 | 288 |
| 275 /** | 289 /** |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 | 429 |
| 416 return { | 430 return { |
| 417 reduceAction: reduceAction, | 431 reduceAction: reduceAction, |
| 418 ClosedFolderState: ClosedFolderState, | 432 ClosedFolderState: ClosedFolderState, |
| 419 NodeState: NodeState, | 433 NodeState: NodeState, |
| 420 SearchState: SearchState, | 434 SearchState: SearchState, |
| 421 SelectedFolderState: SelectedFolderState, | 435 SelectedFolderState: SelectedFolderState, |
| 422 SelectionState: SelectionState, | 436 SelectionState: SelectionState, |
| 423 }; | 437 }; |
| 424 }); | 438 }); |
| OLD | NEW |