| Index: chrome/browser/resources/md_bookmarks/complex_actions.js
|
| diff --git a/chrome/browser/resources/md_bookmarks/complex_actions.js b/chrome/browser/resources/md_bookmarks/complex_actions.js
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..e636cdd3be47b718f005fd7e52e88c4f9bb3a23d
|
| --- /dev/null
|
| +++ b/chrome/browser/resources/md_bookmarks/complex_actions.js
|
| @@ -0,0 +1,68 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +/**
|
| + * @fileoverview Module for functions which produce Action objects based on
|
| + * complex logic and/or relying on the page state.
|
| + *
|
| + * All functions in this file should have a counterpart in actions.js
|
| + * which does not depend on State (removeBookmark vs removeBookmarkSubtree).
|
| + * Functions in complex_actions.js should be unit tested, while functions in
|
| + * actions.js are suitable for use in other test files (eg, reducers_test.js).
|
| + */
|
| +
|
| +cr.define('bookmarks.actions', function() {
|
| + /** @return {BookmarksPageState} */
|
| + function getState() {
|
| + return bookmarks.Store.getInstance().data;
|
| + }
|
| +
|
| + /**
|
| + * @param {string} id
|
| + * @param {string} parentId
|
| + * @param {number} index
|
| + */
|
| + function removeBookmark(id, parentId, index) {
|
| + var descendants = bookmarks.util.getDescendants(getState().nodes, id);
|
| + return bookmarks.actions.removeBookmarkSubtree(
|
| + id, parentId, index, descendants);
|
| + }
|
| +
|
| + /**
|
| + * @param {string} id
|
| + * @param {boolean} add
|
| + * @param {boolean} range
|
| + * @return {!Action}
|
| + */
|
| + function selectItem(id, add, range) {
|
| + var anchor = getState().selection.anchor;
|
| + var toSelect = [];
|
| +
|
| + // TODO(tsergeant): Make it possible to deselect items by ctrl-clicking them
|
| + // again.
|
| + if (range && anchor) {
|
| + var displayedList = bookmarks.util.getDisplayedList(getState());
|
| + var selectedIndex = displayedList.indexOf(id);
|
| + assert(selectedIndex != -1);
|
| + var anchorIndex = displayedList.indexOf(anchor);
|
| + if (anchorIndex == -1)
|
| + anchorIndex = selectedIndex;
|
| +
|
| + var startIndex = Math.min(anchorIndex, selectedIndex);
|
| + var endIndex = Math.max(anchorIndex, selectedIndex);
|
| +
|
| + for (var i = startIndex; i <= endIndex; i++)
|
| + toSelect.push(displayedList[i]);
|
| + } else {
|
| + toSelect.push(id);
|
| + }
|
| +
|
| + return bookmarks.actions.selectItemSet(toSelect, id, add);
|
| + }
|
| +
|
| + return {
|
| + removeBookmark: removeBookmark,
|
| + selectItem: selectItem,
|
| + };
|
| +});
|
|
|