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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 var nodeModification = {}; | 127 var nodeModification = {}; |
128 nodeModification[id] = callback(nodes[id]); | 128 nodeModification[id] = callback(nodes[id]); |
129 return Object.assign({}, nodes, nodeModification); | 129 return Object.assign({}, nodes, nodeModification); |
130 }; | 130 }; |
131 | 131 |
132 /** | 132 /** |
133 * @param {NodeList} nodes | 133 * @param {NodeList} nodes |
134 * @param {Action} action | 134 * @param {Action} action |
135 * @return {NodeList} | 135 * @return {NodeList} |
136 */ | 136 */ |
| 137 NodeState.createBookmark = function(nodes, action) { |
| 138 var nodeModifications = {}; |
| 139 nodeModifications[action.id] = action.node; |
| 140 |
| 141 var parentNode = nodes[action.parentId]; |
| 142 var newChildren = parentNode.children.slice(); |
| 143 newChildren.splice(action.parentIndex, 0, action.id); |
| 144 nodeModifications[action.parentId] = Object.assign({}, parentNode, { |
| 145 children: newChildren, |
| 146 }); |
| 147 |
| 148 return Object.assign({}, nodes, nodeModifications); |
| 149 }; |
| 150 |
| 151 /** |
| 152 * @param {NodeList} nodes |
| 153 * @param {Action} action |
| 154 * @return {NodeList} |
| 155 */ |
137 NodeState.editBookmark = function(nodes, action) { | 156 NodeState.editBookmark = function(nodes, action) { |
138 // Do not allow folders to change URL (making them no longer folders). | 157 // Do not allow folders to change URL (making them no longer folders). |
139 if (!nodes[action.id].url && action.changeInfo.url) | 158 if (!nodes[action.id].url && action.changeInfo.url) |
140 delete action.changeInfo.url; | 159 delete action.changeInfo.url; |
141 | 160 |
142 return NodeState.modifyNode_(nodes, action.id, function(node) { | 161 return NodeState.modifyNode_(nodes, action.id, function(node) { |
143 return /** @type {BookmarkNode} */ ( | 162 return /** @type {BookmarkNode} */ ( |
144 Object.assign({}, node, action.changeInfo)); | 163 Object.assign({}, node, action.changeInfo)); |
145 }); | 164 }); |
146 }; | 165 }; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 return Object.assign({}, nodes, nodeModifications); | 210 return Object.assign({}, nodes, nodeModifications); |
192 }; | 211 }; |
193 | 212 |
194 /** | 213 /** |
195 * @param {NodeList} nodes | 214 * @param {NodeList} nodes |
196 * @param {Action} action | 215 * @param {Action} action |
197 * @return {NodeList} | 216 * @return {NodeList} |
198 */ | 217 */ |
199 NodeState.updateNodes = function(nodes, action) { | 218 NodeState.updateNodes = function(nodes, action) { |
200 switch (action.name) { | 219 switch (action.name) { |
| 220 case 'create-bookmark': |
| 221 return NodeState.createBookmark(nodes, action); |
201 case 'edit-bookmark': | 222 case 'edit-bookmark': |
202 return NodeState.editBookmark(nodes, action); | 223 return NodeState.editBookmark(nodes, action); |
203 case 'remove-bookmark': | 224 case 'remove-bookmark': |
204 return NodeState.removeBookmark(nodes, action); | 225 return NodeState.removeBookmark(nodes, action); |
205 case 'move-bookmark': | 226 case 'move-bookmark': |
206 return NodeState.moveBookmark(nodes, action); | 227 return NodeState.moveBookmark(nodes, action); |
207 case 'refresh-nodes': | 228 case 'refresh-nodes': |
208 return action.nodes; | 229 return action.nodes; |
209 default: | 230 default: |
210 return nodes; | 231 return nodes; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 | 365 |
345 return { | 366 return { |
346 reduceAction: reduceAction, | 367 reduceAction: reduceAction, |
347 ClosedFolderState: ClosedFolderState, | 368 ClosedFolderState: ClosedFolderState, |
348 NodeState: NodeState, | 369 NodeState: NodeState, |
349 SearchState: SearchState, | 370 SearchState: SearchState, |
350 SelectedFolderState: SelectedFolderState, | 371 SelectedFolderState: SelectedFolderState, |
351 SelectionState: SelectionState, | 372 SelectionState: SelectionState, |
352 }; | 373 }; |
353 }); | 374 }); |
OLD | NEW |