diff --git a/src/core/features/course/components/course-index/course-index.ts b/src/core/features/course/components/course-index/course-index.ts index 712e87e37..a7b871765 100644 --- a/src/core/features/course/components/course-index/course-index.ts +++ b/src/core/features/course/components/course-index/course-index.ts @@ -69,6 +69,10 @@ export class CoreCourseCourseIndexComponent implements OnInit { if (!formatOptions || formatOptions.completionusertracked === false) { return; } + + // Collapse all sections first. + this.sections.forEach((section) => section.expanded = false); + const currentSection = await CoreCourseFormatDelegate.getCurrentSection(this.course, this.sections); currentSection.highlighted = true; if (this.selectedId === undefined) { diff --git a/src/core/features/course/components/format/core-course-format.html b/src/core/features/course/components/format/core-course-format.html index 3c8036beb..e1712fea8 100644 --- a/src/core/features/course/components/format/core-course-format.html +++ b/src/core/features/course/components/format/core-course-format.html @@ -65,7 +65,8 @@ -
+
diff --git a/src/core/features/course/components/format/format.ts b/src/core/features/course/components/format/format.ts index 99eb2cc86..f6030ede6 100644 --- a/src/core/features/course/components/format/format.ts +++ b/src/core/features/course/components/format/format.ts @@ -283,12 +283,32 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { * Display the course index modal. */ async openCourseIndex(): Promise { + let selectedId = this.selectedSection?.id; + + if (selectedId == this.allSectionsId) { + // Check current scrolled section. + const allSectionElements: NodeListOf = + this.elementRef.nativeElement.querySelectorAll('section.section-wrapper'); + + const scroll = await this.content.getScrollElement(); + const containerTop = scroll.getBoundingClientRect().top; + + const element = Array.from(allSectionElements).find((element) => { + const position = element.getBoundingClientRect(); + + // The bottom is inside the container or lower. + return position.bottom >= containerTop; + }); + + selectedId = Number(element?.getAttribute('id')) || undefined; + } + const data = await CoreDomUtils.openModal({ component: CoreCourseCourseIndexComponent, componentProps: { course: this.course, sections: this.sections, - selectedId: this.selectedSection?.id, + selectedId: selectedId, }, });