changed description template scroll behavior after deleting a field

This commit is contained in:
Sofia Papacharalampous 2024-05-17 09:44:01 +03:00
parent f928527b26
commit a9468abe61
1 changed files with 45 additions and 62 deletions

View File

@ -577,6 +577,24 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
return result; return result;
} }
private getNextFieldAfterDeletingTocentry(tce: ToCEntry): any {
const sameLevelFields: UntypedFormArray = tce.form?.parent?.parent?.get('fieldSets')?.value;
let tceIndex = -1;
//find tce index
for (let i = 0; i < sameLevelFields.length; i++) {
let section: any = sameLevelFields?.at(i);
let sectionId = section?.id;
if (sectionId == tce.id) {
tceIndex = i;
break;
}
}
return sameLevelFields.at(tceIndex > 0 ? tceIndex-1 : tceIndex+1); //if deleting the first field, find the next one, else find the previous
}
private populateSections(sections: UntypedFormArray, existingNumbering: string, validationRootPath: string): ToCEntry[] { private populateSections(sections: UntypedFormArray, existingNumbering: string, validationRootPath: string): ToCEntry[] {
if (sections == null || sections.controls == null || sections.controls.length == 0) { return null; } if (sections == null || sections.controls == null || sections.controls.length == 0) { return null; }
@ -906,9 +924,6 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
} }
this.reaplyValidators(); this.reaplyValidators();
//update validity
// this.form.controls.sections.updateValueAndValidity();
} }
break; break;
@ -936,7 +951,6 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
const sections = pages.at(pageIndex).get('sections') as UntypedFormArray; const sections = pages.at(pageIndex).get('sections') as UntypedFormArray;
//remove section //remove section
// this._updateSelectedItem(tce);
sections.removeAt(sectionIndex); sections.removeAt(sectionIndex);
//update ordinal //update ordinal
@ -946,50 +960,6 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
} else {//NOT FOUND IN FIRST LEVEL CASE } else {//NOT FOUND IN FIRST LEVEL CASE
//LOOK FOR SUBSECTION CASE //LOOK FOR SUBSECTION CASE
// let parentSectionIndex = -1;
// let sectionIndex = -1;
// for (let j = 0; j < pages.length; j++) {
// const parentSections = pages.at(j).get('sections') as UntypedFormArray;
// for (let i = 0; i < parentSections?.length; i++) {
// const sections = (pages.at(j).get('sections') as UntypedFormArray).at(i).get('sections') as UntypedFormArray;
// for (let k = 0; i < sections?.length; i++) {
// let section = sections.at(i);
// let sectionId = section.get('id').value;
// if (sectionId == tce.id) {
// sectionIndex = k;
// parentSectionIndex = i;
// pageIndex = j;
// break;
// }
// }
// }
// }
// let parentFormArray = tce.form.parent as UntypedFormArray;
// for (let i = 0; i < parentFormArray.length; i++) {
// let section = parentFormArray.at(i);
// if (section.get('id').value == tce.id) {
// index = i;
// break;
// }
// }
// if (sectionIndex >= 0) {
// this._updateSelectedItem(tce);
// const parentFormArray = (pages.at(pageIndex).get('sections') as UntypedFormArray).at(parentSectionIndex).get('sections') as UntypedFormArray;
// parentFormArray.removeAt(sectionIndex);
// //update odrinal
// for (let i = 0; i < parentFormArray.length; i++) {
// parentFormArray.at(i).get('ordinal').patchValue(i);
// }
// }
for (let j = 0; j < pages.length; j++) { for (let j = 0; j < pages.length; j++) {
const parentSections = pages.at(j).get('sections') as UntypedFormArray; const parentSections = pages.at(j).get('sections') as UntypedFormArray;
@ -1041,7 +1011,6 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
//in case selectedtocentrhy is child of the removed element //in case selectedtocentrhy is child of the removed element
// this.refreshToCEntries();
this.onDataNeedsRefresh(); this.onDataNeedsRefresh();
this.formGroup.updateValueAndValidity(); this.formGroup.updateValueAndValidity();
@ -1076,6 +1045,7 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
if (this.selectedTocEntry) { if (this.selectedTocEntry) {
//.find(e => e.id)
if (this.tocEntryIsChildOf(this.selectedTocEntry, tce)) { if (this.tocEntryIsChildOf(this.selectedTocEntry, tce)) {
if (this.selectedTocEntry.type == ToCEntryType.Page) { if (this.selectedTocEntry.type == ToCEntryType.Page) {
this.selectedTocEntry = null; this.selectedTocEntry = null;
@ -1105,6 +1075,17 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
} }
} }
let sameLevelFields: UntypedFormArray = tce.form?.parent?.parent?.get('fieldSets')?.value;
if (sameLevelFields?.length > 1) {
const previousField = this.getNextFieldAfterDeletingTocentry(tce);
const tocentries = this.getTocEntries();
const previousFieldTocEntry = this._findTocEntryById(previousField?.id, tocentries);
this.selectedTocEntry = previousFieldTocEntry;
} else if (sameLevelFields?.length == 1) {
//scroll to parent
let parentId = null; let parentId = null;
if (isFirstLevel) { if (isFirstLevel) {
@ -1113,7 +1094,6 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
parentId = tce.form.parent.parent.get('id').value parentId = tce.form.parent.parent.get('id').value
} }
// const parentId = tce.form.parent.parent.get('id').value;
if (parentId) { if (parentId) {
const tocentries = this.getTocEntries(); const tocentries = this.getTocEntries();
const parent = this._findTocEntryById(parentId, tocentries); const parent = this._findTocEntryById(parentId, tocentries);
@ -1126,6 +1106,9 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
} else { } else {
this.selectedTocEntry = null; this.selectedTocEntry = null;
} }
} else {
this.selectedTocEntry = null;
}
} }
} }
} }