Skip to content
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

feat(FieldInteractionApi): Make addControl work for novo control groups #1556

Closed
wants to merge 2 commits into from
Closed
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
136 changes: 96 additions & 40 deletions projects/novo-elements/src/elements/form/FieldInteractionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,49 +813,53 @@ export class FieldInteractionApi {
let fieldsetIndex: number;
let controlIndex: number;
if (control) {
fieldsetIndex = -1;
controlIndex = -1;

form.fieldsets.forEach((fieldset, fi) => {
fieldset.controls.forEach((fieldsetControl, ci) => {
if (fieldsetControl.key === key) {
fieldsetIndex = fi;
controlIndex = ci;
}
if (!form.fieldsets) {
this.addControlForFormGroup(key, metaForNewField, position, initialValue, form);
} else {
fieldsetIndex = -1;
controlIndex = -1;

form.fieldsets.forEach((fieldset, fi) => {
fieldset.controls.forEach((fieldsetControl, ci) => {
if (fieldsetControl.key === key) {
fieldsetIndex = fi;
controlIndex = ci;
}
});
});
});

// Change the position of the newly added field
switch (position) {
case FieldInteractionApi.FIELD_POSITIONS.ABOVE_FIELD:
// Adding field above active field
// index can stay the same
break;
case FieldInteractionApi.FIELD_POSITIONS.BELOW_FIELD:
// Adding field below active field
controlIndex += 1;
break;
case FieldInteractionApi.FIELD_POSITIONS.TOP_OF_FORM:
// Adding field to the top of the form
controlIndex = 0;
fieldsetIndex = 0;
break;
case FieldInteractionApi.FIELD_POSITIONS.BOTTOM_OF_FORM:
// Adding field to the bottom of the form
fieldsetIndex = form.fieldsets.length - 1;
controlIndex = form.fieldsets[fieldsetIndex].controls.length;
break;
default:
break;
}
// Change the position of the newly added field
switch (position) {
case FieldInteractionApi.FIELD_POSITIONS.ABOVE_FIELD:
// Adding field above active field
// index can stay the same
break;
case FieldInteractionApi.FIELD_POSITIONS.BELOW_FIELD:
// Adding field below active field
controlIndex += 1;
break;
case FieldInteractionApi.FIELD_POSITIONS.TOP_OF_FORM:
// Adding field to the top of the form
controlIndex = 0;
fieldsetIndex = 0;
break;
case FieldInteractionApi.FIELD_POSITIONS.BOTTOM_OF_FORM:
// Adding field to the bottom of the form
fieldsetIndex = form.fieldsets.length - 1;
controlIndex = form.fieldsets[fieldsetIndex].controls.length;
break;
default:
break;
}

if (fieldsetIndex !== -1 && controlIndex !== -1) {
const novoControl = this.formUtils.getControlForField(metaForNewField, this.http, {});
novoControl.hidden = false;
const formControl = new NovoFormControl(initialValue, novoControl);
form.addControl(novoControl.key, formControl);
form.fieldsets[fieldsetIndex].controls.splice(controlIndex, 0, novoControl);
this.triggerEvent({ controlKey: key, prop: 'addControl', value: formControl }, otherForm);
if (fieldsetIndex !== -1 && controlIndex !== -1) {
const novoControl = this.formUtils.getControlForField(metaForNewField, this.http, {});
novoControl.hidden = false;
const formControl = new NovoFormControl(initialValue, novoControl);
form.addControl(novoControl.key, formControl);
form.fieldsets[fieldsetIndex].controls.splice(controlIndex, 0, novoControl);
this.triggerEvent({ controlKey: key, prop: 'addControl', value: formControl }, otherForm);
}
}
}
}
Expand Down Expand Up @@ -922,4 +926,56 @@ export class FieldInteractionApi {
form.fieldInteractionEvents.emit(event);
}
}

private addControlForFormGroup(
key: string,
metaForNewField: {
key?: string;
type?: string;
name?: string;
label?: string;
interactions?: Array<{ event?: string; invokeOnInit?: boolean; script? }>;
},
position: string = FieldInteractionApi.FIELD_POSITIONS.ABOVE_FIELD,
initialValue,
form: NovoFormGroup,
): void {
let controlIndex = -1;
form.controls.forEach((fieldsetControl, ci) => {
if (fieldsetControl.key === key) {
controlIndex = ci;
}
});

// Change the position of the newly added field
switch (position) {
case FieldInteractionApi.FIELD_POSITIONS.ABOVE_FIELD:
// Adding field above active field
// index can stay the same
break;
case FieldInteractionApi.FIELD_POSITIONS.BELOW_FIELD:
// Adding field below active field
controlIndex += 1;
break;
case FieldInteractionApi.FIELD_POSITIONS.TOP_OF_FORM:
// Adding field to the top of the form
controlIndex = 0;
break;
case FieldInteractionApi.FIELD_POSITIONS.BOTTOM_OF_FORM:
// Adding field to the bottom of the form
controlIndex = form.controls.length;
break;
default:
break;
}

if (controlIndex !== -1) {
const novoControl = this.formUtils.getControlForField(metaForNewField, this.http, {});
novoControl.hidden = false;
const formControl = new NovoFormControl(initialValue, novoControl);
form.addControl(novoControl.key, formControl);
form.controls.splice(controlIndex, 0, novoControl);
this.triggerEvent({ controlKey: key, prop: 'addControl', value: formControl }, form);
}
}
}
Loading