summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/actions/selection.js
blob: 4603fcb61cf75831cf8fb4280988001bcb5080c4 (plain)
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
/* 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,
};