UNPKG

@adaptabletools/adaptable

Version:

Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

132 lines (131 loc) 6.5 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import * as React from 'react'; import HelpBlock from '../../../components/HelpBlock'; import { ALL_MODULES } from '../../../AdaptableState/Common/Types'; import Radio from '../../../components/Radio'; import FormLayout, { FormRow } from '../../../components/FormLayout'; import { ACCESS_LEVEL_FULL, ACCESS_LEVEL_HIDDEN, ACCESS_LEVEL_READ_ONLY, } from '../../../Utilities/Constants/GeneralConstants'; import { ADAPTABLE_MODULE_MAP } from '../../../Utilities/Constants/ModuleConstants'; import { DataSource, InfiniteTableGrid, } from '../../../components/InfiniteTable'; import { Box, Flex } from '../../../components/Flex'; const ALL_ENTITLEMENTS_MODULES = ALL_MODULES; const tableDOMProps = { style: { height: '100%', minWidth: '10rem', minHeight: 600, }, }; const columnTypes = { default: { defaultFlex: 1, align: 'center', defaultSortable: false, }, }; const headerOptions = { alwaysReserveSpaceForSortIcon: false, }; const EntitlementsListForm = (props) => { let abOptions = props.adaptableOptions; const entitlements = abOptions?.entitlementOptions?.moduleEntitlements ?? []; if (typeof entitlements === 'function') { return (_jsx(HelpBlock, { className: "twa:mt-2", children: "Entitlements cannot be customized, they are handled by a custom function." })); } const entitlementsMap = React.useMemo(() => { return entitlements.reduce((acc, entitlement) => { acc[entitlement.adaptableModule] = entitlement.accessLevel; return acc; }, {}); }, [abOptions?.entitlementOptions]); const handleOnChange = React.useCallback((module, accessLevel) => { let newEntitlements = entitlements; if (entitlementsMap[module]) { newEntitlements = newEntitlements.map((entitlement) => { if (entitlement.adaptableModule === module) { entitlement.accessLevel = accessLevel; } return entitlement; }); } else { newEntitlements = [ ...newEntitlements, { adaptableModule: module, accessLevel: accessLevel }, ]; } props.onChangedAptableOptions({ ...abOptions, entitlementOptions: { ...abOptions.entitlementOptions, moduleEntitlements: newEntitlements, }, }); }, [abOptions?.entitlementOptions]); if (typeof entitlements === 'function') { return (_jsx(HelpBlock, { className: "twa:mb-2", children: "Entitlements cannot be customized, they are handled by a custom function." })); } const columnsMap = { name: { header: 'Module Name', align: 'start', renderValue: (params) => { return _jsx(_Fragment, { children: params.data.name }); }, }, full: { header: 'Full', render: (params) => { const module = params.data.name; const accessLevel = entitlementsMap[module]; return (_jsx(Radio, { onClick: () => handleOnChange(module, ACCESS_LEVEL_FULL), checked: accessLevel === ACCESS_LEVEL_FULL })); }, }, readonly: { header: 'Read Only', render: (params) => { const module = params.data.name; const accessLevel = entitlementsMap[module]; return (_jsx(Radio, { onClick: () => handleOnChange(module, ACCESS_LEVEL_READ_ONLY), checked: accessLevel === ACCESS_LEVEL_READ_ONLY })); }, }, hidden: { header: 'Hidden', render: (params) => { const module = params.data.name; const accessLevel = entitlementsMap[module]; return (_jsx(Radio, { onClick: () => handleOnChange(module, ACCESS_LEVEL_HIDDEN), checked: accessLevel === ACCESS_LEVEL_HIDDEN })); }, }, }; const data = ALL_ENTITLEMENTS_MODULES.map((module) => ({ name: ADAPTABLE_MODULE_MAP[module] })); let theme = 'light'; if (typeof props.adaptableOptions.initialState === 'object' && props.adaptableOptions.initialState.Theme?.CurrentTheme) { theme = props.adaptableOptions.initialState.Theme.CurrentTheme; } return (_jsx(Box, { className: `${theme} twa:flex-1 twa:h-full`, children: _jsx(DataSource, { data: data, primaryKey: "name", children: _jsx(InfiniteTableGrid, { columnTypes: columnTypes, headerOptions: headerOptions, domProps: tableDOMProps, columns: columnsMap }) }) })); }; const DefaultEntitlementForm = (props) => { const abOptions = props.adaptableOptions; const entitlementOptions = props.adaptableOptions?.entitlementOptions; const defaultEntitlement = entitlementOptions?.defaultAccessLevel; if (typeof defaultEntitlement === 'function') { return (_jsx(HelpBlock, { className: "twa:mb-2", children: "Default Entitlement is controlled by a custom function" })); } const handleDefaultEntitlementChange = React.useCallback((accessLevel) => { props.onChangedAptableOptions({ ...abOptions, entitlementOptions: { ...abOptions.entitlementOptions, defaultAccessLevel: accessLevel, }, }); }, [abOptions?.entitlementOptions?.defaultAccessLevel]); return (_jsx(FormLayout, { className: "twa:mb-2", children: _jsxs(FormRow, { label: "Default Entitlement:", children: [_jsx(Radio, { onClick: () => handleDefaultEntitlementChange(ACCESS_LEVEL_FULL), checked: defaultEntitlement === ACCESS_LEVEL_FULL, className: "twa:mr-3", children: "Visible" }), _jsx(Radio, { onClick: () => handleDefaultEntitlementChange(ACCESS_LEVEL_READ_ONLY), checked: defaultEntitlement === ACCESS_LEVEL_READ_ONLY, className: "twa:mr-3", children: "Read Only" }), _jsx(Radio, { onClick: () => handleDefaultEntitlementChange(ACCESS_LEVEL_HIDDEN), checked: defaultEntitlement === ACCESS_LEVEL_HIDDEN, children: "Hidden" })] }) })); }; const EntitlementsForm = (props) => { let abOptions = props.adaptableOptions; return (_jsxs(Flex, { flexDirection: "column", className: "twa:p-2 twa:h-full", children: [_jsx(DefaultEntitlementForm, { ...props }), _jsx(EntitlementsListForm, { ...props })] })); }; export default EntitlementsForm;