UNPKG

@mui/x-data-grid

Version:

The Community plan edition of the Data Grid components (MUI X).

121 lines (120 loc) 6.66 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useGridStrategyProcessing = exports.GRID_STRATEGIES_PROCESSORS = exports.GRID_DEFAULT_STRATEGY = void 0; var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var _toPropertyKey2 = _interopRequireDefault(require("@babel/runtime/helpers/toPropertyKey")); var React = _interopRequireWildcard(require("react")); var _useGridApiMethod = require("../../utils/useGridApiMethod"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const GRID_DEFAULT_STRATEGY = exports.GRID_DEFAULT_STRATEGY = 'none'; const GRID_STRATEGIES_PROCESSORS = exports.GRID_STRATEGIES_PROCESSORS = { rowTreeCreation: 'rowTree', filtering: 'rowTree', sorting: 'rowTree', visibleRowsLookupCreation: 'rowTree' }; /** * Implements a variant of the Strategy Pattern (see https://en.wikipedia.org/wiki/Strategy_pattern) * * More information and detailed example in (TODO add link to technical doc when ready) * * Some plugins contains custom logic that must only be applied if the right strategy is active. * For instance, the row grouping plugin has a custom filtering algorithm. * This algorithm must be applied by the filtering plugin if the row grouping is the current way of grouping rows, * but not if the tree data is the current way of grouping rows. * * ===================================================================================================================== * * The plugin containing the custom logic must use: * * - `useGridRegisterStrategyProcessor` to register their processor. * When the processor of the active strategy changes, it will fire `"activeStrategyProcessorChange"` to re-apply the processor. * * - `apiRef.current.setStrategyAvailability` to tell if their strategy can be used. * * ===================================================================================================================== * * The plugin or component that needs to apply the custom logic of the current strategy must use: * * - `apiRef.current.applyStrategyProcessor` to run the processor of the active strategy for a given processor name. * * - the "strategyAvailabilityChange" event to update something when the active strategy changes. * Warning: Be careful not to apply the processor several times. * For instance "rowsSet" is fired by `useGridRows` whenever the active strategy changes. * So listening to both would most likely run your logic twice. * * - The "activeStrategyProcessorChange" event to update something when the processor of the active strategy changes. * * ===================================================================================================================== * * Each processor name is part of a strategy group which can only have one active strategy at the time. * For now, there is only one strategy group named `rowTree` which customize * - row tree creation algorithm. * - sorting algorithm. * - filtering algorithm. */ const useGridStrategyProcessing = apiRef => { const availableStrategies = React.useRef(new Map()); const strategiesCache = React.useRef({}); const registerStrategyProcessor = React.useCallback((strategyName, processorName, processor) => { const cleanup = () => { const _ref = strategiesCache.current[processorName], otherProcessors = (0, _objectWithoutPropertiesLoose2.default)(_ref, [strategyName].map(_toPropertyKey2.default)); strategiesCache.current[processorName] = otherProcessors; }; if (!strategiesCache.current[processorName]) { strategiesCache.current[processorName] = {}; } const groupPreProcessors = strategiesCache.current[processorName]; const previousProcessor = groupPreProcessors[strategyName]; groupPreProcessors[strategyName] = processor; if (!previousProcessor || previousProcessor === processor) { return cleanup; } if (strategyName === apiRef.current.getActiveStrategy(GRID_STRATEGIES_PROCESSORS[processorName])) { apiRef.current.publishEvent('activeStrategyProcessorChange', processorName); } return cleanup; }, [apiRef]); const applyStrategyProcessor = React.useCallback((processorName, params) => { const activeStrategy = apiRef.current.getActiveStrategy(GRID_STRATEGIES_PROCESSORS[processorName]); if (activeStrategy == null) { throw new Error("Can't apply a strategy processor before defining an active strategy"); } const groupCache = strategiesCache.current[processorName]; if (!groupCache || !groupCache[activeStrategy]) { throw new Error(`No processor found for processor "${processorName}" on strategy "${activeStrategy}"`); } const processor = groupCache[activeStrategy]; return processor(params); }, [apiRef]); const getActiveStrategy = React.useCallback(strategyGroup => { const strategyEntries = Array.from(availableStrategies.current.entries()); const availableStrategyEntry = strategyEntries.find(([, strategy]) => { if (strategy.group !== strategyGroup) { return false; } return strategy.isAvailable(); }); return availableStrategyEntry?.[0] ?? GRID_DEFAULT_STRATEGY; }, []); const setStrategyAvailability = React.useCallback((strategyGroup, strategyName, isAvailable) => { availableStrategies.current.set(strategyName, { group: strategyGroup, isAvailable }); apiRef.current.publishEvent('strategyAvailabilityChange'); }, [apiRef]); const strategyProcessingApi = { registerStrategyProcessor, applyStrategyProcessor, getActiveStrategy, setStrategyAvailability }; (0, _useGridApiMethod.useGridApiMethod)(apiRef, strategyProcessingApi, 'private'); }; exports.useGridStrategyProcessing = useGridStrategyProcessing;