selection.js (2060B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 SELECT_REQUEST, 9 } = require("resource://devtools/client/netmonitor/src/constants.js"); 10 const { 11 getDisplayedRequests, 12 getSortedRequests, 13 } = require("resource://devtools/client/netmonitor/src/selectors/index.js"); 14 15 const PAGE_SIZE_ITEM_COUNT_RATIO = 5; 16 17 /** 18 * Select request with a given id. 19 */ 20 function selectRequest(id, request) { 21 return { 22 type: SELECT_REQUEST, 23 id, 24 request, 25 }; 26 } 27 28 /** 29 * Select request with a given index (sorted order) 30 */ 31 function selectRequestByIndex(index) { 32 return ({ dispatch, getState }) => { 33 const requests = getSortedRequests(getState()); 34 let itemId; 35 if (index >= 0 && index < requests.length) { 36 itemId = requests[index].id; 37 } 38 dispatch(selectRequest(itemId)); 39 }; 40 } 41 42 /** 43 * Move the selection up to down according to the "delta" parameter. Possible values: 44 * - Number: positive or negative, move up or down by specified distance 45 * - "PAGE_UP" | "PAGE_DOWN" (String): page up or page down 46 * - +Infinity | -Infinity: move to the start or end of the list 47 */ 48 function selectDelta(delta) { 49 return ({ dispatch, getState }) => { 50 const state = getState(); 51 const requests = getDisplayedRequests(state); 52 53 if (!requests.length) { 54 return; 55 } 56 57 const selIndex = requests.findIndex( 58 r => r.id === state.requests.selectedId 59 ); 60 61 if (delta === "PAGE_DOWN") { 62 delta = Math.ceil(requests.length / PAGE_SIZE_ITEM_COUNT_RATIO); 63 } else if (delta === "PAGE_UP") { 64 delta = -Math.ceil(requests.length / PAGE_SIZE_ITEM_COUNT_RATIO); 65 } 66 67 const newIndex = Math.min( 68 Math.max(0, selIndex + delta), 69 requests.length - 1 70 ); 71 const newItem = requests[newIndex]; 72 dispatch(selectRequest(newItem.id, newItem)); 73 }; 74 } 75 76 module.exports = { 77 selectRequest, 78 selectRequestByIndex, 79 selectDelta, 80 };