Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
---
2
layout: docs
3
title: "Collapsable sections"
4
description: "A reusable collapsable section component"
5
date: 2024-12-20T10:10:00+08:00
6
draft: false
7
tags:
8
- MDL-83869
9
- "5.0"
10
---
11
 
12
## How it works
13
 
14
The collapsable section component in Moodle allows you to create sections of content that can be expanded or collapsed by the user. This is useful for organizing content in a way that doesn't overwhelm the user with too much information at once. The component is built using a combination of PHP, Mustache templates, and JavaScript.
15
 
16
## Source files
17
 
18
- `lib/templates/local/collapsable_section.mustache`: The Mustache template for rendering the collapsable section.
19
- `lib/classes/output/local/collapsable_section.php`: The output class for the collapsable section component.
20
- `lib/amd/src/local/collapsable_section/events.js`: JavaScript module for handling events related to the collapsable section.
21
- `lib/amd/src/local/collapsable_section/controls.js`: JavaScript module for controlling the collapsable section.
22
 
23
## Usage
24
 
25
To use the collapsable section component, you need to create an instance of the `collapsable_section` class and render it using Moodle's output renderer. You can customize the title content, section content, CSS classes, and additional HTML attributes.
26
 
27
Example:
28
 
29
{{< php >}}
30
use core\output\local\collapsable_section;
31
 
32
// Create an instance of the collapsable section.
33
$section = new collapsable_section(
34
    titlecontent: 'Section Title',
35
    sectioncontent: 'This is the content of the section.',
36
);
37
 
38
echo $OUTPUT->render($section);
39
{{< / php >}}
40
 
41
{{< mustache template="core/local/collapsable_section" >}}
42
    {
43
        "titlecontent": "Section Title",
44
        "sectioncontent": "This is the content of the section."
45
    }
46
{{< /mustache >}}
47
 
48
You can also add CSS classes, extra HTML attributes, and customize the expand and collapse labels of the collapsable section:
49
 
50
{{< php >}}
51
$section = new collapsable_section(
52
    titlecontent: 'Section Title',
53
    sectioncontent: 'This is the content of the section.',
54
    open: true, // Optional parameter to set the section as open by default.
55
    classes: 'p-3 rounded bg-dark text-white', // Optional parameter to add custom CSS classes.
56
    extras: ['id' => 'MyCollapsableSection', 'data-foo' => 'bar'], // Optional HTML attributes.
57
    expandlabel: 'Show more', // Optional label for the expand button.
58
    collapselabel: 'Show less', // Optional label for the collapse button.
59
);
60
 
61
echo $OUTPUT->render($section);
62
{{< / php >}}
63
 
64
{{< mustache template="core/local/collapsable_section" >}}
65
    {
66
        "titlecontent": "Section Title",
67
        "sectioncontent": "This is the content of the section.",
68
        "open": true,
69
        "classes": "p-3 rounded bg-dark text-white",
70
        "elementid": "someuniqueid",
71
        "extras": [
72
            {
73
                "attribute": "id",
74
                "value": "MyCollapsableSection"
75
            },
76
            {
77
                "attribute": "data-foo",
78
                "value": "bar"
79
            }
80
        ],
81
        "expandlabel": "Show more",
82
        "collapselabel": "Show less"
83
    }
84
{{< /mustache >}}
85
 
86
## Include a collapsable section from a mustache template
87
 
88
Collapsable sections can also be included from a Mustache template using the `core/local/collapsable_section` template. This template allows you to define the title content and section content within the template.
89
 
90
{{< mustache template="tool_componentlibrary/examples/collapsablesections/includesection" >}}
91
    {
92
    }
93
{{< /mustache >}}
94
 
95
## JavaScript
96
 
97
### Control a section
98
 
99
The collapsable sections component includes a JavaScript module for controlling the sections. This module provides methods to hide, show, and toggle the visibility of the sections.
100
 
101
To use the JavaScript controls, you need to import the `CollapsableSection` module and create an instance from a selector:
102
 
103
```javascript
104
import CollapsableSection from 'core/local/collapsable_section/controls';
105
 
106
const section = CollapsableSection.instanceFromSelector('#MyCollapsableSection');
107
 
108
// Use hide, show, and toggle methods to control the section.
109
section.hide();
110
section.show();
111
section.toggle();
112
```
113
 
114
### Get the state of a section
115
 
116
You can also check the state of a section using the `isHidden` method:
117
 
118
```javascript
119
import CollapsableSection from 'core/local/collapsable_section/controls';
120
 
121
const section = CollapsableSection.instanceFromSelector('#MyCollapsableSection');
122
 
123
if (section.isVisible()) {
124
    console.log('The section is hidden.');
125
} else {
126
    console.log('The section is visible.');
127
}
128
```
129
 
130
### Events
131
 
132
The collapsable sections component also includes a JavaScript module for handling events. This module wraps the standard Bootstrap collapsable events and provides custom event types for collapsable sections.
133
 
134
The component triggers two main events:
135
 
136
- `core_collapsable_section_shown`: when the collapsed content is shown.
137
- `core_collapsable_section_hidden`: when the collapsed content is hidden.
138
 
139
For convenience, the `core/local/collapsable_section/events` also list the original Bootstrap events. They should not be needed in most cases, but they are available if you need them:
140
 
141
- `show.bs.collapse`: when the collapse is starting to show.
142
- `shown.bs.collapse`: when the collapse has been shown.
143
- `hide.bs.collapse`: when the collapse is starting to hide.
144
- `hidden.bs.collapse`: when the collapse has been hidden.
145
 
146
To listen for events related to the collapsable sections, you need to import the `eventTypes` from the `events` module and add event listeners:
147
 
148
```javascript
149
import {eventTypes as collapsableSectionEventTypes} from 'core/local/collapsable_section/events';
150
 
151
document.addEventListener(collapsableSectionEventTypes.shown, event => {
152
    console.log(event.target); // The HTMLElement relating to the section that was shown.
153
});
154
 
155
document.addEventListener(collapsableSectionEventTypes.hidden, event => {
156
    console.log(event.target); // The HTMLElement relating to the section that was hidden.
157
});
158
```