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
YUI.add('moodle-course-dragdrop', function (Y, NAME) {
2
 
3
/* eslint-disable no-unused-vars */
4
/**
5
 * Drag and Drop for course sections and course modules.
6
 *
1441 ariadna 7
 * TODO: remove this module as part of MDL-83627.
8
 *
1 efrain 9
 * @module moodle-course-dragdrop
10
 */
11
 
1441 ariadna 12
 
1 efrain 13
var CSS = {
14
    ACTIONAREA: '.actions',
15
    ACTIVITY: 'activity',
16
    ACTIVITYINSTANCE: 'activityinstance',
17
    CONTENT: 'content',
18
    COURSECONTENT: 'course-content',
19
    EDITINGMOVE: 'editing_move',
20
    ICONCLASS: 'iconsmall',
21
    JUMPMENU: 'jumpmenu',
22
    LEFT: 'left',
23
    LIGHTBOX: 'lightbox',
24
    MOVEDOWN: 'movedown',
25
    MOVEUP: 'moveup',
26
    PAGECONTENT: 'page-content',
27
    RIGHT: 'right',
28
    SECTION: 'section',
29
    SECTIONADDMENUS: 'section_add_menus',
30
    SECTIONHANDLE: 'section-handle',
31
    SUMMARY: 'summary',
32
    SECTIONDRAGGABLE: 'sectiondraggable'
33
};
34
 
35
M.course = M.course || {};
36
/**
37
 * Section drag and drop.
38
 *
1441 ariadna 39
 * TODO: remove this module as part of MDL-83627.
40
 *
1 efrain 41
 * @class M.course.dragdrop.section
42
 * @constructor
43
 * @extends M.core.dragdrop
44
 */
45
var DRAGSECTION = function() {
46
    DRAGSECTION.superclass.constructor.apply(this, arguments);
47
};
1441 ariadna 48
 
49
 
1 efrain 50
Y.extend(DRAGSECTION, M.core.dragdrop, {
51
    sectionlistselector: null,
52
 
53
    initializer: function() {
54
        // Set group for parent class
55
        this.groups = [CSS.SECTIONDRAGGABLE];
56
        this.samenodeclass = M.course.format.get_sectionwrapperclass();
57
        this.parentnodeclass = M.course.format.get_containerclass();
58
        // Detect the direction of travel.
59
        this.detectkeyboarddirection = true;
60
 
61
        // Check if we are in single section mode
62
        if (Y.Node.one('.' + CSS.JUMPMENU)) {
63
            return false;
64
        }
65
        // Initialise sections dragging
66
        this.sectionlistselector = M.course.format.get_section_wrapper(Y);
67
        if (this.sectionlistselector) {
68
            this.sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + this.sectionlistselector;
69
 
70
            this.setup_for_section(this.sectionlistselector);
71
 
72
            // Make each li element in the lists of sections draggable
73
            var del = new Y.DD.Delegate({
74
                container: '.' + CSS.COURSECONTENT,
75
                nodes: '.' + CSS.SECTIONDRAGGABLE,
76
                target: true,
77
                handles: ['.' + CSS.LEFT],
78
                dragConfig: {groups: this.groups}
79
            });
80
            del.dd.plug(Y.Plugin.DDProxy, {
81
                // Don't move the node at the end of the drag
82
                moveOnEnd: false
83
            });
84
            del.dd.plug(Y.Plugin.DDConstrained, {
85
                // Keep it inside the .course-content
86
                constrain: '#' + CSS.PAGECONTENT,
87
                stickY: true
88
            });
89
            del.dd.plug(Y.Plugin.DDWinScroll);
90
        }
91
    },
92
 
93
     /**
94
     * Apply dragdrop features to the specified selector or node that refers to section(s)
95
     *
96
     * @method setup_for_section
97
     * @param {String} baseselector The CSS selector or node to limit scope to
98
     */
99
    setup_for_section: function(baseselector) {
100
        Y.Node.all(baseselector).each(function(sectionnode) {
101
            // Determine the section ID
102
            var sectionid = Y.Moodle.core_course.util.section.getId(sectionnode);
103
 
104
            // We skip the top section as it is not draggable
105
            if (sectionid > 0) {
106
                // Remove move icons
107
                var movedown = sectionnode.one('.' + CSS.RIGHT + ' a.' + CSS.MOVEDOWN);
108
                var moveup = sectionnode.one('.' + CSS.RIGHT + ' a.' + CSS.MOVEUP);
109
 
110
                // Add dragger icon
111
                var title = M.util.get_string('movesection', 'moodle', sectionid);
112
                var cssleft = sectionnode.one('.' + CSS.LEFT);
113
 
114
                if ((movedown || moveup) && cssleft) {
115
                    cssleft.setStyle('cursor', 'move');
116
                    cssleft.appendChild(this.get_drag_handle(title, CSS.SECTIONHANDLE, 'icon', true));
117
 
118
                    if (moveup) {
119
                        if (moveup.previous('br')) {
120
                            moveup.previous('br').remove();
121
                        } else if (moveup.next('br')) {
122
                            moveup.next('br').remove();
123
                        }
124
 
125
                        if (moveup.ancestor('.section_action_menu') && moveup.ancestor().get('nodeName').toLowerCase() == 'li') {
126
                            moveup.ancestor().remove();
127
                        } else {
128
                            moveup.remove();
129
                        }
130
                    }
131
                    if (movedown) {
132
                        if (movedown.previous('br')) {
133
                            movedown.previous('br').remove();
134
                        } else if (movedown.next('br')) {
135
                            movedown.next('br').remove();
136
                        }
137
 
138
                        var movedownParentType = movedown.ancestor().get('nodeName').toLowerCase();
139
                        if (movedown.ancestor('.section_action_menu') && movedownParentType == 'li') {
140
                            movedown.ancestor().remove();
141
                        } else {
142
                            movedown.remove();
143
                        }
144
                    }
145
 
146
                    // This section can be moved - add the class to indicate this to Y.DD.
147
                    sectionnode.addClass(CSS.SECTIONDRAGGABLE);
148
                }
149
            }
150
        }, this);
151
    },
152
 
153
    /*
154
     * Drag-dropping related functions
155
     */
156
    drag_start: function(e) {
157
        // Get our drag object
158
        var drag = e.target;
159
        // This is the node that the user started to drag.
160
        var node = drag.get('node');
161
        // This is the container node that will follow the mouse around,
162
        // or during a keyboard drag and drop the original node.
163
        var dragnode = drag.get('dragNode');
164
        if (node === dragnode) {
165
            return;
166
        }
167
        // Creat a dummy structure of the outer elemnents for clean styles application
168
        var containernode = Y.Node.create('<' + M.course.format.get_containernode() +
169
                '></' + M.course.format.get_containernode() + '>');
170
        containernode.addClass(M.course.format.get_containerclass());
171
        var sectionnode = Y.Node.create('<' + M.course.format.get_sectionwrappernode() +
172
                '></' + M.course.format.get_sectionwrappernode() + '>');
173
        sectionnode.addClass(M.course.format.get_sectionwrapperclass());
174
        sectionnode.setStyle('margin', 0);
175
        sectionnode.setContent(node.get('innerHTML'));
176
        containernode.appendChild(sectionnode);
177
        dragnode.setContent(containernode);
178
        dragnode.addClass(CSS.COURSECONTENT);
179
    },
180
 
181
    drag_dropmiss: function(e) {
182
        // Missed the target, but we assume the user intended to drop it
183
        // on the last last ghost node location, e.drag and e.drop should be
184
        // prepared by global_drag_dropmiss parent so simulate drop_hit(e).
185
        this.drop_hit(e);
186
    },
187
 
188
    get_section_index: function(node) {
189
        var sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + M.course.format.get_section_selector(Y),
190
            sectionList = Y.all(sectionlistselector),
191
            nodeIndex = sectionList.indexOf(node),
192
            zeroIndex = sectionList.indexOf(Y.one('#section-0'));
193
 
194
        return (nodeIndex - zeroIndex);
195
    },
196
 
197
    drop_hit: function(e) {
198
        var drag = e.drag;
199
 
200
        // Get references to our nodes and their IDs.
201
        var dragnode = drag.get('node'),
202
            dragnodeid = Y.Moodle.core_course.util.section.getId(dragnode),
203
            loopstart = dragnodeid,
204
 
205
            dropnodeindex = this.get_section_index(dragnode),
206
            loopend = dropnodeindex;
207
 
208
        if (dragnodeid === dropnodeindex) {
209
            return;
210
        }
211
 
212
 
213
        if (loopstart > loopend) {
214
            // If we're going up, we need to swap the loop order
215
            // because loops can't go backwards.
216
            loopstart = dropnodeindex;
217
            loopend = dragnodeid;
218
        }
219
 
220
        // Get the list of nodes.
221
        drag.get('dragNode').removeClass(CSS.COURSECONTENT);
222
        var sectionlist = Y.Node.all(this.sectionlistselector);
223
 
224
        // Add a lightbox if it's not there.
225
        var lightbox = M.util.add_lightbox(Y, dragnode);
226
 
227
        // Handle any variables which we must pass via AJAX.
228
        var params = {},
229
            pageparams = this.get('config').pageparams,
230
            varname;
231
 
232
        for (varname in pageparams) {
233
            if (!pageparams.hasOwnProperty(varname)) {
234
                continue;
235
            }
236
            params[varname] = pageparams[varname];
237
        }
238
 
239
        // Prepare request parameters
240
        params.sesskey = M.cfg.sesskey;
241
        params.courseId = this.get('courseid');
242
        params['class'] = 'section';
243
        params.field = 'move';
244
        params.id = dragnodeid;
245
        params.value = dropnodeindex;
246
 
247
        // Perform the AJAX request.
248
        var uri = M.cfg.wwwroot + this.get('ajaxurl');
249
        Y.io(uri, {
250
            method: 'POST',
251
            data: params,
252
            on: {
253
                start: function() {
254
                    lightbox.show();
255
                },
256
                success: function(tid, response) {
257
                    // Update section titles, we can't simply swap them as
258
                    // they might have custom title
259
                    try {
260
                        var responsetext = Y.JSON.parse(response.responseText);
261
                        if (responsetext.error) {
262
                            new M.core.ajaxException(responsetext);
263
                        }
264
                        M.course.format.process_sections(Y, sectionlist, responsetext, loopstart, loopend);
265
                    } catch (e) {
266
                        // Ignore.
267
                    }
268
 
269
                    // Update all of the section IDs - first unset them, then set them
270
                    // to avoid duplicates in the DOM.
271
                    var index;
272
 
273
                    // Classic bubble sort algorithm is applied to the section
274
                    // nodes between original drag node location and the new one.
275
                    var swapped = false;
276
                    do {
277
                        swapped = false;
278
                        for (index = loopstart; index <= loopend; index++) {
279
                            if (Y.Moodle.core_course.util.section.getId(sectionlist.item(index - 1)) >
280
                                        Y.Moodle.core_course.util.section.getId(sectionlist.item(index))) {
281
                                // Swap section id.
282
                                var sectionid = sectionlist.item(index - 1).get('id');
283
                                sectionlist.item(index - 1).set('id', sectionlist.item(index).get('id'));
284
                                sectionlist.item(index).set('id', sectionid);
285
 
286
                                // See what format needs to swap.
287
                                M.course.format.swap_sections(Y, index - 1, index);
288
 
289
                                // Update flag.
290
                                swapped = true;
291
                            }
292
                            sectionlist.item(index).setAttribute('data-sectionid',
293
                                Y.Moodle.core_course.util.section.getId(sectionlist.item(index)));
294
                        }
295
                        loopend = loopend - 1;
296
                    } while (swapped);
297
 
298
                    window.setTimeout(function() {
299
                        lightbox.hide();
300
                    }, 250);
301
 
302
                    // Update course state.
303
                    M.course.coursebase.invoke_function('updateMovedSectionState');
304
                },
305
 
306
                failure: function(tid, response) {
307
                    this.ajax_failure(response);
308
                    lightbox.hide();
309
                }
310
            },
311
            context: this
312
        });
313
    }
314
 
315
}, {
316
    NAME: 'course-dragdrop-section',
317
    ATTRS: {
318
        courseid: {
319
            value: null
320
        },
321
        ajaxurl: {
322
            value: 0
323
        },
324
        config: {
325
            value: 0
326
        }
327
    }
328
});
329
 
330
M.course = M.course || {};
331
M.course.init_section_dragdrop = function(params) {
332
    new DRAGSECTION(params);
333
};
334
/**
335
 * Resource drag and drop.
336
 *
1441 ariadna 337
 * TODO: remove this module as part of MDL-83627.
338
 *
1 efrain 339
 * @class M.course.dragdrop.resource
340
 * @constructor
341
 * @extends M.core.dragdrop
342
 */
343
var DRAGRESOURCE = function() {
344
    DRAGRESOURCE.superclass.constructor.apply(this, arguments);
345
};
1441 ariadna 346
 
347
 
1 efrain 348
Y.extend(DRAGRESOURCE, M.core.dragdrop, {
349
    initializer: function() {
350
        // Set group for parent class
351
        this.groups = ['resource'];
352
        this.samenodeclass = CSS.ACTIVITY;
353
        this.parentnodeclass = CSS.SECTION;
354
 
355
        this.samenodelabel = {
356
            identifier: 'afterresource',
357
            component: 'moodle'
358
        };
359
        this.parentnodelabel = {
360
            identifier: 'totopofsection',
361
            component: 'moodle'
362
        };
363
 
364
        // Go through all sections
365
        var sectionlistselector = M.course.format.get_section_selector(Y);
366
        if (sectionlistselector) {
367
            sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + sectionlistselector;
368
            this.setup_for_section(sectionlistselector);
369
 
370
            // Initialise drag & drop for all resources/activities
371
            var nodeselector = sectionlistselector.slice(CSS.COURSECONTENT.length + 2) + ' li.' + CSS.ACTIVITY;
372
            var del = new Y.DD.Delegate({
373
                container: '.' + CSS.COURSECONTENT,
374
                nodes: nodeselector,
375
                target: true,
376
                handles: ['.' + CSS.EDITINGMOVE],
377
                dragConfig: {groups: this.groups}
378
            });
379
            del.dd.plug(Y.Plugin.DDProxy, {
380
                // Don't move the node at the end of the drag
381
                moveOnEnd: false,
382
                cloneNode: true
383
            });
384
            del.dd.plug(Y.Plugin.DDConstrained, {
385
                // Keep it inside the .course-content
386
                constrain: '#' + CSS.PAGECONTENT
387
            });
388
            del.dd.plug(Y.Plugin.DDWinScroll);
389
 
390
            M.course.coursebase.register_module(this);
391
            M.course.dragres = this;
392
        }
393
    },
394
 
395
    /**
396
     * Apply dragdrop features to the specified selector or node that refers to section(s)
397
     *
398
     * @method setup_for_section
399
     * @param {String} baseselector The CSS selector or node to limit scope to
400
     */
401
    setup_for_section: function(baseselector) {
402
        Y.Node.all(baseselector).each(function(sectionnode) {
403
            var resources = sectionnode.one('.' + CSS.CONTENT + ' ul.' + CSS.SECTION);
404
            // See if resources ul exists, if not create one
405
            if (!resources) {
406
                resources = Y.Node.create('<ul></ul>');
407
                resources.addClass(CSS.SECTION);
408
                sectionnode.one('.' + CSS.CONTENT + ' div.' + CSS.SUMMARY).insert(resources, 'after');
409
            }
410
            resources.setAttribute('data-draggroups', this.groups.join(' '));
411
            // Define empty ul as droptarget, so that item could be moved to empty list
412
            new Y.DD.Drop({
413
                node: resources,
414
                groups: this.groups,
415
                padding: '20 0 20 0'
416
            });
417
 
418
            // Initialise each resource/activity in this section
419
            this.setup_for_resource('#' + sectionnode.get('id') + ' li.' + CSS.ACTIVITY);
420
        }, this);
421
    },
422
 
423
    /**
424
     * Apply dragdrop features to the specified selector or node that refers to resource(s)
425
     *
426
     * @method setup_for_resource
427
     * @param {String} baseselector The CSS selector or node to limit scope to
428
     */
429
    setup_for_resource: function(baseselector) {
430
        Y.Node.all(baseselector).each(function(resourcesnode) {
431
            var draggroups = resourcesnode.getData('draggroups');
432
            if (!draggroups) {
433
                // This Drop Node has not been set up. Configure it now.
434
                resourcesnode.setAttribute('data-draggroups', this.groups.join(' '));
435
                // Define empty ul as droptarget, so that item could be moved to empty list
436
                new Y.DD.Drop({
437
                    node: resourcesnode,
438
                    groups: this.groups,
439
                    padding: '20 0 20 0'
440
                });
441
            }
442
 
443
            // Replace move icons
444
            var move = resourcesnode.one('a.' + CSS.EDITINGMOVE);
445
            if (move) {
446
                var sr = move.getData('sectionreturn');
447
                move.replace(this.get_drag_handle(M.util.get_string('movecoursemodule', 'moodle'),
448
                             CSS.EDITINGMOVE, CSS.ICONCLASS, true).setAttribute('data-sectionreturn', sr));
449
            }
450
        }, this);
451
    },
452
 
453
    drag_start: function(e) {
454
        // Get our drag object
455
        var drag = e.target;
456
        if (drag.get('dragNode') === drag.get('node')) {
457
            // We do not want to modify the contents of the real node.
458
            // They will be the same during a keyboard drag and drop.
459
            return;
460
        }
461
        drag.get('dragNode').setContent(drag.get('node').get('innerHTML'));
462
        drag.get('dragNode').all('img.iconsmall').setStyle('vertical-align', 'baseline');
463
    },
464
 
465
    drag_dropmiss: function(e) {
466
        // Missed the target, but we assume the user intended to drop it
467
        // on the last last ghost node location, e.drag and e.drop should be
468
        // prepared by global_drag_dropmiss parent so simulate drop_hit(e).
469
        this.drop_hit(e);
470
    },
471
 
472
    drop_hit: function(e) {
473
        var drag = e.drag;
474
        // Get a reference to our drag node
475
        var dragnode = drag.get('node');
476
        var dropnode = e.drop.get('node');
477
 
478
        // Add spinner if it not there
479
        var actionarea = dragnode.one(CSS.ACTIONAREA);
480
        var spinner = M.util.add_spinner(Y, actionarea);
481
 
482
        var params = {};
483
 
484
        // Handle any variables which we must pass back through to
485
        var pageparams = this.get('config').pageparams;
486
        var varname;
487
        for (varname in pageparams) {
488
            params[varname] = pageparams[varname];
489
        }
490
 
491
        // Variables needed to update the course state.
492
        var cmid = Number(Y.Moodle.core_course.util.cm.getId(dragnode));
493
        var beforeid = null;
494
 
495
        // Prepare request parameters
496
        params.sesskey = M.cfg.sesskey;
497
        params.courseId = this.get('courseid');
498
        params['class'] = 'resource';
499
        params.field = 'move';
500
        params.id = cmid;
501
        params.sectionId = Y.Moodle.core_course.util.section.getId(dropnode.ancestor(M.course.format.get_section_wrapper(Y), true));
502
 
503
        if (dragnode.next()) {
504
            beforeid = Number(Y.Moodle.core_course.util.cm.getId(dragnode.next()));
505
            params.beforeId = beforeid;
506
        }
507
 
508
        // Do AJAX request
509
        var uri = M.cfg.wwwroot + this.get('ajaxurl');
510
 
511
        Y.io(uri, {
512
            method: 'POST',
513
            data: params,
514
            on: {
515
                start: function() {
516
                    this.lock_drag_handle(drag, CSS.EDITINGMOVE);
517
                    spinner.show();
518
                },
519
                success: function(tid, response) {
520
                    var responsetext = Y.JSON.parse(response.responseText);
521
                    // Update course state.
522
                    M.course.coursebase.invoke_function(
523
                        'updateMovedCmState',
524
                        {
525
                            cmid: cmid,
526
                            beforeid: beforeid,
527
                            visible: responsetext.visible,
528
                        }
529
                    );
530
                    // Set visibility in course content.
531
                    var params = {element: dragnode, visible: responsetext.visible};
532
                    M.course.coursebase.invoke_function('set_visibility_resource_ui', params);
533
                    this.unlock_drag_handle(drag, CSS.EDITINGMOVE);
534
                    window.setTimeout(function() {
535
                        spinner.hide();
536
                    }, 250);
537
                },
538
                failure: function(tid, response) {
539
                    this.ajax_failure(response);
540
                    this.unlock_drag_handle(drag, CSS.SECTIONHANDLE);
541
                    spinner.hide();
542
                    // TODO: revert nodes location
543
                }
544
            },
545
            context: this
546
        });
547
    }
548
}, {
549
    NAME: 'course-dragdrop-resource',
550
    ATTRS: {
551
        courseid: {
552
            value: null
553
        },
554
        ajaxurl: {
555
            value: 0
556
        },
557
        config: {
558
            value: 0
559
        }
560
    }
561
});
562
 
563
M.course = M.course || {};
564
M.course.init_resource_dragdrop = function(params) {
565
    new DRAGRESOURCE(params);
566
};
567
 
568
 
569
}, '@VERSION@', {
570
    "requires": [
571
        "base",
572
        "node",
573
        "io",
574
        "dom",
575
        "dd",
576
        "dd-scroll",
577
        "moodle-core-dragdrop",
578
        "moodle-core-notification",
579
        "moodle-course-coursebase",
580
        "moodle-course-util"
581
    ]
582
});