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
/**
2
 * Section drag and drop.
3
 *
1441 ariadna 4
 * TODO: remove this module as part of MDL-83627.
5
 *
1 efrain 6
 * @class M.course.dragdrop.section
7
 * @constructor
8
 * @extends M.core.dragdrop
9
 */
10
var DRAGSECTION = function() {
11
    DRAGSECTION.superclass.constructor.apply(this, arguments);
12
};
1441 ariadna 13
 
14
Y.log(
15
    'YUI M.course.dragdrop.section is deprecated. Please, add support_components to your course format.',
16
    'warn',
17
    'moodle-course-coursebase'
18
);
19
 
1 efrain 20
Y.extend(DRAGSECTION, M.core.dragdrop, {
21
    sectionlistselector: null,
22
 
23
    initializer: function() {
24
        // Set group for parent class
25
        this.groups = [CSS.SECTIONDRAGGABLE];
26
        this.samenodeclass = M.course.format.get_sectionwrapperclass();
27
        this.parentnodeclass = M.course.format.get_containerclass();
28
        // Detect the direction of travel.
29
        this.detectkeyboarddirection = true;
30
 
31
        // Check if we are in single section mode
32
        if (Y.Node.one('.' + CSS.JUMPMENU)) {
33
            return false;
34
        }
35
        // Initialise sections dragging
36
        this.sectionlistselector = M.course.format.get_section_wrapper(Y);
37
        if (this.sectionlistselector) {
38
            this.sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + this.sectionlistselector;
39
 
40
            this.setup_for_section(this.sectionlistselector);
41
 
42
            // Make each li element in the lists of sections draggable
43
            var del = new Y.DD.Delegate({
44
                container: '.' + CSS.COURSECONTENT,
45
                nodes: '.' + CSS.SECTIONDRAGGABLE,
46
                target: true,
47
                handles: ['.' + CSS.LEFT],
48
                dragConfig: {groups: this.groups}
49
            });
50
            del.dd.plug(Y.Plugin.DDProxy, {
51
                // Don't move the node at the end of the drag
52
                moveOnEnd: false
53
            });
54
            del.dd.plug(Y.Plugin.DDConstrained, {
55
                // Keep it inside the .course-content
56
                constrain: '#' + CSS.PAGECONTENT,
57
                stickY: true
58
            });
59
            del.dd.plug(Y.Plugin.DDWinScroll);
60
        }
61
    },
62
 
63
     /**
64
     * Apply dragdrop features to the specified selector or node that refers to section(s)
65
     *
66
     * @method setup_for_section
67
     * @param {String} baseselector The CSS selector or node to limit scope to
68
     */
69
    setup_for_section: function(baseselector) {
70
        Y.Node.all(baseselector).each(function(sectionnode) {
71
            // Determine the section ID
72
            var sectionid = Y.Moodle.core_course.util.section.getId(sectionnode);
73
 
74
            // We skip the top section as it is not draggable
75
            if (sectionid > 0) {
76
                // Remove move icons
77
                var movedown = sectionnode.one('.' + CSS.RIGHT + ' a.' + CSS.MOVEDOWN);
78
                var moveup = sectionnode.one('.' + CSS.RIGHT + ' a.' + CSS.MOVEUP);
79
 
80
                // Add dragger icon
81
                var title = M.util.get_string('movesection', 'moodle', sectionid);
82
                var cssleft = sectionnode.one('.' + CSS.LEFT);
83
 
84
                if ((movedown || moveup) && cssleft) {
85
                    cssleft.setStyle('cursor', 'move');
86
                    cssleft.appendChild(this.get_drag_handle(title, CSS.SECTIONHANDLE, 'icon', true));
87
 
88
                    if (moveup) {
89
                        if (moveup.previous('br')) {
90
                            moveup.previous('br').remove();
91
                        } else if (moveup.next('br')) {
92
                            moveup.next('br').remove();
93
                        }
94
 
95
                        if (moveup.ancestor('.section_action_menu') && moveup.ancestor().get('nodeName').toLowerCase() == 'li') {
96
                            moveup.ancestor().remove();
97
                        } else {
98
                            moveup.remove();
99
                        }
100
                    }
101
                    if (movedown) {
102
                        if (movedown.previous('br')) {
103
                            movedown.previous('br').remove();
104
                        } else if (movedown.next('br')) {
105
                            movedown.next('br').remove();
106
                        }
107
 
108
                        var movedownParentType = movedown.ancestor().get('nodeName').toLowerCase();
109
                        if (movedown.ancestor('.section_action_menu') && movedownParentType == 'li') {
110
                            movedown.ancestor().remove();
111
                        } else {
112
                            movedown.remove();
113
                        }
114
                    }
115
 
116
                    // This section can be moved - add the class to indicate this to Y.DD.
117
                    sectionnode.addClass(CSS.SECTIONDRAGGABLE);
118
                }
119
            }
120
        }, this);
121
    },
122
 
123
    /*
124
     * Drag-dropping related functions
125
     */
126
    drag_start: function(e) {
127
        // Get our drag object
128
        var drag = e.target;
129
        // This is the node that the user started to drag.
130
        var node = drag.get('node');
131
        // This is the container node that will follow the mouse around,
132
        // or during a keyboard drag and drop the original node.
133
        var dragnode = drag.get('dragNode');
134
        if (node === dragnode) {
135
            return;
136
        }
137
        // Creat a dummy structure of the outer elemnents for clean styles application
138
        var containernode = Y.Node.create('<' + M.course.format.get_containernode() +
139
                '></' + M.course.format.get_containernode() + '>');
140
        containernode.addClass(M.course.format.get_containerclass());
141
        var sectionnode = Y.Node.create('<' + M.course.format.get_sectionwrappernode() +
142
                '></' + M.course.format.get_sectionwrappernode() + '>');
143
        sectionnode.addClass(M.course.format.get_sectionwrapperclass());
144
        sectionnode.setStyle('margin', 0);
145
        sectionnode.setContent(node.get('innerHTML'));
146
        containernode.appendChild(sectionnode);
147
        dragnode.setContent(containernode);
148
        dragnode.addClass(CSS.COURSECONTENT);
149
    },
150
 
151
    drag_dropmiss: function(e) {
152
        // Missed the target, but we assume the user intended to drop it
153
        // on the last last ghost node location, e.drag and e.drop should be
154
        // prepared by global_drag_dropmiss parent so simulate drop_hit(e).
155
        this.drop_hit(e);
156
    },
157
 
158
    get_section_index: function(node) {
159
        var sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + M.course.format.get_section_selector(Y),
160
            sectionList = Y.all(sectionlistselector),
161
            nodeIndex = sectionList.indexOf(node),
162
            zeroIndex = sectionList.indexOf(Y.one('#section-0'));
163
 
164
        return (nodeIndex - zeroIndex);
165
    },
166
 
167
    drop_hit: function(e) {
168
        var drag = e.drag;
169
 
170
        // Get references to our nodes and their IDs.
171
        var dragnode = drag.get('node'),
172
            dragnodeid = Y.Moodle.core_course.util.section.getId(dragnode),
173
            loopstart = dragnodeid,
174
 
175
            dropnodeindex = this.get_section_index(dragnode),
176
            loopend = dropnodeindex;
177
 
178
        if (dragnodeid === dropnodeindex) {
179
            Y.log("Skipping move - same location moving " + dragnodeid + " to " + dropnodeindex, 'debug', 'moodle-course-dragdrop');
180
            return;
181
        }
182
 
183
        Y.log("Moving from position " + dragnodeid + " to position " + dropnodeindex, 'debug', 'moodle-course-dragdrop');
184
 
185
        if (loopstart > loopend) {
186
            // If we're going up, we need to swap the loop order
187
            // because loops can't go backwards.
188
            loopstart = dropnodeindex;
189
            loopend = dragnodeid;
190
        }
191
 
192
        // Get the list of nodes.
193
        drag.get('dragNode').removeClass(CSS.COURSECONTENT);
194
        var sectionlist = Y.Node.all(this.sectionlistselector);
195
 
196
        // Add a lightbox if it's not there.
197
        var lightbox = M.util.add_lightbox(Y, dragnode);
198
 
199
        // Handle any variables which we must pass via AJAX.
200
        var params = {},
201
            pageparams = this.get('config').pageparams,
202
            varname;
203
 
204
        for (varname in pageparams) {
205
            if (!pageparams.hasOwnProperty(varname)) {
206
                continue;
207
            }
208
            params[varname] = pageparams[varname];
209
        }
210
 
211
        // Prepare request parameters
212
        params.sesskey = M.cfg.sesskey;
213
        params.courseId = this.get('courseid');
214
        params['class'] = 'section';
215
        params.field = 'move';
216
        params.id = dragnodeid;
217
        params.value = dropnodeindex;
218
 
219
        // Perform the AJAX request.
220
        var uri = M.cfg.wwwroot + this.get('ajaxurl');
221
        Y.io(uri, {
222
            method: 'POST',
223
            data: params,
224
            on: {
225
                start: function() {
226
                    lightbox.show();
227
                },
228
                success: function(tid, response) {
229
                    // Update section titles, we can't simply swap them as
230
                    // they might have custom title
231
                    try {
232
                        var responsetext = Y.JSON.parse(response.responseText);
233
                        if (responsetext.error) {
234
                            new M.core.ajaxException(responsetext);
235
                        }
236
                        M.course.format.process_sections(Y, sectionlist, responsetext, loopstart, loopend);
237
                    } catch (e) {
238
                        // Ignore.
239
                    }
240
 
241
                    // Update all of the section IDs - first unset them, then set them
242
                    // to avoid duplicates in the DOM.
243
                    var index;
244
 
245
                    // Classic bubble sort algorithm is applied to the section
246
                    // nodes between original drag node location and the new one.
247
                    var swapped = false;
248
                    do {
249
                        swapped = false;
250
                        for (index = loopstart; index <= loopend; index++) {
251
                            if (Y.Moodle.core_course.util.section.getId(sectionlist.item(index - 1)) >
252
                                        Y.Moodle.core_course.util.section.getId(sectionlist.item(index))) {
253
                                Y.log("Swapping " + Y.Moodle.core_course.util.section.getId(sectionlist.item(index - 1)) +
254
                                        " with " + Y.Moodle.core_course.util.section.getId(sectionlist.item(index)));
255
                                // Swap section id.
256
                                var sectionid = sectionlist.item(index - 1).get('id');
257
                                sectionlist.item(index - 1).set('id', sectionlist.item(index).get('id'));
258
                                sectionlist.item(index).set('id', sectionid);
259
 
260
                                // See what format needs to swap.
261
                                M.course.format.swap_sections(Y, index - 1, index);
262
 
263
                                // Update flag.
264
                                swapped = true;
265
                            }
266
                            sectionlist.item(index).setAttribute('data-sectionid',
267
                                Y.Moodle.core_course.util.section.getId(sectionlist.item(index)));
268
                        }
269
                        loopend = loopend - 1;
270
                    } while (swapped);
271
 
272
                    window.setTimeout(function() {
273
                        lightbox.hide();
274
                    }, 250);
275
 
276
                    // Update course state.
277
                    M.course.coursebase.invoke_function('updateMovedSectionState');
278
                },
279
 
280
                failure: function(tid, response) {
281
                    this.ajax_failure(response);
282
                    lightbox.hide();
283
                }
284
            },
285
            context: this
286
        });
287
    }
288
 
289
}, {
290
    NAME: 'course-dragdrop-section',
291
    ATTRS: {
292
        courseid: {
293
            value: null
294
        },
295
        ajaxurl: {
296
            value: 0
297
        },
298
        config: {
299
            value: 0
300
        }
301
    }
302
});
303
 
304
M.course = M.course || {};
305
M.course.init_section_dragdrop = function(params) {
306
    new DRAGSECTION(params);
307
};