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
 * Resource drag and drop.
3
 *
1441 ariadna 4
 * TODO: remove this module as part of MDL-83627.
5
 *
1 efrain 6
 * @class M.course.dragdrop.resource
7
 * @constructor
8
 * @extends M.core.dragdrop
9
 */
10
var DRAGRESOURCE = function() {
11
    DRAGRESOURCE.superclass.constructor.apply(this, arguments);
12
};
1441 ariadna 13
 
14
Y.log(
15
    'YUI M.course.dragdrop.resource is deprecated. Please, add support_components to your course format.',
16
    'warn',
17
    'moodle-course-coursebase'
18
);
19
 
1 efrain 20
Y.extend(DRAGRESOURCE, M.core.dragdrop, {
21
    initializer: function() {
22
        // Set group for parent class
23
        this.groups = ['resource'];
24
        this.samenodeclass = CSS.ACTIVITY;
25
        this.parentnodeclass = CSS.SECTION;
26
 
27
        this.samenodelabel = {
28
            identifier: 'afterresource',
29
            component: 'moodle'
30
        };
31
        this.parentnodelabel = {
32
            identifier: 'totopofsection',
33
            component: 'moodle'
34
        };
35
 
36
        // Go through all sections
37
        var sectionlistselector = M.course.format.get_section_selector(Y);
38
        if (sectionlistselector) {
39
            sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + sectionlistselector;
40
            this.setup_for_section(sectionlistselector);
41
 
42
            // Initialise drag & drop for all resources/activities
43
            var nodeselector = sectionlistselector.slice(CSS.COURSECONTENT.length + 2) + ' li.' + CSS.ACTIVITY;
44
            var del = new Y.DD.Delegate({
45
                container: '.' + CSS.COURSECONTENT,
46
                nodes: nodeselector,
47
                target: true,
48
                handles: ['.' + CSS.EDITINGMOVE],
49
                dragConfig: {groups: this.groups}
50
            });
51
            del.dd.plug(Y.Plugin.DDProxy, {
52
                // Don't move the node at the end of the drag
53
                moveOnEnd: false,
54
                cloneNode: true
55
            });
56
            del.dd.plug(Y.Plugin.DDConstrained, {
57
                // Keep it inside the .course-content
58
                constrain: '#' + CSS.PAGECONTENT
59
            });
60
            del.dd.plug(Y.Plugin.DDWinScroll);
61
 
62
            M.course.coursebase.register_module(this);
63
            M.course.dragres = this;
64
        }
65
    },
66
 
67
    /**
68
     * Apply dragdrop features to the specified selector or node that refers to section(s)
69
     *
70
     * @method setup_for_section
71
     * @param {String} baseselector The CSS selector or node to limit scope to
72
     */
73
    setup_for_section: function(baseselector) {
74
        Y.Node.all(baseselector).each(function(sectionnode) {
75
            var resources = sectionnode.one('.' + CSS.CONTENT + ' ul.' + CSS.SECTION);
76
            // See if resources ul exists, if not create one
77
            if (!resources) {
78
                resources = Y.Node.create('<ul></ul>');
79
                resources.addClass(CSS.SECTION);
80
                sectionnode.one('.' + CSS.CONTENT + ' div.' + CSS.SUMMARY).insert(resources, 'after');
81
            }
82
            resources.setAttribute('data-draggroups', this.groups.join(' '));
83
            // Define empty ul as droptarget, so that item could be moved to empty list
84
            new Y.DD.Drop({
85
                node: resources,
86
                groups: this.groups,
87
                padding: '20 0 20 0'
88
            });
89
 
90
            // Initialise each resource/activity in this section
91
            this.setup_for_resource('#' + sectionnode.get('id') + ' li.' + CSS.ACTIVITY);
92
        }, this);
93
    },
94
 
95
    /**
96
     * Apply dragdrop features to the specified selector or node that refers to resource(s)
97
     *
98
     * @method setup_for_resource
99
     * @param {String} baseselector The CSS selector or node to limit scope to
100
     */
101
    setup_for_resource: function(baseselector) {
102
        Y.Node.all(baseselector).each(function(resourcesnode) {
103
            var draggroups = resourcesnode.getData('draggroups');
104
            if (!draggroups) {
105
                // This Drop Node has not been set up. Configure it now.
106
                resourcesnode.setAttribute('data-draggroups', this.groups.join(' '));
107
                // Define empty ul as droptarget, so that item could be moved to empty list
108
                new Y.DD.Drop({
109
                    node: resourcesnode,
110
                    groups: this.groups,
111
                    padding: '20 0 20 0'
112
                });
113
            }
114
 
115
            // Replace move icons
116
            var move = resourcesnode.one('a.' + CSS.EDITINGMOVE);
117
            if (move) {
118
                var sr = move.getData('sectionreturn');
119
                move.replace(this.get_drag_handle(M.util.get_string('movecoursemodule', 'moodle'),
120
                             CSS.EDITINGMOVE, CSS.ICONCLASS, true).setAttribute('data-sectionreturn', sr));
121
            }
122
        }, this);
123
    },
124
 
125
    drag_start: function(e) {
126
        // Get our drag object
127
        var drag = e.target;
128
        if (drag.get('dragNode') === drag.get('node')) {
129
            // We do not want to modify the contents of the real node.
130
            // They will be the same during a keyboard drag and drop.
131
            return;
132
        }
133
        drag.get('dragNode').setContent(drag.get('node').get('innerHTML'));
134
        drag.get('dragNode').all('img.iconsmall').setStyle('vertical-align', 'baseline');
135
    },
136
 
137
    drag_dropmiss: function(e) {
138
        // Missed the target, but we assume the user intended to drop it
139
        // on the last last ghost node location, e.drag and e.drop should be
140
        // prepared by global_drag_dropmiss parent so simulate drop_hit(e).
141
        this.drop_hit(e);
142
    },
143
 
144
    drop_hit: function(e) {
145
        var drag = e.drag;
146
        // Get a reference to our drag node
147
        var dragnode = drag.get('node');
148
        var dropnode = e.drop.get('node');
149
 
150
        // Add spinner if it not there
151
        var actionarea = dragnode.one(CSS.ACTIONAREA);
152
        var spinner = M.util.add_spinner(Y, actionarea);
153
 
154
        var params = {};
155
 
156
        // Handle any variables which we must pass back through to
157
        var pageparams = this.get('config').pageparams;
158
        var varname;
159
        for (varname in pageparams) {
160
            params[varname] = pageparams[varname];
161
        }
162
 
163
        // Variables needed to update the course state.
164
        var cmid = Number(Y.Moodle.core_course.util.cm.getId(dragnode));
165
        var beforeid = null;
166
 
167
        // Prepare request parameters
168
        params.sesskey = M.cfg.sesskey;
169
        params.courseId = this.get('courseid');
170
        params['class'] = 'resource';
171
        params.field = 'move';
172
        params.id = cmid;
173
        params.sectionId = Y.Moodle.core_course.util.section.getId(dropnode.ancestor(M.course.format.get_section_wrapper(Y), true));
174
 
175
        if (dragnode.next()) {
176
            beforeid = Number(Y.Moodle.core_course.util.cm.getId(dragnode.next()));
177
            params.beforeId = beforeid;
178
        }
179
 
180
        // Do AJAX request
181
        var uri = M.cfg.wwwroot + this.get('ajaxurl');
182
 
183
        Y.io(uri, {
184
            method: 'POST',
185
            data: params,
186
            on: {
187
                start: function() {
188
                    this.lock_drag_handle(drag, CSS.EDITINGMOVE);
189
                    spinner.show();
190
                },
191
                success: function(tid, response) {
192
                    var responsetext = Y.JSON.parse(response.responseText);
193
                    // Update course state.
194
                    M.course.coursebase.invoke_function(
195
                        'updateMovedCmState',
196
                        {
197
                            cmid: cmid,
198
                            beforeid: beforeid,
199
                            visible: responsetext.visible,
200
                        }
201
                    );
202
                    // Set visibility in course content.
203
                    var params = {element: dragnode, visible: responsetext.visible};
204
                    M.course.coursebase.invoke_function('set_visibility_resource_ui', params);
205
                    this.unlock_drag_handle(drag, CSS.EDITINGMOVE);
206
                    window.setTimeout(function() {
207
                        spinner.hide();
208
                    }, 250);
209
                },
210
                failure: function(tid, response) {
211
                    this.ajax_failure(response);
212
                    this.unlock_drag_handle(drag, CSS.SECTIONHANDLE);
213
                    spinner.hide();
214
                    // TODO: revert nodes location
215
                }
216
            },
217
            context: this
218
        });
219
    }
220
}, {
221
    NAME: 'course-dragdrop-resource',
222
    ATTRS: {
223
        courseid: {
224
            value: null
225
        },
226
        ajaxurl: {
227
            value: 0
228
        },
229
        config: {
230
            value: 0
231
        }
232
    }
233
});
234
 
235
M.course = M.course || {};
236
M.course.init_resource_dragdrop = function(params) {
237
    new DRAGRESOURCE(params);
238
};