UNPKG

@react-awesome-query-builder/core

Version:
90 lines 5.04 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import Immutable from "immutable"; import uuid from "./uuid"; import { getNewValueForFieldOp } from "./getNewValueForFieldOp"; import { isImmutable } from "./stuff"; import { defaultOperatorOptions, defaultGroupProperties, getDefaultField, getDefaultFieldSrc, getDefaultOperator, defaultGroupConjunction } from "./defaultUtils"; import { validateValue, validateRange } from "./validation"; import { getFieldConfig } from "./configUtils"; export * from "./defaultUtils"; export var defaultRuleProperties = function defaultRuleProperties(config) { var parentRuleGroupField = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var item = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var canUseDefaultFieldAndOp = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; var canGetFirst = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; var field = null, operator = null, fieldSrc = null; var showErrorMessage = config.settings.showErrorMessage; if (item) { var _item$properties, _item$properties2, _item$properties3; fieldSrc = item === null || item === void 0 || (_item$properties = item.properties) === null || _item$properties === void 0 ? void 0 : _item$properties.fieldSrc; field = item === null || item === void 0 || (_item$properties2 = item.properties) === null || _item$properties2 === void 0 ? void 0 : _item$properties2.field; operator = item === null || item === void 0 || (_item$properties3 = item.properties) === null || _item$properties3 === void 0 ? void 0 : _item$properties3.operator; } else if (canUseDefaultFieldAndOp) { field = getDefaultField(config, canGetFirst, parentRuleGroupField); if (field) { fieldSrc = isImmutable(field) ? "func" : "field"; } else { fieldSrc = getDefaultFieldSrc(config); } operator = getDefaultOperator(config, field, true); } else { fieldSrc = getDefaultFieldSrc(config); } var current = new Immutable.Map({ fieldSrc: fieldSrc, field: field, operator: operator, value: new Immutable.List(), valueSrc: new Immutable.List(), //used for complex operators like proximity operatorOptions: defaultOperatorOptions(config, operator, field) }); if (showErrorMessage) { current = current.set("valueError", new Immutable.List()); } if (field && operator) { var canFix = false; var _getNewValueForFieldO = getNewValueForFieldOp({ validateValue: validateValue, validateRange: validateRange }, config, config, current, field, operator, "operator", canFix), newValue = _getNewValueForFieldO.newValue, newValueSrc = _getNewValueForFieldO.newValueSrc, newValueType = _getNewValueForFieldO.newValueType, newValueError = _getNewValueForFieldO.newValueError, newFieldError = _getNewValueForFieldO.newFieldError; current = current.set("value", newValue).set("valueSrc", newValueSrc).set("valueType", newValueType); if (showErrorMessage) { current = current.set("valueError", newValueError).set("fieldError", newFieldError); } } var fieldConfig = getFieldConfig(config, field); if ((fieldConfig === null || fieldConfig === void 0 ? void 0 : fieldConfig.type) === "!group") { var conjunction = defaultGroupConjunction(config, fieldConfig); current = current.set("conjunction", conjunction); } return current; }; export var defaultItemProperties = function defaultItemProperties(config, item) { return (item === null || item === void 0 ? void 0 : item.type) == "group" ? defaultGroupProperties(config) : defaultRuleProperties(config, null, item); }; export var defaultRule = function defaultRule(id, config) { return _defineProperty({}, id, new Immutable.Map({ type: "rule", id: id, properties: defaultRuleProperties(config) })); }; export var defaultRoot = function defaultRoot(config) { var canAddDefaultRule = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; return new Immutable.Map({ type: "group", id: uuid(), children1: new Immutable.OrderedMap(canAddDefaultRule ? _objectSpread({}, defaultRule(uuid(), config)) : {}), properties: defaultGroupProperties(config) }); };