2022-07-14 07:02:35 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import { StyledDynamicInput } from "./StyledControls";
|
|
|
|
|
import CodeEditor, {
|
|
|
|
|
CodeEditorExpected,
|
|
|
|
|
} from "components/editorComponents/CodeEditor";
|
|
|
|
|
import {
|
|
|
|
|
EditorModes,
|
|
|
|
|
EditorSize,
|
|
|
|
|
EditorTheme,
|
|
|
|
|
TabBehaviour,
|
|
|
|
|
} from "components/editorComponents/CodeEditor/EditorConfig";
|
|
|
|
|
import { ColumnProperties } from "widgets/TableWidgetV2/component/Constants";
|
|
|
|
|
import { isDynamicValue } from "utils/DynamicBindingUtils";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { isString } from "utils/helpers";
|
|
|
|
|
import {
|
|
|
|
|
JSToString,
|
|
|
|
|
stringToJS,
|
2022-09-26 04:35:04 +00:00
|
|
|
} from "components/editorComponents/ActionCreator/utils";
|
2022-07-14 07:02:35 +00:00
|
|
|
import { AdditionalDynamicDataTree } from "utils/autocomplete/customTreeTypeDefCreator";
|
|
|
|
|
|
|
|
|
|
const PromptMessage = styled.span`
|
|
|
|
|
line-height: 17px;
|
|
|
|
|
`;
|
|
|
|
|
const CurlyBraces = styled.span`
|
|
|
|
|
color: ${(props) => props.theme.colors.codeMirror.background.hoverState};
|
|
|
|
|
background-color: #ffffff;
|
|
|
|
|
border-radius: 2px;
|
|
|
|
|
padding: 2px;
|
|
|
|
|
margin: 0px 2px;
|
|
|
|
|
font-size: 10px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
type InputTextProp = {
|
|
|
|
|
label: string;
|
|
|
|
|
value: string;
|
|
|
|
|
onChange: (event: React.ChangeEvent<HTMLTextAreaElement> | string) => void;
|
|
|
|
|
evaluatedValue?: any;
|
|
|
|
|
expected?: CodeEditorExpected;
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
dataTreePath?: string;
|
|
|
|
|
additionalDynamicData: AdditionalDynamicDataTree;
|
|
|
|
|
theme: EditorTheme;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function InputText(props: InputTextProp) {
|
|
|
|
|
const {
|
|
|
|
|
additionalDynamicData,
|
|
|
|
|
dataTreePath,
|
|
|
|
|
evaluatedValue,
|
|
|
|
|
expected,
|
|
|
|
|
onChange,
|
|
|
|
|
placeholder,
|
|
|
|
|
theme,
|
|
|
|
|
value,
|
|
|
|
|
} = props;
|
|
|
|
|
return (
|
|
|
|
|
<StyledDynamicInput>
|
|
|
|
|
<CodeEditor
|
|
|
|
|
additionalDynamicData={additionalDynamicData}
|
|
|
|
|
dataTreePath={dataTreePath}
|
|
|
|
|
evaluatedValue={evaluatedValue}
|
|
|
|
|
expected={expected}
|
|
|
|
|
input={{
|
|
|
|
|
value: value,
|
|
|
|
|
onChange: onChange,
|
|
|
|
|
}}
|
|
|
|
|
mode={EditorModes.TEXT_WITH_BINDING}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
promptMessage={
|
|
|
|
|
<PromptMessage>
|
|
|
|
|
Access the current cell using <CurlyBraces>{"{{"}</CurlyBraces>
|
|
|
|
|
currentRow.columnName
|
|
|
|
|
<CurlyBraces>{"}}"}</CurlyBraces>
|
|
|
|
|
</PromptMessage>
|
|
|
|
|
}
|
|
|
|
|
size={EditorSize.EXTENDED}
|
|
|
|
|
tabBehaviour={TabBehaviour.INDENT}
|
|
|
|
|
theme={theme}
|
|
|
|
|
/>
|
|
|
|
|
</StyledDynamicInput>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ComputeTablePropertyControlV2 extends BaseControl<
|
|
|
|
|
ComputeTablePropertyControlPropsV2
|
|
|
|
|
> {
|
2022-12-16 04:35:51 +00:00
|
|
|
static getBindingPrefix(tableName: string) {
|
|
|
|
|
return `{{${tableName}.processedTableData.map((currentRow, currentIndex) => ( `;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bindingSuffix = `))}}`;
|
|
|
|
|
|
2022-07-14 07:02:35 +00:00
|
|
|
render() {
|
|
|
|
|
const {
|
|
|
|
|
dataTreePath,
|
|
|
|
|
defaultValue,
|
|
|
|
|
expected,
|
|
|
|
|
label,
|
|
|
|
|
propertyValue,
|
|
|
|
|
theme,
|
|
|
|
|
} = this.props;
|
2022-12-16 04:35:51 +00:00
|
|
|
const tableName = this.props.widgetProperties.widgetName;
|
2022-07-14 07:02:35 +00:00
|
|
|
const value =
|
|
|
|
|
propertyValue && isDynamicValue(propertyValue)
|
2022-09-30 06:09:26 +00:00
|
|
|
? ComputeTablePropertyControlV2.getInputComputedValue(
|
|
|
|
|
propertyValue,
|
2022-12-16 04:35:51 +00:00
|
|
|
tableName,
|
2022-09-30 06:09:26 +00:00
|
|
|
)
|
2022-07-14 07:02:35 +00:00
|
|
|
: propertyValue
|
|
|
|
|
? propertyValue
|
|
|
|
|
: defaultValue;
|
|
|
|
|
const evaluatedProperties = this.props.widgetProperties;
|
|
|
|
|
|
|
|
|
|
const columns: Record<string, ColumnProperties> =
|
|
|
|
|
evaluatedProperties.primaryColumns || {};
|
|
|
|
|
const currentRow: { [key: string]: any } = {};
|
|
|
|
|
Object.values(columns).forEach((column) => {
|
|
|
|
|
currentRow[column.alias || column.originalId] = undefined;
|
|
|
|
|
});
|
|
|
|
|
// Load default value in evaluated value
|
|
|
|
|
if (value && !propertyValue) {
|
|
|
|
|
this.onTextChange(value);
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<InputText
|
|
|
|
|
additionalDynamicData={{
|
|
|
|
|
currentRow,
|
|
|
|
|
currentIndex: -1,
|
|
|
|
|
}}
|
|
|
|
|
dataTreePath={dataTreePath}
|
|
|
|
|
expected={expected}
|
|
|
|
|
label={label}
|
|
|
|
|
onChange={this.onTextChange}
|
|
|
|
|
theme={theme}
|
|
|
|
|
value={value}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 04:35:51 +00:00
|
|
|
static getInputComputedValue = (propertyValue: string, tableName: string) => {
|
|
|
|
|
const bindingPrefix = ComputeTablePropertyControlV2.getBindingPrefix(
|
|
|
|
|
tableName,
|
|
|
|
|
);
|
2022-07-14 07:02:35 +00:00
|
|
|
|
2022-12-16 04:35:51 +00:00
|
|
|
if (propertyValue.includes(bindingPrefix)) {
|
|
|
|
|
const value = `${propertyValue.substring(
|
|
|
|
|
bindingPrefix.length,
|
|
|
|
|
propertyValue.length -
|
|
|
|
|
ComputeTablePropertyControlV2.bindingSuffix.length,
|
|
|
|
|
)}`;
|
|
|
|
|
return JSToString(value);
|
|
|
|
|
} else {
|
|
|
|
|
return propertyValue;
|
|
|
|
|
}
|
2022-07-14 07:02:35 +00:00
|
|
|
};
|
|
|
|
|
|
2022-12-16 04:35:51 +00:00
|
|
|
getComputedValue = (value: string, tableName: string) => {
|
|
|
|
|
if (
|
|
|
|
|
!isDynamicValue(value) &&
|
|
|
|
|
!this.props.additionalControlData?.isArrayValue
|
|
|
|
|
) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 07:02:35 +00:00
|
|
|
const stringToEvaluate = stringToJS(value);
|
2022-12-16 04:35:51 +00:00
|
|
|
|
2022-07-14 07:02:35 +00:00
|
|
|
if (stringToEvaluate === "") {
|
|
|
|
|
return stringToEvaluate;
|
|
|
|
|
}
|
2022-12-16 04:35:51 +00:00
|
|
|
|
|
|
|
|
return `${ComputeTablePropertyControlV2.getBindingPrefix(
|
|
|
|
|
tableName,
|
|
|
|
|
)}${stringToEvaluate}${ComputeTablePropertyControlV2.bindingSuffix}`;
|
2022-07-14 07:02:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onTextChange = (event: React.ChangeEvent<HTMLTextAreaElement> | string) => {
|
|
|
|
|
let value = "";
|
|
|
|
|
if (typeof event !== "string") {
|
|
|
|
|
value = event.target?.value;
|
|
|
|
|
} else {
|
|
|
|
|
value = event;
|
|
|
|
|
}
|
|
|
|
|
if (isString(value)) {
|
|
|
|
|
const output = this.getComputedValue(
|
|
|
|
|
value,
|
|
|
|
|
this.props.widgetProperties.widgetName,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.updateProperty(this.props.propertyName, output);
|
|
|
|
|
} else {
|
|
|
|
|
this.updateProperty(this.props.propertyName, value);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "TABLE_COMPUTE_VALUE";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ComputeTablePropertyControlPropsV2 extends ControlProps {
|
|
|
|
|
defaultValue?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ComputeTablePropertyControlV2;
|