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