import { runInAction } from "mobx"; import { observer } from "mobx-react"; import { FC } from "react"; import { useTranslation } from "react-i18next"; import { useTheme } from "styled-components"; import ChartView from "../../../Charts/ChartView"; import ChartableMixin, { axesMatch, ChartAxis } from "../../../ModelMixins/ChartableMixin"; import { BaseModel } from "../../../Models/Definition/Model"; import Checkbox from "../../../Styled/Checkbox/Checkbox"; import { Li, Ul } from "../../../Styled/List"; import { TextSpan } from "../../../Styled/Text"; interface IChartItem { chartItem: any; } interface IChartItemSelector { item: BaseModel; } export const ChartItem: FC = observer( ({ chartItem }: IChartItem) => { const { t } = useTranslation(); const lineColor = chartItem.isSelectedInWorkbench ? chartItem.getColor() : "#fff"; const toggleActive = () => { const catalogItem = chartItem.item; runInAction(() => { const shouldSelect = !chartItem.isSelectedInWorkbench; chartItem.updateIsSelectedInWorkbench(shouldSelect); if (shouldSelect) { unselectChartItemsWithXAxisNotMatching( catalogItem.terria.workbench.items, chartItem.xAxis ); } }); }; return ( {chartItem.name} ); } ); const ChartItemSelector: FC = observer( ({ item }: IChartItemSelector) => { const theme = useTheme(); const chartView = new ChartView(item.terria); // We don't need to show selectors for moment datasets. They are part of // discretelytimevarying items and have a separate chart button to enable/disable. const chartItems = chartView.chartItems .filter((c) => c.item === item) .filter((c) => c.type !== "momentPoints" && c.type !== "momentLines") .sort((a, b) => (a.name >= b.name ? 1 : -1)); if (chartItems && chartItems.length === 0) return null; return ( ); } ); function unselectChartItemsWithXAxisNotMatching( items: BaseModel[], requiredAxis: ChartAxis ) { items.forEach((item) => { if (ChartableMixin.isMixedInto(item)) { item.chartItems.forEach((chartItem) => { if (!axesMatch(chartItem.xAxis, requiredAxis)) { chartItem.updateIsSelectedInWorkbench(false); } }); } }); } export default ChartItemSelector;