2020-04-15 11:42:11 +00:00
|
|
|
import React from "react";
|
2021-04-26 10:35:59 +00:00
|
|
|
import { get, has, isString } from "lodash";
|
2020-04-15 11:42:11 +00:00
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
2020-06-10 12:16:50 +00:00
|
|
|
import { ControlWrapper, StyledPropertyPaneButton } from "./StyledControls";
|
2020-04-15 11:42:11 +00:00
|
|
|
import styled from "constants/DefaultTheme";
|
|
|
|
|
import { FormIcons } from "icons/FormIcons";
|
|
|
|
|
import { AnyStyledComponent } from "styled-components";
|
2020-07-01 10:01:07 +00:00
|
|
|
import CodeEditor from "components/editorComponents/CodeEditor";
|
|
|
|
|
import {
|
|
|
|
|
EditorModes,
|
|
|
|
|
EditorSize,
|
|
|
|
|
EditorTheme,
|
|
|
|
|
TabBehaviour,
|
|
|
|
|
} from "components/editorComponents/CodeEditor/EditorConfig";
|
2021-03-15 12:17:56 +00:00
|
|
|
import { Size, Category } from "components/ads/Button";
|
2021-04-26 10:35:59 +00:00
|
|
|
import { AllChartData, ChartData } from "widgets/ChartWidget";
|
|
|
|
|
import { generateReactKey } from "utils/generators";
|
2021-03-15 12:17:56 +00:00
|
|
|
|
|
|
|
|
const Wrapper = styled.div`
|
|
|
|
|
background-color: ${(props) =>
|
|
|
|
|
props.theme.colors.propertyPane.dropdownSelectBg};
|
|
|
|
|
padding: 0 8px;
|
|
|
|
|
`;
|
2020-04-15 11:42:11 +00:00
|
|
|
|
|
|
|
|
const StyledOptionControlWrapper = styled(ControlWrapper)`
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-start;
|
|
|
|
|
padding: 0;
|
|
|
|
|
width: 100%;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledDynamicInput = styled.div`
|
|
|
|
|
width: 100%;
|
|
|
|
|
&&& {
|
|
|
|
|
input {
|
|
|
|
|
border: none;
|
2020-12-24 04:32:25 +00:00
|
|
|
color: ${(props) => props.theme.colors.textOnDarkBG};
|
|
|
|
|
background: ${(props) => props.theme.colors.paneInputBG};
|
2020-04-15 11:42:11 +00:00
|
|
|
&:focus {
|
|
|
|
|
border: none;
|
2020-12-24 04:32:25 +00:00
|
|
|
color: ${(props) => props.theme.colors.textOnDarkBG};
|
|
|
|
|
background: ${(props) => props.theme.colors.paneInputBG};
|
2020-04-15 11:42:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledDeleteIcon = styled(FormIcons.DELETE_ICON as AnyStyledComponent)`
|
|
|
|
|
padding: 0;
|
|
|
|
|
position: relative;
|
|
|
|
|
margin-left: 15px;
|
|
|
|
|
cursor: pointer;
|
2021-03-15 12:17:56 +00:00
|
|
|
|
|
|
|
|
&&& svg {
|
|
|
|
|
path {
|
|
|
|
|
fill: ${(props) => props.theme.colors.propertyPane.jsIconBg};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ActionHolder = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
width: 100%;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledLabel = styled.label`
|
|
|
|
|
margin: 8px auto 8px 0;
|
|
|
|
|
|
|
|
|
|
&& {
|
|
|
|
|
color: ${(props) => props.theme.colors.propertyPane.label};
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const Box = styled.div`
|
|
|
|
|
height: 16px;
|
2020-04-15 11:42:11 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
type RenderComponentProps = {
|
2021-04-26 10:35:59 +00:00
|
|
|
index: string;
|
|
|
|
|
item: ChartData;
|
|
|
|
|
length: number;
|
|
|
|
|
validationMessage: {
|
|
|
|
|
data: string;
|
2020-04-15 11:42:11 +00:00
|
|
|
seriesName: string;
|
|
|
|
|
};
|
2021-04-26 10:35:59 +00:00
|
|
|
deleteOption: (index: string) => void;
|
|
|
|
|
updateOption: (index: string, key: string, value: string) => void;
|
2020-06-05 16:20:23 +00:00
|
|
|
evaluated: {
|
|
|
|
|
seriesName: string;
|
|
|
|
|
data: Array<{ x: string; y: string }> | any;
|
|
|
|
|
};
|
2021-03-15 12:17:56 +00:00
|
|
|
theme: EditorTheme;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function DataControlComponent(props: RenderComponentProps) {
|
2020-06-05 16:20:23 +00:00
|
|
|
const {
|
|
|
|
|
deleteOption,
|
|
|
|
|
updateOption,
|
|
|
|
|
item,
|
|
|
|
|
index,
|
|
|
|
|
length,
|
|
|
|
|
evaluated,
|
2021-04-26 10:35:59 +00:00
|
|
|
validationMessage,
|
2020-06-05 16:20:23 +00:00
|
|
|
} = props;
|
2021-04-26 10:35:59 +00:00
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
return (
|
|
|
|
|
<StyledOptionControlWrapper orientation={"VERTICAL"}>
|
2021-03-15 12:17:56 +00:00
|
|
|
<ActionHolder>
|
2021-03-17 11:34:53 +00:00
|
|
|
<StyledLabel>Series Title</StyledLabel>
|
2021-03-15 12:17:56 +00:00
|
|
|
{length > 1 && (
|
|
|
|
|
<StyledDeleteIcon
|
|
|
|
|
height={20}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
deleteOption(index);
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2021-03-15 12:17:56 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</ActionHolder>
|
2020-04-29 10:29:02 +00:00
|
|
|
<StyledOptionControlWrapper orientation={"HORIZONTAL"}>
|
2020-07-01 10:01:07 +00:00
|
|
|
<CodeEditor
|
2021-04-28 10:28:39 +00:00
|
|
|
evaluatedValue={evaluated?.seriesName}
|
2020-06-05 16:20:23 +00:00
|
|
|
expected={"string"}
|
|
|
|
|
input={{
|
|
|
|
|
value: item.seriesName,
|
|
|
|
|
onChange: (
|
|
|
|
|
event: React.ChangeEvent<HTMLTextAreaElement> | string,
|
|
|
|
|
) => {
|
2020-10-12 13:06:05 +00:00
|
|
|
let value: string = event as string;
|
2020-06-05 16:20:23 +00:00
|
|
|
if (typeof event !== "string") {
|
|
|
|
|
value = event.target.value;
|
|
|
|
|
}
|
|
|
|
|
updateOption(index, "seriesName", value);
|
|
|
|
|
},
|
2020-04-29 10:29:02 +00:00
|
|
|
}}
|
2020-07-01 10:01:07 +00:00
|
|
|
mode={EditorModes.TEXT_WITH_BINDING}
|
2020-06-05 16:20:23 +00:00
|
|
|
placeholder="Series Name"
|
2021-04-28 10:28:39 +00:00
|
|
|
size={EditorSize.EXTENDED}
|
|
|
|
|
tabBehaviour={TabBehaviour.INPUT}
|
|
|
|
|
theme={props.theme}
|
2020-04-29 10:29:02 +00:00
|
|
|
/>
|
|
|
|
|
</StyledOptionControlWrapper>
|
2021-03-15 12:17:56 +00:00
|
|
|
<StyledLabel>Series Data</StyledLabel>
|
2020-06-05 16:20:23 +00:00
|
|
|
<StyledDynamicInput
|
|
|
|
|
className={"t--property-control-chart-series-data-control"}
|
|
|
|
|
>
|
2020-07-01 10:01:07 +00:00
|
|
|
<CodeEditor
|
2021-04-28 10:28:39 +00:00
|
|
|
evaluatedValue={evaluated?.data}
|
2020-06-05 16:20:23 +00:00
|
|
|
expected={`Array<x:string, y:number>`}
|
2020-04-15 11:42:11 +00:00
|
|
|
input={{
|
|
|
|
|
value: item.data,
|
|
|
|
|
onChange: (
|
|
|
|
|
event: React.ChangeEvent<HTMLTextAreaElement> | string,
|
|
|
|
|
) => {
|
2020-10-12 13:06:05 +00:00
|
|
|
let value: string = event as string;
|
2020-04-15 11:42:11 +00:00
|
|
|
if (typeof event !== "string") {
|
|
|
|
|
value = event.target.value;
|
|
|
|
|
}
|
|
|
|
|
updateOption(index, "data", value);
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
meta={{
|
2021-04-26 10:35:59 +00:00
|
|
|
error: has(validationMessage, "data")
|
|
|
|
|
? get(validationMessage, "data")
|
|
|
|
|
: "",
|
2020-04-15 11:42:11 +00:00
|
|
|
touched: true,
|
|
|
|
|
}}
|
2020-07-01 10:01:07 +00:00
|
|
|
mode={EditorModes.JSON_WITH_BINDING}
|
2020-04-15 11:42:11 +00:00
|
|
|
placeholder=""
|
2021-04-28 10:28:39 +00:00
|
|
|
size={EditorSize.EXTENDED}
|
|
|
|
|
tabBehaviour={TabBehaviour.INPUT}
|
|
|
|
|
theme={props.theme}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
|
|
|
|
</StyledDynamicInput>
|
2021-04-28 10:28:39 +00:00
|
|
|
<Box />
|
2020-04-15 11:42:11 +00:00
|
|
|
</StyledOptionControlWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ChartDataControl extends BaseControl<ControlProps> {
|
2020-09-15 16:54:15 +00:00
|
|
|
render() {
|
2021-04-26 10:35:59 +00:00
|
|
|
const chartData: AllChartData = isString(this.props.propertyValue)
|
|
|
|
|
? {}
|
2020-09-16 08:11:24 +00:00
|
|
|
: this.props.propertyValue;
|
2020-11-11 11:58:26 +00:00
|
|
|
|
2021-04-26 10:35:59 +00:00
|
|
|
const dataLength = Object.keys(chartData).length;
|
|
|
|
|
const { validationMessage } = this.props;
|
|
|
|
|
|
|
|
|
|
const evaluatedValue = this.props.evaluatedValue;
|
|
|
|
|
const firstKey = Object.keys(chartData)[0] as string;
|
|
|
|
|
|
2021-02-16 10:29:08 +00:00
|
|
|
if (this.props.widgetProperties.chartType === "PIE_CHART") {
|
2021-04-26 10:35:59 +00:00
|
|
|
const data = dataLength
|
|
|
|
|
? get(chartData, `${firstKey}`)
|
2021-02-16 10:29:08 +00:00
|
|
|
: {
|
|
|
|
|
seriesName: "",
|
2021-04-26 10:35:59 +00:00
|
|
|
data: [],
|
2021-02-16 10:29:08 +00:00
|
|
|
};
|
2021-04-26 10:35:59 +00:00
|
|
|
|
2021-02-16 10:29:08 +00:00
|
|
|
return (
|
|
|
|
|
<DataControlComponent
|
2021-04-28 10:28:39 +00:00
|
|
|
deleteOption={this.deleteOption}
|
|
|
|
|
evaluated={get(evaluatedValue, `${firstKey}`)}
|
2021-04-26 10:35:59 +00:00
|
|
|
index={firstKey}
|
2021-02-16 10:29:08 +00:00
|
|
|
item={data}
|
|
|
|
|
length={1}
|
2021-04-28 10:28:39 +00:00
|
|
|
theme={this.props.theme}
|
2021-02-16 10:29:08 +00:00
|
|
|
updateOption={this.updateOption}
|
2021-04-26 10:35:59 +00:00
|
|
|
validationMessage={get(validationMessage, `${firstKey}`)}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-04-26 10:35:59 +00:00
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
return (
|
2021-04-28 10:28:39 +00:00
|
|
|
<>
|
2021-03-15 12:17:56 +00:00
|
|
|
<Wrapper>
|
2021-04-26 10:35:59 +00:00
|
|
|
{Object.keys(chartData).map((key: string) => {
|
|
|
|
|
const data = get(chartData, `${key}`);
|
|
|
|
|
|
2021-03-15 12:17:56 +00:00
|
|
|
return (
|
|
|
|
|
<DataControlComponent
|
2021-04-28 10:28:39 +00:00
|
|
|
deleteOption={this.deleteOption}
|
|
|
|
|
evaluated={get(evaluatedValue, `${key}`)}
|
2021-04-26 10:35:59 +00:00
|
|
|
index={key}
|
2021-03-15 12:17:56 +00:00
|
|
|
item={data}
|
2021-04-28 10:28:39 +00:00
|
|
|
key={key}
|
2021-03-15 12:17:56 +00:00
|
|
|
length={dataLength}
|
2021-04-28 10:28:39 +00:00
|
|
|
theme={this.props.theme}
|
2021-03-15 12:17:56 +00:00
|
|
|
updateOption={this.updateOption}
|
2021-04-26 10:35:59 +00:00
|
|
|
validationMessage={get(validationMessage, `${key}`)}
|
2021-03-15 12:17:56 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</Wrapper>
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
<StyledPropertyPaneButton
|
2021-04-28 10:28:39 +00:00
|
|
|
category={Category.tertiary}
|
2020-04-15 11:42:11 +00:00
|
|
|
icon="plus"
|
|
|
|
|
onClick={this.addOption}
|
2021-03-15 12:17:56 +00:00
|
|
|
size={Size.medium}
|
2021-04-28 10:28:39 +00:00
|
|
|
tag="button"
|
|
|
|
|
text="Add Series"
|
|
|
|
|
type="button"
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
2021-04-28 10:28:39 +00:00
|
|
|
</>
|
2020-04-15 11:42:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 10:35:59 +00:00
|
|
|
deleteOption = (index: string) => {
|
|
|
|
|
this.deleteProperties([`${this.props.propertyName}.${index}`]);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateOption = (
|
2021-04-26 10:35:59 +00:00
|
|
|
index: string,
|
2020-04-15 11:42:11 +00:00
|
|
|
propertyName: string,
|
|
|
|
|
updatedValue: string,
|
|
|
|
|
) => {
|
2021-03-04 05:24:47 +00:00
|
|
|
this.updateProperty(
|
2021-04-26 10:35:59 +00:00
|
|
|
`${this.props.propertyName}.${index}.${propertyName}`,
|
2021-03-04 05:24:47 +00:00
|
|
|
updatedValue,
|
|
|
|
|
);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2021-04-26 10:35:59 +00:00
|
|
|
/**
|
|
|
|
|
* it adds new series data object in the chartData
|
|
|
|
|
*/
|
2020-04-15 11:42:11 +00:00
|
|
|
addOption = () => {
|
2021-04-26 10:35:59 +00:00
|
|
|
const randomString = generateReactKey();
|
|
|
|
|
|
|
|
|
|
this.updateProperty(`${this.props.propertyName}.${randomString}`, {
|
2021-03-04 05:24:47 +00:00
|
|
|
seriesName: "",
|
|
|
|
|
data: JSON.stringify([{ x: "label", y: 50 }]),
|
|
|
|
|
});
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "CHART_DATA";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ChartDataControl;
|