Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
1441 ariadna 87
    /**
88
     * Get a fallback element when there is no CM in the section.
89
     *
90
     * This is used to show the correct dropzone position.
91
     *
92
     * @returns {element|null} the las course module element of the section.
93
     */
94
    getLastCmFallback() {
95
        return null;
96
    }
97
 
1 efrain 98
    // Drag and drop methods.
99
 
100
    /**
101
     * The element drop start hook.
102
     *
103
     * @param {Object} dropdata the dropdata
104
     */
105
    dragStart(dropdata) {
106
        this.reactive.dispatch('sectionDrag', [dropdata.id], true);
107
    }
108
 
109
    /**
110
     * The element drop end hook.
111
     *
112
     * @param {Object} dropdata the dropdata
113
     */
114
    dragEnd(dropdata) {
115
        this.reactive.dispatch('sectionDrag', [dropdata.id], false);
116
    }
117
 
118
    /**
119
     * Validate if the drop data can be dropped over the component.
120
     *
121
     * @param {Object} dropdata the exported drop data.
122
     * @returns {boolean}
123
     */
124
    validateDropData(dropdata) {
125
        // We accept files.
126
        if (dropdata?.type === 'files') {
127
            return true;
128
        }
129
        // We accept any course module unless it can form a subsection loop.
130
        if (dropdata?.type === 'cm') {
1441 ariadna 131
            if (this.section?.component && dropdata?.hasdelegatedsection === true) {
1 efrain 132
                return false;
133
            }
134
            return true;
135
        }
136
        if (dropdata?.type === 'section') {
1441 ariadna 137
            // Sections controlled by a plugin cannot accept sections.
138
            if (this.section.component !== null) {
139
                return false;
140
            }
141
            // We accept any section but yourself and the next one.
1 efrain 142
            return dropdata?.id != this.id && dropdata?.number != this.section.number + 1;
143
        }
144
        return false;
145
    }
146
 
147
    /**
148
     * Display the component dropzone.
149
     *
150
     * @param {Object} dropdata the accepted drop data
151
     */
152
    showDropZone(dropdata) {
153
        if (dropdata.type == 'files') {
154
            this.addOverlay({
155
                content: getString('addfilehere', 'core'),
156
                icon: Templates.renderPix('t/download', 'core'),
157
            }).then(() => {
158
                // Check if we still need the file dropzone.
159
                if (!this.dragdrop?.isDropzoneVisible()) {
160
                    this.removeOverlay();
161
                }
162
                return;
163
            }).catch((error) => {
164
                throw error;
165
            });
166
        }
167
        if (dropdata.type == 'cm') {
1441 ariadna 168
            const lastCm = this.getLastCm();
169
            lastCm?.classList.add(this.classes.DROPDOWN);
170
            if (!lastCm) {
171
                this.getLastCmFallback()?.classList.add(this.classes.DROPDOWN);
172
            }
1 efrain 173
        }
174
        if (dropdata.type == 'section') {
175
            this.element.classList.remove(this.classes.DROPUP);
176
            this.element.classList.add(this.classes.DROPDOWN);
177
        }
178
    }
179
 
180
    /**
181
     * Hide the component dropzone.
182
     */
183
    hideDropZone() {
184
        this.getLastCm()?.classList.remove(this.classes.DROPDOWN);
1441 ariadna 185
        this.getLastCmFallback()?.classList.remove(this.classes.DROPDOWN);
1 efrain 186
        this.element.classList.remove(this.classes.DROPUP);
187
        this.element.classList.remove(this.classes.DROPDOWN);
188
        this.removeOverlay();
189
    }
190
 
191
    /**
192
     * Drop event handler.
193
     *
194
     * @param {Object} dropdata the accepted drop data
195
     * @param {Event} event the drop event
196
     */
197
    drop(dropdata, event) {
198
        // File handling.
199
        if (dropdata.type == 'files') {
200
            this.reactive.uploadFiles(
201
                this.section.id,
202
                this.section.number,
203
                dropdata.files
204
            );
205
            return;
206
        }
207
        // Call the move mutation.
208
        if (dropdata.type == 'cm') {
209
            const mutation = (event.altKey) ? 'cmDuplicate' : 'cmMove';
210
            this.reactive.dispatch(mutation, [dropdata.id], this.id);
211
        }
212
        if (dropdata.type == 'section') {
213
            this.reactive.dispatch('sectionMoveAfter', [dropdata.id], this.id);
214
        }
215
    }
216
}