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