tor-browser

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

audit.js (2517B)


      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  accessibility: { AUDIT_TYPE },
      9 } = require("resource://devtools/shared/constants.js");
     10 const {
     11  AUDIT,
     12  AUDITING,
     13  AUDIT_PROGRESS,
     14  FILTER_TOGGLE,
     15  FILTERS,
     16  RESET,
     17  SELECT,
     18 } = require("resource://devtools/client/accessibility/constants.js");
     19 
     20 /**
     21 * Initial state definition
     22 */
     23 function getInitialState() {
     24  return {
     25    filters: {
     26      [FILTERS.ALL]: false,
     27      [FILTERS.CONTRAST]: false,
     28      [FILTERS.KEYBOARD]: false,
     29      [FILTERS.TEXT_LABEL]: false,
     30    },
     31    auditing: [],
     32    progress: null,
     33  };
     34 }
     35 
     36 /**
     37 * State with all filters active.
     38 */
     39 function allActiveFilters() {
     40  return {
     41    [FILTERS.ALL]: true,
     42    [FILTERS.CONTRAST]: true,
     43    [FILTERS.KEYBOARD]: true,
     44    [FILTERS.TEXT_LABEL]: true,
     45  };
     46 }
     47 
     48 function audit(state = getInitialState(), action) {
     49  switch (action.type) {
     50    case FILTER_TOGGLE: {
     51      const { filter } = action;
     52      let { filters } = state;
     53      const isToggledToActive = !filters[filter];
     54 
     55      if (filter === FILTERS.NONE) {
     56        filters = getInitialState().filters;
     57      } else if (filter === FILTERS.ALL) {
     58        filters = isToggledToActive
     59          ? allActiveFilters()
     60          : getInitialState().filters;
     61      } else {
     62        filters = {
     63          ...filters,
     64          [filter]: isToggledToActive,
     65        };
     66 
     67        const allAuditTypesActive = Object.values(AUDIT_TYPE)
     68          .filter(filterKey => filters.hasOwnProperty(filterKey))
     69          .every(filterKey => filters[filterKey]);
     70        if (isToggledToActive && !filters[FILTERS.ALL] && allAuditTypesActive) {
     71          filters[FILTERS.ALL] = true;
     72        } else if (!isToggledToActive && filters[FILTERS.ALL]) {
     73          filters[FILTERS.ALL] = false;
     74        }
     75      }
     76 
     77      return {
     78        ...state,
     79        filters,
     80      };
     81    }
     82    case AUDITING: {
     83      const { auditing } = action;
     84 
     85      return {
     86        ...state,
     87        auditing,
     88      };
     89    }
     90    case AUDIT:
     91      return {
     92        ...state,
     93        auditing: getInitialState().auditing,
     94        progress: null,
     95      };
     96    case AUDIT_PROGRESS: {
     97      const { progress } = action;
     98 
     99      return {
    100        ...state,
    101        progress,
    102      };
    103    }
    104    case SELECT:
    105    case RESET:
    106      return getInitialState();
    107    default:
    108      return state;
    109  }
    110 }
    111 
    112 exports.audit = audit;