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
            return;
203
        }
204
 
205
 
206
        if (loopstart > loopend) {
207
            // If we're going up, we need to swap the loop order
208
            // because loops can't go backwards.
209
            loopstart = dropnodeindex;
210
            loopend = dragnodeid;
211
        }
212
 
213
        // Get the list of nodes.
214
        drag.get('dragNode').removeClass(CSS.COURSECONTENT);
215
        var sectionlist = Y.Node.all(this.sectionlistselector);
216
 
217
        // Add a lightbox if it's not there.
218
        var lightbox = M.util.add_lightbox(Y, dragnode);
219
 
220
        // Handle any variables which we must pass via AJAX.
221
        var params = {},
222
            pageparams = this.get('config').pageparams,
223
            varname;
224
 
225
        for (varname in pageparams) {
226
            if (!pageparams.hasOwnProperty(varname)) {
227
                continue;
228
            }
229
            params[varname] = pageparams[varname];
230
        }
231
 
232
        // Prepare request parameters
233
        params.sesskey = M.cfg.sesskey;
234
        params.courseId = this.get('courseid');
235
        params['class'] = 'section';
236
        params.field = 'move';
237
        params.id = dragnodeid;
238
        params.value = dropnodeindex;
239
 
240
        // Perform the AJAX request.
241
        var uri = M.cfg.wwwroot + this.get('ajaxurl');
242
        Y.io(uri, {
243
            method: 'POST',
244
            data: params,
245
            on: {
246
                start: function() {
247
                    lightbox.show();
248
                },
249
                success: function(tid, response) {
250
                    // Update section titles, we can't simply swap them as
251
                    // they might have custom title
252
                    try {
253
                        var responsetext = Y.JSON.parse(response.responseText);
254
                        if (responsetext.error) {
255
                            new M.core.ajaxException(responsetext);
256
                        }
257
                        M.course.format.process_sections(Y, sectionlist, responsetext, loopstart, loopend);
258
                    } catch (e) {
259
                        // Ignore.
260
                    }
261
 
262
                    // Update all of the section IDs - first unset them, then set them
263
                    // to avoid duplicates in the DOM.
264
                    var index;
265
 
266
                    // Classic bubble sort algorithm is applied to the section
267
                    // nodes between original drag node location and the new one.
268
                    var swapped = false;
269
                    do {
270
                        swapped = false;
271
                        for (index = loopstart; index <= loopend; index++) {
272
                            if (Y.Moodle.core_course.util.section.getId(sectionlist.item(index - 1)) >
273
                                        Y.Moodle.core_course.util.section.getId(sectionlist.item(index))) {
274
                                // Swap section id.
275
                                var sectionid = sectionlist.item(index - 1).get('id');
276
                                sectionlist.item(index - 1).set('id', sectionlist.item(index).get('id'));
277
                                sectionlist.item(index).set('id', sectionid);
278
 
279
                                // See what format needs to swap.
280
                                M.course.format.swap_sections(Y, index - 1, index);
281
 
282
                                // Update flag.
283
                                swapped = true;
284
                            }
285
                            sectionlist.item(index).setAttribute('data-sectionid',
286
                                Y.Moodle.core_course.util.section.getId(sectionlist.item(index)));
287
                        }
288
                        loopend = loopend - 1;
289
                    } while (swapped);
290
 
291
                    window.setTimeout(function() {
292
                        lightbox.hide();
293
                    }, 250);
294
 
295
                    // Update course state.
296
                    M.course.coursebase.invoke_function('updateMovedSectionState');
297
                },
298
 
299
                failure: function(tid, response) {
300
                    this.ajax_failure(response);
301
                    lightbox.hide();
302
                }
303
            },
304
            context: this
305
        });
306
    }
307
 
308
}, {
309
    NAME: 'course-dragdrop-section',
310
    ATTRS: {
311
        courseid: {
312
            value: null
313
        },
314
        ajaxurl: {
315
            value: 0
316
        },
317
        config: {
318
            value: 0
319
        }
320
    }
321
});
322
 
323
M.course = M.course || {};
324
M.course.init_section_dragdrop = function(params) {
325
    new DRAGSECTION(params);
326
};
327
/**
328
 * Resource drag and drop.
329
 *
330
 * @class M.course.dragdrop.resource
331
 * @constructor
332
 * @extends M.core.dragdrop
333
 */
