2020-04-15 11:42:11 +00:00
|
|
|
import React from "react";
|
2020-05-29 06:07:56 +00:00
|
|
|
import _ 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";
|
|
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
|
index: number;
|
|
|
|
|
item: {
|
|
|
|
|
seriesName: string;
|
2020-06-05 16:20:23 +00:00
|
|
|
data: Array<{ x: string; y: string }> | string;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
length: number;
|
2020-05-29 06:07:56 +00:00
|
|
|
isValid: boolean;
|
|
|
|
|
validationMessage: string;
|
2020-10-12 13:06:05 +00:00
|
|
|
deleteOption: (index: number) => void;
|
|
|
|
|
updateOption: (index: number, 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,
|
|
|
|
|
isValid,
|
|
|
|
|
evaluated,
|
|
|
|
|
} = props;
|
2020-04-15 11:42:11 +00:00
|
|
|
return (
|
|
|
|
|
<StyledOptionControlWrapper orientation={"VERTICAL"}>
|
2021-03-15 12:17:56 +00:00
|
|
|
<ActionHolder>
|
|
|
|
|
<StyledLabel>Series Tittle</StyledLabel>
|
|
|
|
|
{length > 1 && (
|
|
|
|
|
<StyledDeleteIcon
|
|
|
|
|
height={20}
|
|
|
|
|
width={20}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
deleteOption(index);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</ActionHolder>
|
2020-04-29 10:29:02 +00:00
|
|
|
<StyledOptionControlWrapper orientation={"HORIZONTAL"}>
|
2020-07-01 10:01:07 +00:00
|
|
|
<CodeEditor
|
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-06-11 11:03:16 +00:00
|
|
|
evaluatedValue={evaluated?.seriesName}
|
2021-03-15 12:17:56 +00:00
|
|
|
theme={props.theme}
|
2020-07-01 10:01:07 +00:00
|
|
|
size={EditorSize.EXTENDED}
|
|
|
|
|
mode={EditorModes.TEXT_WITH_BINDING}
|
|
|
|
|
tabBehaviour={TabBehaviour.INPUT}
|
2020-06-05 16:20:23 +00:00
|
|
|
placeholder="Series Name"
|
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
|
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);
|
|
|
|
|
},
|
|
|
|
|
}}
|
2020-06-11 11:03:16 +00:00
|
|
|
evaluatedValue={evaluated?.data}
|
2020-04-15 11:42:11 +00:00
|
|
|
meta={{
|
2020-06-04 13:49:22 +00:00
|
|
|
error: isValid ? "" : "There is an error",
|
2020-04-15 11:42:11 +00:00
|
|
|
touched: true,
|
|
|
|
|
}}
|
2021-03-15 12:17:56 +00:00
|
|
|
theme={props.theme}
|
2020-07-01 10:01:07 +00:00
|
|
|
size={EditorSize.EXTENDED}
|
|
|
|
|
mode={EditorModes.JSON_WITH_BINDING}
|
|
|
|
|
tabBehaviour={TabBehaviour.INPUT}
|
2020-04-15 11:42:11 +00:00
|
|
|
placeholder=""
|
|
|
|
|
/>
|
|
|
|
|
</StyledDynamicInput>
|
2021-03-15 12:17:56 +00:00
|
|
|
<Box></Box>
|
2020-04-15 11:42:11 +00:00
|
|
|
</StyledOptionControlWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ChartDataControl extends BaseControl<ControlProps> {
|
2020-05-29 06:07:56 +00:00
|
|
|
getValidations = (message: string, isValid: boolean, len: number) => {
|
|
|
|
|
const validations: Array<{
|
|
|
|
|
isValid: boolean;
|
|
|
|
|
validationMessage: string;
|
|
|
|
|
}> = [];
|
|
|
|
|
let index = -1;
|
|
|
|
|
let validationMessage = "";
|
|
|
|
|
if (message.indexOf("##") !== -1) {
|
|
|
|
|
const messages = message.split("##");
|
|
|
|
|
index = Number(messages[0]);
|
|
|
|
|
validationMessage = messages[1];
|
|
|
|
|
}
|
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
|
if (i === index) {
|
|
|
|
|
validations.push({
|
|
|
|
|
isValid: false,
|
|
|
|
|
validationMessage: validationMessage,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
validations.push({
|
|
|
|
|
isValid: true,
|
|
|
|
|
validationMessage: "",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return validations;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-11 11:58:26 +00:00
|
|
|
getEvaluatedValue = () => {
|
|
|
|
|
if (Array.isArray(this.props.evaluatedValue)) {
|
|
|
|
|
return this.props.evaluatedValue;
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-15 16:54:15 +00:00
|
|
|
render() {
|
2020-09-16 08:11:24 +00:00
|
|
|
const chartData: Array<{ seriesName: string; data: string }> = _.isString(
|
|
|
|
|
this.props.propertyValue,
|
|
|
|
|
)
|
|
|
|
|
? []
|
|
|
|
|
: this.props.propertyValue;
|
|
|
|
|
const dataLength = chartData.length;
|
2020-05-29 06:07:56 +00:00
|
|
|
const { validationMessage, isValid } = this.props;
|
|
|
|
|
const validations: Array<{
|
|
|
|
|
isValid: boolean;
|
|
|
|
|
validationMessage: string;
|
|
|
|
|
}> = this.getValidations(
|
|
|
|
|
validationMessage || "",
|
|
|
|
|
isValid,
|
|
|
|
|
chartData.length,
|
|
|
|
|
);
|
2020-11-11 11:58:26 +00:00
|
|
|
|
|
|
|
|
const evaluatedValue = this.getEvaluatedValue();
|
2021-02-16 10:29:08 +00:00
|
|
|
if (this.props.widgetProperties.chartType === "PIE_CHART") {
|
|
|
|
|
const data = chartData.length
|
|
|
|
|
? chartData[0]
|
|
|
|
|
: {
|
|
|
|
|
seriesName: "",
|
|
|
|
|
data: "",
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<DataControlComponent
|
|
|
|
|
index={0}
|
|
|
|
|
item={data}
|
|
|
|
|
length={1}
|
|
|
|
|
deleteOption={this.deleteOption}
|
|
|
|
|
updateOption={this.updateOption}
|
|
|
|
|
isValid={validations[0].isValid}
|
|
|
|
|
validationMessage={validations[0].validationMessage}
|
|
|
|
|
evaluated={evaluatedValue[0]}
|
2021-03-15 12:17:56 +00:00
|
|
|
theme={this.props.theme}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-04-15 11:42:11 +00:00
|
|
|
return (
|
|
|
|
|
<React.Fragment>
|
2021-03-15 12:17:56 +00:00
|
|
|
<Wrapper>
|
|
|
|
|
{chartData.map((data, index) => {
|
|
|
|
|
return (
|
|
|
|
|
<DataControlComponent
|
|
|
|
|
key={index}
|
|
|
|
|
index={index}
|
|
|
|
|
item={data}
|
|
|
|
|
length={dataLength}
|
|
|
|
|
deleteOption={this.deleteOption}
|
|
|
|
|
updateOption={this.updateOption}
|
|
|
|
|
isValid={validations[index].isValid}
|
|
|
|
|
validationMessage={validations[index].validationMessage}
|
|
|
|
|
evaluated={evaluatedValue[index]}
|
|
|
|
|
theme={this.props.theme}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</Wrapper>
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
<StyledPropertyPaneButton
|
|
|
|
|
icon="plus"
|
2021-03-15 12:17:56 +00:00
|
|
|
tag="button"
|
|
|
|
|
type="button"
|
|
|
|
|
text="Add Series"
|
2020-04-15 11:42:11 +00:00
|
|
|
onClick={this.addOption}
|
2021-03-15 12:17:56 +00:00
|
|
|
size={Size.medium}
|
|
|
|
|
category={Category.tertiary}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteOption = (index: number) => {
|
2021-03-04 05:24:47 +00:00
|
|
|
this.deleteProperties([`${this.props.propertyName}[${index}]`]);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateOption = (
|
|
|
|
|
index: number,
|
|
|
|
|
propertyName: string,
|
|
|
|
|
updatedValue: string,
|
|
|
|
|
) => {
|
2021-03-04 05:24:47 +00:00
|
|
|
this.updateProperty(
|
|
|
|
|
`${this.props.propertyName}[${index}].${propertyName}`,
|
|
|
|
|
updatedValue,
|
|
|
|
|
);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
addOption = () => {
|
|
|
|
|
const chartData: Array<{
|
|
|
|
|
seriesName: string;
|
2020-09-15 16:54:15 +00:00
|
|
|
data: string;
|
|
|
|
|
}> = this.props.propertyValue;
|
2021-03-04 05:24:47 +00:00
|
|
|
this.updateProperty(`${this.props.propertyName}[${chartData.length}]`, {
|
|
|
|
|
seriesName: "",
|
|
|
|
|
data: JSON.stringify([{ x: "label", y: 50 }]),
|
|
|
|
|
});
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "CHART_DATA";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ChartDataControl;
|