Skip to content

Commit 8b0fb16

Browse files
authored
fix(frontend): hold the Python UDF parameter table's Name and Type cells locked when the panel enables its form group (apache#8848)
### What changes were proposed in this PR? Closes apache#8847. The Python UDF operator's "UI parameters" table (apache#5043) infers each parameter's name and type from the `self.UiParameter(...)` declarations in the operator's code, so the widget disables those two columns' controls as it builds each row: a name typed into the table does not reach the code, and the parameter's value then never reaches it either (checked on main: the table's name changes, the code's does not). The lock does not survive on the operator property panel. `setInteractivity(true)` enables the whole form group -- one task after every render, and again whenever the workflow becomes modifiable, for instance when a run ends -- and formly mirrors a control's state back into `props.disabled`, so the one-time disable is simply undone. Which state a given panel opening shows depends on which of the two ran last: measured on main, the cells were editable on 4 of 5 openings, and always after a run. The Form View, which never enables a group wholesale, has always shown them locked, so the two views disagreed as well. The widget now holds its own lock. On each cell it locks it registers Angular's disabled-change hook of the control (`registerOnDisabledChange`, the same hook formly's mirror and the input's value accessor use): when a host enables the control, the hook disables it again in the microtask after that enable, and announces the status change as the enable did. Both details matter and were found on a running instance: - deferred to the microtask, because the input's value accessor is one more hook on the same control, registered after this one when the cell renders; disabling from inside the enable's own round of hooks left the accessor having been told "enabled" last, so the box was editable over a locked control; - announced rather than silent (`disable()`, not `disable({emitEvent: false})`), because ng-zorro's `nz-input` paints its `disabled` attribute from the control's status changes; a silent disable left it painting the box editable again at the next change detection. A cell the widget configures editable -- the Value column, or any cell once reconfigured -- is released from the hold, so whether it is editable stays the host's call: a read-only panel still locks everything, and a host that disables the whole field still reaches the Value cell. The frame is untouched: two other approaches were tried first and dropped, re-disabling from the frame after `enable()` (formly's mirror has already set `props.disabled` to false by then) and a formly expression on `props.disabled` (formly re-evaluates expressions on model changes only, and registers them at populate, so `enable()` never triggers one). ### Any related issues, documentation, discussions? Closes apache#8847. Split out of apache#8846 (the same table under the Form View's renames and hides), which is based on main and does not touch this; the two are independent and can land in either order. The table's fixed 250px columns clip Name and Type at the panel's default 260px width, which is why the cells have to be brought into view to see any of this; that is a separate layout bug, filed separately. ### How was this PR tested? Unit tests (vitest). The widget's spec: a locked cell's control is held locked when a host enables it, in the microtask after the enable and announcing the status change, while the Value cell is left to the host; a cell configured editable again is released from the hold; a cell the host disables again before the microtask is left alone; a control that is not a `FormControl` gets no hook, and a control is hooked once however often it is configured. A second spec stands the table up under real formly (`FormlyModule.forRoot`, a form, the builder row template formly-jsonschema produces, `nz-input` cells as in the app): the rows build with Name and Type locked and Value editable; a wholesale `form.enable()`, as the panel does, leaves Name and Type locked, read from the inputs' `disabled` state and not only from the controls'; a read-only `form.disable()` locks everything and a later `enable()` gives only the Value cell back. Each guard was deletion-checked and turned a named test red. eslint, prettier and the production (AOT) build pass; the full frontend suite passes; every statement, function and branch this PR adds is covered. Checked on a local instance (a Python source feeding a Python UDF whose code declares two UI parameters), the panel widened so the columns are in view. On main the cells are editable on most openings (4 of 5) and after every run; with this PR they stayed locked on every opening and through a forced `setInteractivity(true)` followed by real change-detection passes, read from the DOM as well as from the controls. The Value cell follows the host in both. #### Video **Before** https://github.com/user-attachments/assets/53ac4826-b5ec-4852-a5c9-22685306839d **After** https://github.com/user-attachments/assets/9bcc2a52-fdb3-4764-99cb-63a31a18fa44 ### Was this PR authored or co-authored using generative AI tooling? Yes. Generated-by: Claude Code (Anthropic). Co-authored with Claude, reviewed line by line by the author before submission. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 312a0ce commit 8b0fb16

3 files changed

