jquery-ui/tests/unit/draggable/helper.js

153 lines
4.2 KiB
JavaScript
Raw Normal View History

define( [
2016-04-03 16:21:57 +00:00
"qunit",
"jquery",
"lib/helper",
"ui/widgets/draggable"
2016-04-03 16:21:57 +00:00
], function( QUnit, $, helper ) {
"use strict";
return $.extend( helper, {
2016-04-03 16:21:57 +00:00
testDragPosition: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
msg = msg ? msg + "." : "";
$( el ).one( "dragstop", function( event, ui ) {
var positionExpected = { left: ui.originalPosition.left + expectedDX, top: ui.originalPosition.top + expectedDY };
2016-04-03 16:21:57 +00:00
assert.deepEqual( ui.position, positionExpected, "position dragged[" + dx + ", " + dy + "] " + msg );
2015-08-24 12:59:44 +00:00
} );
},
2016-04-03 16:21:57 +00:00
testDragOffset: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
msg = msg ? msg + "." : "";
var offsetBefore = el.offset(),
offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };
$( el ).one( "dragstop", function( event, ui ) {
2016-04-03 16:21:57 +00:00
assert.deepEqual( ui.offset, offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
2015-08-24 12:59:44 +00:00
} );
},
2016-04-03 16:21:57 +00:00
testDragHelperOffset: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
msg = msg ? msg + "." : "";
var offsetBefore = el.offset(),
offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };
$( el ).one( "dragstop", function( event, ui ) {
2016-04-03 16:21:57 +00:00
assert.deepEqual( ui.helper.offset(), offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
2015-08-24 12:59:44 +00:00
} );
},
2016-04-03 16:21:57 +00:00
testDrag: function( assert, el, handle, dx, dy, expectedDX, expectedDY, msg ) {
this.testDragPosition( assert, el, dx, dy, expectedDX, expectedDY, msg );
this.testDragOffset( assert, el, dx, dy, expectedDX, expectedDY, msg );
$( handle ).simulate( "drag", {
dx: dx,
dy: dy
2015-08-24 12:59:44 +00:00
} );
},
2016-04-03 16:21:57 +00:00
shouldMovePositionButNotOffset: function( assert, el, msg, handle ) {
handle = handle || el;
2016-04-03 16:21:57 +00:00
this.testDragPosition( assert, el, 100, 100, 100, 100, msg );
this.testDragHelperOffset( assert, el, 100, 100, 0, 0, msg );
$( handle ).simulate( "drag", {
dx: 100,
dy: 100
2015-08-24 12:59:44 +00:00
} );
},
2016-04-03 16:21:57 +00:00
shouldMove: function( assert, el, msg, handle ) {
handle = handle || el;
2016-04-03 16:21:57 +00:00
this.testDrag( assert, el, handle, 100, 100, 100, 100, msg );
},
2016-04-03 16:21:57 +00:00
shouldNotMove: function( assert, el, msg, handle ) {
handle = handle || el;
2016-04-03 16:21:57 +00:00
this.testDrag( assert, el, handle, 100, 100, 0, 0, msg );
},
2016-04-03 16:21:57 +00:00
shouldNotDrag: function( assert, el, msg, handle ) {
handle = handle || el;
var newOffset,
element = $( el ),
beginOffset = element.offset();
element.on( "dragstop", function() {
2016-04-03 16:21:57 +00:00
assert.ok( false, "should not drag " + msg );
2015-08-24 12:59:44 +00:00
} );
$( handle ).simulate( "drag", {
dx: 100,
dy: 100
2015-08-24 12:59:44 +00:00
} );
newOffset = element.offset();
// Also assert that draggable did not move, to ensure it isn't just
// that drag did not fire and draggable still somehow moved
2016-04-03 16:21:57 +00:00
assert.equal( newOffset.left, beginOffset.left, "Offset left should not be different" );
assert.equal( newOffset.top, beginOffset.top, "Offset top should not be different" );
element.off( "dragstop" );
},
setScrollable: function( what, isScrollable ) {
var overflow = isScrollable ? "scroll" : "hidden";
2015-08-24 12:59:44 +00:00
$( what ).css( { overflow: overflow, overflowX: overflow, overflowY: overflow } );
},
2016-04-03 16:21:57 +00:00
testScroll: function( assert, el, position ) {
var oldPosition = $( "#main" ).css( "position" );
2015-08-24 12:59:44 +00:00
$( "#main" ).css( { position: position, top: "0px", left: "0px" } );
2016-04-03 16:21:57 +00:00
this.shouldMove( assert, el, position + " parent" );
$( "#main" ).css( "position", oldPosition );
},
restoreScroll: function( what ) {
$( what ).scrollTop( 0 ).scrollLeft( 0 );
},
setScroll: function( what ) {
$( what ).scrollTop( 100 ).scrollLeft( 100 );
},
border: function( el, side ) {
return parseInt( el.css( "border-" + side + "-width" ), 10 ) || 0;
},
margin: function( el, side ) {
return parseInt( el.css( "margin-" + side ), 10 ) || 0;
},
move: function( el, x, y ) {
$( el ).simulate( "drag", {
dx: x,
dy: y
2015-08-24 12:59:44 +00:00
} );
},
trackMouseCss: function( el ) {
el.on( "drag", function() {
el.data( "last_dragged_cursor", $( "body" ).css( "cursor" ) );
2015-08-24 12:59:44 +00:00
} );
},
trackAppendedParent: function( el ) {
2015-08-24 12:59:44 +00:00
// TODO: appendTo is currently ignored if helper is original (see #7044)
el.draggable( "option", "helper", "clone" );
// Get what parent is at time of drag
2015-08-24 12:59:44 +00:00
el.on( "drag", function( e, ui ) {
el.data( "last_dragged_parent", ui.helper.parent()[ 0 ] );
2015-08-24 12:59:44 +00:00
} );
}
} );
} );