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 cm component.
|
|
|
18 |
*
|
|
|
19 |
* This component is used to control specific course modules interactions like drag and drop
|
|
|
20 |
* in both course index and course content.
|
|
|
21 |
*
|
|
|
22 |
* @module core_courseformat/local/courseeditor/dndcmitem
|
|
|
23 |
* @class core_courseformat/local/courseeditor/dndcmitem
|
|
|
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 |
|
|
|
30 |
export default class extends BaseComponent {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Configure the component drag and drop.
|
|
|
34 |
*
|
|
|
35 |
* @param {number} cmid course module id
|
|
|
36 |
*/
|
|
|
37 |
configDragDrop(cmid) {
|
|
|
38 |
|
|
|
39 |
this.id = cmid;
|
|
|
40 |
|
|
|
41 |
// Drag and drop is only available for components compatible course formats.
|
|
|
42 |
if (this.reactive.isEditing && this.reactive.supportComponents) {
|
|
|
43 |
// Init element drag and drop.
|
|
|
44 |
this.dragdrop = new DragDrop(this);
|
|
|
45 |
// Save dropzone classes.
|
|
|
46 |
this.classes = this.dragdrop.getClasses();
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Remove all subcomponents dependencies.
|
|
|
52 |
*/
|
|
|
53 |
destroy() {
|
|
|
54 |
if (this.dragdrop !== undefined) {
|
|
|
55 |
this.dragdrop.unregister();
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Enable or disable the draggable property.
|
|
|
61 |
*
|
|
|
62 |
* @param {bool} value the new draggable value
|
|
|
63 |
*/
|
|
|
64 |
setDraggable(value) {
|
|
|
65 |
this.dragdrop?.setDraggable(value);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// Drag and drop methods.
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* The element drop start hook.
|
|
|
72 |
*
|
|
|
73 |
* @param {Object} dropdata the dropdata
|
|
|
74 |
*/
|
|
|
75 |
dragStart(dropdata) {
|
|
|
76 |
this.reactive.dispatch('cmDrag', [dropdata.id], true);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* The element drop end hook.
|
|
|
81 |
*
|
|
|
82 |
* @param {Object} dropdata the dropdata
|
|
|
83 |
*/
|
|
|
84 |
dragEnd(dropdata) {
|
|
|
85 |
this.reactive.dispatch('cmDrag', [dropdata.id], false);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Get the draggable data of this component.
|
|
|
90 |
*
|
|
|
91 |
* @returns {Object} exported course module drop data
|
|
|
92 |
*/
|
|
|
93 |
getDraggableData() {
|
|
|
94 |
const exporter = this.reactive.getExporter();
|
|
|
95 |
return exporter.cmDraggableData(this.reactive.state, this.id);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Validate if the drop data can be dropped over the component.
|
|
|
100 |
*
|
|
|
101 |
* @param {Object} dropdata the exported drop data.
|
|
|
102 |
* @returns {boolean}
|
|
|
103 |
*/
|
|
|
104 |
validateDropData(dropdata) {
|
|
|
105 |
if (dropdata?.type !== 'cm') {
|
|
|
106 |
return false;
|
|
|
107 |
}
|
|
|
108 |
// Prevent delegated sections loops.
|
|
|
109 |
if (dropdata?.delegatesection === true) {
|
|
|
110 |
const mycminfo = this.reactive.get('cm', this.id);
|
|
|
111 |
const mysection = this.reactive.get('section', mycminfo.sectionid);
|
|
|
112 |
if (mysection?.component !== null) {
|
|
|
113 |
return false;
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
return true;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Display the component dropzone.
|
|
|
121 |
*
|
|
|
122 |
* @param {Object} dropdata the accepted drop data
|
|
|
123 |
*/
|
|
|
124 |
showDropZone(dropdata) {
|
|
|
125 |
// If we are the next cmid of the dragged element we accept the drop because otherwise it
|
|
|
126 |
// will get captured by the section. However, we won't trigger any mutation.
|
|
|
127 |
if (dropdata.nextcmid != this.id && dropdata.id != this.id) {
|
|
|
128 |
this.element.classList.add(this.classes.DROPUP);
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Hide the component dropzone.
|
|
|
134 |
*/
|
|
|
135 |
hideDropZone() {
|
|
|
136 |
this.element.classList.remove(this.classes.DROPUP);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Drop event handler.
|
|
|
141 |
*
|
|
|
142 |
* @param {Object} dropdata the accepted drop data
|
|
|
143 |
* @param {Event} event the drop event
|
|
|
144 |
*/
|
|
|
145 |
drop(dropdata, event) {
|
|
|
146 |
// Call the move mutation if necessary.
|
|
|
147 |
if (dropdata.id != this.id && dropdata.nextcmid != this.id) {
|
|
|
148 |
const mutation = (event.altKey) ? 'cmDuplicate' : 'cmMove';
|
|
|
149 |
this.reactive.dispatch(mutation, [dropdata.id], null, this.id);
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
}
|