Lines changed: 290 additions & 3 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { Component } from "@angular/core";
21+
import { TestBed } from "@angular/core/testing";
22+
import { ReactiveFormsModule, UntypedFormGroup } from "@angular/forms";
23+
import { FieldType, FieldTypeConfig, FormlyFieldConfig, FormlyForm, FormlyModule } from "@ngx-formly/core";
24+
import { NzInputModule } from "ng-zorro-antd/input";
25+
import { NotificationService } from "../../../common/service/notification/notification.service";
26+
import { UiUdfParametersSyncService } from "../../service/code-editor/ui-udf-parameters-sync.service";
27+
import { WorkflowActionService } from "../../service/workflow-graph/model/workflow-action.service";
28+
import { UiUdfParametersComponent } from "./ui-udf-parameters.component";
29+
30+
/**
31+
* The cell formly builds a string column into: an nz-input bound to the control, as in the app.
32+
* nz-input paints its disabled attribute from the control's status changes, so a lock the control
33+
* carries silently is not one the reader sees -- which is why these tests read the inputs.
34+
*/
35+
@Component({
36+
template: '<input nz-input [formControl]="formControl" />',
37+
imports: [ReactiveFormsModule, NzInputModule],
38+
})
39+
class InputCellType extends FieldType<FieldTypeConfig> {}
40+
41+
/** A form holding one parameter table, with the builder row template formly-jsonschema produces. */
42+
@Component({
43+
template:
44+
'<form [formGroup]="form"><formly-form [form]="form" [model]="model" [fields]="fields"></formly-form></form>',
45+
imports: [ReactiveFormsModule, FormlyForm],
46+
})
47+
class HostComponent {
48+
form = new UntypedFormGroup({});
49+
model = {
50+
uiParameters: [
51+
{ value: "0.5", attribute: { attributeName: "threshold", attributeType: "double" } },
52+
{ value: "high", attribute: { attributeName: "label", attributeType: "string" } },
53+
],
54+
};
55+
fields: FormlyFieldConfig[] = [
56+
{
57+
key: "uiParameters",
58+
type: "ui-udf-parameters",
59+
fieldArray: () => ({
60+
fieldGroup: [
61+
{ key: "value", type: "plain" },
62+
{
63+
key: "attribute",
64+
fieldGroup: [
65+
{ key: "attributeName", type: "plain" },
66+
{ key: "attributeType", type: "plain" },
67+
],
68+
},
69+
],
70+
}),
71+
},
72+
];
73+
}
74+
75+
/**
76+
* Real formly, no mocks of it: the operator property panel enables and disables its whole form
77+
* group (setInteractivity), and formly mirrors a control's state into props.disabled, so the
78+
* widget's lock on the Name and Type columns has to survive a wholesale enable(). The widget's own
79+
* spec drives populate on hand-made rows; this one proves the lock under formly's own build.
80+
*/
81+
describe("UiUdfParametersComponent under real formly", () => {
82+
beforeEach(async () => {
83+
await TestBed.configureTestingModule({
84+
imports: [
85+
HostComponent,
86+
FormlyModule.forRoot({
87+
types: [
88+
{ name: "ui-udf-parameters", component: UiUdfParametersComponent },
89+
{ name: "plain", component: InputCellType },
90+
],
91+
}),
92+
],
93+
providers: [
94+
{
95+
provide: WorkflowActionService,
96+
useValue: {
97+
checkWorkflowModificationEnabled: () => true,
98+
getJointGraphWrapper: () => ({ getCurrentHighlightedOperatorIDs: () => ["op-1"] }),
99+
},
100+
},
101+
{ provide: UiUdfParametersSyncService, useValue: { addParameter: vi.fn() } },
102+
{ provide: NotificationService, useValue: { error: vi.fn() } },
103+
],
104+
}).compileComponents();
105+
});
106+
107+
function render() {
108+
const fixture = TestBed.createComponent(HostComponent);
109+
fixture.detectChanges();
110+
const form = fixture.componentInstance.form;
111+
const cell = (row: number, path: string) => form.get(`uiParameters.${row}.${path}`)!;
112+
const locks = () => [
113+
cell(0, "value").disabled,
114+
cell(0, "attribute.attributeName").disabled,
115+
cell(0, "attribute.attributeType").disabled,
116+
cell(1, "attribute.attributeName").disabled,
117+
];
118+
return { fixture, form, locks };
119+
}
120+
121+
it("builds the rows with the Name and Type cells locked and the Value cell editable", () => {
122+
const { locks } = render();
123+
124+
expect(locks()).toEqual([false, true, true, true]);
125+
});
126+
127+
// The lock goes back on in the microtask after the host's enable() (see keepLocked), so the
128+
// assertions wait one; what the reader sees is read from the inputs, not only the controls.
129+
const settled = async (fixture: { detectChanges(): void }) => {
130+
await Promise.resolve();
131+
fixture.detectChanges();
132+
};
133+
const inputsDisabled = (fixture: { nativeElement: HTMLElement }) =>
134+
Array.from(fixture.nativeElement.querySelectorAll<HTMLInputElement>(".ui-udf-parameter-row:not(.header) input"))
135+
.slice(0, 3)
136+
.map(input => input.disabled);
137+
138+
it("keeps the Name and Type cells locked when the host enables the whole form group, as the panel does", async () => {
139+
const { fixture, form, locks } = render();
140+
141+
form.enable();
142+
await settled(fixture);
143+
144+
expect(locks()).toEqual([false, true, true, true]);
145+
expect(inputsDisabled(fixture)).toEqual([false, true, true]);
146+
});
147+
148+
it("locks everything for a read-only host and gives only the Value cell back on enable", async () => {
149+
const { fixture, form, locks } = render();
150+
151+
form.disable();
152+
await settled(fixture);
153+
expect(locks()).toEqual([true, true, true, true]);
154+
expect(inputsDisabled(fixture)).toEqual([true, true, true]);
155+
156+
form.enable();
157+
await settled(fixture);
158+
expect(locks()).toEqual([false, true, true, true]);
159+
expect(inputsDisabled(fixture)).toEqual([false, true, true]);
160+
});
161+
});

