Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Test page for the collapsable section output component.
19
 *
20
 * @copyright  2024 Ferran Recio <ferran@moodle.com>
21
 * @package    core
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require_once(__DIR__ . '/../../../../config.php');
26
 
27
use core\output\local\collapsable_section;
28
 
29
defined('BEHAT_SITE_RUNNING') || die();
30
 
31
/**
32
 * Generate the title content.
33
 *
34
 * @param string $content The content to be displayed inside the button.
35
 * @return string
36
 */
37
function title_content(string $content): string {
38
    return ucfirst($content) . ' title';
39
}
40
 
41
/**
42
 * Generate the section content.
43
 *
44
 * @param string $content The content to be displayed inside the dialog.
45
 * @return string
46
 */
47
function section_content(string $content): string {
48
    global $OUTPUT;
49
    $icon = $OUTPUT->pix_icon('t/hide', 'Eye icon');
50
    return '
51
        <p>This is the ' . $content . ' content.</p>
52
        <p>Some rich content <a href="">Link</a> ' . $icon . '.</p>
53
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
54
        tempor <b>incididunt ut labore et dolore magna aliqua</b>. Ut enim ad minim veniam,
55
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
56
        consequat.</p>
57
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
58
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
59
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
60
        consequat.</p>
61
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
62
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
63
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
64
        consequat.</p>
65
    ';
66
}
67
 
68
global $CFG, $PAGE, $OUTPUT;
69
$PAGE->set_url('/lib/tests/behat/fixtures/collapsable_section_output_testpage.php');
70
$PAGE->add_body_class('limitedwidth');
71
require_login();
72
$PAGE->set_context(core\context\system::instance());
73
$PAGE->set_title('Collapsable section test page');
74
 
75
echo $OUTPUT->header();
76
 
77
echo "<h2>Collapsable section test page</h2>";
78
echo $OUTPUT->paragraph('This page is used to test the collapsable section output component.');
79
 
80
$sample = 'closed section';
81
echo '<div id="closedsection" class="mb-4 border border-1 rounded-3 p-3">';
82
echo $OUTPUT->paragraph($sample . ' example');
83
$collapsable = new collapsable_section(
84
    titlecontent: title_content($sample),
85
    sectioncontent: section_content($sample),
86
);
87
echo $OUTPUT->render($collapsable);
88
echo '</div>';
89
 
90
$sample = 'open section';
91
echo '<div id="opensection" class="mb-4 border border-1 rounded-3 p-3">';
92
echo $OUTPUT->paragraph($sample . ' example');
93
$collapsable = new collapsable_section(
94
    titlecontent: title_content($sample),
95
    sectioncontent: section_content($sample),
96
    open: true,
97
);
98
echo $OUTPUT->render($collapsable);
99
echo '</div>';
100
 
101
$sample = 'extra classes';
102
echo '<div id="extraclasses" class="mb-4 border border-1 rounded-3 p-3">';
103
echo $OUTPUT->paragraph($sample . ' example');
104
$collapsable = new collapsable_section(
105
    titlecontent: title_content($sample),
106
    sectioncontent: section_content($sample),
107
    classes: 'bg-dark text-white p-3 rounded-3 extraclass',
108
);
109
echo $OUTPUT->render($collapsable);
110
echo '</div>';
111
 
112
$sample = 'extra attributes';
113
echo '<div id="extraattributes" class="mb-4 border border-1 rounded-3 p-3">';
114
echo $OUTPUT->paragraph($sample . ' example');
115
$collapsable = new collapsable_section(
116
    titlecontent: title_content($sample),
117
    sectioncontent: section_content($sample),
118
    extras: ['data-foo' => 'bar', 'id' => 'myid'],
119
);
120
echo $OUTPUT->render($collapsable);
121
echo '</div>';
122
 
123
$sample = 'custom labels';
124
echo '<div id="customlabels" class="mb-4 border border-1 rounded-3 p-3">';
125
echo $OUTPUT->paragraph($sample . ' example');
126
$collapsable = new collapsable_section(
127
    titlecontent: title_content($sample),
128
    sectioncontent: section_content($sample),
129
    expandlabel: 'Custom expand',
130
    collapselabel: 'Custom collapse',
131
);
132
echo $OUTPUT->render($collapsable);
133
echo '</div>';
134
 
135
$sample = 'javascript controls';
136
echo '<div id="jscontrols" class="mb-4 border border-1 rounded-3 p-3">';
137
echo $OUTPUT->paragraph($sample . ' example');
138
echo '
139
<div class="d-flex justify-content-center">
140
    <button class="btn btn-secondary mx-2" id="toggleBtn">Toggle</button>
141
    <button class="btn btn-secondary mx-2" id="showBtn">Show</button>
142
    <button class="btn btn-secondary mx-2" id="hideBtn">Hide</button>
143
    <button class="btn btn-secondary mx-2" id="testBtn">Test state</button>
144
    <div class="d-flex align-content-center mx-2 rounded p-2 border">
145
        Current state: <div class="d-inline-block" id="state">?</div>
146
    </div>
147
</div>';
148
echo '<div class="rounded my-2 p-2 border">
149
        Last event: <div class="d-inline-block" id="lastevent">?</div>
150
    </div>';
151
$collapsable = new collapsable_section(
152
    titlecontent: title_content($sample),
153
    sectioncontent: section_content($sample),
154
    extras: ['id' => 'jsCollapsable'],
155
);
156
echo $OUTPUT->render($collapsable);
157
echo '</div>';
158
 
159
$inlinejs = <<<EOF
160
    require(
161
        [
162
            'core/local/collapsable_section/controls',
163
            'core/local/collapsable_section/events'
164
        ],
165
        function(
166
            CollapsableSection,
167
            events
168
        ) {
169
 
170
            const section = CollapsableSection.instanceFromSelector('#jsCollapsable');
171
 
172
            document.getElementById('toggleBtn').addEventListener('click', function() {
173
                section.toggle();
174
            });
175
 
176
            document.getElementById('showBtn').addEventListener('click', function() {
177
                section.show();
178
            });
179
 
180
            document.getElementById('hideBtn').addEventListener('click', function() {
181
                section.hide();
182
            });
183
 
184
            document.getElementById('testBtn').addEventListener('click', function() {
185
                document.getElementById('state').textContent = section.isVisible() ? 'visible' : 'hidden';
186
            });
187
 
188
            const jscontrolregion = document.getElementById('jscontrols');
189
 
190
            jscontrolregion.addEventListener(events.eventTypes.shown, function() {
191
                document.getElementById('lastevent').textContent = 'Section shown';
192
            });
193
 
194
            jscontrolregion.addEventListener(events.eventTypes.hidden, function() {
195
                document.getElementById('lastevent').textContent = 'Section hidden';
196
            });
197
        }
198
    );
199
EOF;
200
 
201
$PAGE->requires->js_amd_inline($inlinejs);
202
 
203
echo '</div>';
204
echo $OUTPUT->footer();