Skip to content

Commit 3b74beb

Browse files
MDL-89250 core_courseformat: fix stuck collapse-all toggle state
1 parent 70771c1 commit 3b74beb

5 files changed

Lines changed: 70 additions & 21 deletions

File tree

public/course/format/amd/build/local/content.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/course/format/amd/build/local/content.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/course/format/amd/src/local/content.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ export default class Component extends BaseComponent {
318318
/**
319319
* Refresh the collapse/expand all sections element.
320320
*
321+
* Delegated sections, like subsections, are nested inside a regular section, so they are only
322+
* visible when their parent section is expanded. They must not drive the toggler state.
323+
*
321324
* @param {Object} state The state data
322325
*/
323326
_refreshAllSectionsToggler(state) {
@@ -327,26 +330,15 @@ export default class Component extends BaseComponent {
327330
}
328331

329332
const sectionIsCollapsible = this._getCollapsibleSections();
330-
331-
// Check if we have all sections collapsed/expanded.
332-
let allcollapsed = true;
333-
let allexpanded = true;
334-
state.section.forEach(
335-
section => {
336-
if (sectionIsCollapsible[section.id]) {
337-
allcollapsed = allcollapsed && section.contentcollapsed;
338-
allexpanded = allexpanded && !section.contentcollapsed;
339-
}
340-
}
333+
const sections = [...state.section.values()].filter(
334+
section => sectionIsCollapsible[section.id] && section.component === null
341335
);
342-
if (allcollapsed) {
343-
target.classList.add(this.classes.COLLAPSED);
344-
target.setAttribute('aria-expanded', false);
345-
}
346-
if (allexpanded) {
347-
target.classList.remove(this.classes.COLLAPSED);
348-
target.setAttribute('aria-expanded', true);
349-
}
336+
337+
// The toggler only offers to expand all when every listed section is collapsed.
338+
const allcollapsed = sections.length > 0 && sections.every(section => section.contentcollapsed);
339+
340+
target.classList.toggle(this.classes.COLLAPSED, allcollapsed);
341+
target.setAttribute('aria-expanded', !allcollapsed);
350342
}
351343

352344
/**
@@ -704,6 +696,7 @@ export default class Component extends BaseComponent {
704696
promise.then((html, js) => {
705697
Templates.replaceNode(sectionitem, html, js);
706698
this._indexContents();
699+
this._refreshAllSectionsToggler(this.reactive.state);
707700
pendingReload.resolve();
708701
}).catch(() => {
709702
pendingReload.resolve();

public/course/format/tests/behat/content_preferences.feature

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,42 @@ Feature: Course content collapsed user preferences
7575
And I should see "Activity sample 3" in the "region-main" "region"
7676
And I should not see "Activity sample 4" in the "region-main" "region"
7777
And I should see "Activity sample 5" in the "region-main" "region"
78+
79+
@javascript
80+
Scenario: The collapse all toggle is updated when only some sections are expanded
81+
Given I am on the "C1" "Course" page logged in as "teacher1"
82+
And I should see "Collapse all" in the "region-main" "region"
83+
And I click on "Collapse all" "button" in the "region-main" "region"
84+
And I should see "Expand all" in the "region-main" "region"
85+
And I should not see "Activity sample 1" in the "region-main" "region"
86+
# Expanding a single section is enough to offer collapsing all of them again.
87+
When I click on "Expand" "link" in the "Section 1" "section"
88+
Then I should see "Collapse all" in the "region-main" "region"
89+
But I should not see "Expand all" in the "region-main" "region"
90+
91+
@javascript
92+
Scenario: The collapse all toggle is updated when a new section is added
93+
Given I am on the "C1" "Course" page logged in as "teacher1"
94+
And I turn editing mode on
95+
And I click on "Collapse all" "button" in the "region-main" "region"
96+
And I should see "Expand all" in the "region-main" "region"
97+
And the "aria-expanded" attribute of "#collapsesections" "css_element" should contain "false"
98+
# New sections are expanded by default, so the toggle switches back to "Collapse all".
99+
When I click on "Add section" "link" in the "course-addsection" "region"
100+
Then I should see "Collapse all" in the "region-main" "region"
101+
And the "aria-expanded" attribute of "#collapsesections" "css_element" should contain "true"
102+
But I should not see "Expand all" in the "region-main" "region"
103+
104+
@javascript
105+
Scenario: The collapse all toggle is updated when a section is deleted
106+
Given I am on the "C1" "Course" page logged in as "teacher1"
107+
And I turn editing mode on
108+
And I click on "Collapse all" "button" in the "region-main" "region"
109+
# Expand a single section to get a mixed state.
110+
And I click on "Expand" "link" in the "Section 5" "section"
111+
And I should see "Collapse all" in the "region-main" "region"
112+
# Delete the only expanded section, so the toggle goes back to "Expand all".
113+
When I delete section "5"
114+
And I click on "Delete" "button" in the "Delete section?" "dialogue"
115+
Then I should see "Expand all" in the "region-main" "region"
116+
But I should not see "Collapse all" in the "region-main" "region"

public/mod/subsection/tests/behat/subsection_coursepage.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ Feature: Users view subsections on course page
3434
And I click on "Expand" "link" in the "Subsection1" "activity"
3535
And I click on "Page1 in Subsection1" "link" in the "Subsection1" "activity"
3636

37+
@javascript
38+
Scenario: Expanded subsections do not keep the collapse all toggle stuck
39+
Given I am on the "C1" "Course" page logged in as "student1"
40+
And I should see "Collapse all" in the "region-main" "region"
41+
# Collapse every top level section but leave the subsections expanded.
42+
When I click on "Collapse" "link" in the "General" "section"
43+
And I click on "Collapse" "link" in the "Section 1" "section"
44+
And I click on "Collapse" "link" in the "Section 2" "section"
45+
And I click on "Collapse" "link" in the "Section 3" "section"
46+
And I should not see "New database" in the "region-main" "region"
47+
# Subsections are hidden inside a collapsed section, so they must not hold the toggle back.
48+
Then I should see "Expand all" in the "region-main" "region"
49+
But I should not see "Collapse all" in the "region-main" "region"
50+
# The toggle must expand everything on the first click instead of being a dead click.
51+
And I click on "Expand all" "button" in the "region-main" "region"
52+
And I should see "New database" in the "region-main" "region"
53+
3754
@javascript
3855
Scenario: Teacher can create activities inside subsections on course page
3956
When I log in as "teacher1"

0 commit comments

Comments
 (0)