Skip to content

Commit

Permalink
respect maxNesting in rule-group during d-n-d
Browse files Browse the repository at this point in the history
  • Loading branch information
ukrbublik committed Nov 13, 2024
1 parent 4684bb7 commit e7e5877
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/core/modules/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ interface TreeUtils {
expandTreeSubpath(path: ImmutablePath, ...suffix: string[]): ImmutablePath;
fixEmptyGroupsInTree(tree: ImmutableTree): ImmutableTree;
fixPathsInTree(tree: ImmutableTree): ImmutableTree;
getFlatTree(tree: ImmutableTree): FlatTree;
getFlatTree(tree: ImmutableTree, config?: Config): FlatTree;
getTotalReordableNodesCountInTree(tree: ImmutableTree): number;
getTotalRulesCountInTree(tree: ImmutableTree): number;
isEmptyTree(tree: ImmutableTree): boolean;
Expand Down
11 changes: 10 additions & 1 deletion packages/core/modules/utils/treeUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Immutable from "immutable";
import {toImmutableList, isImmutable, applyToJS as immutableToJs} from "./stuff";
import {getFieldConfig} from "./configUtils";
import {jsToImmutable} from "../import/tree";
import uuid from "./uuid";

Expand Down Expand Up @@ -199,7 +200,7 @@ export const fixEmptyGroupsInTree = (tree) => {
* @param {Immutable.Map} tree
* @return {Object} {flat, items}
*/
export const getFlatTree = (tree) => {
export const getFlatTree = (tree, config) => {
let flat = [];
let items = {};
let cases = [];
Expand Down Expand Up @@ -230,6 +231,11 @@ export const getFlatTree = (tree) => {
const hasChildren = childrenIds?.length > 0;
const parentId = path.length ? path[path.length-1] : null;
const closestRuleGroupId = [...path].reverse().find(id => items[id].type == "rule_group");
const field = item.getIn(["properties", "field"]);
const fieldConfig = field && config && getFieldConfig(config, field);
const maxNesting = fieldConfig?.maxNesting;
const closestRuleGroupMaxNesting = items?.[closestRuleGroupId]?.maxNesting;
const closestRuleGroupLev = items?.[closestRuleGroupId]?.lev;
const currentCaseId = isCaseGroup ? id : caseId;

// Calculations before
Expand Down Expand Up @@ -271,6 +277,9 @@ export const getFlatTree = (tree) => {
caseId: currentCaseId,
caseNo,
closestRuleGroupId,
closestRuleGroupLev,
closestRuleGroupMaxNesting,
maxNesting,
path: path.concat(id),
lev: lev, // depth level (0 for root node)
atomicLev, // same as lev, but rules inside rule_group retains same number
Expand Down
4 changes: 2 additions & 2 deletions packages/core/modules/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export const _validateTree = (
const allErrors = [];
let flatItems, oldFlatItems;
if (includeItemsPositions) {
flatItems = getFlatTree(fixedTree).items;
flatItems = getFlatTree(fixedTree, config).items;
}
for (const id in meta.errors) {
let { path, errors } = meta.errors[id];
Expand Down Expand Up @@ -276,7 +276,7 @@ export const _validateTree = (
if (isDeleted) {
// get positions from old tree
if (!oldFlatItems) {
oldFlatItems = getFlatTree(tree).items;
oldFlatItems = getFlatTree(tree, config).items;
}
flatItem = oldFlatItems[id];
}
Expand Down
14 changes: 12 additions & 2 deletions packages/ui/modules/components/containers/SortableContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const createSortableContainer = (Builder, CanMoveFn = null) =>
}

onPropsChanged(nextProps) {
this.tree = getFlatTree(nextProps.tree);
this.tree = getFlatTree(nextProps.tree, nextProps.config);
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down Expand Up @@ -578,6 +578,11 @@ const createSortableContainer = (Builder, CanMoveFn = null) =>
// can't restruct `rule_group`
const isRuleGroupAffected = (fromII.type == "rule_group" || !!fromII.closestRuleGroupId || toII.type == "rule_group" || !!toII.closestRuleGroupId);
const targetRuleGroupId = isPend && toII.type == "rule_group" ? toII.id : toII.closestRuleGroupId;
const targetRuleGroupMaxNesting = isPend && toII.type == "rule_group" ? toII.maxNesting : toII.closestRuleGroupMaxNesting;
const closestRuleGroupLev = isPend && toII.type == "rule_group" ? toII.lev : toII.closestRuleGroupLev;
const newDepthLevInRuleGroup = (toParentII ? toParentII.lev + 1 : toII.lev)
+ (fromII.depth || (fromII.type == "group" ? 1 : 0))
- (closestRuleGroupLev || 0);
const isForbiddenRuleGroupChange = isRuleGroupAffected && fromII.closestRuleGroupId != targetRuleGroupId;
const isForbiddenCaseChange =
// can't move `case_group` anywhere but before/after anoter `case_group`
Expand All @@ -590,8 +595,13 @@ const createSortableContainer = (Builder, CanMoveFn = null) =>
const isForbiddenStructChange = isForbiddenCaseChange || isForbiddenRuleGroupChange;
const isLockedChange = toII.isLocked || fromII.isLocked || toParentII && toParentII.isLocked;

if (maxNesting && newDepthLev > maxNesting)
if (maxNesting && newDepthLev > maxNesting) {
return false;
}

if (targetRuleGroupMaxNesting && newDepthLevInRuleGroup > targetRuleGroupMaxNesting) {
return false;
}

if (isStructChange && (!canRegroup || isForbiddenStructChange || isLockedChange))
return false;
Expand Down

0 comments on commit e7e5877

Please sign in to comment.