MDL-34209 JavaScript: Simplify section drag/drop to fix reordering issues

This commit is contained in:
Andrew Nicols 2013-10-09 22:41:33 +08:00
parent 56cc9b387e
commit b57bc5855c
2 changed files with 84 additions and 39 deletions

View file

@ -16,7 +16,7 @@ YUI.add('moodle-core-dragdrop', function(Y) {
Y.extend(DRAGDROP, Y.Base, {
goingup : null,
lasty : null,
absgoingup : null,
samenodeclass : null,
parentnodeclass : null,
groups : [],
@ -125,23 +125,38 @@ YUI.add('moodle-core-dragdrop', function(Y) {
},
global_drag_drag : function(e) {
var drag = e.target;
var drag = e.target,
info = e.info;
// Check that drag object belongs to correct group
if (!this.in_group(drag)) {
return;
}
//Get the last y point
var y = drag.lastXY[1];
//is it greater than the lasty var?
if (y < this.lasty) {
//We are going up
// Note, we test both < and > situations here. We don't want to
// effect a change in direction if the user is only moving side
// to side with no Y position change.
// Detect changes in the position relative to the start point.
if (info.start[1] < info.xy[1]) {
// We are going up if our final position is higher than our start position.
this.absgoingup = true;
} else if (info.start[1] > info.xy[1]) {
// Otherwise we're going down.
this.absgoingup = false;
}
// Detect changes in the position relative to the last movement.
if (info.delta[1] < 0) {
// We are going up if our final position is higher than our start position.
this.goingup = true;
} else {
//We are going down.
} else if (info.delta[1] > 0) {
// Otherwise we're going down.
this.goingup = false;
}
//Cache for next check
this.lasty = y;
this.drag_drag(e);
},
@ -157,12 +172,16 @@ YUI.add('moodle-core-dragdrop', function(Y) {
this.lastdroptarget = e.drop;
//Are we dropping on the same node?
if (drop.hasClass(this.samenodeclass)) {
//Are we not going up?
if (!this.goingup) {
drop = drop.next('.'+this.samenodeclass);
var where;
if (this.goingup) {
where = "before";
} else {
where = "after";
}
//Add the node
e.drop.get('node').ancestor().insertBefore(drag, drop);
// Add the node contents so that it's moved, otherwise only the drag handle is moved.
drop.insert(drag, where);
} else if ((drop.hasClass(this.parentnodeclass) || drop.test('[data-droptarget="1"]')) && !drop.contains(drag)) {
// We are dropping on parent node and it is empty
if (this.goingup) {