1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// 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.
import {assert} from 'chrome://resources/js/assert.m.js';
import {Action} from 'chrome://resources/js/cr/ui/store.m.js';
import {IncognitoAvailability, ROOT_NODE_ID} from './constants.js';
import {BookmarksPageState, NodeMap} from './types.js';
import {getDescendants, getDisplayedList, normalizeNode} from './util.js';
/**
* @fileoverview Module for functions which produce action objects. These are
* listed in one place to document available actions and their parameters.
*/
/**
* @param {string} id
* @param {chrome.bookmarks.BookmarkTreeNode} treeNode
*/
export function createBookmark(id, treeNode) {
return {
name: 'create-bookmark',
id: id,
parentId: treeNode.parentId,
parentIndex: treeNode.index,
node: normalizeNode(treeNode),
};
}
/**
* @param {string} id
* @param {{title: string, url: (string|undefined)}} changeInfo
* @return {!Action}
*/
export function editBookmark(id, changeInfo) {
return {
name: 'edit-bookmark',
id: id,
changeInfo: changeInfo,
};
}
/**
* @param {string} id
* @param {string} parentId
* @param {number} index
* @param {string} oldParentId
* @param {number} oldIndex
* @return {!Action}
*/
export function moveBookmark(id, parentId, index, oldParentId, oldIndex) {
return {
name: 'move-bookmark',
id: id,
parentId: parentId,
index: index,
oldParentId: oldParentId,
oldIndex: oldIndex,
};
}
/**
* @param {string} id
* @param {!Array<string>} newChildIds
*/
export function reorderChildren(id, newChildIds) {
return {
name: 'reorder-children',
id: id,
children: newChildIds,
};
}
/**
* @param {string} id
* @param {string} parentId
* @param {number} index
* @param {NodeMap} nodes
* @return {!Action}
*/
export function removeBookmark(id, parentId, index, nodes) {
const descendants = getDescendants(nodes, id);
return {
name: 'remove-bookmark',
id: id,
descendants: descendants,
parentId: parentId,
index: index,
};
}
/**
* @param {NodeMap} nodeMap
* @return {!Action}
*/
export function refreshNodes(nodeMap) {
return {
name: 'refresh-nodes',
nodes: nodeMap,
};
}
/**
* @param {string} id
* @param {NodeMap} nodes Current node state. Can be omitted in tests.
* @return {?Action}
*/
export function selectFolder(id, nodes) {
if (nodes && (id === ROOT_NODE_ID || !nodes[id] || nodes[id].url)) {
console.warn('Tried to select invalid folder: ' + id);
return null;
}
return {
name: 'select-folder',
id: id,
};
}
/**
* @param {string} id
* @param {boolean} open
* @return {!Action}
*/
export function changeFolderOpen(id, open) {
return {
name: 'change-folder-open',
id: id,
open: open,
};
}
/** @return {!Action} */
export function clearSearch() {
return {
name: 'clear-search',
};
}
/** @return {!Action} */
export function deselectItems() {
return {
name: 'deselect-items',
};
}
/**
* @param {string} id
* @param {BookmarksPageState} state
* @param {{
* clear: boolean,
* range: boolean,
* toggle: boolean}} config Options for how the selection should change:
* - clear: If true, clears the previous selection before adding this one
* - range: If true, selects all items from the anchor to this item
* - toggle: If true, toggles the selection state of the item. Cannot be
* used with clear or range.
* @return {!Action}
*/
export function selectItem(id, state, config) {
assert(!config.toggle || !config.range);
assert(!config.toggle || !config.clear);
const anchor = state.selection.anchor;
const toSelect = [];
let newAnchor = id;
if (config.range && anchor) {
const displayedList = getDisplayedList(state);
const selectedIndex = displayedList.indexOf(id);
assert(selectedIndex !== -1);
let anchorIndex = displayedList.indexOf(anchor);
if (anchorIndex === -1) {
anchorIndex = selectedIndex;
}
// When performing a range selection, don't change the anchor from what
// was used in this selection.
newAnchor = displayedList[anchorIndex];
const startIndex = Math.min(anchorIndex, selectedIndex);
const endIndex = Math.max(anchorIndex, selectedIndex);
for (let i = startIndex; i <= endIndex; i++) {
toSelect.push(displayedList[i]);
}
} else {
toSelect.push(id);
}
return {
name: 'select-items',
clear: config.clear,
toggle: config.toggle,
anchor: newAnchor,
items: toSelect,
};
}
/**
* @param {Array<string>} ids
* @param {BookmarksPageState} state
* @param {string=} anchor
* @return {!Action}
*/
export function selectAll(ids, state, anchor) {
return {
name: 'select-items',
clear: true,
toggle: false,
anchor: anchor ? anchor : state.selection.anchor,
items: ids,
};
}
/**
* @param {string} id
* @return {!Action}
*/
export function updateAnchor(id) {
return {
name: 'update-anchor',
anchor: id,
};
}
/**
* @param {string} term
* @return {!Action}
*/
export function setSearchTerm(term) {
if (!term) {
return clearSearch();
}
return {
name: 'start-search',
term: term,
};
}
/**
* @param {!Array<string>} ids
* @return {!Action}
*/
export function setSearchResults(ids) {
return {
name: 'finish-search',
results: ids,
};
}
/**
* @param {IncognitoAvailability} availability
* @return {!Action}
*/
export function setIncognitoAvailability(availability) {
assert(availability !== IncognitoAvailability.FORCED);
return {
name: 'set-incognito-availability',
value: availability,
};
}
/**
* @param {boolean} canEdit
* @return {!Action}
*/
export function setCanEditBookmarks(canEdit) {
return {
name: 'set-can-edit',
value: canEdit,
};
}
|