/* eslint-disable no-extra-semi */ import {List as ImmutableList, Map as ImmutableMap} from "immutable"; import {ElementType, ReactElement, Factory} from "react"; //////////////// // common ///////////////// type AnyObject = object; type MongoValue = any; type ElasticSearchQueryType = string; type JsonLogicResult = { logic?: JsonLogicTree, data?: Object, errors?: Array } type JsonLogicTree = Object; type JsonLogicValue = any; type JsonLogicField = { "var": string } type RuleValue = boolean | number | string | Date | Array | any; type Optional = { [P in keyof T]?: T[P]; } type TypedMap = { [key: string]: T; } // You can not use a union for types on a key, but can define overloaded accessors of different types. // Key can be a string OR number type TypedKeyMap = { [key: string]: T; [key: number]: T; } type Empty = null | undefined; type ValueSource = "value" | "field" | "func" | "const"; type RuleGroupModes = "struct" | "some" | "array"; type TypedValueSourceMap = { [key in ValueSource]: T; } type JsonGroup = { type: "group", id?: string, children1?: {[id: string]: JsonGroup|JsonRule|JsonRuleGroup|JsonRuleGroupExt}, properties?: { conjunction: string, not?: boolean, } } type JsonRuleGroup = { type: "rule_group", id?: string, children1?: {[id: string]: JsonRule}, properties?: { field: string | Empty, mode?: RuleGroupModes, } } type JsonRuleGroupExt = { type: "rule_group", id?: string, children1?: {[id: string]: JsonRule}, properties?: { field: string | Empty, mode: RuleGroupModes, operator: string | Empty, value: Array, valueSrc: Array, valueType: Array, valueError?: Array, } } type JsonRule = { type: "rule", properties: { field: string | Empty, operator: string | Empty, value: Array, valueSrc: Array, valueType: Array, valueError?: Array, operatorOptions?: AnyObject } } export type JsonTree = JsonGroup; export type ImmutableTree = ImmutableMap; //////////////// // Query, Builder, Utils, Config ///////////////// export interface Utils { // export jsonLogicFormat(tree: ImmutableTree, config: Config): JsonLogicResult; queryBuilderFormat(tree: ImmutableTree, config: Config): Object; queryString(tree: ImmutableTree, config: Config, isForDisplay?: boolean): string; sqlFormat(tree: ImmutableTree, config: Config): string; mongodbFormat(tree: ImmutableTree, config: Config): Object; elasticSearchFormat(tree: ImmutableTree, config: Config): Object; // load, save getTree(tree: ImmutableTree, light?: boolean): JsonTree; loadTree(jsonTree: JsonTree): ImmutableTree; checkTree(tree: ImmutableTree, config: Config): ImmutableTree; isValidTree(tree: ImmutableTree): boolean; // import loadFromJsonLogic(logicTree: JsonLogicTree | undefined, config: Config): ImmutableTree; isJsonLogic(value: any): boolean; // other uuid(): string; simulateAsyncFetch(all: AsyncFetchListValues, pageSize?: number, delay?: number): AsyncFetchListValuesFn; } export interface BuilderProps { tree: ImmutableTree, config: Config, actions: {[key: string]: Function}, } export interface QueryProps { conjunctions: Conjunctions; operators: Operators; widgets: Widgets; types: Types; settings: Settings; fields: Fields; funcs?: Funcs; value: ImmutableTree; onChange(immutableTree: ImmutableTree, config: Config): void; renderBuilder(props: BuilderProps): ReactElement; } export type Builder = ElementType; export type Query = ElementType; export interface Config { conjunctions: Conjunctions, operators: Operators, widgets: Widgets, types: Types, settings: Settings, fields: Fields, funcs?: Funcs, } ///////////////// // Widgets, WidgetProps ///////////////// type FormatValue = (val: RuleValue, fieldDef: Field, wgtDef: Widget, isForDisplay: boolean, op: string, opDef: Operator, rightFieldDef?: Field) => string; type SqlFormatValue = (val: RuleValue, fieldDef: Field, wgtDef: Widget, op: string, opDef: Operator, rightFieldDef?: Field) => string; type MongoFormatValue = (val: RuleValue, fieldDef: Field, wgtDef: Widget, op: string, opDef: Operator) => MongoValue; type ValidateValue = (val: RuleValue, fieldSettings: FieldSettings) => boolean | string | null; type ElasticSearchFormatValue = (queryType: ElasticSearchQueryType, val: RuleValue, op: string, field: string, config: Config) => AnyObject | null; interface BaseWidgetProps { value: RuleValue, setValue(val: RuleValue, asyncListValues?: Array): void, placeholder: string, field: string, operator: string, fieldDefinition: Field, config: Config, delta?: number, customProps?: AnyObject, readonly?: boolean, } interface RangeWidgetProps extends BaseWidgetProps { placeholders: Array, textSeparators: Array, } export type WidgetProps = (BaseWidgetProps | RangeWidgetProps) & FieldSettings; export type TextWidgetProps = BaseWidgetProps & TextFieldSettings; export type DateTimeWidgetProps = BaseWidgetProps & DateTimeFieldSettings; export type BooleanWidgetProps = BaseWidgetProps & BooleanFieldSettings; export type NumberWidgetProps = BaseWidgetProps & NumberFieldSettings; export type SelectWidgetProps = BaseWidgetProps & SelectFieldSettings; export type TreeSelectWidgetProps = BaseWidgetProps & TreeSelectFieldSettings; export type RangeSliderWidgetProps = RangeWidgetProps & NumberFieldSettings; export interface BaseWidget { customProps?: AnyObject, type: string, jsType?: string, factory: Factory, valueSrc?: ValueSource, valuePlaceholder?: string, valueLabel?: string, fullWidth?: boolean, formatValue: FormatValue, sqlFormatValue: SqlFormatValue, mongoFormatValue?: MongoFormatValue, elasticSearchFormatValue?: ElasticSearchFormatValue, //obsolete: validateValue?: ValidateValue, } export interface RangeableWidget extends BaseWidget { singleWidget?: string, valueLabels?: Array, } export interface FieldWidget { customProps?: AnyObject, valueSrc: "field", valuePlaceholder?: string, valueLabel?: string, formatValue: FormatValue, // with rightFieldDef sqlFormatValue: SqlFormatValue, // with rightFieldDef //obsolete: validateValue?: ValidateValue, } export type TextWidget = BaseWidget & TextFieldSettings; export type DateTimeWidget = RangeableWidget & DateTimeFieldSettings; export type BooleanWidget = BaseWidget & BooleanFieldSettings; export type NumberWidget = RangeableWidget & NumberFieldSettings; export type SelectWidget = BaseWidget & SelectFieldSettings; export type TreeSelectWidget = BaseWidget & TreeSelectFieldSettings; export type Widget = FieldWidget | TextWidget | DateTimeWidget | BooleanWidget | NumberWidget | SelectWidget | TreeSelectWidget | RangeableWidget | BaseWidget; export type Widgets = TypedMap; ///////////////// // Conjunctions ///////////////// type FormatConj = (children: ImmutableList, conj: string, not: boolean, isForDisplay?: boolean) => string; type SqlFormatConj = (children: ImmutableList, conj: string, not: boolean) => string; export interface Conjunction { label: string, formatConj: FormatConj, sqlFormatConj: SqlFormatConj, mongoConj: string, reversedConj?: string, } export type Conjunctions = TypedMap; export interface ConjunctionOption { id: string, key: string, label: string, checked: boolean, } export interface ConjsProps { path: string, readonly?: boolean, disabled?: boolean, selectedConjunction?: string, setConjunction(conj: string): void, conjunctionOptions?: TypedMap, config?: Config, not: boolean, setNot(not: boolean): void, showNot?: boolean, notLabel?: string, } ///////////////// // Rule, Group ///////////////// export interface ButtonProps { type: "addRule" | "addGroup" | "delRule" | "delGroup" | "addRuleGroup" | "delRuleGroup", onClick(): void, label: string, config?: Config, } export interface ButtonGroupProps { children: ReactElement, config?: Config, } export interface ProviderProps { children: ReactElement, config?: Config, } export type ValueSourceItem = { label: string, } type ValueSourcesItems = TypedValueSourceMap; export interface ValueSourcesProps { config?: Config, valueSources: ValueSourcesItems, valueSrc?: ValueSource, setValueSrc(valueSrc: string): void, readonly?: boolean, title: string, } export interface ConfirmModalProps { onOk(): void, okText: string, cancelText?: string, title: string, } export interface RuleErrorProps { error: string, } ///////////////// // Operators ///////////////// type FormatOperator = (field: string, op: string, vals: string | Array, valueSrc?: ValueSource, valueType?: string, opDef?: Operator, operatorOptions?: AnyObject, isForDisplay?: boolean) => string; type MongoFormatOperator = (field: string, op: string, vals: MongoValue | Array, useExpr?: boolean, valueSrc?: ValueSource, valueType?: string, opDef?: Operator, operatorOptions?: AnyObject) => Object; type SqlFormatOperator = (field: string, op: string, vals: string | Array, valueSrc?: ValueSource, valueType?: string, opDef?: Operator, operatorOptions?: AnyObject) => string; type JsonLogicFormatOperator = (field: JsonLogicField, op: string, vals: JsonLogicValue | Array, opDef?: Operator, operatorOptions?: AnyObject) => JsonLogicTree; type ElasticSearchFormatQueryType = (valueType: string) => ElasticSearchQueryType; interface ProximityConfig { optionLabel: string, optionTextBefore: string, optionPlaceholder: string, minProximity: number, maxProximity: number, defaults: { proximity: number, }, customProps?: AnyObject, } export interface ProximityProps extends ProximityConfig { options: ImmutableMap, setOption: (key: string, value: any) => void, config: Config, } export interface ProximityOptions extends ProximityConfig { factory: Factory, } interface BaseOperator { label: string, reversedOp: string, isNotOp?: boolean, cardinality?: number, formatOp?: FormatOperator, labelForFormat?: string, mongoFormatOp?: MongoFormatOperator, sqlOp?: string, sqlFormatOp?: SqlFormatOperator, jsonLogic?: string | JsonLogicFormatOperator, _jsonLogicIsRevArgs?: boolean, elasticSearchQueryType?: ElasticSearchQueryType | ElasticSearchFormatQueryType, valueSources?: Array, } interface UnaryOperator extends BaseOperator { //cardinality: 0, } interface BinaryOperator extends BaseOperator { //cardinality: 1, } interface Operator2 extends BaseOperator { //cardinality: 2 textSeparators: Array, valueLabels: Array, isSpecialRange?: boolean, } interface OperatorProximity extends Operator2 { options: ProximityOptions, } export type Operator = UnaryOperator | BinaryOperator | Operator2 | OperatorProximity; export type Operators = TypedMap; ///////////////// // Types ///////////////// interface WidgetConfigForType { widgetProps?: Optional, opProps?: Optional, operators?: Array, } interface Type { valueSources?: Array, defaultOperator?: string, widgets: TypedMap, } export type Types = TypedMap; ///////////////// // Fields ///////////////// type FieldType = string | "!struct" | "!group"; interface ListItem { value: any, title?: string, } interface TreeItem extends ListItem { children?: Array, parent?: any, disabled?: boolean, selectable?: boolean, disableCheckbox?: boolean, checkable?: boolean, } type TreeData = Array; type ListValues = TypedMap | TypedKeyMap | Array | Array; type AsyncFetchListValues = ListValues; interface AsyncFetchListValuesResult { values: AsyncFetchListValues, hasMore?: boolean, } type AsyncFetchListValuesFn = (search: string | null, offset: number) => Promise; export interface BasicFieldSettings { validateValue?: ValidateValue, } export interface TextFieldSettings extends BasicFieldSettings { maxLength?: number, maxRows?: number, } export interface NumberFieldSettings extends BasicFieldSettings { min?: number, max?: number, step?: number, marks?: {[mark: number]: ReactElement | string} } export interface DateTimeFieldSettings extends BasicFieldSettings { timeFormat?: string, dateFormat?: string, valueFormat?: string, use12Hours?: boolean, useKeyboard?: boolean, } export interface SelectFieldSettings extends BasicFieldSettings { listValues?: ListValues, allowCustomValues?: boolean, showSearch?: boolean, showCheckboxes?: boolean, asyncFetch?: AsyncFetchListValuesFn, useLoadMore?: boolean, useAsyncSearch?: boolean, forceAsyncSearch?: boolean, } export interface TreeSelectFieldSettings extends BasicFieldSettings { listValues?: TreeData, treeExpandAll?: boolean, treeSelectOnlyLeafs?: boolean, } export interface BooleanFieldSettings extends BasicFieldSettings { labelYes?: ReactElement | string, labelNo?: ReactElement | string, } export type FieldSettings = NumberFieldSettings | DateTimeFieldSettings | SelectFieldSettings | TreeSelectFieldSettings | BooleanFieldSettings | TextFieldSettings | BasicFieldSettings; interface BaseField { type: FieldType, label?: string, tooltip?: string, } interface ValueField extends BaseField { type: string, preferWidgets?: Array, valueSources?: Array, funcs?: Array, tableName?: string, // legacy: PR #18, PR #20 fieldName?: string, jsonLogicVar?: string, fieldSettings?: FieldSettings, defaultValue?: RuleValue, widgets?: TypedMap, mainWidgetProps?: Optional, hideForSelect?: boolean, hideForCompare?: boolean, //obsolete - moved to FieldSettings listValues?: ListValues, allowCustomValues?: boolean, } interface SimpleField extends ValueField { label2?: string, operators?: Array, defaultOperator?: string, excludeOperators?: Array, } interface FieldStruct extends BaseField { type: "!struct", subfields: Fields, } interface FieldGroup extends BaseField { type: "!group", subfields: Fields, mode: RuleGroupModes, } interface FieldGroupExt extends BaseField { type: "!group", subfields: Fields, mode: "array", operators?: Array, defaultOperator?: string, initialEmptyWhere?: boolean, showNot?: boolean, conjunctions?: Array, } export type Field = SimpleField; type FieldOrGroup = FieldStruct | FieldGroup | FieldGroupExt | Field; export type Fields = TypedMap; ///////////////// // FieldProps ///////////////// export type FieldItem = { items?: FieldItems, key: string, path?: string, label: string, fullLabel?: string, altLabel?: string, tooltip?: string, disabled?: boolean, } type FieldItems = TypedMap; export interface FieldProps { items: FieldItems, setField(path: string): void, selectedKey: string | Empty, selectedKeys?: Array | Empty, selectedPath?: Array | Empty, selectedLabel?: string | Empty, selectedAltLabel?: string | Empty, selectedFullLabel?: string | Empty, config?: Config, customProps?: AnyObject, placeholder?: string, selectedOpts?: {tooltip?: string}, readonly?: boolean, } ///////////////// // Settings ///////////////// type ValueSourcesInfo = {[vs in ValueSource]?: {label: string, widget?: string}} type AntdPosition = "topLeft" | "topCenter" | "topRight" | "bottomLeft" | "bottomCenter" | "bottomRight"; type AntdSize = "small" | "large" | "medium"; type ChangeFieldStrategy = "default" | "keep" | "first" | "none"; type FormatReverse = (q: string, op: string, reversedOp: string, operatorDefinition: Operator, revOperatorDefinition: Operator, isForDisplay: boolean) => string; type SqlFormatReverse = (q: string, op: string, reversedOp: string, operatorDefinition: Operator, revOperatorDefinition: Operator) => string; type FormatField = (field: string, parts: Array, label2: string, fieldDefinition: Field, config: Config, isForDisplay: boolean) => string; type CanCompareFieldWithField = (leftField: string, leftFieldConfig: Field, rightField: string, rightFieldConfig: Field, op: string) => boolean; type FormatAggr = (whereStr: string, aggrField: string, operator: string, value: string | Array, valueSrc: ValueSource, valueType: string, opDef: Operator, operatorOptions: AnyObject, isForDisplay: boolean, aggrFieldDef: Field) => string; export interface LocaleSettings { locale?: { moment?: string, antd?: Object, material?: Object, }, theme?: { material?: Object, }, valueLabel?: string, valuePlaceholder?: string, fieldLabel?: string, operatorLabel?: string, fieldPlaceholder?: string, funcPlaceholder?: string, funcLabel?: string, operatorPlaceholder?: string, deleteLabel?: string, addGroupLabel?: string, addRuleLabel?: string, addSubRuleLabel?: string, delGroupLabel?: string, notLabel?: string, valueSourcesPopupTitle?: string, removeRuleConfirmOptions?: { title?: string, okText?: string, okType?: string, }, removeGroupConfirmOptions?: { title?: string, okText?: string, okType?: string, }, } export interface RenderSettings { renderField?: Factory, renderOperator?: Factory, renderFunc?: Factory, renderConjs?: Factory, renderButton?: Factory, renderButtonGroup?: Factory, renderProvider?: Factory, renderValueSources?: Factory, renderConfirm?: ConfirmFunc, useConfirm?: () => Function, renderSize?: AntdSize, dropdownPlacement?: AntdPosition, groupActionsPosition?: AntdPosition, showLabels?: boolean, maxLabelsLength?: number, customFieldSelectProps?: AnyObject, renderBeforeWidget?: Factory, renderAfterWidget?: Factory, renderBeforeActions?: Factory, renderAfterActions?: Factory, renderRuleError?: Factory, defaultSliderWidth?: string, defaultSelectWidth?: string, defaultSearchWidth?: string, defaultMaxRows?: number, } export interface BehaviourSettings { valueSourcesInfo?: ValueSourcesInfo, canCompareFieldWithField?: CanCompareFieldWithField, canReorder?: boolean, canRegroup?: boolean, showNot?: boolean, maxNesting?: number, setOpOnChangeField: Array, clearValueOnChangeField?: boolean, clearValueOnChangeOp?: boolean, canLeaveEmptyGroup?: boolean, immutableGroupsMode?: boolean, immutableFieldsMode?: boolean, immutableOpsMode?: boolean, immutableValuesMode?: boolean, maxNumberOfRules?: Number, showErrorMessage?: boolean, canShortMongoQuery?: boolean, convertableWidgets?: TypedMap>, } export interface OtherSettings { fieldSeparator?: string, fieldSeparatorDisplay?: string, formatReverse?: FormatReverse, sqlFormatReverse?: SqlFormatReverse, formatField?: FormatField, formarAggr?: FormatAggr, } export type Settings = LocaleSettings & RenderSettings & BehaviourSettings & OtherSettings; ///////////////// // Funcs ///////////////// type SqlFormatFunc = (formattedArgs: { [key: string]: string }) => string; type FormatFunc = (formattedArgs: { [key: string]: string }, isForDisplay: boolean) => string; type MongoFormatFunc = (formattedArgs: { [key: string]: MongoValue }) => MongoValue; type JsonLogicFormatFunc = (formattedArgs: { [key: string]: JsonLogicValue }) => JsonLogicTree; type JsonLogicImportFunc = (val: JsonLogicValue) => Array; interface FuncGroup { type?: "!struct", label?: string, subfields: TypedMap, } export interface Func { returnType: string, args: TypedMap, label?: string, sqlFunc?: string, mongoFunc?: string, mongoArgsAsObject?: boolean, jsonLogic?: string | JsonLogicFormatFunc, // Deprecated! // Calling methods on objects was remvoed in JsonLogic 2.x // https://github.com/jwadhams/json-logic-js/issues/86 jsonLogicIsMethod?: boolean, jsonLogicImport?: JsonLogicImportFunc, formatFunc?: FormatFunc, sqlFormatFunc?: SqlFormatFunc, mongoFormatFunc?: MongoFormatFunc, renderBrackets?: Array, renderSeps?: Array, } export interface FuncArg extends ValueField { isOptional?: boolean, showPrefix?: boolean, } export type Funcs = TypedMap; ///////////////// // BasicConfig ///////////////// export interface BasicConfig extends Config { conjunctions: { AND: Conjunction, OR: Conjunction, }, operators: { equal: BinaryOperator, not_equal: BinaryOperator, less: BinaryOperator, less_or_equal: BinaryOperator, greater: BinaryOperator, greater_or_equal: BinaryOperator, like: BinaryOperator, not_like: BinaryOperator, starts_with: BinaryOperator, ends_with: BinaryOperator, between: Operator2, not_between: Operator2, is_empty: UnaryOperator, is_not_empty: UnaryOperator, select_equals: BinaryOperator, select_not_equals: BinaryOperator, select_any_in: BinaryOperator, select_not_any_in: BinaryOperator, multiselect_equals: BinaryOperator, multiselect_not_equals: BinaryOperator, proximity: OperatorProximity, }, widgets: { text: TextWidget, textarea: TextWidget, number: NumberWidget, slider: NumberWidget, rangeslider: NumberWidget, select: SelectWidget, multiselect: SelectWidget, treeselect: TreeSelectWidget, treemultiselect: TreeSelectWidget, date: DateTimeWidget, time: DateTimeWidget, datetime: DateTimeWidget, boolean: BooleanWidget, field: FieldWidget, func: FieldWidget, }, types: { text: Type, number: Type, date: Type, time: Type, datetime: Type, select: Type, multiselect: Type, treeselect: Type, treemultiselect: Type, boolean: Type, }, settings: Settings, } ///////////////// // ReadyWidgets ///////////////// type ConfirmFunc = (opts: ConfirmModalProps) => void; interface VanillaWidgets { // vanilla core widgets VanillaFieldSelect: ElementType, VanillaConjs: ElementType, VanillaButton: ElementType, VanillaButtonGroup: ElementType, VanillaProvider: ElementType, VanillaValueSources: ElementType, vanillaConfirm: ConfirmFunc, // vanilla core widgets VanillaBooleanWidget: ElementType, VanillaTextWidget: ElementType, VanillaTextAreaWidget: ElementType, VanillaDateWidget: ElementType, VanillaTimeWidget: ElementType, VanillaDateTimeWidget: ElementType, VanillaMultiSelectWidget: ElementType, VanillaSelectWidget: ElementType, VanillaNumberWidget: ElementType, VanillaSliderWidget: ElementType, } export interface AntdWidgets { // antd core widgets FieldSelect: ElementType, FieldDropdown: ElementType, FieldCascader: ElementType, FieldTreeSelect: ElementType, Button: ElementType, ButtonGroup: ElementType, Conjs: ElementType, Provider: ElementType, ValueSources: ElementType, confirm: ConfirmFunc, // antd value widgets TextWidget: ElementType, TextAreaWidget: ElementType, NumberWidget: ElementType, SliderWidget: ElementType, RangeWidget: ElementType, SelectWidget: ElementType, MultiSelectWidget: ElementType, TreeSelectWidget: ElementType, DateWidget: ElementType, TimeWidget: ElementType, DateTimeWidget: ElementType, BooleanWidget: ElementType, } interface ReadyWidgets extends VanillaWidgets { ValueFieldWidget: ElementType, FuncWidget: ElementType, } export interface MaterialWidgets { // material core widgets MaterialFieldSelect: ElementType, MaterialConjs: ElementType, MaterialButton: ElementType, MaterialButtonGroup: ElementType, MaterialProvider: ElementType, MaterialValueSources: ElementType, MaterialConfirm: ConfirmFunc, MaterialUseConfirm: () => Function, // material core widgets MaterialBooleanWidget: ElementType, MaterialTextWidget: ElementType, MaterialTextAreaWidget: ElementType, MaterialDateWidget: ElementType, MaterialTimeWidget: ElementType, MaterialDateTimeWidget: ElementType, MaterialMultiSelectWidget: ElementType, MaterialSelectWidget: ElementType, MaterialNumberWidget: ElementType, MaterialSliderWidget: ElementType, MaterialRangeWidget: ElementType, MaterialAutocompleteWidget: ElementType, } ///////////////// export const Utils: Utils; export const Query: Query; export const Builder: Builder; export const BasicConfig: BasicConfig; export const BasicFuncs: Funcs; export const Widgets: ReadyWidgets;