2008-06-06 03:58:15 +00:00
|
|
|
/*
|
2009-02-02 07:12:37 +00:00
|
|
|
* draggable_core.js
|
2008-06-06 03:58:15 +00:00
|
|
|
*/
|
2009-02-01 00:25:58 +00:00
|
|
|
|
2013-01-12 05:33:45 +00:00
|
|
|
(function( $ ) {
|
2008-06-06 06:34:09 +00:00
|
|
|
|
2013-03-26 13:18:58 +00:00
|
|
|
module( "draggable: core" );
|
2013-01-12 05:33:45 +00:00
|
|
|
|
|
|
|
test( "element types", function() {
|
|
|
|
var typeNames = (
|
|
|
|
"p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form" +
|
|
|
|
",table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr" +
|
|
|
|
",acronym,code,samp,kbd,var,img,hr" +
|
|
|
|
",input,button,label,select,iframe"
|
|
|
|
).split(",");
|
2008-06-07 06:09:04 +00:00
|
|
|
|
2012-11-09 20:02:12 +00:00
|
|
|
expect( typeNames.length * 2 );
|
2012-10-30 23:12:17 +00:00
|
|
|
|
2013-01-12 05:33:45 +00:00
|
|
|
$.each( typeNames, function( i ) {
|
2012-11-03 20:17:16 +00:00
|
|
|
var offsetBefore, offsetAfter,
|
2013-01-12 05:33:45 +00:00
|
|
|
typeName = typeNames[ i ],
|
|
|
|
el = $( document.createElement( typeName ) ).appendTo("#qunit-fixture");
|
|
|
|
|
|
|
|
if ( typeName === "table" ) {
|
|
|
|
el.append("<tr><td>content</td></tr>");
|
|
|
|
}
|
2012-11-03 20:17:16 +00:00
|
|
|
|
2013-03-29 00:03:14 +00:00
|
|
|
// intrinsic height is incorrect in FF for buttons with no content
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=471763
|
|
|
|
// Support: FF
|
|
|
|
if ( typeName === "button" ) {
|
|
|
|
el.text( "button" );
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:08:48 +00:00
|
|
|
el.draggable({ cancel: "" });
|
2012-10-30 23:12:17 +00:00
|
|
|
offsetBefore = el.offset();
|
2012-12-09 02:27:37 +00:00
|
|
|
el.simulate( "drag", {
|
|
|
|
dx: 50,
|
|
|
|
dy: 50
|
|
|
|
});
|
2012-11-03 20:17:16 +00:00
|
|
|
offsetAfter = el.offset();
|
2013-01-12 05:33:45 +00:00
|
|
|
|
|
|
|
// Support: FF, Chrome, and IE9,
|
|
|
|
// there are some rounding errors in so we can't say equal, we have to settle for close enough
|
2013-03-29 00:03:14 +00:00
|
|
|
closeEnough( offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + "> left" );
|
|
|
|
closeEnough( offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + "> top" );
|
2008-06-07 06:09:04 +00:00
|
|
|
el.draggable("destroy");
|
|
|
|
el.remove();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-01-12 05:33:45 +00:00
|
|
|
test( "No options, relative", function() {
|
2013-10-20 12:55:02 +00:00
|
|
|
expect( 2 );
|
2013-10-20 13:35:12 +00:00
|
|
|
TestHelpers.draggable.shouldMove( $( "#draggable1" ).draggable(), "no options, relative" );
|
2008-06-04 02:34:33 +00:00
|
|
|
});
|
|
|
|
|
2013-01-12 05:33:45 +00:00
|
|
|
test( "No options, absolute", function() {
|
2013-10-20 12:55:02 +00:00
|
|
|
expect( 2 );
|
2013-10-20 13:35:12 +00:00
|
|
|
TestHelpers.draggable.shouldMove( $( "#draggable2" ).draggable(), "no options, absolute" );
|
2008-06-04 02:34:33 +00:00
|
|
|
});
|
|
|
|
|
2013-01-12 05:33:45 +00:00
|
|
|
test( "resizable handle with complex markup (#8756 / #8757)", function() {
|
2012-11-19 14:06:36 +00:00
|
|
|
expect( 2 );
|
|
|
|
|
2013-03-26 13:18:58 +00:00
|
|
|
$( "#draggable1" )
|
2012-11-19 14:06:36 +00:00
|
|
|
.append(
|
2012-12-26 13:08:48 +00:00
|
|
|
$("<div>")
|
2013-01-12 05:33:45 +00:00
|
|
|
.addClass("ui-resizable-handle ui-resizable-w")
|
|
|
|
.append( $("<div>") )
|
2012-11-19 14:06:36 +00:00
|
|
|
);
|
|
|
|
|
2012-12-26 13:08:48 +00:00
|
|
|
var handle = $(".ui-resizable-w div"),
|
2013-03-26 13:18:58 +00:00
|
|
|
target = $( "#draggable1" ).draggable().resizable({ handles: "all" });
|
2012-11-19 14:06:36 +00:00
|
|
|
|
2012-12-09 02:27:37 +00:00
|
|
|
// todo: fix resizable so it doesn't require a mouseover
|
|
|
|
handle.simulate("mouseover").simulate( "drag", { dx: -50 } );
|
2012-11-19 14:06:36 +00:00
|
|
|
equal( target.width(), 250, "compare width" );
|
|
|
|
|
2012-12-09 02:27:37 +00:00
|
|
|
// todo: fix resizable so it doesn't require a mouseover
|
|
|
|
handle.simulate("mouseover").simulate( "drag", { dx: 50 } );
|
2012-11-19 14:06:36 +00:00
|
|
|
equal( target.width(), 200, "compare width" );
|
|
|
|
});
|
|
|
|
|
2013-03-26 13:18:00 +00:00
|
|
|
test( "#8269: Removing draggable element on drop", function() {
|
2013-03-27 20:47:57 +00:00
|
|
|
expect( 2 );
|
2013-03-26 13:18:00 +00:00
|
|
|
|
2013-03-27 20:47:57 +00:00
|
|
|
var element = $( "#draggable1" ).wrap( "<div id='wrapper' />" ).draggable({
|
|
|
|
stop: function() {
|
|
|
|
ok( true, "stop still called despite element being removed from DOM on drop" );
|
|
|
|
}
|
|
|
|
}),
|
2013-03-26 13:18:00 +00:00
|
|
|
dropOffset = $( "#droppable" ).offset();
|
|
|
|
|
|
|
|
$( "#droppable" ).droppable({
|
|
|
|
drop: function() {
|
|
|
|
$( "#wrapper" ).remove();
|
|
|
|
ok( true, "element removed from DOM on drop" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-04-07 16:17:19 +00:00
|
|
|
// Support: Opera 12.10, Safari 5.1, jQuery <1.8
|
|
|
|
if ( TestHelpers.draggable.unreliableContains ) {
|
|
|
|
ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" );
|
2013-04-14 16:58:42 +00:00
|
|
|
ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" );
|
2013-04-07 16:17:19 +00:00
|
|
|
} else {
|
|
|
|
element.simulate( "drag", {
|
|
|
|
handle: "corner",
|
|
|
|
x: dropOffset.left,
|
|
|
|
y: dropOffset.top
|
|
|
|
});
|
|
|
|
}
|
2013-03-26 13:18:00 +00:00
|
|
|
});
|
|
|
|
|
2014-08-19 19:09:28 +00:00
|
|
|
// http://bugs.jqueryui.com/ticket/7778
|
|
|
|
// drag element breaks in IE8 when its content is replaced onmousedown
|
|
|
|
test( "Stray mousemove after mousedown still drags", function() {
|
|
|
|
expect( 2 );
|
|
|
|
|
|
|
|
var element = $( "#draggable1" ).draggable({ scroll: false });
|
|
|
|
|
|
|
|
// In IE8, when content is placed under the mouse (e.g. when draggable content is replaced
|
|
|
|
// on mousedown), mousemove is triggered on those elements even though the mouse hasn't moved.
|
|
|
|
// Support: IE <9
|
|
|
|
element.bind( "mousedown", function() {
|
|
|
|
$( document ).simulate( "mousemove", { button: -1 });
|
|
|
|
});
|
|
|
|
|
|
|
|
TestHelpers.draggable.shouldMove( element, "element is draggable" );
|
|
|
|
});
|
|
|
|
|
2013-04-02 13:08:17 +00:00
|
|
|
test( "#6258: not following mouse when scrolled and using overflow-y: scroll", function() {
|
|
|
|
expect( 2 );
|
|
|
|
|
|
|
|
var element = $( "#draggable1" ).draggable({
|
|
|
|
stop: function( event, ui ) {
|
|
|
|
equal( ui.position.left, 1, "left position is correct despite overflow on HTML" );
|
|
|
|
equal( ui.position.top, 1, "top position is correct despite overflow on HTML" );
|
|
|
|
$( "html" )
|
|
|
|
.css( "overflow-y", oldOverflowY )
|
|
|
|
.css( "overflow-x", oldOverflowX )
|
|
|
|
.scrollTop( 0 )
|
|
|
|
.scrollLeft( 0 );
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
oldOverflowY = $( "html" ).css( "overflow-y" ),
|
|
|
|
oldOverflowX = $( "html" ).css( "overflow-x" );
|
|
|
|
|
2013-10-19 15:12:35 +00:00
|
|
|
TestHelpers.forceScrollableWindow();
|
|
|
|
|
2013-04-02 13:08:17 +00:00
|
|
|
$( "html" )
|
|
|
|
.css( "overflow-y", "scroll" )
|
|
|
|
.css( "overflow-x", "scroll" )
|
|
|
|
.scrollTop( 300 )
|
|
|
|
.scrollLeft( 300 );
|
|
|
|
|
|
|
|
element.simulate( "drag", {
|
|
|
|
dx: 1,
|
|
|
|
dy: 1,
|
|
|
|
moves: 1
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-10-29 22:35:42 +00:00
|
|
|
test( "#9315: jumps down with offset of scrollbar", function() {
|
2013-08-12 23:31:39 +00:00
|
|
|
expect( 2 );
|
|
|
|
|
|
|
|
var element = $( "#draggable2" ).draggable({
|
|
|
|
stop: function( event, ui ) {
|
|
|
|
equal( ui.position.left, 11, "left position is correct when position is absolute" );
|
|
|
|
equal( ui.position.top, 11, "top position is correct when position is absolute" );
|
|
|
|
$( "html" ).scrollTop( 0 ).scrollLeft( 0 );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-10-19 15:12:35 +00:00
|
|
|
TestHelpers.forceScrollableWindow();
|
|
|
|
|
2013-08-12 23:31:39 +00:00
|
|
|
$( "html" ).scrollTop( 300 ).scrollLeft( 300 );
|
|
|
|
|
|
|
|
element.simulate( "drag", {
|
|
|
|
dx: 1,
|
|
|
|
dy: 1,
|
|
|
|
moves: 1
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-23 19:34:04 +00:00
|
|
|
test( "scroll offset with fixed ancestors", function() {
|
2013-04-03 16:26:39 +00:00
|
|
|
expect( 2 );
|
|
|
|
|
|
|
|
var startValue = 300,
|
2014-08-23 19:34:04 +00:00
|
|
|
element = $( "#draggable1" )
|
|
|
|
// http://bugs.jqueryui.com/ticket/5009
|
|
|
|
// scroll not working with parent's position fixed
|
|
|
|
.wrap( "<div id='wrapper' />" )
|
|
|
|
// http://bugs.jqueryui.com/ticket/9612
|
|
|
|
// abspos elements inside of fixed elements moving away from the mouse when scrolling
|
|
|
|
.wrap( "<div id='wrapper2' />" )
|
|
|
|
.draggable({
|
|
|
|
drag: function() {
|
|
|
|
startValue += 100;
|
|
|
|
$( document ).scrollTop( startValue ).scrollLeft( startValue );
|
|
|
|
},
|
|
|
|
stop: function( event, ui ) {
|
|
|
|
equal( ui.position.left, 10, "left position is correct when parent position is fixed" );
|
|
|
|
equal( ui.position.top, 10, "top position is correct when parent position is fixed" );
|
|
|
|
$( document ).scrollTop( 0 ).scrollLeft( 0 );
|
|
|
|
}
|
|
|
|
});
|
2013-04-03 16:26:39 +00:00
|
|
|
|
2013-10-19 15:12:35 +00:00
|
|
|
TestHelpers.forceScrollableWindow();
|
|
|
|
|
2013-04-03 16:26:39 +00:00
|
|
|
$( "#wrapper" ).css( "position", "fixed" );
|
2014-08-23 19:34:04 +00:00
|
|
|
$( "#wrapper2" ).css( "position", "absolute" );
|
2013-04-03 16:26:39 +00:00
|
|
|
|
|
|
|
element.simulate( "drag", {
|
|
|
|
dx: 10,
|
|
|
|
dy: 10,
|
|
|
|
moves: 3
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-10 03:00:58 +00:00
|
|
|
$( [ "hidden", "auto", "scroll" ] ).each(function() {
|
|
|
|
var overflow = this;
|
|
|
|
|
|
|
|
// http://bugs.jqueryui.com/ticket/9379 - position bug in scrollable div
|
|
|
|
// http://bugs.jqueryui.com/ticket/10147 - Wrong position in a parent with "overflow: hidden"
|
|
|
|
test( "position in scrollable parent with overflow: " + overflow, function() {
|
|
|
|
expect( 2 );
|
|
|
|
|
|
|
|
$( "#qunit-fixture" ).html( "<div id='outer'><div id='inner'></div><div id='dragged'>a</div></div>" );
|
|
|
|
$( "#inner" ).css({ position: "absolute", width: "500px", height: "500px" });
|
|
|
|
$( "#outer" ).css({ position: "absolute", width: "300px", height: "300px" });
|
|
|
|
$( "#dragged" ).css({ width: "10px", height: "10px" });
|
|
|
|
|
|
|
|
var moves = 3,
|
|
|
|
startValue = 0,
|
|
|
|
dragDelta = 20,
|
|
|
|
delta = 100,
|
|
|
|
|
|
|
|
// we scroll after each drag event, so subtract 1 from number of moves for expected
|
|
|
|
expected = delta + ( ( moves - 1 ) * dragDelta ),
|
|
|
|
element = $( "#dragged" ).draggable({
|
|
|
|
drag: function() {
|
|
|
|
startValue += dragDelta;
|
|
|
|
$( "#outer" ).scrollTop( startValue ).scrollLeft( startValue );
|
|
|
|
},
|
|
|
|
stop: function( event, ui ) {
|
|
|
|
equal( ui.position.left, expected, "left position is correct when grandparent is scrolled" );
|
|
|
|
equal( ui.position.top, expected, "top position is correct when grandparent is scrolled" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$( "#outer" ).css( "overflow", overflow );
|
2013-10-29 22:35:42 +00:00
|
|
|
|
2014-08-10 03:00:58 +00:00
|
|
|
element.simulate( "drag", {
|
|
|
|
dy: delta,
|
|
|
|
dx: delta,
|
|
|
|
moves: moves
|
2013-10-29 22:35:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-07 12:19:30 +00:00
|
|
|
test( "#5727: draggable from iframe", function() {
|
2013-10-21 14:59:54 +00:00
|
|
|
expect( 1 );
|
2010-06-14 15:20:03 +00:00
|
|
|
|
2013-12-14 22:38:33 +00:00
|
|
|
var iframeBody, draggable1,
|
|
|
|
iframe = $( "<iframe />" ).appendTo( "#qunit-fixture" ),
|
|
|
|
iframeDoc = ( iframe[ 0 ].contentWindow || iframe[ 0 ].contentDocument ).document;
|
|
|
|
|
|
|
|
iframeDoc.write( "<!doctype html><html><body>" );
|
|
|
|
iframeDoc.close();
|
|
|
|
|
|
|
|
iframeBody = $( iframeDoc.body ).append( "<div style='width: 2px; height: 2px;' />" );
|
|
|
|
draggable1 = iframeBody.find( "div" );
|
2010-06-14 15:20:03 +00:00
|
|
|
|
|
|
|
draggable1.draggable();
|
|
|
|
|
|
|
|
equal( draggable1.closest( iframeBody ).length, 1 );
|
|
|
|
|
2013-10-21 14:59:54 +00:00
|
|
|
// TODO: fix draggable within an IFRAME to fire events on the element properly
|
|
|
|
// and these TestHelpers.draggable.shouldMove relies on events for testing
|
|
|
|
//TestHelpers.draggable.shouldMove( draggable1, "draggable from an iframe" );
|
2010-06-14 15:20:03 +00:00
|
|
|
});
|
|
|
|
|
2013-05-02 19:07:34 +00:00
|
|
|
test( "#8399: A draggable should become the active element after you are finished interacting with it, but not before.", function() {
|
|
|
|
expect( 2 );
|
|
|
|
|
|
|
|
var element = $( "<a href='#'>link</a>" ).appendTo( "#qunit-fixture" ).draggable();
|
|
|
|
|
|
|
|
$( document ).one( "mousemove", function() {
|
|
|
|
notStrictEqual( document.activeElement, element.get( 0 ), "moving a draggable anchor did not make it the active element" );
|
|
|
|
});
|
|
|
|
|
|
|
|
TestHelpers.draggable.move( element, 50, 50 );
|
|
|
|
|
|
|
|
strictEqual( document.activeElement, element.get( 0 ), "finishing moving a draggable anchor made it the active element" );
|
|
|
|
});
|
|
|
|
|
2014-08-16 13:44:05 +00:00
|
|
|
asyncTest( "blur behavior", function() {
|
|
|
|
expect( 3 );
|
|
|
|
|
|
|
|
var element = $( "#draggable1" ).draggable(),
|
|
|
|
focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );
|
|
|
|
|
|
|
|
TestHelpers.onFocus( focusElement, function() {
|
|
|
|
strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );
|
2013-05-01 22:01:32 +00:00
|
|
|
|
2014-08-16 13:44:05 +00:00
|
|
|
TestHelpers.draggable.move( focusElement, 1, 1 );
|
2013-05-01 22:01:32 +00:00
|
|
|
|
2014-08-16 13:44:05 +00:00
|
|
|
// http://bugs.jqueryui.com/ticket/10527
|
|
|
|
// Draggable: Can't select option in modal dialog (IE8)
|
|
|
|
strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused after mousing down on itself" );
|
2013-05-01 22:01:32 +00:00
|
|
|
|
|
|
|
TestHelpers.draggable.move( element, 50, 50 );
|
|
|
|
|
2014-08-16 13:44:05 +00:00
|
|
|
// http://bugs.jqueryui.com/ticket/4261
|
|
|
|
// active element should blur when mousing down on a draggable
|
|
|
|
notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" );
|
2013-05-01 22:01:32 +00:00
|
|
|
start();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-30 14:57:30 +00:00
|
|
|
test( "ui-draggable-handle assigned to appropriate element", function( assert ) {
|
2014-07-15 17:38:00 +00:00
|
|
|
expect( 5 );
|
2013-12-13 03:10:06 +00:00
|
|
|
|
2014-07-15 17:38:00 +00:00
|
|
|
var p = $( "<p>" ).appendTo( "#qunit-fixture" ),
|
|
|
|
element = $( "<div><p></p></div>" ).appendTo( "#qunit-fixture" ).draggable();
|
2015-01-30 14:57:30 +00:00
|
|
|
assert.hasClasses( element, "ui-draggable-handle" );
|
2013-12-13 03:10:06 +00:00
|
|
|
|
|
|
|
element.draggable( "option", "handle", "p" );
|
2015-01-30 14:57:30 +00:00
|
|
|
assert.lacksClasses( element, "ui-draggable-handle" );
|
|
|
|
assert.hasClasses( element.find( "p" ), "ui-draggable-handle",
|
2014-07-15 17:38:00 +00:00
|
|
|
"ensure handle class name is constrained within the draggble (#10212)" );
|
2015-01-30 14:57:30 +00:00
|
|
|
assert.lacksClasses( p, "ui-draggable-handle" );
|
2014-07-15 17:38:00 +00:00
|
|
|
|
2013-12-13 03:10:06 +00:00
|
|
|
element.draggable( "destroy" );
|
2015-01-30 14:57:30 +00:00
|
|
|
assert.lacksClasses( element.find( "p" ), "ui-draggable-handle" );
|
2013-12-13 03:10:06 +00:00
|
|
|
});
|
|
|
|
|
2015-01-30 14:57:30 +00:00
|
|
|
test( "ui-draggable-handle managed correctly in nested draggables", function( assert ) {
|
2014-07-15 19:16:26 +00:00
|
|
|
expect( 4 );
|
|
|
|
var parent = $( "<div><div></div></div>" ).draggable().appendTo( "#qunit-fixture" ),
|
|
|
|
child = parent.find( "div" ).draggable();
|
|
|
|
|
2015-01-30 14:57:30 +00:00
|
|
|
assert.hasClasses( parent, "ui-draggable-handle", "parent has class name on init" );
|
|
|
|
assert.hasClasses( child, "ui-draggable-handle", "child has class name on init" );
|
2014-07-15 19:16:26 +00:00
|
|
|
|
|
|
|
parent.draggable( "destroy" );
|
2015-01-30 14:57:30 +00:00
|
|
|
assert.lacksClasses( parent, "ui-draggable-handle", "parent loses class name on destroy" );
|
|
|
|
assert.hasClasses( child, "ui-draggable-handle", "child retains class name on destroy" );
|
2014-07-15 19:16:26 +00:00
|
|
|
});
|
|
|
|
|
2013-03-29 00:03:14 +00:00
|
|
|
// http://bugs.jqueryui.com/ticket/7772
|
|
|
|
// when css 'right' is set, element resizes on drag
|
|
|
|
test( "setting right/bottom css shouldn't cause resize", function() {
|
2014-08-22 15:51:15 +00:00
|
|
|
expect( 4 );
|
2013-03-29 00:03:14 +00:00
|
|
|
|
|
|
|
var finalOffset,
|
|
|
|
element = $( "#draggable3" ),
|
|
|
|
origWidth = element.width(),
|
|
|
|
origHeight = element.height(),
|
|
|
|
origOffset = element.offset();
|
|
|
|
|
|
|
|
element.draggable();
|
|
|
|
|
|
|
|
TestHelpers.draggable.move( element, -50, -50 );
|
|
|
|
|
|
|
|
finalOffset = element.offset();
|
|
|
|
finalOffset.left += 50;
|
|
|
|
finalOffset.top += 50;
|
|
|
|
|
2014-08-21 01:10:33 +00:00
|
|
|
closeEnough( element.width(), origWidth, 1, "element retains width" );
|
|
|
|
closeEnough( element.height(), origHeight, 1, "element retains height" );
|
2014-08-22 15:51:15 +00:00
|
|
|
closeEnough( finalOffset.top, origOffset.top, "element moves the correct vertical distance" );
|
|
|
|
closeEnough( finalOffset.top, origOffset.top, "element moves the correct horizontal distance" );
|
2013-03-29 00:03:14 +00:00
|
|
|
});
|
|
|
|
|
2013-01-12 05:33:45 +00:00
|
|
|
})( jQuery );
|