Proyectos de Subversion Moodle

Rev

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