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 |
* in both course index and course content.
|
|
|
21 |
*
|
|
|
22 |
* @module core_courseformat/local/courseeditor/dndsection
|
|
|
23 |
* @class core_courseformat/local/courseeditor/dndsection
|
|
|
24 |
* @copyright 2021 Ferran Recio <ferran@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
import {BaseComponent, DragDrop} from 'core/reactive';
|
|
|
29 |
import {getString} from 'core/str';
|
|
|
30 |
import {prefetchStrings} from 'core/prefetch';
|
|
|
31 |
import Templates from 'core/templates';
|
|
|
32 |
|
|
|
33 |
// Load global strings.
|
|
|
34 |
prefetchStrings('core', ['addfilehere']);
|
|
|
35 |
|
|
|
36 |
export default class extends BaseComponent {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Save some values form the state.
|
|
|
40 |
*
|
|
|
41 |
* @param {Object} state the current state
|
|
|
42 |
*/
|
|
|
43 |
configState(state) {
|
|
|
44 |
this.id = this.element.dataset.id;
|
|
|
45 |
this.section = state.section.get(this.id);
|
|
|
46 |
this.course = state.course;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Register state values and the drag and drop subcomponent.
|
|
|
51 |
*
|
|
|
52 |
* @param {BaseComponent} sectionitem section item component
|
|
|
53 |
*/
|
|
|
54 |
configDragDrop(sectionitem) {
|
|
|
55 |
// Drag and drop is only available for components compatible course formats.
|
|
|
56 |
if (this.reactive.isEditing && this.reactive.supportComponents) {
|
|
|
57 |
// Init the inner dragable element.
|
|
|
58 |
this.sectionitem = sectionitem;
|
|
|
59 |
// Init the dropzone.
|
|
|
60 |
this.dragdrop = new DragDrop(this);
|
|
|
61 |
// Save dropzone classes.
|
|
|
62 |
this.classes = this.dragdrop.getClasses();
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Remove all subcomponents dependencies.
|
|
|
68 |
*/
|
|
|
69 |
destroy() {
|
|
|
70 |
if (this.sectionitem !== undefined) {
|
|
|
71 |
this.sectionitem.unregister();
|
|
|
72 |
}
|
|
|
73 |
if (this.dragdrop !== undefined) {
|
|
|
74 |
this.dragdrop.unregister();
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Get the last CM element of that section.
|
|
|
80 |
*
|
|
|
81 |
* @returns {element|null} the las course module element of the section.
|
|
|
82 |
*/
|
|
|
83 |
getLastCm() {
|
|
|
84 |
return null;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Drag and drop methods.
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* The element drop start hook.
|
|
|
91 |
*
|
|
|
92 |
* @param {Object} dropdata the dropdata
|
|
|
93 |
*/
|
|
|
94 |
dragStart(dropdata) {
|
|
|
95 |
this.reactive.dispatch('sectionDrag', [dropdata.id], true);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* The element drop end hook.
|
|
|
100 |
*
|
|
|
101 |
* @param {Object} dropdata the dropdata
|
|
|
102 |
*/
|
|
|
103 |
dragEnd(dropdata) {
|
|
|
104 |
this.reactive.dispatch('sectionDrag', [dropdata.id], false);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Validate if the drop data can be dropped over the component.
|
|
|
109 |
*
|
|
|
110 |
* @param {Object} dropdata the exported drop data.
|
|
|
111 |
* @returns {boolean}
|
|
|
112 |
*/
|
|
|
113 |
validateDropData(dropdata) {
|
|
|
114 |
// We accept files.
|
|
|
115 |
if (dropdata?.type === 'files') {
|
|
|
116 |
return true;
|
|
|
117 |
}
|
|
|
118 |
// We accept any course module unless it can form a subsection loop.
|
|
|
119 |
if (dropdata?.type === 'cm') {
|
|
|
120 |
if (this.section?.component && dropdata?.delegatesection === true) {
|
|
|
121 |
return false;
|
|
|
122 |
}
|
|
|
123 |
return true;
|
|
|
124 |
}
|
|
|
125 |
// We accept any section but yourself and the next one.
|
|
|
126 |
if (dropdata?.type === 'section') {
|
|
|
127 |
return dropdata?.id != this.id && dropdata?.number != this.section.number + 1;
|
|
|
128 |
}
|
|
|
129 |
return false;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Display the component dropzone.
|
|
|
134 |
*
|
|
|
135 |
* @param {Object} dropdata the accepted drop data
|
|
|
136 |
*/
|
|
|
137 |
showDropZone(dropdata) {
|
|
|
138 |
if (dropdata.type == 'files') {
|
|
|
139 |
this.addOverlay({
|
|
|
140 |
content: getString('addfilehere', 'core'),
|
|
|
141 |
icon: Templates.renderPix('t/download', 'core'),
|
|
|
142 |
}).then(() => {
|
|
|
143 |
// Check if we still need the file dropzone.
|
|
|
144 |
if (!this.dragdrop?.isDropzoneVisible()) {
|
|
|
145 |
this.removeOverlay();
|
|
|
146 |
}
|
|
|
147 |
return;
|
|
|
148 |
}).catch((error) => {
|
|
|
149 |
throw error;
|
|
|
150 |
});
|
|
|
151 |
}
|
|
|
152 |
if (dropdata.type == 'cm') {
|
|
|
153 |
this.getLastCm()?.classList.add(this.classes.DROPDOWN);
|
|
|
154 |
}
|
|
|
155 |
if (dropdata.type == 'section') {
|
|
|
156 |
this.element.classList.remove(this.classes.DROPUP);
|
|
|
157 |
this.element.classList.add(this.classes.DROPDOWN);
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Hide the component dropzone.
|
|
|
163 |
*/
|
|
|
164 |
hideDropZone() {
|
|
|
165 |
this.getLastCm()?.classList.remove(this.classes.DROPDOWN);
|
|
|
166 |
this.element.classList.remove(this.classes.DROPUP);
|
|
|
167 |
this.element.classList.remove(this.classes.DROPDOWN);
|
|
|
168 |
this.removeOverlay();
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Drop event handler.
|
|
|
173 |
*
|
|
|
174 |
* @param {Object} dropdata the accepted drop data
|
|
|
175 |
* @param {Event} event the drop event
|
|
|
176 |
*/
|
|
|
177 |
drop(dropdata, event) {
|
|
|
178 |
// File handling.
|
|
|
179 |
if (dropdata.type == 'files') {
|
|
|
180 |
this.reactive.uploadFiles(
|
|
|
181 |
this.section.id,
|
|
|
182 |
this.section.number,
|
|
|
183 |
dropdata.files
|
|
|
184 |
);
|
|
|
185 |
return;
|
|
|
186 |
}
|
|
|
187 |
// Call the move mutation.
|
|
|
188 |
if (dropdata.type == 'cm') {
|
|
|
189 |
const mutation = (event.altKey) ? 'cmDuplicate' : 'cmMove';
|
|
|
190 |
this.reactive.dispatch(mutation, [dropdata.id], this.id);
|
|
|
191 |
}
|
|
|
192 |
if (dropdata.type == 'section') {
|
|
|
193 |
this.reactive.dispatch('sectionMoveAfter', [dropdata.id], this.id);
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
}
|