summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/actions/selection.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/client/netmonitor/src/actions/selection.js
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/netmonitor/src/actions/selection.js')
-rw-r--r--devtools/client/netmonitor/src/actions/selection.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/devtools/client/netmonitor/src/actions/selection.js b/devtools/client/netmonitor/src/actions/selection.js
new file mode 100644
index 0000000000..4603fcb61c
--- /dev/null
+++ b/devtools/client/netmonitor/src/actions/selection.js
@@ -0,0 +1,80 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const {
+ SELECT_REQUEST,
+} = require("resource://devtools/client/netmonitor/src/constants.js");
+const {
+ getDisplayedRequests,
+ getSortedRequests,
+} = require("resource://devtools/client/netmonitor/src/selectors/index.js");
+
+const PAGE_SIZE_ITEM_COUNT_RATIO = 5;
+
+/**
+ * Select request with a given id.
+ */
+function selectRequest(id, request) {
+ return {
+ type: SELECT_REQUEST,
+ id,
+ request,
+ };
+}
+
+/**
+ * Select request with a given index (sorted order)
+ */
+function selectRequestByIndex(index) {
+ return ({ dispatch, getState }) => {
+ const requests = getSortedRequests(getState());
+ let itemId;
+ if (index >= 0 && index < requests.length) {
+ itemId = requests[index].id;
+ }
+ dispatch(selectRequest(itemId));
+ };
+}
+
+/**
+ * Move the selection up to down according to the "delta" parameter. Possible values:
+ * - Number: positive or negative, move up or down by specified distance
+ * - "PAGE_UP" | "PAGE_DOWN" (String): page up or page down
+ * - +Infinity | -Infinity: move to the start or end of the list
+ */
+function selectDelta(delta) {
+ return ({ dispatch, getState }) => {
+ const state = getState();
+ const requests = getDisplayedRequests(state);
+
+ if (!requests.length) {
+ return;
+ }
+
+ const selIndex = requests.findIndex(
+ r => r.id === state.requests.selectedId
+ );
+
+ if (delta === "PAGE_DOWN") {
+ delta = Math.ceil(requests.length / PAGE_SIZE_ITEM_COUNT_RATIO);
+ } else if (delta === "PAGE_UP") {
+ delta = -Math.ceil(requests.length / PAGE_SIZE_ITEM_COUNT_RATIO);
+ }
+
+ const newIndex = Math.min(
+ Math.max(0, selIndex + delta),
+ requests.length - 1
+ );
+ const newItem = requests[newIndex];
+ dispatch(selectRequest(newItem.id, newItem));
+ };
+}
+
+module.exports = {
+ selectRequest,
+ selectRequestByIndex,
+ selectDelta,
+};