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

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

Issue 2795623002: MD Bookmarks: Handle bookmark creation (Closed)
Patch Set: Update test Created 3 years, 8 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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 var nodeModification = {}; 128 var nodeModification = {};
129 nodeModification[id] = callback(nodes[id]); 129 nodeModification[id] = callback(nodes[id]);
130 return Object.assign({}, nodes, nodeModification); 130 return Object.assign({}, nodes, nodeModification);
131 }; 131 };
132 132
133 /** 133 /**
134 * @param {NodeList} nodes 134 * @param {NodeList} nodes
135 * @param {Action} action 135 * @param {Action} action
136 * @return {NodeList} 136 * @return {NodeList}
137 */ 137 */
138 NodeState.createBookmark = function(nodes, action) {
139 var nodeModifications = {};
140 nodeModifications[action.id] = action.node;
141
142 var parentNode = nodes[action.parentId];
143 var newChildren = parentNode.children.slice();
144 newChildren.splice(action.parentIndex, 0, action.id);
145 nodeModifications[action.parentId] = Object.assign({}, parentNode, {
146 children: newChildren,
147 });
148
149 return Object.assign({}, nodes, nodeModifications);
150 };
151
152 /**
153 * @param {NodeList} nodes
154 * @param {Action} action
155 * @return {NodeList}
156 */
138 NodeState.editBookmark = function(nodes, action) { 157 NodeState.editBookmark = function(nodes, action) {
139 // Do not allow folders to change URL (making them no longer folders). 158 // Do not allow folders to change URL (making them no longer folders).
140 if (!nodes[action.id].url && action.changeInfo.url) 159 if (!nodes[action.id].url && action.changeInfo.url)
141 delete action.changeInfo.url; 160 delete action.changeInfo.url;
142 161
143 return NodeState.modifyNode_(nodes, action.id, function(node) { 162 return NodeState.modifyNode_(nodes, action.id, function(node) {
144 return /** @type {BookmarkNode} */ ( 163 return /** @type {BookmarkNode} */ (
145 Object.assign({}, node, action.changeInfo)); 164 Object.assign({}, node, action.changeInfo));
146 }); 165 });
147 }; 166 };
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 return Object.assign({}, nodes, nodeModifications); 211 return Object.assign({}, nodes, nodeModifications);
193 }; 212 };
194 213
195 /** 214 /**
196 * @param {NodeList} nodes 215 * @param {NodeList} nodes
197 * @param {Action} action 216 * @param {Action} action
198 * @return {NodeList} 217 * @return {NodeList}
199 */ 218 */
200 NodeState.updateNodes = function(nodes, action) { 219 NodeState.updateNodes = function(nodes, action) {
201 switch (action.name) { 220 switch (action.name) {
221 case 'create-bookmark':
222 return NodeState.createBookmark(nodes, action);
202 case 'edit-bookmark': 223 case 'edit-bookmark':
203 return NodeState.editBookmark(nodes, action); 224 return NodeState.editBookmark(nodes, action);
204 case 'remove-bookmark': 225 case 'remove-bookmark':
205 return NodeState.removeBookmark(nodes, action); 226 return NodeState.removeBookmark(nodes, action);
206 case 'move-bookmark': 227 case 'move-bookmark':
207 return NodeState.moveBookmark(nodes, action); 228 return NodeState.moveBookmark(nodes, action);
208 case 'refresh-nodes': 229 case 'refresh-nodes':
209 return action.nodes; 230 return action.nodes;
210 default: 231 default:
211 return nodes; 232 return nodes;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 369
349 return { 370 return {
350 reduceAction: reduceAction, 371 reduceAction: reduceAction,
351 ClosedFolderState: ClosedFolderState, 372 ClosedFolderState: ClosedFolderState,
352 NodeState: NodeState, 373 NodeState: NodeState,
353 SearchState: SearchState, 374 SearchState: SearchState,
354 SelectedFolderState: SelectedFolderState, 375 SelectedFolderState: SelectedFolderState,
355 SelectionState: SelectionState, 376 SelectionState: SelectionState,
356 }; 377 };
357 }); 378 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_bookmarks/api_listener.js ('k') | chrome/browser/resources/md_bookmarks/util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698