Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('moodle-mod_feedback-dragdrop', function(Y) {
2
    var DRAGDROPNAME = 'mod_feedback_dragdrop';
3
    var CSS = {
4
        DRAGAREA: '#feedback_dragarea',
5
        DRAGITEMCLASS: 'feedback_itemlist',
6
        DRAGITEM: '.row.feedback_itemlist',
7
        DRAGLIST: '#feedback_dragarea form',
8
        DRAGHANDLE: 'itemhandle'
9
    };
10
 
11
    var DRAGDROP = function() {
12
        DRAGDROP.superclass.constructor.apply(this, arguments);
13
    };
14
 
15
    Y.extend(DRAGDROP, M.core.dragdrop, {
16
 
17
        initializer : function(params) {
18
            //Static Vars
19
            this.cmid = params.cmid;
20
            this.goingUp = false, lastY = 0;
21
 
22
            var groups = ['feedbackitem'];
23
 
24
            var handletitle = M.util.get_string('move_item', 'feedback');
25
 
26
            //Get the list of li's in the lists and add the drag handle.
27
            basenode = Y.Node.one(CSS.DRAGLIST);
28
            listitems = basenode.all(CSS.DRAGITEM).each(function(v) {
29
                var item_id = this.get_node_id(v.get('id')); //Get the id of the feedback item.
30
                var mydraghandle = this.get_drag_handle(handletitle, CSS.DRAGHANDLE, 'icon');
31
                v.append(mydraghandle); // Insert the new handle into the item box.
32
            }, this);
33
 
34
            //We use a delegate to make all items draggable
35
            var del = new Y.DD.Delegate({
36
                container: CSS.DRAGLIST,
37
                nodes: CSS.DRAGITEM,
38
                target: {
39
                    padding: '0 0 0 20'
40
                },
41
                handles: ['.' + CSS.DRAGHANDLE],
42
                dragConfig: {groups: groups}
43
            });
44
 
45
            //Add plugins to the delegate
46
            del.dd.plug(Y.Plugin.DDProxy, {
47
                // Don't move the node at the end of the drag
48
                moveOnEnd: false,
49
                cloneNode: true
50
            });
51
            del.dd.plug(Y.Plugin.DDConstrained, {
52
                // Keep it inside the .course-content
53
                constrain: CSS.DRAGAREA
54
            });
55
            del.dd.plug(Y.Plugin.DDWinScroll);
56
 
57
            //Listen for all drop:over events
58
            del.on('drop:over', this.drop_over_handler, this);
59
            //Listen for all drag:drag events
60
            del.on('drag:drag',  this.drag_drag_handler, this);
61
            //Listen for all drag:start events
62
            del.on('drag:start',  this.drag_start_handler, this);
63
            //Listen for a drag:end events
64
            del.on('drag:end',  this.drag_end_handler, this);
65
            //Listen for all drag:drophit events
66
            del.on('drag:drophit',  this.drag_drophit_handler, this);
67
            //Listen for all drag:dropmiss events
68
            del.on('drag:dropmiss',  this.drag_dropmiss_handler, this);
69
 
70
            //Create targets for drop.
71
            var droparea = Y.Node.one(CSS.DRAGLIST);
72
            var tar = new Y.DD.Drop({
73
                groups: groups,
74
                node: droparea
75
            });
76
 
77
        },
78
 
79
        /**
80
         * Handles the drop:over event.
81
         *
82
         * @param e the event
83
         * @return void
84
         */
85
        drop_over_handler : function(e) {
86
            //Get a reference to our drag and drop nodes
87
            var drag = e.drag.get('node'),
88
                drop = e.drop.get('node');
89
 
90
            //Are we dropping on an li node?
91
            if (drop.hasClass(CSS.DRAGITEMCLASS)) {
92
                //Are we not going up?
93
                if (!this.goingUp) {
94
                    drop = drop.get('nextSibling');
95
                }
96
                //Add the node to this list
97
                e.drop.get('node').get('parentNode').insertBefore(drag, drop);
98
                //Resize this nodes shim, so we can drop on it later.
99
                e.drop.sizeShim();
100
            }
101
        },
102
 
103
        /**
104
         * Handles the drag:drag event.
105
         *
106
         * @param e the event
107
         * @return void
108
         */
109
        drag_drag_handler : function(e) {
110
            //Get the last y point
111
            var y = e.target.lastXY[1];
112
            //Is it greater than the lastY var?
113
            if (y < this.lastY) {
114
                //We are going up
115
                this.goingUp = true;
116
            } else {
117
                //We are going down.
118
                this.goingUp = false;
119
            }
120
            //Cache for next check
121
            this.lastY = y;
122
        },
123
 
124
        /**
125
         * Handles the drag:start event.
126
         *
127
         * @param e the event
128
         * @return void
129
         */
130
        drag_start_handler : function(e) {
131
            //Get our drag object
132
            var drag = e.target;
133
 
134
            //Set some styles here
135
            drag.get('node').addClass('drag_target_active');
136
            drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
137
            drag.get('dragNode').addClass('drag_item_active');
138
            drag.get('dragNode').setStyles({
139
                borderColor: drag.get('node').getStyle('borderColor'),
140
                backgroundColor: drag.get('node').getStyle('backgroundColor')
141
            });
142
        },
143
 
144
        /**
145
         * Handles the drag:end event.
146
         *
147
         * @param e the event
148
         * @return void
149
         */
150
        drag_end_handler : function(e) {
151
            var drag = e.target;
152
            //Put our styles back
153
            drag.get('node').removeClass('drag_target_active');
154
        },
155
 
156
        /**
157
         * Handles the drag:drophit event.
158
         *
159
         * @param e the event
160
         * @return void
161
         */
162
        drag_drophit_handler : function(e) {
163
            var drop = e.drop.get('node'),
164
                drag = e.drag.get('node');
165
            dragnode = Y.one(drag);
166
            if (!drop.hasClass(CSS.DRAGITEMCLASS)) {
167
                if (!drop.contains(drag)) {
168
                    drop.appendChild(drag);
169
                }
170
                var childElement;
171
                var elementId;
172
                var elements = [];
173
                drop.all(CSS.DRAGITEM).each(function(v) {
174
                    childElement = v.one('.felement')?.one('[id^="feedback_item_"]');
175
                    if (childElement) {
176
                        elementId = this.get_node_id(childElement.get('id'));
177
                        if (elements.indexOf(elementId) == -1) {
178
                            elements.push(elementId);
179
                        }
180
                    }
181
                }, this);
182
                var spinner = M.util.add_spinner(Y, dragnode);
183
                this.save_item_order(this.cmid, elements.toString(), spinner);
184
           }
185
        },
186
 
187
        /**
188
         * Save the new item order.
189
         *
190
         * @param cmid the coursemodule id
191
         * @param itemorder A comma separated list with item ids
192
         * @param spinner The spinner icon shown while saving
193
         * @return void
194
         */
195
        save_item_order : function(cmid, itemorder, spinner) {
196
 
197
            Y.io(M.cfg.wwwroot + '/mod/feedback/ajax.php', {
198
                //The needed paramaters
199
                data: {action: 'saveitemorder',
200
                       id: cmid,
201
                       itemorder: itemorder,
202
                       sesskey: M.cfg.sesskey
203
                },
204
 
205
                timeout: 5000, //5 seconds for timeout I think it is enough.
206
 
207
                //Define the events.
208
                on: {
209
                    start : function(transactionid) {
210
                        spinner.show();
211
                    },
212
                    success : function(transactionid, xhr) {
213
                        var response = xhr.responseText;
214
                        var ergebnis = Y.JSON.parse(response);
215
                        window.setTimeout(function(e) {
216
                            spinner.hide();
217
                        }, 250);
218
                        require(['core/notification', 'core/str', 'core/toast'], function(Notification, Strings, Toast) {
219
                            Strings.get_string('changessaved', 'core').then(function(saveString) {
220
                                return Toast.add(saveString);
221
                            }).catch(Notification.exception);
222
                        });
223
                    },
224
                    failure : function(transactionid, xhr) {
225
                        var msg = {
226
                            name : xhr.status+' '+xhr.statusText,
227
                            message : xhr.responseText
228
                        };
229
                        return new M.core.exception(msg);
230
                        //~ this.ajax_failure(xhr);
231
                        spinner.hide();
232
                    }
233
                },
234
                context:this
235
            });
236
        },
237
 
238
        /**
239
         * Returns the numeric id from the dom id of an item.
240
         *
241
         * @param id The dom id, f.g.: feedback_item_22
242
         * @return int
243
         */
244
        get_node_id : function(id) {
245
            return Number(id.replace(/^.*feedback_item_/i, ''));
246
        }
247
 
248
    }, {
249
        NAME : DRAGDROPNAME,
250
        ATTRS : {
251
            cmid : {
252
                value : 0
253
            }
254
        }
255
 
256
    });
257
 
258
    M.mod_feedback = M.mod_feedback || {};
259
    M.mod_feedback.init_dragdrop = function(params) {
260
        return new DRAGDROP(params);
261
    }
262
 
263
}, '@VERSION@', {
264
    requires:['io', 'json-parse', 'dd-constrain', 'dd-proxy', 'dd-drop', 'dd-scroll', 'moodle-core-dragdrop', 'moodle-core-notification']
265
});