‎frontend/src/app/workspace/component/ui-udf-parameters/ui-udf-parameters.component.spec.ts‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
import { FormControl, UntypedFormArray } from "@angular/forms";
20+
import { FormControl, FormGroup, UntypedFormArray } from "@angular/forms";
2121
import { ComponentFixture, TestBed } from "@angular/core/testing";
2222
import { By } from "@angular/platform-browser";
2323
import { FormlyFieldConfig } from "@ngx-formly/core";
@@ -130,6 +130,88 @@ describe("UiUdfParametersComponent", () => {
130130
});
131131
});
132132

133+
// The operator property panel enables its whole form group when it turns interactive, and formly
134+
// mirrors a control's state into props.disabled, so a one-time disable of the Name and Type cells
135+
// did not survive there (the Form View, which never enables a group wholesale, showed them
136+
// locked). A locked cell's control is held through Angular's disabled-change hook.
137+
it("holds a locked cell locked when the host enables its control, and leaves the Value cell to the host", async () => {
138+
const valueControl = new FormControl("42");
139+
const nameControl = new FormControl("threshold");
140+
const typeControl = new FormControl("double");
141+
const rowField = rowConfig([
142+
{ key: "value", formControl: valueControl },
143+
{ key: "attributeName", formControl: nameControl },
144+
{ key: "attributeType", formControl: typeControl },
145+
]);
146+
component.onPopulate({ model: [{}], fieldGroup: [rowField] } as FormlyFieldConfig);
147+
expect(nameControl.disabled).toBe(true);
148+
const nameStatus = vitest.fn();
149+
nameControl.statusChanges.subscribe(nameStatus);
150+
151+
nameControl.enable();
152+
typeControl.enable();
153+
valueControl.disable();
154+
valueControl.enable();
155+
// The lock goes back on in the microtask after the host's enable() has told every hook,
156+
// the input's value accessor included; see keepLocked.
157+
expect(nameControl.enabled).toBe(true);
158+
await Promise.resolve();
159+
160+
expect(nameControl.disabled).toBe(true);
161+
expect(typeControl.disabled).toBe(true);
162+
expect(valueControl.enabled).toBe(true);
163+
// Announced like the enable() was: the cell's nz-input paints its disabled attribute from it.
164+
expect(nameStatus.mock.calls.map(call => call[0])).toEqual(["VALID", "DISABLED"]);
165+
});
166+
167+
it("locks a cell whose control is not a FormControl without a hold hook, and does not register twice", () => {
168+
const group = new FormGroup({ inner: new FormControl("x") });
169+
(component as any).applyDisabledState({ formControl: group }, true);
170+
expect(group.disabled).toBe(true);
171+
172+
const nameControl = new FormControl("threshold");
173+
const register = vitest.spyOn(nameControl, "registerOnDisabledChange");
174+
(component as any).applyDisabledState({ formControl: nameControl }, true);
175+
(component as any).applyDisabledState({ formControl: nameControl }, true);
176+
expect(register).toHaveBeenCalledTimes(1);
177+
});
178+
179+
it("releases a cell from the hold once it is configured editable again", async () => {
180+
const nameControl = new FormControl("threshold");
181+
const rowField = rowConfig([
182+
{ key: "value", formControl: new FormControl("42") },
183+
{ key: "attributeName", formControl: nameControl },
184+
{ key: "attributeType", formControl: new FormControl("double") },
185+
]);
186+
component.onPopulate({ model: [{}], fieldGroup: [rowField] } as FormlyFieldConfig);
187+
expect(nameControl.disabled).toBe(true);
188+
189+
(component as any).applyDisabledState(component.getColumnField(rowField, component.fieldColumns[1]), false);
190+
nameControl.disable();
191+
nameControl.enable();
192+
await Promise.resolve();
193+
194+
expect(nameControl.enabled).toBe(true);
195+
});
196+
197+
it("leaves a locked cell alone when the host has disabled it again before the microtask", async () => {
198+
const nameControl = new FormControl("threshold");
199+
const rowField = rowConfig([
200+
{ key: "value", formControl: new FormControl("42") },
201+
{ key: "attributeName", formControl: nameControl },
202+
{ key: "attributeType", formControl: new FormControl("double") },
203+
]);
204+
component.onPopulate({ model: [{}], fieldGroup: [rowField] } as FormlyFieldConfig);
205+
const disable = vitest.spyOn(nameControl, "disable");
206+
207+
nameControl.enable();
208+
nameControl.disable({ emitEvent: false });
209+
await Promise.resolve();
210+
211+
expect(disable).toHaveBeenCalledTimes(1);
212+
expect(nameControl.disabled).toBe(true);
213+
});
214+
133215
it("should edit a row that names a resource with that resource's browser, and leave others alone", () => {
134216
const columns = () =>
135217
rowConfig([

‎frontend/src/app/workspace/component/ui-udf-parameters/ui-udf-parameters.component.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
import { Component } from "@angular/core";
2020
import { NgFor, NgIf } from "@angular/common";
21+
import { FormControl, type AbstractControl } from "@angular/forms";
2122
import { FieldArrayType, FormlyFieldConfig, FormlyModule } from "@ngx-formly/core";
2223
import { NzButtonComponent } from "ng-zorro-antd/button";
2324
import { NzWaveDirective } from "ng-zorro-antd/core/wave";
@@ -62,6 +63,9 @@ export class UiUdfParametersComponent extends FieldArrayType<FormlyFieldConfig>
6263
private readonly disabledStateConfigured = new WeakMap<FormlyFieldConfig, boolean>();
6364
// The resource each row's value editor was configured for.
6465
private readonly rowResources = new WeakMap<FormlyFieldConfig, string>();
66+
// The controls of the cells currently locked, and the controls that carry the hold hook (see keepLocked).
67+
private readonly lockedControls = new WeakSet<AbstractControl>();
68+
private readonly hookedControls = new WeakSet<AbstractControl>();
6569

6670
readonly fieldColumns: UiUdfParameterColumn[] = [
6771
VALUE_COLUMN,
@@ -208,8 +212,48 @@ export class UiUdfParametersComponent extends FieldArrayType<FormlyFieldConfig>
208212
}
209213

210214
private applyDisabledState(field: FormlyFieldConfig, disabled: boolean): void {
211-
if (disabled) field.formControl?.disable({ emitEvent: false });
212-
else field.formControl?.enable({ emitEvent: false });
215+
const control = field.formControl;
216+
if (!control) return;
217+
if (disabled) {
218+
control.disable({ emitEvent: false });
219+
this.keepLocked(control);
220+
} else {
221+
this.lockedControls.delete(control);
222+
control.enable({ emitEvent: false });
223+
}
224+
}
225+
226+
/**
227+
* Hold a locked cell locked under any host. The operator property panel enables its whole form
228+
* group when it turns interactive (and again after a run), and formly mirrors a control's state
229+
* back into props.disabled, so a one-time disable did not survive there: the Name and Type cells
230+
* came back editable on the canvas, while the Form View, which never enables a group wholesale,
231+
* showed them locked. Angular tells a control's disabled-change hooks about every enable() and
232+
* disable(), whoever calls them (formly's mirror is such a hook), so the one registered here
233+
* disables the control again once a host has enabled it, and formly's mirror then records the
234+
* lock in props.disabled as well. The disabling waits for the microtask after the enable(): the
235+
* input's own value accessor is one more of these hooks, registered after this one when the cell
236+
* renders, and had the control been disabled from inside the enable()'s round of hooks, the
237+
* accessor would still have been told "enabled" last and the box would have shown editable over a
238+
* locked control. The disabling also announces itself (statusChanges), as the enable() did: the
239+
* cell's nz-input paints its disabled attribute from the status it was last told, and told only
240+
* "enabled" it would paint the box editable again at the next change detection. Registered once
241+
* per control, for good; a cell configured editable again leaves lockedControls
242+
* (applyDisabledState) and the hook lets it be.
243+
*/
244+
private keepLocked(control: AbstractControl): void {
245+
this.lockedControls.add(control);
246+
// The hook is a FormControl's (a cell's control always is one; a group has none to register).
247+
if (!(control instanceof FormControl) || this.hookedControls.has(control)) return;
248+
this.hookedControls.add(control);
249+
control.registerOnDisabledChange((isDisabled: boolean) => {
250+
if (isDisabled) return;
251+
queueMicrotask(() => {
252+
if (control.enabled && this.lockedControls.has(control)) {
253+
control.disable();
254+
}
255+
});
256+
});
213257
}
214258

215259
trackByParameterName = (index: number, parameter: any): string | number => {

0 commit comments

Comments
 (0)