| 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 for functions which produce action objects. These are | 6 * @fileoverview Module for functions which create actions that do not |
| 7 * listed in one place to document available actions and their parameters. | 7 * rely on Page state. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 cr.define('bookmarks.actions', function() { | 10 cr.define('bookmarks.actions', function() { |
| 11 /** | 11 /** |
| 12 * @param {string} id | 12 * @param {string} id |
| 13 * @param {BookmarkTreeNode} treeNode | 13 * @param {BookmarkTreeNode} treeNode |
| 14 */ | 14 */ |
| 15 function createBookmark(id, treeNode) { | 15 function createBookmark(id, treeNode) { |
| 16 return { | 16 return { |
| 17 name: 'create-bookmark', | 17 name: 'create-bookmark', |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 index: index, | 51 index: index, |
| 52 oldParentId: oldParentId, | 52 oldParentId: oldParentId, |
| 53 oldIndex: oldIndex, | 53 oldIndex: oldIndex, |
| 54 }; | 54 }; |
| 55 } | 55 } |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * @param {string} id | 58 * @param {string} id |
| 59 * @param {string} parentId | 59 * @param {string} parentId |
| 60 * @param {number} index | 60 * @param {number} index |
| 61 * @param {NodeList} nodes | 61 * @param {!Set<string>} descendants |
| 62 * @return {!Action} | 62 * @return {!Action} |
| 63 */ | 63 */ |
| 64 function removeBookmark(id, parentId, index, nodes) { | 64 function removeBookmarkSubtree(id, parentId, index, descendants) { |
| 65 var descendants = bookmarks.util.getDescendants(nodes, id); | |
| 66 return { | 65 return { |
| 67 name: 'remove-bookmark', | 66 name: 'remove-bookmark', |
| 68 id: id, | 67 id: id, |
| 69 descendants: descendants, | 68 descendants: descendants, |
| 70 parentId: parentId, | 69 parentId: parentId, |
| 71 index: index, | 70 index: index, |
| 72 }; | 71 }; |
| 73 } | 72 } |
| 74 | 73 |
| 75 /** | 74 /** |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 }; | 107 }; |
| 109 } | 108 } |
| 110 | 109 |
| 111 /** @return {!Action} */ | 110 /** @return {!Action} */ |
| 112 function clearSearch() { | 111 function clearSearch() { |
| 113 return { | 112 return { |
| 114 name: 'clear-search', | 113 name: 'clear-search', |
| 115 }; | 114 }; |
| 116 } | 115 } |
| 117 | 116 |
| 117 /** |
| 118 * TODO(tsergeant): Make |items| a Set instead of an Array. |
| 119 * @param {!Array<string>} items |
| 120 * @param {string} anchor |
| 121 * @param {boolean} add |
| 122 * @return {!Action} |
| 123 */ |
| 124 function selectItemSet(items, anchor, add) { |
| 125 return { |
| 126 name: 'select-items', |
| 127 items: items, |
| 128 anchor: anchor, |
| 129 add: add, |
| 130 }; |
| 131 } |
| 132 |
| 118 /** @return {!Action} */ | 133 /** @return {!Action} */ |
| 119 function deselectItems() { | 134 function deselectItems() { |
| 120 return { | 135 return { |
| 121 name: 'deselect-items', | 136 name: 'deselect-items', |
| 122 }; | 137 }; |
| 123 } | 138 } |
| 124 | 139 |
| 125 /** | |
| 126 * @param {string} id | |
| 127 * @param {boolean} add | |
| 128 * @param {boolean} range | |
| 129 * @param {BookmarksPageState} state | |
| 130 * @return {!Action} | |
| 131 */ | |
| 132 function selectItem(id, add, range, state) { | |
| 133 var anchor = state.selection.anchor; | |
| 134 var toSelect = []; | |
| 135 | |
| 136 // TODO(tsergeant): Make it possible to deselect items by ctrl-clicking them | |
| 137 // again. | |
| 138 if (range && anchor) { | |
| 139 var displayedList = bookmarks.util.getDisplayedList(state); | |
| 140 var selectedIndex = displayedList.indexOf(id); | |
| 141 assert(selectedIndex != -1); | |
| 142 var anchorIndex = displayedList.indexOf(anchor); | |
| 143 if (anchorIndex == -1) | |
| 144 anchorIndex = selectedIndex; | |
| 145 | |
| 146 var startIndex = Math.min(anchorIndex, selectedIndex); | |
| 147 var endIndex = Math.max(anchorIndex, selectedIndex); | |
| 148 | |
| 149 for (var i = startIndex; i <= endIndex; i++) | |
| 150 toSelect.push(displayedList[i]); | |
| 151 } else { | |
| 152 toSelect.push(id); | |
| 153 } | |
| 154 | |
| 155 return { | |
| 156 name: 'select-items', | |
| 157 add: add, | |
| 158 anchor: id, | |
| 159 items: toSelect, | |
| 160 }; | |
| 161 } | |
| 162 | |
| 163 /** | 140 /** |
| 164 * @param {string} term | 141 * @param {string} term |
| 165 * @return {!Action} | 142 * @return {!Action} |
| 166 */ | 143 */ |
| 167 function setSearchTerm(term) { | 144 function setSearchTerm(term) { |
| 168 if (!term) | 145 if (!term) |
| 169 return clearSearch(); | 146 return clearSearch(); |
| 170 | 147 |
| 171 return { | 148 return { |
| 172 name: 'start-search', | 149 name: 'start-search', |
| (...skipping 13 matching lines...) Expand all Loading... |
| 186 } | 163 } |
| 187 | 164 |
| 188 return { | 165 return { |
| 189 changeFolderOpen: changeFolderOpen, | 166 changeFolderOpen: changeFolderOpen, |
| 190 clearSearch: clearSearch, | 167 clearSearch: clearSearch, |
| 191 createBookmark: createBookmark, | 168 createBookmark: createBookmark, |
| 192 deselectItems: deselectItems, | 169 deselectItems: deselectItems, |
| 193 editBookmark: editBookmark, | 170 editBookmark: editBookmark, |
| 194 moveBookmark: moveBookmark, | 171 moveBookmark: moveBookmark, |
| 195 refreshNodes: refreshNodes, | 172 refreshNodes: refreshNodes, |
| 196 removeBookmark: removeBookmark, | 173 removeBookmarkSubtree: removeBookmarkSubtree, |
| 197 selectFolder: selectFolder, | 174 selectFolder: selectFolder, |
| 198 selectItem: selectItem, | 175 selectItemSet: selectItemSet, |
| 199 setSearchResults: setSearchResults, | 176 setSearchResults: setSearchResults, |
| 200 setSearchTerm: setSearchTerm, | 177 setSearchTerm: setSearchTerm, |
| 201 }; | 178 }; |
| 202 }); | 179 }); |
| OLD | NEW |