summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/selectors/history.js
blob: 531db8b8a87fb7b9fa05be2ff1c683dc138d038f (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* 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 {
  HISTORY_BACK,
  HISTORY_FORWARD,
} = require("resource://devtools/client/webconsole/constants.js");

function getHistory(state) {
  return state.history;
}

function getHistoryEntries(state) {
  return state.history.entries;
}

function getHistoryValue(state, direction) {
  if (direction == HISTORY_BACK) {
    return getPreviousHistoryValue(state);
  }
  if (direction == HISTORY_FORWARD) {
    return getNextHistoryValue(state);
  }
  return null;
}

function getNextHistoryValue(state) {
  if (state.history.position < state.history.entries.length - 1) {
    return state.history.entries[state.history.position + 1];
  }

  // The user didn't pick up anything from the history and returned
  // back to the previous value (if any) that was in the input box.
  return state.history.originalUserValue;
}

function getPreviousHistoryValue(state) {
  if (state.history.position > 0) {
    return state.history.entries[state.history.position - 1];
  }
  return null;
}

function getReverseSearchResult(state) {
  const { history } = state;
  const { currentReverseSearchResults, currentReverseSearchResultsPosition } =
    history;

  if (
    !Array.isArray(currentReverseSearchResults) ||
    currentReverseSearchResults.length === 0 ||
    !Number.isInteger(currentReverseSearchResultsPosition)
  ) {
    return null;
  }
  return currentReverseSearchResults[currentReverseSearchResultsPosition];
}

function getReverseSearchResultPosition(state) {
  const { history } = state;
  const { currentReverseSearchResultsPosition } = history;
  if (!Number.isInteger(currentReverseSearchResultsPosition)) {
    return currentReverseSearchResultsPosition;
  }

  return currentReverseSearchResultsPosition + 1;
}

function getReverseSearchTotalResults(state) {
  const { history } = state;
  const { currentReverseSearchResults } = history;
  if (!currentReverseSearchResults) {
    return null;
  }

  return currentReverseSearchResults.length;
}

function getTerminalEagerResult(state) {
  const { history } = state;
  return history.terminalEagerResult;
}

module.exports = {
  getHistory,
  getHistoryEntries,
  getHistoryValue,
  getReverseSearchResult,
  getReverseSearchResultPosition,
  getReverseSearchTotalResults,
  getTerminalEagerResult,
};