tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

history.js (2480B)


      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  HISTORY_BACK,
      9  HISTORY_FORWARD,
     10 } = require("resource://devtools/client/webconsole/constants.js");
     11 
     12 function getHistory(state) {
     13  return state.history;
     14 }
     15 
     16 function getHistoryEntries(state) {
     17  return state.history.entries;
     18 }
     19 
     20 function getHistoryValue(state, direction) {
     21  if (direction == HISTORY_BACK) {
     22    return getPreviousHistoryValue(state);
     23  }
     24  if (direction == HISTORY_FORWARD) {
     25    return getNextHistoryValue(state);
     26  }
     27  return null;
     28 }
     29 
     30 function getNextHistoryValue(state) {
     31  if (state.history.position < state.history.entries.length - 1) {
     32    return state.history.entries[state.history.position + 1];
     33  }
     34 
     35  // The user didn't pick up anything from the history and returned
     36  // back to the previous value (if any) that was in the input box.
     37  return state.history.originalUserValue;
     38 }
     39 
     40 function getPreviousHistoryValue(state) {
     41  if (state.history.position > 0) {
     42    return state.history.entries[state.history.position - 1];
     43  }
     44  return null;
     45 }
     46 
     47 function getReverseSearchResult(state) {
     48  const { history } = state;
     49  const { currentReverseSearchResults, currentReverseSearchResultsPosition } =
     50    history;
     51 
     52  if (
     53    !Array.isArray(currentReverseSearchResults) ||
     54    currentReverseSearchResults.length === 0 ||
     55    !Number.isInteger(currentReverseSearchResultsPosition)
     56  ) {
     57    return null;
     58  }
     59  return currentReverseSearchResults[currentReverseSearchResultsPosition];
     60 }
     61 
     62 function getReverseSearchResultPosition(state) {
     63  const { history } = state;
     64  const { currentReverseSearchResultsPosition } = history;
     65  if (!Number.isInteger(currentReverseSearchResultsPosition)) {
     66    return currentReverseSearchResultsPosition;
     67  }
     68 
     69  return currentReverseSearchResultsPosition + 1;
     70 }
     71 
     72 function getReverseSearchTotalResults(state) {
     73  const { history } = state;
     74  const { currentReverseSearchResults } = history;
     75  if (!currentReverseSearchResults) {
     76    return null;
     77  }
     78 
     79  return currentReverseSearchResults.length;
     80 }
     81 
     82 function getTerminalEagerResult(state) {
     83  const { history } = state;
     84  return history.terminalEagerResult;
     85 }
     86 
     87 module.exports = {
     88  getHistory,
     89  getHistoryEntries,
     90  getHistoryValue,
     91  getReverseSearchResult,
     92  getReverseSearchResultPosition,
     93  getReverseSearchTotalResults,
     94  getTerminalEagerResult,
     95 };