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 section component.
|
|
|
18 |
*
|
|
|
19 |
* This component is used to control specific course section interactions like drag and drop.
|
|
|
20 |
*
|
|
|
21 |
* @module core_courseformat/local/courseindex/section
|
|
|
22 |
* @class core_courseformat/local/courseindex/section
|
|
|
23 |
* @copyright 2021 Ferran Recio <ferran@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
import SectionTitle from 'core_courseformat/local/courseindex/sectiontitle';
|
|
|
28 |
import DndSection from 'core_courseformat/local/courseeditor/dndsection';
|
1441 |
ariadna |
29 |
import log from "core/log";
|
1 |
efrain |
30 |
|
|
|
31 |
export default class Component extends DndSection {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Constructor hook.
|
|
|
35 |
*/
|
|
|
36 |
create() {
|
|
|
37 |
// Optional component name for debugging.
|
|
|
38 |
this.name = 'courseindex_section';
|
|
|
39 |
// Default query selectors.
|
|
|
40 |
this.selectors = {
|
1441 |
ariadna |
41 |
SECTION: `[data-for='section']`,
|
1 |
efrain |
42 |
SECTION_ITEM: `[data-for='section_item']`,
|
|
|
43 |
SECTION_TITLE: `[data-for='section_title']`,
|
|
|
44 |
CM_LAST: `[data-for="cm"]:last-child`,
|
1441 |
ariadna |
45 |
DND_ALLOWED: `[data-courseindexdndallowed='true']`,
|
1 |
efrain |
46 |
};
|
|
|
47 |
// Default classes to toggle on refresh.
|
|
|
48 |
this.classes = {
|
|
|
49 |
SECTIONHIDDEN: 'dimmed',
|
|
|
50 |
SECTIONCURRENT: 'current',
|
|
|
51 |
LOCKED: 'editinprogress',
|
|
|
52 |
RESTRICTIONS: 'restrictions',
|
|
|
53 |
PAGEITEM: 'pageitem',
|
|
|
54 |
OVERLAYBORDERS: 'overlay-preview-borders',
|
|
|
55 |
};
|
|
|
56 |
|
|
|
57 |
// We need our id to watch specific events.
|
|
|
58 |
this.id = this.element.dataset.id;
|
|
|
59 |
this.isPageItem = false;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Static method to create a component instance form the mustahce template.
|
|
|
64 |
*
|
|
|
65 |
* @param {string} target the DOM main element or its ID
|
|
|
66 |
* @param {object} selectors optional css selector overrides
|
|
|
67 |
* @return {Component}
|
|
|
68 |
*/
|
|
|
69 |
static init(target, selectors) {
|
1441 |
ariadna |
70 |
let element = document.querySelector(target);
|
|
|
71 |
// TODO Remove this if condition as part of MDL-83851.
|
|
|
72 |
if (!element) {
|
|
|
73 |
log.debug('Init component with id is deprecated, use a query selector instead.');
|
|
|
74 |
element = document.getElementById(target);
|
|
|
75 |
}
|
1 |
efrain |
76 |
return new this({
|
1441 |
ariadna |
77 |
element,
|
1 |
efrain |
78 |
selectors,
|
|
|
79 |
});
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Initial state ready method.
|
|
|
84 |
*
|
|
|
85 |
* @param {Object} state the initial state
|
|
|
86 |
*/
|
|
|
87 |
stateReady(state) {
|
|
|
88 |
this.configState(state);
|
|
|
89 |
const sectionItem = this.getElement(this.selectors.SECTION_ITEM);
|
|
|
90 |
// Drag and drop is only available for components compatible course formats.
|
1441 |
ariadna |
91 |
if (this.reactive.isEditing && this.reactive.supportComponents && document.querySelector(this.selectors.DND_ALLOWED)) {
|
1 |
efrain |
92 |
// Init the inner dragable element passing the full section as affected region.
|
|
|
93 |
const titleitem = new SectionTitle({
|
|
|
94 |
...this,
|
|
|
95 |
element: sectionItem,
|
|
|
96 |
fullregion: this.element,
|
|
|
97 |
});
|
|
|
98 |
this.configDragDrop(titleitem);
|
|
|
99 |
}
|
|
|
100 |
// Check if the current url is the section url.
|
|
|
101 |
const section = state.section.get(this.id);
|
|
|
102 |
if (window.location.href == section.sectionurl.replace(/&/g, "&")) {
|
|
|
103 |
this.reactive.dispatch('setPageItem', 'section', this.id);
|
|
|
104 |
sectionItem.scrollIntoView();
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Component watchers.
|
|
|
110 |
*
|
|
|
111 |
* @returns {Array} of watchers
|
|
|
112 |
*/
|
|
|
113 |
getWatchers() {
|
|
|
114 |
return [
|
|
|
115 |
{watch: `section[${this.id}]:deleted`, handler: this.remove},
|
|
|
116 |
{watch: `section[${this.id}]:updated`, handler: this._refreshSection},
|
|
|
117 |
{watch: `course.pageItem:updated`, handler: this._refreshPageItem},
|
|
|
118 |
];
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Get the last CM element of that section.
|
|
|
123 |
*
|
|
|
124 |
* @returns {element|null}
|
|
|
125 |
*/
|
|
|
126 |
getLastCm() {
|
|
|
127 |
return this.getElement(this.selectors.CM_LAST);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Update a course index section using the state information.
|
|
|
132 |
*
|
|
|
133 |
* @param {Object} param details the update details.
|
|
|
134 |
* @param {Object} param.element the section element
|
|
|
135 |
*/
|
|
|
136 |
_refreshSection({element}) {
|
|
|
137 |
// Update classes.
|
|
|
138 |
const sectionItem = this.getElement(this.selectors.SECTION_ITEM);
|
|
|
139 |
sectionItem.classList.toggle(this.classes.SECTIONHIDDEN, !element.visible);
|
|
|
140 |
sectionItem.classList.toggle(this.classes.RESTRICTIONS, element.hasrestrictions ?? false);
|
|
|
141 |
this.element.classList.toggle(this.classes.SECTIONCURRENT, element.current);
|
|
|
142 |
this.element.classList.toggle(this.classes.DRAGGING, element.dragging ?? false);
|
|
|
143 |
this.element.classList.toggle(this.classes.LOCKED, element.locked ?? false);
|
|
|
144 |
this.locked = element.locked;
|
|
|
145 |
// Update title.
|
|
|
146 |
this.getElement(this.selectors.SECTION_TITLE).innerHTML = element.title;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Handle a page item update.
|
|
|
151 |
*
|
|
|
152 |
* @param {Object} details the update details
|
|
|
153 |
* @param {Object} details.state the state data.
|
|
|
154 |
* @param {Object} details.element the course state data.
|
|
|
155 |
*/
|
|
|
156 |
_refreshPageItem({element, state}) {
|
|
|
157 |
if (!element.pageItem) {
|
|
|
158 |
return;
|
|
|
159 |
}
|
1441 |
ariadna |
160 |
|
|
|
161 |
const containsPageItem = this._isPageItemInThisSection(element.pageItem);
|
|
|
162 |
|
|
|
163 |
if (!containsPageItem || this._isParentSectionIndexCollapsed(state)) {
|
1 |
efrain |
164 |
this.pageItem = false;
|
|
|
165 |
this.getElement(this.selectors.SECTION_ITEM).classList.remove(this.classes.PAGEITEM);
|
|
|
166 |
return;
|
|
|
167 |
}
|
1441 |
ariadna |
168 |
|
1 |
efrain |
169 |
const section = state.section.get(this.id);
|
|
|
170 |
if (section.indexcollapsed && !element.pageItem?.isStatic) {
|
1441 |
ariadna |
171 |
this.pageItem = containsPageItem;
|
1 |
efrain |
172 |
} else {
|
|
|
173 |
this.pageItem = (element.pageItem.type == 'section' && element.pageItem.id == this.id);
|
|
|
174 |
}
|
|
|
175 |
const sectionItem = this.getElement(this.selectors.SECTION_ITEM);
|
|
|
176 |
sectionItem.classList.toggle(this.classes.PAGEITEM, this.pageItem ?? false);
|
|
|
177 |
if (this.pageItem && !this.reactive.isEditing) {
|
|
|
178 |
this.element.scrollIntoView({block: "nearest"});
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
1441 |
ariadna |
183 |
* Check if the page item is inside this section.
|
|
|
184 |
*
|
|
|
185 |
* @private
|
|
|
186 |
* @param {Object} pageItem
|
|
|
187 |
* @param {Object} pageItem.sectionId the current page item section id.
|
|
|
188 |
* @returns {boolean}
|
|
|
189 |
*/
|
|
|
190 |
_isPageItemInThisSection(pageItem) {
|
|
|
191 |
if (pageItem.sectionId == this.id) {
|
|
|
192 |
return true;
|
|
|
193 |
}
|
|
|
194 |
// Check for any possible subsections.
|
|
|
195 |
const subsection = this.element.querySelector(`${this.selectors.SECTION}[data-id="${pageItem.sectionId}"]`);
|
|
|
196 |
if (subsection) {
|
|
|
197 |
return true;
|
|
|
198 |
}
|
|
|
199 |
return false;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Check if the parent section index is collapsed.
|
|
|
204 |
*
|
|
|
205 |
* @private
|
|
|
206 |
* @param {Object} state the current state
|
|
|
207 |
* @returns {boolean|null} null if no parent section is found.
|
|
|
208 |
*/
|
|
|
209 |
_isParentSectionIndexCollapsed(state) {
|
|
|
210 |
const parentElement = this.element.parentElement.closest(this.selectors.SECTION);
|
|
|
211 |
if (!parentElement || !parentElement.dataset.id) {
|
|
|
212 |
return null;
|
|
|
213 |
}
|
|
|
214 |
const parentSection = state.section.get(parentElement.dataset.id);
|
|
|
215 |
return !!parentSection.indexcollapsed;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
1 |
efrain |
219 |
* Overridden version of the component addOverlay async method.
|
|
|
220 |
*
|
|
|
221 |
* The course index is not compatible with overlay elements.
|
|
|
222 |
*/
|
|
|
223 |
async addOverlay() {
|
|
|
224 |
this.element.classList.add(this.classes.OVERLAYBORDERS);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Overridden version of the component removeOverlay.
|
|
|
229 |
*
|
|
|
230 |
* The course index is not compatible with overlay elements.
|
|
|
231 |
*/
|
|
|
232 |
removeOverlay() {
|
|
|
233 |
this.element.classList.remove(this.classes.OVERLAYBORDERS);
|
|
|
234 |
}
|
|
|
235 |
}
|