Skip to content

Commit ffab89e

Browse files
NiGhTTraXmikesherov
authored andcommitted
Droppable: update ddmanager when scope changes through setOption. Fixed #9287 - droppable: Scope option cannot be changed after initialization.
1 parent a3715a7 commit ffab89e

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

tests/unit/droppable/droppable_options.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,48 @@ test("{ addClasses: false }", function() {
3535
ok(!el.is(".ui-droppable"), "'ui-droppable' class not added");
3636
el.droppable("destroy");
3737
});
38+
39+
test( "scope", function() {
40+
expect( 4 );
41+
var droppableOffset, draggableOffset, oldDraggableOffset, dx, dy,
42+
draggable1 = $("<div></div>").appendTo( "#qunit-fixture" ).draggable({ revert: "invalid" }),
43+
draggable2 = $("<div></div>").appendTo( "#qunit-fixture" ).droppable(),
44+
droppable = $("<div></div>").appendTo( "#qunit-fixture" ).droppable(),
45+
newScope = "test";
46+
47+
draggable1.draggable( "option", "scope", newScope );
48+
droppable.droppable( "option", "scope", newScope );
49+
50+
// Test that droppable accepts draggable with new scope.
51+
droppableOffset = droppable.offset();
52+
draggableOffset = draggable1.offset();
53+
dx = droppableOffset.left - draggableOffset.left;
54+
dy = droppableOffset.top - draggableOffset.top;
55+
56+
draggable1.simulate( "drag", {
57+
dx: dx,
58+
dy: dy
59+
});
60+
61+
draggableOffset = draggable1.offset();
62+
equal( draggableOffset.left, droppableOffset.left );
63+
equal( draggableOffset.top, droppableOffset.top );
64+
65+
// Test that droppable doesn't accept draggable with old scope.
66+
draggableOffset = draggable2.offset();
67+
dx = droppableOffset.left - draggableOffset.left;
68+
dy = droppableOffset.top - draggableOffset.top;
69+
oldDraggableOffset = draggableOffset;
70+
71+
draggable2.simulate( "drag", {
72+
dx: dx,
73+
dy: dy
74+
});
75+
76+
draggableOffset = draggable2.offset();
77+
equal( draggableOffset.left, oldDraggableOffset.left );
78+
equal( draggableOffset.top, oldDraggableOffset.top );
79+
});
3880
/*
3981
test("greedy", function() {
4082
ok(false, 'missing test - untested code is broken code');
@@ -44,10 +86,6 @@ test("hoverClass", function() {
4486
ok(false, 'missing test - untested code is broken code');
4587
});
4688
47-
test("scope", function() {
48-
ok(false, 'missing test - untested code is broken code');
49-
});
50-
5189
test("tolerance, fit", function() {
5290
ok(false, 'missing test - untested code is broken code');
5391
});

ui/jquery.ui.droppable.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,31 @@ $.widget( "ui.droppable", {
6363
}
6464
};
6565

66-
// Add the reference and positions to the manager
67-
$.ui.ddmanager.droppables[ o.scope ] = $.ui.ddmanager.droppables[ o.scope ] || [];
68-
$.ui.ddmanager.droppables[ o.scope ].push( this );
66+
this._addToManager( o.scope );
6967

7068
o.addClasses && this.element.addClass( "ui-droppable" );
7169

7270
},
7371

74-
_destroy: function() {
75-
var i = 0,
76-
drop = $.ui.ddmanager.droppables[ this.options.scope ];
72+
_addToManager: function( scope ) {
73+
// Add the reference and positions to the manager
74+
$.ui.ddmanager.droppables[ scope ] = $.ui.ddmanager.droppables[ scope ] || [];
75+
$.ui.ddmanager.droppables[ scope ].push( this );
76+
},
7777

78+
_splice: function( drop ) {
79+
var i = 0;
7880
for ( ; i < drop.length; i++ ) {
7981
if ( drop[ i ] === this ) {
8082
drop.splice( i, 1 );
8183
}
8284
}
85+
},
86+
87+
_destroy: function() {
88+
var drop = $.ui.ddmanager.droppables[ this.options.scope ];
89+
90+
this._splice( drop );
8391

8492
this.element.removeClass( "ui-droppable ui-droppable-disabled" );
8593
},
@@ -90,7 +98,13 @@ $.widget( "ui.droppable", {
9098
this.accept = $.isFunction( value ) ? value : function( d ) {
9199
return d.is( value );
92100
};
101+
} else if ( key === "scope" ) {
102+
var drop = $.ui.ddmanager.droppables[ this.options.scope ];
103+
104+
this._splice( drop );
105+
this._addToManager( value );
93106
}
107+
94108
this._super( key, value );
95109
},
96110

0 commit comments

Comments
 (0)