|
| 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 | +import { Component } from "@angular/core"; |
| 20 | +import { NgFor, NgIf } from "@angular/common"; |
| 21 | +import { FieldArrayType, FormlyFieldConfig, FormlyModule } from "@ngx-formly/core"; |
| 22 | + |
| 23 | +type UiUdfParameterColumn = Readonly<{ label: string; key: string; parentKey?: string; disabled: boolean }>; |
| 24 | + |
| 25 | +/** Renders inferred Python UDF UI parameters with editable values and locked name/type columns. */ |
| 26 | +@Component({ |
| 27 | + selector: "texera-ui-udf-parameters", |
| 28 | + templateUrl: "./ui-udf-parameters.component.html", |
| 29 | + styleUrls: ["./ui-udf-parameters.component.scss"], |
| 30 | + imports: [NgIf, NgFor, FormlyModule], |
| 31 | +}) |
| 32 | +export class UiUdfParametersComponent extends FieldArrayType<FormlyFieldConfig> { |
| 33 | + private readonly disabledStateConfigured = new WeakMap<FormlyFieldConfig, boolean>(); |
| 34 | + |
| 35 | + readonly fieldColumns: UiUdfParameterColumn[] = [ |
| 36 | + { label: "Value", key: "value", disabled: false }, |
| 37 | + { label: "Name", key: "attributeName", parentKey: "attribute", disabled: true }, |
| 38 | + { label: "Type", key: "attributeType", parentKey: "attribute", disabled: true }, |
| 39 | + ]; |
| 40 | + |
| 41 | + override onPopulate(field: FormlyFieldConfig): void { |
| 42 | + this.configureRowTemplate(this.getFieldArrayTemplate(field)); |
| 43 | + super.onPopulate(field); |
| 44 | + field.fieldGroup?.forEach(rowField => this.configureRowFields(rowField)); |
| 45 | + } |
| 46 | + |
| 47 | + /** Finds the Formly field config that backs one visible column in a parameter row. */ |
| 48 | + getColumnField(rowField: FormlyFieldConfig, column: UiUdfParameterColumn): FormlyFieldConfig | undefined { |
| 49 | + return this.getChildField(column.parentKey ? this.getChildField(rowField, column.parentKey) : rowField, column.key); |
| 50 | + } |
| 51 | + |
| 52 | + private getFieldArrayTemplate(field: FormlyFieldConfig): FormlyFieldConfig | undefined { |
| 53 | + return typeof field.fieldArray === "function" ? undefined : field.fieldArray; |
| 54 | + } |
| 55 | + |
| 56 | + private configureRowTemplate(rowField: FormlyFieldConfig | undefined): void { |
| 57 | + this.configureRowColumns(rowField, this.setDisabledMetadata.bind(this)); |
| 58 | + } |
| 59 | + |
| 60 | + private configureRowFields(rowField: FormlyFieldConfig | undefined): void { |
| 61 | + this.configureRowColumns(rowField, this.configureDisabledState.bind(this)); |
| 62 | + } |
| 63 | + |
| 64 | + private configureRowColumns( |
| 65 | + rowField: FormlyFieldConfig | undefined, |
| 66 | + configureColumn: (field: FormlyFieldConfig | undefined, disabled: boolean) => void |
| 67 | + ): void { |
| 68 | + if (!rowField) return; |
| 69 | + |
| 70 | + this.fieldColumns.forEach(column => configureColumn(this.getColumnField(rowField, column), column.disabled)); |
| 71 | + } |
| 72 | + |
| 73 | + private getChildField(rowField: FormlyFieldConfig | undefined, key: string): FormlyFieldConfig | undefined { |
| 74 | + return rowField?.fieldGroup?.find(fieldConfig => fieldConfig.key === key); |
| 75 | + } |
| 76 | + |
| 77 | + /** Sets Formly disabled metadata and keeps controls created later in sync through an onInit hook. */ |
| 78 | + private configureDisabledState(field: FormlyFieldConfig | undefined, disabled: boolean): void { |
| 79 | + if (!field) return; |
| 80 | + |
| 81 | + this.setDisabledMetadata(field, disabled); |
| 82 | + |
| 83 | + if (this.disabledStateConfigured.get(field) === disabled) { |
| 84 | + this.applyDisabledState(field, disabled); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const previousOnInit = field.hooks?.onInit; |
| 89 | + field.hooks = { |
| 90 | + ...(field.hooks ?? {}), |
| 91 | + onInit: initializedField => { |
| 92 | + previousOnInit?.(initializedField); |
| 93 | + this.applyDisabledState(initializedField, disabled); |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + this.disabledStateConfigured.set(field, disabled); |
| 98 | + this.applyDisabledState(field, disabled); |
| 99 | + } |
| 100 | + |
| 101 | + private setDisabledMetadata(field: FormlyFieldConfig | undefined, disabled: boolean): void { |
| 102 | + if (!field) return; |
| 103 | + |
| 104 | + field.props = { ...(field.props ?? {}), disabled }; |
| 105 | + |
| 106 | + // Keep deprecated templateOptions in sync for existing Formly wrappers that still read it. |
| 107 | + (field as any).templateOptions = { ...((field as any).templateOptions ?? {}), disabled }; |
| 108 | + } |
| 109 | + |
| 110 | + private applyDisabledState(field: FormlyFieldConfig, disabled: boolean): void { |
| 111 | + if (disabled) field.formControl?.disable({ emitEvent: false }); |
| 112 | + else field.formControl?.enable({ emitEvent: false }); |
| 113 | + } |
| 114 | + |
| 115 | + trackByParameterName = (index: number, parameter: any): string | number => { |
| 116 | + return parameter?.attribute?.attributeName ?? index; |
| 117 | + }; |
| 118 | +} |
0 commit comments