1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Course index main component.
|
|
|
18 |
*
|
|
|
19 |
* @module core_courseformat/local/courseindex/courseindex
|
|
|
20 |
* @class core_courseformat/local/courseindex/courseindex
|
|
|
21 |
* @copyright 2021 Ferran Recio <ferran@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
import {BaseComponent} from 'core/reactive';
|
|
|
26 |
import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
|
|
|
27 |
import jQuery from 'jquery';
|
|
|
28 |
import ContentTree from 'core_courseformat/local/courseeditor/contenttree';
|
|
|
29 |
|
|
|
30 |
export default class Component extends BaseComponent {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Constructor hook.
|
|
|
34 |
*/
|
|
|
35 |
create() {
|
|
|
36 |
// Optional component name for debugging.
|
|
|
37 |
this.name = 'courseindex';
|
|
|
38 |
// Default query selectors.
|
|
|
39 |
this.selectors = {
|
|
|
40 |
SECTION: `[data-for='section']`,
|
|
|
41 |
SECTION_CMLIST: `[data-for='cmlist']`,
|
|
|
42 |
CM: `[data-for='cm']`,
|
|
|
43 |
TOGGLER: `[data-action="togglecourseindexsection"]`,
|
|
|
44 |
COLLAPSE: `[data-toggle="collapse"]`,
|
|
|
45 |
DRAWER: `.drawer`,
|
|
|
46 |
};
|
|
|
47 |
// Default classes to toggle on refresh.
|
|
|
48 |
this.classes = {
|
|
|
49 |
SECTIONHIDDEN: 'dimmed',
|
|
|
50 |
CMHIDDEN: 'dimmed',
|
|
|
51 |
SECTIONCURRENT: 'current',
|
|
|
52 |
COLLAPSED: `collapsed`,
|
|
|
53 |
SHOW: `show`,
|
|
|
54 |
};
|
|
|
55 |
// Arrays to keep cms and sections elements.
|
|
|
56 |
this.sections = {};
|
|
|
57 |
this.cms = {};
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Static method to create a component instance form the mustache template.
|
|
|
62 |
*
|
|
|
63 |
* @param {element|string} target the DOM main element or its ID
|
|
|
64 |
* @param {object} selectors optional css selector overrides
|
|
|
65 |
* @return {Component}
|
|
|
66 |
*/
|
|
|
67 |
static init(target, selectors) {
|
|
|
68 |
return new this({
|
|
|
69 |
element: document.getElementById(target),
|
|
|
70 |
reactive: getCurrentCourseEditor(),
|
|
|
71 |
selectors,
|
|
|
72 |
});
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Initial state ready method.
|
|
|
77 |
*
|
|
|
78 |
* @param {Object} state the state data
|
|
|
79 |
*/
|
|
|
80 |
stateReady(state) {
|
|
|
81 |
// Activate section togglers.
|
|
|
82 |
this.addEventListener(this.element, 'click', this._sectionTogglers);
|
|
|
83 |
|
|
|
84 |
// Get cms and sections elements.
|
|
|
85 |
const sections = this.getElements(this.selectors.SECTION);
|
|
|
86 |
sections.forEach((section) => {
|
|
|
87 |
this.sections[section.dataset.id] = section;
|
|
|
88 |
});
|
|
|
89 |
const cms = this.getElements(this.selectors.CM);
|
|
|
90 |
cms.forEach((cm) => {
|
|
|
91 |
this.cms[cm.dataset.id] = cm;
|
|
|
92 |
});
|
|
|
93 |
|
|
|
94 |
// Set the page item if any.
|
|
|
95 |
this._refreshPageItem({element: state.course, state});
|
|
|
96 |
|
|
|
97 |
// Configure Aria Tree.
|
|
|
98 |
this.contentTree = new ContentTree(this.element, this.selectors, this.reactive.isEditing);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
getWatchers() {
|
|
|
102 |
return [
|
|
|
103 |
{watch: `section.indexcollapsed:updated`, handler: this._refreshSectionCollapsed},
|
|
|
104 |
{watch: `cm:created`, handler: this._createCm},
|
|
|
105 |
{watch: `cm:deleted`, handler: this._deleteCm},
|
|
|
106 |
{watch: `section:created`, handler: this._createSection},
|
|
|
107 |
{watch: `section:deleted`, handler: this._deleteSection},
|
|
|
108 |
{watch: `course.pageItem:created`, handler: this._refreshPageItem},
|
|
|
109 |
{watch: `course.pageItem:updated`, handler: this._refreshPageItem},
|
|
|
110 |
// Sections and cm sorting.
|
|
|
111 |
{watch: `course.sectionlist:updated`, handler: this._refreshCourseSectionlist},
|
|
|
112 |
{watch: `section.cmlist:updated`, handler: this._refreshSectionCmlist},
|
|
|
113 |
];
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Setup sections toggler.
|
|
|
118 |
*
|
|
|
119 |
* Toggler click is delegated to the main course index element because new sections can
|
|
|
120 |
* appear at any moment and this way we prevent accidental double bindings.
|
|
|
121 |
*
|
|
|
122 |
* @param {Event} event the triggered event
|
|
|
123 |
*/
|
|
|
124 |
_sectionTogglers(event) {
|
|
|
125 |
const sectionlink = event.target.closest(this.selectors.TOGGLER);
|
|
|
126 |
const isChevron = event.target.closest(this.selectors.COLLAPSE);
|
|
|
127 |
|
|
|
128 |
if (sectionlink || isChevron) {
|
|
|
129 |
|
|
|
130 |
const section = event.target.closest(this.selectors.SECTION);
|
|
|
131 |
const toggler = section.querySelector(this.selectors.COLLAPSE);
|
|
|
132 |
const isCollapsed = toggler?.classList.contains(this.classes.COLLAPSED) ?? false;
|
|
|
133 |
|
|
|
134 |
// Update the state.
|
|
|
135 |
const sectionId = section.getAttribute('data-id');
|
|
|
136 |
if (!sectionlink || isCollapsed) {
|
|
|
137 |
this.reactive.dispatch(
|
|
|
138 |
'sectionIndexCollapsed',
|
|
|
139 |
[sectionId],
|
|
|
140 |
!isCollapsed
|
|
|
141 |
);
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Update section collapsed.
|
|
|
148 |
*
|
|
|
149 |
* @param {object} args
|
|
|
150 |
* @param {object} args.element The leement to be expanded
|
|
|
151 |
*/
|
|
|
152 |
_refreshSectionCollapsed({element}) {
|
|
|
153 |
const target = this.getElement(this.selectors.SECTION, element.id);
|
|
|
154 |
if (!target) {
|
|
|
155 |
throw new Error(`Unkown section with ID ${element.id}`);
|
|
|
156 |
}
|
|
|
157 |
// Check if it is already done.
|
|
|
158 |
const toggler = target.querySelector(this.selectors.COLLAPSE);
|
|
|
159 |
const isCollapsed = toggler?.classList.contains(this.classes.COLLAPSED) ?? false;
|
|
|
160 |
|
|
|
161 |
if (element.indexcollapsed !== isCollapsed) {
|
|
|
162 |
this._expandSectionNode(element);
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Expand a section node.
|
|
|
168 |
*
|
|
|
169 |
* By default the method will use element.indexcollapsed to decide if the
|
|
|
170 |
* section is opened or closed. However, using forceValue it is possible
|
|
|
171 |
* to open or close a section independant from the indexcollapsed attribute.
|
|
|
172 |
*
|
|
|
173 |
* @param {Object} element the course module state element
|
|
|
174 |
* @param {boolean} forceValue optional forced expanded value
|
|
|
175 |
*/
|
|
|
176 |
_expandSectionNode(element, forceValue) {
|
|
|
177 |
const target = this.getElement(this.selectors.SECTION, element.id);
|
|
|
178 |
const toggler = target.querySelector(this.selectors.COLLAPSE);
|
|
|
179 |
let collapsibleId = toggler.dataset.target ?? toggler.getAttribute("href");
|
|
|
180 |
if (!collapsibleId) {
|
|
|
181 |
return;
|
|
|
182 |
}
|
|
|
183 |
collapsibleId = collapsibleId.replace('#', '');
|
|
|
184 |
const collapsible = document.getElementById(collapsibleId);
|
|
|
185 |
if (!collapsible) {
|
|
|
186 |
return;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
if (forceValue === undefined) {
|
|
|
190 |
forceValue = (element.indexcollapsed) ? false : true;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
// Course index is based on Bootstrap 4 collapsibles. To collapse them we need jQuery to
|
|
|
194 |
// interact with collapsibles methods. Hopefully, this will change in Bootstrap 5 because
|
|
|
195 |
// it does not require jQuery anymore (when MDL-71979 is integrated).
|
|
|
196 |
const togglerValue = (forceValue) ? 'show' : 'hide';
|
|
|
197 |
jQuery(collapsible).collapse(togglerValue);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
/**
|
|
|
201 |
* Handle a page item update.
|
|
|
202 |
*
|
|
|
203 |
* @param {Object} details the update details
|
|
|
204 |
* @param {Object} details.state the state data.
|
|
|
205 |
* @param {Object} details.element the course state data.
|
|
|
206 |
*/
|
|
|
207 |
_refreshPageItem({element, state}) {
|
|
|
208 |
if (!element?.pageItem?.isStatic || element.pageItem.type != 'cm') {
|
|
|
209 |
return;
|
|
|
210 |
}
|
|
|
211 |
// Check if we need to uncollapse the section and scroll to the element.
|
|
|
212 |
const section = state.section.get(element.pageItem.sectionId);
|
|
|
213 |
if (section.indexcollapsed) {
|
|
|
214 |
this._expandSectionNode(section, true);
|
|
|
215 |
setTimeout(
|
|
|
216 |
() => this.cms[element.pageItem.id]?.scrollIntoView({block: "nearest"}),
|
|
|
217 |
250
|
|
|
218 |
);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Create a newcm instance.
|
|
|
224 |
*
|
|
|
225 |
* @param {object} param
|
|
|
226 |
* @param {Object} param.state
|
|
|
227 |
* @param {Object} param.element
|
|
|
228 |
*/
|
|
|
229 |
async _createCm({state, element}) {
|
|
|
230 |
// Create a fake node while the component is loading.
|
|
|
231 |
const fakeelement = document.createElement('li');
|
|
|
232 |
fakeelement.classList.add('bg-pulse-grey', 'w-100');
|
|
|
233 |
fakeelement.innerHTML = ' ';
|
|
|
234 |
this.cms[element.id] = fakeelement;
|
|
|
235 |
// Place the fake node on the correct position.
|
|
|
236 |
this._refreshSectionCmlist({
|
|
|
237 |
state,
|
|
|
238 |
element: state.section.get(element.sectionid),
|
|
|
239 |
});
|
|
|
240 |
// Collect render data.
|
|
|
241 |
const exporter = this.reactive.getExporter();
|
|
|
242 |
const data = exporter.cm(state, element);
|
|
|
243 |
// Create the new content.
|
|
|
244 |
const newcomponent = await this.renderComponent(fakeelement, 'core_courseformat/local/courseindex/cm', data);
|
|
|
245 |
// Replace the fake node with the real content.
|
|
|
246 |
const newelement = newcomponent.getElement();
|
|
|
247 |
this.cms[element.id] = newelement;
|
|
|
248 |
fakeelement.parentNode.replaceChild(newelement, fakeelement);
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Create a new section instance.
|
|
|
253 |
*
|
|
|
254 |
* @param {Object} details the update details.
|
|
|
255 |
* @param {Object} details.state the state data.
|
|
|
256 |
* @param {Object} details.element the element data.
|
|
|
257 |
*/
|
|
|
258 |
async _createSection({state, element}) {
|
|
|
259 |
// Create a fake node while the component is loading.
|
|
|
260 |
const fakeelement = document.createElement('div');
|
|
|
261 |
fakeelement.classList.add('bg-pulse-grey', 'w-100');
|
|
|
262 |
fakeelement.innerHTML = ' ';
|
|
|
263 |
this.sections[element.id] = fakeelement;
|
|
|
264 |
// Place the fake node on the correct position.
|
|
|
265 |
this._refreshCourseSectionlist({
|
|
|
266 |
state,
|
|
|
267 |
element: state.course,
|
|
|
268 |
});
|
|
|
269 |
// Collect render data.
|
|
|
270 |
const exporter = this.reactive.getExporter();
|
|
|
271 |
const data = exporter.section(state, element);
|
|
|
272 |
// Create the new content.
|
|
|
273 |
const newcomponent = await this.renderComponent(fakeelement, 'core_courseformat/local/courseindex/section', data);
|
|
|
274 |
// Replace the fake node with the real content.
|
|
|
275 |
const newelement = newcomponent.getElement();
|
|
|
276 |
this.sections[element.id] = newelement;
|
|
|
277 |
fakeelement.parentNode.replaceChild(newelement, fakeelement);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
/**
|
|
|
281 |
* Refresh a section cm list.
|
|
|
282 |
*
|
|
|
283 |
* @param {object} param
|
|
|
284 |
* @param {Object} param.element
|
|
|
285 |
*/
|
|
|
286 |
_refreshSectionCmlist({element}) {
|
|
|
287 |
const cmlist = element.cmlist ?? [];
|
|
|
288 |
const listparent = this.getElement(this.selectors.SECTION_CMLIST, element.id);
|
|
|
289 |
this._fixOrder(listparent, cmlist, this.cms);
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
/**
|
|
|
293 |
* Refresh the section list.
|
|
|
294 |
*
|
|
|
295 |
* @param {object} param
|
|
|
296 |
* @param {Object} param.state
|
|
|
297 |
*/
|
|
|
298 |
_refreshCourseSectionlist({state}) {
|
|
|
299 |
const sectionlist = this.reactive.getExporter().listedSectionIds(state);
|
|
|
300 |
this._fixOrder(this.element, sectionlist, this.sections);
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* Fix/reorder the section or cms order.
|
|
|
305 |
*
|
|
|
306 |
* @param {Element} container the HTML element to reorder.
|
|
|
307 |
* @param {Array} neworder an array with the ids order
|
|
|
308 |
* @param {Array} allitems the list of html elements that can be placed in the container
|
|
|
309 |
*/
|
|
|
310 |
_fixOrder(container, neworder, allitems) {
|
|
|
311 |
|
|
|
312 |
// Empty lists should not be visible.
|
|
|
313 |
if (!neworder.length) {
|
|
|
314 |
container.classList.add('hidden');
|
|
|
315 |
container.innerHTML = '';
|
|
|
316 |
return;
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
// Grant the list is visible (in case it was empty).
|
|
|
320 |
container.classList.remove('hidden');
|
|
|
321 |
|
|
|
322 |
// Move the elements in order at the beginning of the list.
|
|
|
323 |
neworder.forEach((itemid, index) => {
|
|
|
324 |
const item = allitems[itemid];
|
|
|
325 |
// Get the current element at that position.
|
|
|
326 |
const currentitem = container.children[index];
|
|
|
327 |
if (currentitem === undefined) {
|
|
|
328 |
container.append(item);
|
|
|
329 |
return;
|
|
|
330 |
}
|
|
|
331 |
if (currentitem !== item && item) {
|
|
|
332 |
container.insertBefore(item, currentitem);
|
|
|
333 |
}
|
|
|
334 |
});
|
|
|
335 |
// Remove the remaining elements.
|
|
|
336 |
while (container.children.length > neworder.length) {
|
|
|
337 |
container.removeChild(container.lastChild);
|
|
|
338 |
}
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
/**
|
|
|
342 |
* Remove a cm from the list.
|
|
|
343 |
*
|
|
|
344 |
* The actual DOM element removal is delegated to the cm component.
|
|
|
345 |
*
|
|
|
346 |
* @param {object} param
|
|
|
347 |
* @param {Object} param.element
|
|
|
348 |
*/
|
|
|
349 |
_deleteCm({element}) {
|
|
|
350 |
delete this.cms[element.id];
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
/**
|
|
|
354 |
* Remove a section from the list.
|
|
|
355 |
*
|
|
|
356 |
* The actual DOM element removal is delegated to the section component.
|
|
|
357 |
*
|
|
|
358 |
* @param {Object} details the update details.
|
|
|
359 |
* @param {Object} details.element the element data.
|
|
|
360 |
*/
|
|
|
361 |
_deleteSection({element}) {
|
|
|
362 |
delete this.sections[element.id];
|
|
|
363 |
}
|
|
|
364 |
}
|