/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import { ControlHeader } from '@superset-ui/chart-controls'; import { css, styled, t } from '@superset-ui/core'; import { Form } from '@superset-ui/core/components'; import { Tag } from 'src/components'; import { FC, useState } from 'react'; import { isZoomConfigsLinear, isZoomConfigsExp } from './typeguards'; import { ZoomConfigs, ZoomConfigsControlProps } from './types'; import { computeConfigValues, toFixedConfig, toLinearConfig, toExpConfig, } from './zoomUtil'; import ZoomConfigsChart from './ZoomConfigsChart'; import { ControlFormItem } from '../ColumnConfigControl/ControlForm'; export const StyledControlFormItem = styled(ControlFormItem)` ${({ theme }) => css` border-radius: ${theme.borderRadius}px; `} `; export const ZoomConfigControl: FC = ({ value, onChange = () => {}, name, label, description, renderTrigger, hovered, validationErrors, }) => { const initBaseWidth = value ? value.configs.width : 0; const initBaseHeight = value ? value.configs.height : 0; const initBaseSlope = value?.configs.slope !== undefined ? value.configs.slope : 0; const initBaseExponent = value?.configs.exponent !== undefined ? value.configs.exponent : 0; const [baseWidth, setBaseWidth] = useState(initBaseWidth); const [baseHeight, setBaseHeight] = useState(initBaseHeight); const [baseSlope, setBaseSlope] = useState(initBaseSlope); const [baseExponent, setBaseExponent] = useState(initBaseExponent); const onChartChange = (newConfig: ZoomConfigs) => { onChange(newConfig); }; const onBaseWidthChange = (width: number) => { console.log('now in onbasewidthcahnge'); setBaseWidth(width); if (!value) { return; } const newValue = { ...value }; newValue.configs.width = width; newValue.values = computeConfigValues(newValue); onChange(newValue); }; const onBaseHeightChange = (height: number) => { setBaseHeight(height); if (!value) { return; } const newValue = { ...value }; newValue.configs.height = height; newValue.values = computeConfigValues(newValue); onChange(newValue); }; const onBaseSlopeChange = (slope: number) => { setBaseSlope(slope); if (value && isZoomConfigsLinear(value)) { const newValue = { ...value }; newValue.configs.slope = slope; newValue.values = computeConfigValues(newValue); onChange(newValue); } }; const onBaseExponentChange = (exponent: number) => { setBaseExponent(exponent); if (value && isZoomConfigsExp(value)) { const newValue = { ...value }; newValue.configs.exponent = exponent; newValue.values = computeConfigValues(newValue); onChange(newValue); } }; const onShapeChange = (shape: ZoomConfigs['type']) => { if (!value) return; const baseValues = { width: baseWidth, height: baseHeight, slope: baseSlope, exponent: baseExponent, zoom: value?.configs.zoom, }; switch (shape) { case 'FIXED': { const newFixedConfig = toFixedConfig(baseValues); onChange(newFixedConfig); break; } case 'LINEAR': { const newLinearConfig = toLinearConfig(baseValues); onChange(newLinearConfig); break; } case 'EXP': { const newLogConfig = toExpConfig(baseValues); onChange(newLogConfig); break; } default: break; } }; const controlHeaderProps = { name, label, description, renderTrigger, hovered, validationErrors, }; const shapeLabel = t('Shape'); const shapeDescription = t( 'Select shape for computing values. "FIXED" sets all zoom levels to the same size. "LINEAR" increases sizes linearly based on specified slope. "EXP" increases sizes exponentially based on specified exponent', ); const baseWidthLabel = t('Base width'); const baseWidthDescription = t( 'The width of the current zoom level to compute all widths from', ); const baseHeightLabel = t('Base height'); const baseHeightDescription = t( 'The height of the current zoom level to compute all heights from', ); const baseSlopeLabel = t('Base slope'); const baseSlopeDescription = t( 'The slope to compute all sizes from. "LINEAR" only', ); const baseExponentLabel = t('Base exponent'); const baseExponentDescription = t( 'The exponent to compute all sizes from. "EXP" only', ); return (
Current Zoom: {value?.configs.zoom}
); }; export default ZoomConfigControl;