tor-browser

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

search.js (2979B)


      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  ADD_SEARCH_QUERY,
      9  ADD_SEARCH_RESULT,
     10  CLEAR_SEARCH_RESULTS,
     11  ADD_ONGOING_SEARCH,
     12  SEARCH_STATUS,
     13  TOGGLE_SEARCH_CASE_SENSITIVE_SEARCH,
     14  UPDATE_SEARCH_STATUS,
     15  SET_TARGET_SEARCH_RESULT,
     16 } = require("resource://devtools/client/netmonitor/src/constants.js");
     17 
     18 /**
     19 * Search reducer stores the following data:
     20 * - query [String]: the string the user is looking for
     21 * - results [Object]: the list of search results
     22 * - ongoingSearch [Object]: the object representing the current search
     23 * - status [String]: status of the current search (see constants.js)
     24 */
     25 function Search(overrideParams = {}) {
     26  return Object.assign(
     27    {
     28      query: "",
     29      results: [],
     30      ongoingSearch: null,
     31      status: SEARCH_STATUS.INITIAL,
     32      caseSensitive: false,
     33      targetSearchResult: null,
     34    },
     35    overrideParams
     36  );
     37 }
     38 
     39 function search(state = new Search(), action) {
     40  switch (action.type) {
     41    case ADD_SEARCH_QUERY:
     42      return onAddSearchQuery(state, action);
     43    case ADD_SEARCH_RESULT:
     44      return onAddSearchResult(state, action);
     45    case CLEAR_SEARCH_RESULTS:
     46      return onClearSearchResults(state);
     47    case ADD_ONGOING_SEARCH:
     48      return onAddOngoingSearch(state, action);
     49    case TOGGLE_SEARCH_CASE_SENSITIVE_SEARCH:
     50      return onToggleCaseSensitiveSearch(state);
     51    case UPDATE_SEARCH_STATUS:
     52      return onUpdateSearchStatus(state, action);
     53    case SET_TARGET_SEARCH_RESULT:
     54      return onSetTargetSearchResult(state, action);
     55  }
     56  return state;
     57 }
     58 
     59 function onAddSearchQuery(state, action) {
     60  if (state.query == action.query) {
     61    return state;
     62  }
     63  return {
     64    ...state,
     65    query: action.query,
     66  };
     67 }
     68 
     69 function onAddSearchResult(state, action) {
     70  const { resource } = action;
     71  const results = state.results.slice();
     72  results.push({
     73    resource,
     74    results: action.result,
     75  });
     76 
     77  return {
     78    ...state,
     79    results,
     80  };
     81 }
     82 
     83 function onClearSearchResults(state) {
     84  if (!state.results.length) {
     85    return state;
     86  }
     87  return {
     88    ...state,
     89    results: [],
     90  };
     91 }
     92 
     93 function onAddOngoingSearch(state, action) {
     94  if (state.ongoingSearch == action.ongoingSearch) {
     95    return state;
     96  }
     97  return {
     98    ...state,
     99    ongoingSearch: action.ongoingSearch,
    100  };
    101 }
    102 
    103 function onToggleCaseSensitiveSearch(state) {
    104  return {
    105    ...state,
    106    caseSensitive: !state.caseSensitive,
    107  };
    108 }
    109 
    110 function onUpdateSearchStatus(state, action) {
    111  if (state.status == action.status) {
    112    return state;
    113  }
    114  return {
    115    ...state,
    116    status: action.status,
    117  };
    118 }
    119 
    120 function onSetTargetSearchResult(state, action) {
    121  if (state.targetSearchResult == action.searchResult) {
    122    return state;
    123  }
    124  return {
    125    ...state,
    126    targetSearchResult: action.searchResult,
    127  };
    128 }
    129 
    130 module.exports = {
    131  Search,
    132  search,
    133 };