Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * Resource drag and drop.
3
 *
4
 * @class M.course.dragdrop.resource
5
 * @constructor
6
 * @extends M.core.dragdrop
7
 */
8
var DRAGRESOURCE = function() {
9
    DRAGRESOURCE.superclass.constructor.apply(this, arguments);
10
};
11
Y.extend(DRAGRESOURCE, M.core.dragdrop, {
12
    initializer: function() {
13
        // Set group for parent class
14
        this.groups = ['resource'];
15
        this.samenodeclass = CSS.ACTIVITY;
16
        this.parentnodeclass = CSS.SECTION;
17
 
18
        this.samenodelabel = {
19
            identifier: 'dragtoafter',
20
            component: 'quiz'
21
        };
22
        this.parentnodelabel = {
23
            identifier: 'dragtostart',
24
            component: 'quiz'
25
        };
26
 
27
        // Go through all sections
28
        this.setup_for_section();
29
 
30
        // Initialise drag & drop for all resources/activities
31
        var nodeselector = 'li.' + CSS.ACTIVITY;
32
        var del = new Y.DD.Delegate({
33
            container: '.' + CSS.COURSECONTENT,
34
            nodes: nodeselector,
35
            target: true,
36
            handles: ['.' + CSS.EDITINGMOVE],
37
            dragConfig: {groups: this.groups}
38
        });
39
        del.dd.plug(Y.Plugin.DDProxy, {
40
            // Don't move the node at the end of the drag
41
            moveOnEnd: false,
42
            cloneNode: true
43
        });
44
        del.dd.plug(Y.Plugin.DDConstrained, {
45
            // Keep it inside the .mod-quiz-edit-content
46
            constrain: '#' + CSS.SLOTS
47
        });
48
        del.dd.plug(Y.Plugin.DDWinScroll);
49
 
50
        M.mod_quiz.quizbase.register_module(this);
51
        M.mod_quiz.dragres = this;
52
    },
53
 
54
    /**
55
     * Apply dragdrop features to the specified selector or node that refers to section(s)
56
     *
57
     * @method setup_for_section
58
     * @param {String} baseselector The CSS selector or node to limit scope to
59
     */
60
    setup_for_section: function() {
61
        Y.Node.all('.mod-quiz-edit-content ul.slots ul.section').each(function(resources) {
62
            resources.setAttribute('data-draggroups', this.groups.join(' '));
63
            // Define empty ul as droptarget, so that item could be moved to empty list
64
            new Y.DD.Drop({
65
                node: resources,
66
                groups: this.groups,
67
                padding: '20 0 20 0'
68
            });
69
 
70
            // Initialise each resource/activity in this section
71
            this.setup_for_resource('li.activity');
72
        }, this);
73
    },
74
 
75
    /**
76
     * Apply dragdrop features to the specified selector or node that refers to resource(s)
77
     *
78
     * @method setup_for_resource
79
     * @param {String} baseselector The CSS selector or node to limit scope to
80
     */
81
    setup_for_resource: function(baseselector) {
82
        Y.Node.all(baseselector).each(function(resourcesnode) {
83
            // Replace move icons
84
            var move = resourcesnode.one('a.' + CSS.EDITINGMOVE);
85
            if (move) {
86
                var resourcedraghandle = this.get_drag_handle(M.util.get_string('move', 'moodle'),
87
                                                              CSS.EDITINGMOVE, CSS.ICONCLASS, true);
88
                move.replace(resourcedraghandle);
89
            }
90
        }, this);
91
    },
92
 
93
    drag_start: function(e) {
94
        // Get our drag object
95
        var drag = e.target;
96
        drag.get('dragNode').setContent(drag.get('node').get('innerHTML'));
97
        drag.get('dragNode').all('.icon').setStyle('vertical-align', 'baseline');
98
    },
99
 
100
    drag_dropmiss: function(e) {
101
        // Missed the target, but we assume the user intended to drop it
102
        // on the last ghost node location, e.drag and e.drop should be
103
        // prepared by global_drag_dropmiss parent so simulate drop_hit(e).
104
        this.drop_hit(e);
105
    },
106
 
107
    drop_hit: function(e) {
108
        var drag = e.drag;
109
        // Get a reference to our drag node
110
        var dragnode = drag.get('node');
111
 
112
        // Add spinner if it not there
113
        var actionarea = dragnode.one(CSS.ACTIONAREA);
114
        var spinner = M.util.add_spinner(Y, actionarea);
115
 
116
        var params = {};
117
 
118
        // Handle any variables which we must pass back through to
119
        var pageparams = this.get('config').pageparams;
120
        var varname;
121
        for (varname in pageparams) {
122
            params[varname] = pageparams[varname];
123
        }
124
 
125
        // Prepare request parameters
126
        params.sesskey = M.cfg.sesskey;
127
        params.courseid = this.get('courseid');
128
        params.quizid = this.get('quizid');
129
        params['class'] = 'resource';
130
        params.field = 'move';
131
        params.id = Number(Y.Moodle.mod_quiz.util.slot.getId(dragnode));
132
        params.sectionId = Y.Moodle.core_course.util.section.getId(dragnode.ancestor('li.section', true));
133
 
134
        var previousslot = dragnode.previous(SELECTOR.SLOT);
135
        if (previousslot) {
136
            params.previousid = Number(Y.Moodle.mod_quiz.util.slot.getId(previousslot));
137
        }
138
 
139
        var previouspage = dragnode.previous(SELECTOR.PAGE);
140
        if (previouspage) {
141
            params.page = Number(Y.Moodle.mod_quiz.util.page.getId(previouspage));
142
        }
143
 
144
        // Do AJAX request
145
        var uri = M.cfg.wwwroot + this.get('ajaxurl');
146
 
147
        Y.io(uri, {
148
            method: 'POST',
149
            data: params,
150
            on: {
151
                start: function() {
152
                    this.lock_drag_handle(drag, CSS.EDITINGMOVE);
153
                    spinner.show();
154
                },
155
                success: function(tid, response) {
156
                    var responsetext = Y.JSON.parse(response.responseText);
157
                    var params = {element: dragnode, visible: responsetext.visible};
158
                    M.mod_quiz.quizbase.invoke_function('set_visibility_resource_ui', params);
159
                    this.unlock_drag_handle(drag, CSS.EDITINGMOVE);
160
                    window.setTimeout(function() {
161
                        spinner.hide();
162
                    }, 250);
163
                    M.mod_quiz.resource_toolbox.reorganise_edit_page();
164
                },
165
                failure: function(tid, response) {
166
                    this.ajax_failure(response);
167
                    this.unlock_drag_handle(drag, CSS.SECTIONHANDLE);
168
                    spinner.hide();
169
                    window.location.reload(true);
170
                }
171
            },
172
            context: this
173
        });
174
    },
175
 
176
    global_drop_over: function(e) {
177
        // Overriding parent method so we can stop the slots being dragged before the first page node.
178
 
179
        // Check that drop object belong to correct group.
180
        if (!e.drop || !e.drop.inGroup(this.groups)) {
181
            return;
182
        }
183
 
184
        // Get a reference to our drag and drop nodes.
185
        var drag = e.drag.get('node'),
186
            drop = e.drop.get('node');
187
 
188
        // Save last drop target for the case of missed target processing.
189
        this.lastdroptarget = e.drop;
190
 
191
        // Are we dropping within the same parent node?
192
        if (drop.hasClass(this.samenodeclass)) {
193
            var where;
194
 
195
            if (this.goingup) {
196
                where = "before";
197
            } else {
198
                where = "after";
199
            }
200
 
201
            drop.insert(drag, where);
202
        } else if ((drop.hasClass(this.parentnodeclass) || drop.test('[data-droptarget="1"]')) && !drop.contains(drag)) {
203
            // We are dropping on parent node and it is empty
204
            if (this.goingup) {
205
                drop.append(drag);
206
            } else {
207
                drop.prepend(drag);
208
            }
209
        }
210
        this.drop_over(e);
211
    }
212
}, {
213
    NAME: 'mod_quiz-dragdrop-resource',
214
    ATTRS: {
215
        courseid: {
216
            value: null
217
        },
218
        quizid: {
219
            value: null
220
        },
221
        ajaxurl: {
222
            value: 0
223
        },
224
        config: {
225
            value: 0
226
        }
227
    }
228
});
229
 
230
M.mod_quiz = M.mod_quiz || {};
231
M.mod_quiz.init_resource_dragdrop = function(params) {
232
    new DRAGRESOURCE(params);
233
};