334
var DRAGRESOURCE = function() {
335
    DRAGRESOURCE.superclass.constructor.apply(this, arguments);
336
};
337
Y.extend(DRAGRESOURCE, M.core.dragdrop, {
338
    initializer: function() {
339
        // Set group for parent class
340
        this.groups = ['resource'];
341
        this.samenodeclass = CSS.ACTIVITY;
342
        this.parentnodeclass = CSS.SECTION;
343
 
344
        this.samenodelabel = {
345
            identifier: 'afterresource',
346
            component: 'moodle'
347
        };
348
        this.parentnodelabel = {
349
            identifier: 'totopofsection',
350
            component: 'moodle'
351
        };
352
 
353
        // Go through all sections
354
        var sectionlistselector = M.course.format.get_section_selector(Y);
355
        if (sectionlistselector) {
356
            sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + sectionlistselector;
357
            this.setup_for_section(sectionlistselector);
358
 
359
            // Initialise drag & drop for all resources/activities
360
            var nodeselector = sectionlistselector.slice(CSS.COURSECONTENT.length + 2) + ' li.' + CSS.ACTIVITY;
361
            var del = new Y.DD.Delegate({
362
                container: '.' + CSS.COURSECONTENT,
363
                nodes: nodeselector,
364
                target: true,
365
                handles: ['.' + CSS.EDITINGMOVE],
366
                dragConfig: {groups: this.groups}
367
            });
368
            del.dd.plug(Y.Plugin.DDProxy, {
369
                // Don't move the node at the end of the drag
370
                moveOnEnd: false,
371
                cloneNode: true
372
            });
373
            del.dd.plug(Y.Plugin.DDConstrained, {
374
                // Keep it inside the .course-content
375
                constrain: '#' + CSS.PAGECONTENT
376
            });
377
            del.dd.plug(Y.Plugin.DDWinScroll);
378
 
379
            M.course.coursebase.register_module(this);
380
            M.course.dragres = this;
381
        }
382
    },
383
 
384
    /**
385
     * Apply dragdrop features to the specified selector or node that refers to section(s)
386
     *
387
     * @method setup_for_section
388
     * @param {String} baseselector The CSS selector or node to limit scope to
389
     */
390
    setup_for_section: function(baseselector) {
391
        Y.Node.all(baseselector).each(function(sectionnode) {
392
            var resources = sectionnode.one('.' + CSS.CONTENT + ' ul.' + CSS.SECTION);
393
            // See if resources ul exists, if not create one
394
            if (!resources) {
395
                resources = Y.Node.create('<ul></ul>');
396
                resources.addClass(CSS.SECTION);
397
                sectionnode.one('.' + CSS.CONTENT + ' div.' + CSS.SUMMARY).insert(resources, 'after');
398
            }
399
            resources.setAttribute('data-draggroups', this.groups.join(' '));
400
            // Define empty ul as droptarget, so that item could be moved to empty list
401
            new Y.DD.Drop({
402
                node: resources,
403
                groups: this.groups,
404
                padding: '20 0 20 0'
405
            });
406
 
407
            // Initialise each resource/activity in this section
408
            this.setup_for_resource('#' + sectionnode.get('id') + ' li.' + CSS.ACTIVITY);
409
        }, this);
410
    },
411
 
412
    /**
413
     * Apply dragdrop features to the specified selector or node that refers to resource(s)
414
     *
415
     * @method setup_for_resource
416
     * @param {String} baseselector The CSS selector or node to limit scope to
417
     */
418
    setup_for_resource: function(baseselector) {
419
        Y.Node.all(baseselector).each(function(resourcesnode) {
420
            var draggroups = resourcesnode.getData('draggroups');
421
            if (!draggroups) {
422
                // This Drop Node has not been set up. Configure it now.
423
                resourcesnode.setAttribute('data-draggroups', this.groups.join(' '));
424
                // Define empty ul as droptarget, so that item could be moved to empty list
425
                new Y.DD.Drop({
426
                    node: resourcesnode,
427
                    groups: this.groups,
428
                    padding: '20 0 20 0'
429
                });
430
            }
431
 
432
            // Replace move icons
433
            var move = resourcesnode.one('a.' + CSS.EDITINGMOVE);
434
            if (move) {
435
                var sr = move.getData('sectionreturn');
436
                move.replace(this.get_drag_handle(M.util.get_string('movecoursemodule', 'moodle'),
437
                             CSS.EDITINGMOVE, CSS.ICONCLASS, true).setAttribute('data-sectionreturn', sr));
438
            }
439
        }, this);
440
    },
441
 
442
    drag_start: function(e) {
443
        // Get our drag object
444
        var drag = e.target;
445
        if (drag.get('dragNode') === drag.get('node')) {
446
            // We do not want to modify the contents of the real node.
447
            // They will be the same during a keyboard drag and drop.
448
            return;
449
        }
450
        drag.get('dragNode').setContent(drag.get('node').get('innerHTML'));
451
        drag.get('dragNode').all('img.iconsmall').setStyle('vertical-align', 'baseline');
452
    },
453
 
454
    drag_dropmiss: function(e) {
455
        // Missed the target, but we assume the user intended to drop it
456
        // on the last last ghost node location, e.drag and e.drop should be
457
        // prepared by global_drag_dropmiss parent so simulate drop_hit(e).
458
        this.drop_hit(e);
459
    },
460
 
461
    drop_hit: function(e) {
462
        var drag = e.drag;
463
        // Get a reference to our drag node
464
        var dragnode = drag.get('node');
465
        var dropnode = e.drop.get('node');
466
 
467
        // Add spinner if it not there
468
        var actionarea = dragnode.one(CSS.ACTIONAREA);
469
        var spinner = M.util.add_spinner(Y, actionarea);
470
 
471
        var params = {};
472
 
473
        // Handle any variables which we must pass back through to
474
        var pageparams = this.get('config').pageparams;
475
        var varname;
476
        for (varname in pageparams) {
477
            params[varname] = pageparams[varname];
478
        }
479
 
480
        // Variables needed to update the course state.
481
        var cmid = Number(Y.Moodle.core_course.util.cm.getId(dragnode));
482
        var beforeid = null;
483
 
484
        // Prepare request parameters
485
        params.sesskey = M.cfg.sesskey;
486
        params.courseId = this.get('courseid');
487
        params['class'] = 'resource';
488
        params.field = 'move';
489
        params.id = cmid;
490
        params.sectionId = Y.Moodle.core_course.util.section.getId(dropnode.ancestor(M.course.format.get_section_wrapper(Y), true));
491
 
492
        if (dragnode.next()) {
493
            beforeid = Number(Y.Moodle.core_course.util.cm.getId(dragnode.next()));
494
            params.beforeId = beforeid;
495
        }
496
 
497
        // Do AJAX request
498
        var uri = M.cfg.wwwroot + this.get('ajaxurl');
499
 
500
        Y.io(uri, {
501
            method: 'POST',
502
            data: params,
503
            on: {
504
                start: function() {
505
                    this.lock_drag_handle(drag, CSS.EDITINGMOVE);
506
                    spinner.show();
507
                },
508
                success: function(tid, response) {
509
                    var responsetext = Y.JSON.parse(response.responseText);
510
                    // Update course state.
511
                    M.course.coursebase.invoke_function(
512
                        'updateMovedCmState',
513
                        {
514
                            cmid: cmid,
515
                            beforeid: beforeid,
516
                            visible: responsetext.visible,
517
                        }
518
                    );
519
                    // Set visibility in course content.
520
                    var params = {element: dragnode, visible: responsetext.visible};
521
                    M.course.coursebase.invoke_function('set_visibility_resource_ui', params);
522
                    this.unlock_drag_handle(drag, CSS.EDITINGMOVE);
523
                    window.setTimeout(function() {
524
                        spinner.hide();
525
                    }, 250);
526
                },
527
                failure: function(tid, response) {
528
                    this.ajax_failure(response);
529
                    this.unlock_drag_handle(drag, CSS.SECTIONHANDLE);
530
                    spinner.hide();
531
                    // TODO: revert nodes location
532
                }
533
            },
534
            context: this
535
        });
536
    }
537
}, {
538
    NAME: 'course-dragdrop-resource',
539
    ATTRS: {
540
        courseid: {
541
            value: null
542
        },
543
        ajaxurl: {
544
            value: 0
545
        },
546
        config: {
547
            value: 0
548
        }
549
    }
550
});
551
 
552
M.course = M.course || {};
553
M.course.init_resource_dragdrop = function(params) {
554
    new DRAGRESOURCE(params);
555
};
556
 
557
 
558
}, '@VERSION@', {
559
    "requires": [
560
        "base",
561
        "node",
562
        "io",
563
        "dom",
564
        "dd",
565
        "dd-scroll",
566
        "moodle-core-dragdrop",
567
        "moodle-core-notification",
568
        "moodle-course-coursebase",
569
        "moodle-course-util"
570
    ]
571
});