Skip to content

[pull] master from axelor:master #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## 7.3.3 (2025-03-12)

#### Fix

* Revert json field processing improvements

<details>

Previous version introduce json field processing improvements. Due to the nature of the change, this introduced
issue in json fields behavior. This has been reverted and will be re-added on further version.

</details>


## 7.3.2 (2025-03-12)

#### Feature
Expand Down
66 changes: 38 additions & 28 deletions axelor-front/src/services/client/meta-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,18 +520,13 @@ export function processView(
}
}

[
"canNew",
"canView",
"canEdit",
"canRemove",
"canSelect",
"canDelete",
].forEach((name) => {
if (item[name] === "false" || item[name] === "true") {
item[name] = item[name] === "true";
}
});
["canNew", "canView", "canEdit", "canRemove", "canSelect", "canDelete"].forEach(
(name) => {
if (item[name] === "false" || item[name] === "true") {
item[name] = item[name] === "true";
}
},
);

if (item.items) {
processView(meta, item, view);
Expand Down Expand Up @@ -581,19 +576,15 @@ export function processView(
flexbox: true,
items: [],
};
let jsonFields: JsonField[] = item.jsonFields ?? [];

jsonFields = item.jsonFields.filter(
let panel: Schema | null = null;
let panelTab: Schema | null = null;
item.jsonFields = item.jsonFields.filter(
(x: JsonField) => !(x as JsonField).forceHidden,
);
jsonFields.sort((x: Schema, y: Schema) => {
item.jsonFields.sort((x: Schema, y: Schema) => {
return x.sequence - y.sequence;
});
jsonFields.forEach((field: Schema) => {
if (field.nameField) {
field.nameColumn = field.nameField;
}

item.jsonFields.forEach((field: Schema) => {
if (field.widgetAttrs) {
if (typeof field.widgetAttrs === "string") {
field.widgetAttrs = JSON.parse(field.widgetAttrs);
Expand Down Expand Up @@ -623,12 +614,27 @@ export function processView(
if (field.type === "panel" || field.type === "separator") {
field.visibleInGrid = false;
}
if (field.type === "panel") {
panel = { ...field, items: [] };
if ((field.widgetAttrs || {}).sidebar && parent) {
panel.sidebar = true;
parent.width = "large";
}
if ((field.widgetAttrs || {}).tab) {
panelTab = panelTab || {
type: "panel-tabs",
colSpan: 12,
items: [],
};
panelTab.items?.push(panel);
} else {
editor.items?.push(panel);
}
return;
}
if (field.type !== "separator") {
field.title = field.title || field.autoTitle;
}
if (field.type === "panel" && field.widgetAttrs?.sidebar && parent) {
parent.width = "large";
}
const colSpan = (field.widgetAttrs || {}).colSpan || field.colSpan;
if (field.type === "one-to-many") {
field.type = "many-to-many";
Expand All @@ -640,12 +646,16 @@ export function processView(
) {
field.colSpan = colSpan || 12;
}
if (panel) {
panel.items?.push(field);
} else {
editor.items?.push(field);
}
});

item.jsonFields = jsonFields.reduce(
(acc, x) => ({ ...acc, [x.name]: x }),
{},
);
if (panelTab) {
editor.items?.push(panelTab);
}

if (isFormField) {
item.widget = "json-field";
Expand Down
2 changes: 0 additions & 2 deletions axelor-front/src/services/client/meta.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,6 @@ export interface JsonField extends Omit<Field, "type"> {
columnSequence: number;
visibleInGrid?: boolean;

nameField?: boolean;

jsonTarget?: string;
jsonField?: string;
jsonPath?: string;
Expand Down
151 changes: 16 additions & 135 deletions axelor-front/src/views/form/builder/form-editors.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Box, clsx } from "@axelor/ui";
import { ScopeProvider } from "bunshi/react";
import { SetStateAction, atom, useAtom, useAtomValue, useSetAtom } from "jotai";
import { ScopeProvider } from "bunshi/react";
import { atomFamily, selectAtom, useAtomCallback } from "jotai/utils";
import filter from "lodash/filter";
import getObjValue from "lodash/get";
import isEqual from "lodash/isEqual";
import isNumber from "lodash/isNumber";
import filter from "lodash/filter";
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";

import { MaterialIcon } from "@axelor/ui/icons/material-icon";
Expand All @@ -18,8 +18,8 @@ import { i18n } from "@/services/client/i18n";
import { ViewData } from "@/services/client/meta";
import {
Editor,
Field,
FormView,
JsonField,
Panel,
Property,
Schema,
Expand Down Expand Up @@ -63,71 +63,6 @@ export type FormEditorProps = FieldEditorProps & {
fields: Record<string, Property>;
};

/**
* For the given json field, check if the context field value match with the record data.
*
* @param field the json field
* @param record the record to match against
*/
function isContextFieldMatch(field: JsonField, record: DataRecord): boolean {
if (field.contextField) {
return (
String(field.contextFieldValue) === String(record[field.contextField]?.id)
);
}
return true;
}

function createJsonEditor(
schema: Schema,
jsonFields: Record<string, JsonField>,
) {
const editor: Schema = {
layout: schema.type === "panel-json" ? "table" : undefined,
flexbox: true,
jsonFields,
items: [],
};

let panel: Schema | undefined = undefined;
let panelTab: Schema | undefined = undefined;

for (const field of Object.values(jsonFields)) {
if (field.type === "panel") {
panel = { ...field, items: [] };
if ((field.widgetAttrs || {}).sidebar && parent) {
panel.sidebar = true;
}
if ((field.widgetAttrs || {}).tab) {
panelTab = panelTab || {
type: "panel-tabs",
colSpan: 12,
items: [],
};
panelTab.items?.push(panel);
} else {
editor.items?.push(panel);
}
continue;
}
if (panel) {
panel.items?.push(field);
} else {
editor.items?.push(field);
}
}

if (panelTab) {
editor.items?.push(panelTab);
}

if (!schema.viewer) {
editor.viewer = true;
}

return editor as FormView;
}

function processEditor(schema: Schema) {
const editor: Editor = schema.editor;
const widgetAttrs = editor.widgetAttrs ?? {};
Expand Down Expand Up @@ -1197,72 +1132,7 @@ const RecordEditor = memo(function RecordEditor({
);
});

function JsonEditor(props: FormEditorProps) {
const { schema, formAtom } = props;

const fieldsAtom = useMemo(
() => selectAtom(formAtom, (o) => o.fields),
[formAtom],
);

const modelFields = useAtomValue(fieldsAtom);
const jsonFields: Record<string, JsonField> = useMemo(
() => schema.jsonFields ?? {},
[schema.jsonFields],
);

const editorFieldsRef = useRef<Record<string, JsonField>>({});
const editorFieldsAtom = useMemo(() => {
return atom((get) => {
if (
Object.values(jsonFields)
.filter((x) => x.contextField)
.map((x) => x.contextField!)
.filter((x) => modelFields[x])
.filter((x, i, a) => a.indexOf(x) === i).length === 0
) {
return jsonFields;
}
const record = get(formAtom).record;
let lastPanelExcluded = false;
const nextFields = Object.entries(jsonFields)
.filter(([_, field]) => {
if (field.type === "panel") {
const res = isContextFieldMatch(field, record);
lastPanelExcluded = !res;
return res;
} else if (lastPanelExcluded) {
return false;
}

return isContextFieldMatch(field, record);
})
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});

// if fields changed, update the ref
const currentValues = Object.values(editorFieldsRef.current);
const nextValues = Object.values(nextFields);
if (
currentValues.length !== nextValues.length ||
currentValues.some((x, i) => x !== nextValues[i])
) {
editorFieldsRef.current = nextFields;
}

return editorFieldsRef.current;
});
}, [formAtom, jsonFields, modelFields]);

const editorFields = useAtomValue(editorFieldsAtom);
const editor = useMemo(
() => createJsonEditor(schema, editorFields),
[editorFields, schema],
);

return <JsonEditorInner {...props} editor={editor} />;
}

function JsonEditorInner({
function JsonEditor({
schema,
editor,
fields,
Expand All @@ -1279,7 +1149,7 @@ function JsonEditorInner({
);
const model = useAtomValue(modelAtom);
const jsonModel = schema.jsonModel;
const jsonFields: Record<string, JsonField> = schema.jsonFields ?? {};
const jsonFields = processJsonFields(schema);
const jsonNameField = Object.values(jsonFields).find((x) => x.nameColumn);
const jsonValueRef = useRef<DataRecord>();

Expand Down Expand Up @@ -1371,6 +1241,17 @@ function JsonEditorInner({
);
}

function processJsonFields(schema: Schema) {
const fields: Record<string, Schema> = schema.jsonFields ?? {};
return Object.entries(fields).reduce((acc, [k, v]) => {
const { nameField: nameColumn, ...field } = v;
return {
...acc,
[k]: nameColumn ? { ...field, nameColumn } : field,
};
}, {}) as Record<string, Field>;
}

function processJsonView(schema: Schema, jsonFields: any) {
const result = { ...schema, $json: true } as Schema;

Expand Down
29 changes: 17 additions & 12 deletions axelor-front/src/views/form/builder/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uniqueId from "lodash/uniqueId";
import isUndefined from "lodash/isUndefined";
import pick from "lodash/pick";
import uniq from "lodash/uniq";
import uniqueId from "lodash/uniqueId";

import { DataContext, DataRecord } from "@/services/client/data.types";
import {
Expand All @@ -15,8 +15,8 @@ import {
View,
Widget,
} from "@/services/client/meta.types";
import { getJSON } from "@/utils/data-record";
import { toCamelCase, toKebabCase, toSnakeCase } from "@/utils/names";
import { getJSON } from "@/utils/data-record";

import {
getBaseDummy,
Expand All @@ -27,8 +27,8 @@ import { moment } from "@/services/client/l10n.ts";
import { MetaData, ViewData } from "@/services/client/meta";
import { LoadingCache } from "@/utils/cache";
import convert from "@/utils/convert";
import { findViewItems } from "@/utils/schema";
import { Attrs, DEFAULT_ATTRS, FormState } from "./types";
import { findViewItems } from "@/utils/schema";

import * as WIDGETS from "../widgets";

Expand Down Expand Up @@ -251,21 +251,19 @@ export function processContextValues(
values[fieldName] = value
? compactJson(value, {
findItem: (jsonPath: string) => {
function findTargetNames(schema: Schema): string[] {
function findTargetNames(schema: Schema): Schema[] {
const items =
schema.type !== "panel-related" ? (schema.items ?? []) : [];
const nested = items.flatMap((item) => findTargetNames(item));
return [
...items
.filter((item) => item.json && item.name === fieldName)
.map((item) => {
const itemFields: Record<string, JsonField> =
item.jsonFields ?? {};
return Object.values(itemFields).find(
(jsonItem: JsonField) => jsonItem.name === jsonPath,
)?.targetName as string;
})
.filter(Boolean),
.map(
(item) =>
item.jsonFields?.find(
(jsonItem: JsonField) => jsonItem.name === jsonPath,
)?.targetName,
),
...nested,
];
}
Expand Down Expand Up @@ -435,6 +433,13 @@ export function processView(
);
}

if (Array.isArray(res.jsonFields)) {
res.jsonFields = res.jsonFields.reduce(
(prev, field) => ({ ...prev, [field.name!]: field }),
{},
);
}

return res;
}

Expand Down
Loading