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

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

Issue 2804503002: MD Bookmarks: Allow 'complex' actions to access the page state directly (Closed)
Patch Set: Self-review 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
(Empty)
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
3 // found in the LICENSE file.
4
5 /**
6 * @fileoverview Module for functions which produce Action objects based on
7 * complex logic and/or relying on the page state.
8 *
9 * All functions in this file should have a counterpart in actions.js
10 * which does not depend on State (removeBookmark vs removeBookmarkSubtree).
11 * Functions in complex_actions.js should be unit tested, while functions in
12 * actions.js are suitable for use in other test files (eg, reducers_test.js).
13 */
14
15 cr.define('bookmarks.actions', function() {
16 /** @return {BookmarksPageState} */
17 function getState() {
18 return bookmarks.Store.getInstance().data;
19 }
20
21 /**
22 * @param {string} id
23 * @param {string} parentId
24 * @param {number} index
25 */
26 function removeBookmark(id, parentId, index) {
27 var descendants = bookmarks.util.getDescendants(getState().nodes, id);
28 return bookmarks.actions.removeBookmarkSubtree(
29 id, parentId, index, descendants);
30 }
31
32 /**
33 * @param {string} id
34 * @param {boolean} add
35 * @param {boolean} range
36 * @return {!Action}
37 */
38 function selectItem(id, add, range) {
39 var anchor = getState().selection.anchor;
40 var toSelect = [];
41
42 // TODO(tsergeant): Make it possible to deselect items by ctrl-clicking them
43 // again.
44 if (range && anchor) {
45 var displayedList = bookmarks.util.getDisplayedList(getState());
46 var selectedIndex = displayedList.indexOf(id);
47 assert(selectedIndex != -1);
48 var anchorIndex = displayedList.indexOf(anchor);
49 if (anchorIndex == -1)
50 anchorIndex = selectedIndex;
51
52 var startIndex = Math.min(anchorIndex, selectedIndex);
53 var endIndex = Math.max(anchorIndex, selectedIndex);
54
55 for (var i = startIndex; i <= endIndex; i++)
56 toSelect.push(displayedList[i]);
57 } else {
58 toSelect.push(id);
59 }
60
61 return bookmarks.actions.selectItemSet(toSelect, id, add);
62 }
63
64 return {
65 removeBookmark: removeBookmark,
66 selectItem: selectItem,
67 };
68 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698