aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/draggable/helper.js
blob: c1920f23bb8a12b6278ab9e236b4a5379bfecc21 (plain)
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
define( [
	"qunit",
	"jquery",
	"lib/helper",
	"ui/widgets/draggable"
], function( QUnit, $, helper ) {
"use strict";

return $.extend( helper, {

	// TODO: remove the unreliable offset hacks
	unreliableOffset: $.ui.ie && ( !document.documentMode || document.documentMode < 8 ) ? 2 : 0,

	// Support: Opera 12.10, Safari 5.1, jQuery <1.8

	unreliableContains: ( function() {
		var element = $( "<div>" );
		return $.contains( element[ 0 ].ownerDocument, element[ 0 ] );
	} )(),

	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 };
			assert.deepEqual( ui.position, positionExpected, "position dragged[" + dx + ", " + dy + "] " + msg );
		} );
	},

	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 ) {
			assert.deepEqual( ui.offset, offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
		} );
	},

	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 ) {
			assert.deepEqual( ui.helper.offset(), offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
		} );
	},

	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
		} );
	},

	shouldMovePositionButNotOffset: function( assert, el, msg, handle ) {
		handle = handle || el;
		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
		} );
	},

	shouldMove: function( assert, el, msg, handle ) {
		handle = handle || el;
		this.testDrag( assert, el, handle, 100, 100, 100, 100, msg );
	},

	shouldNotMove: function( assert, el, msg, handle ) {
		handle = handle || el;
		this.testDrag( assert, el, handle, 100, 100, 0, 0, msg );
	},

	shouldNotDrag: function( assert, el, msg, handle ) {
		handle = handle || el;

		var newOffset,
			element = $( el ),
			beginOffset = element.offset();

		element.on( "dragstop", function() {
			assert.ok( false, "should not drag " + msg );
		} );

		$( handle ).simulate( "drag", {
			dx: 100,
			dy: 100
		} );

		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
		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";
		$( what ).css( { overflow: overflow, overflowX: overflow, overflowY: overflow } );
	},

	testScroll: function( assert, el, position ) {
		var oldPosition = $( "#main" ).css( "position" );
		$( "#main" ).css( { position: position, top: "0px", left: "0px" } );
		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
		} );
	},

	trackMouseCss: function( el ) {
		el.on( "drag", function() {
			el.data( "last_dragged_cursor", $( "body" ).css( "cursor" ) );
		} );
	},

	trackAppendedParent: function( el ) {

		// TODO: appendTo is currently ignored if helper is original (see #7044)
		el.draggable( "option", "helper", "clone" );

		// Get what parent is at time of drag
		el.on( "drag", function( e, ui ) {
			el.data( "last_dragged_parent", ui.helper.parent()[ 0 ] );
		} );
	}
} );

} );