From 5a5947d1b04a5cda0c8967a197bd1b4c73f8cf39 Mon Sep 17 00:00:00 2001 From: Matt Gilman Date: Tue, 30 Apr 2024 22:22:53 -0400 Subject: [PATCH] NIFI-13110: (#8719) - Auto-selecting the relationship when the source processor only offers a single relationship. This closes #8719 --- .../source-processor.component.ts | 85 +++++++++++++------ 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/pages/flow-designer/ui/canvas/items/connection/source/source-processor/source-processor.component.ts b/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/pages/flow-designer/ui/canvas/items/connection/source/source-processor/source-processor.component.ts index c6c2cb3b17..c9ff303dc7 100644 --- a/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/pages/flow-designer/ui/canvas/items/connection/source/source-processor/source-processor.component.ts +++ b/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/pages/flow-designer/ui/canvas/items/connection/source/source-processor/source-processor.component.ts @@ -20,6 +20,7 @@ import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/f import { MatCheckboxModule } from '@angular/material/checkbox'; import { Relationship } from '../../../../../../state/flow'; +import { NiFiCommon } from '../../../../../../../../service/nifi-common.service'; export interface RelationshipItem { relationshipName: string; @@ -53,8 +54,8 @@ export class SourceProcessor implements ControlValueAccessor { isDisabled = false; isTouched = false; - onTouched!: () => void; - onChange!: (selectedRelationships: string[]) => void; + onTouched: (() => void) | null = null; + onChange: ((selectedRelationships: string[]) => void) | null = null; name!: string; relationships!: Relationship[]; @@ -62,36 +63,62 @@ export class SourceProcessor implements ControlValueAccessor { relationshipItems!: RelationshipItem[]; selectedRelationships!: string[]; - processRelationships(): void { - if (this.relationships && this.selectedRelationships) { - this.relationshipItems = this.relationships.map((relationship) => { - return { - relationshipName: relationship.name, - selected: this.selectedRelationships.includes(relationship.name), - available: true - }; - }); + constructor(private nifiCommon: NiFiCommon) {} - const unavailableRelationships: string[] = this.selectedRelationships.filter( - (selectedRelationship) => - !this.relationships.some((relationship) => relationship.name == selectedRelationship) - ); - unavailableRelationships.forEach((unavailableRelationship) => { - this.relationshipItems.push({ - relationshipName: unavailableRelationship, - selected: true, - available: false + processRelationships(): void { + if (this.relationships) { + if (this.nifiCommon.isEmpty(this.selectedRelationships)) { + this.relationshipItems = this.relationships.map((relationship) => { + return { + relationshipName: relationship.name, + selected: this.relationships.length === 1, + available: true + }; }); - }); + + this.considerDefaultSelection(); + } else { + this.relationshipItems = this.relationships.map((relationship) => { + return { + relationshipName: relationship.name, + selected: this.selectedRelationships.includes(relationship.name), + available: true + }; + }); + + const unavailableRelationships: string[] = this.selectedRelationships.filter( + (selectedRelationship) => + !this.relationships.some((relationship) => relationship.name == selectedRelationship) + ); + unavailableRelationships.forEach((unavailableRelationship) => { + this.relationshipItems.push({ + relationshipName: unavailableRelationship, + selected: true, + available: false + }); + }); + } + } + } + + considerDefaultSelection(): void { + const callbacksConfigured: boolean = this.onChange != null && this.onTouched != null; + const autoSelected: boolean = + this.relationships?.length === 1 && this.nifiCommon.isEmpty(this.selectedRelationships); + + if (callbacksConfigured && autoSelected) { + this.handleChanged(); } } registerOnChange(onChange: (selectedPrioritizers: string[]) => void): void { this.onChange = onChange; + this.considerDefaultSelection(); } registerOnTouched(onTouch: () => void): void { this.onTouched = onTouch; + this.considerDefaultSelection(); } setDisabledState(isDisabled: boolean): void { @@ -104,14 +131,16 @@ export class SourceProcessor implements ControlValueAccessor { } handleChanged() { - // mark the component as touched if not already - if (!this.isTouched) { - this.isTouched = true; - this.onTouched(); - } + if (this.onTouched && this.onChange) { + // mark the component as touched if not already + if (!this.isTouched) { + this.isTouched = true; + this.onTouched(); + } - // emit the changes - this.onChange(this.serializeSelectedRelationships()); + // emit the changes + this.onChange(this.serializeSelectedRelationships()); + } } private serializeSelectedRelationships(): string[] {