Debian Codegraph @

tests/unit/draggable/helper.js

raw symbols SCIP
1 define( [
2 "qunit",
3 "jquery",
4 "lib/helper",
5 "ui/widgets/draggable"
6 ], function( QUnit, $, helper ) {
7 "use strict";
8
9 return $.extend( helper, {
10
11 testDragPosition: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
12 msg = msg ? msg + "." : "";
13
14 $( el ).one( "dragstop", function( event, ui ) {
15 var positionExpected = { left: ui.originalPosition.left + expectedDX, top: ui.originalPosition.top + expectedDY };
16 assert.deepEqual( ui.position, positionExpected, "position dragged[" + dx + ", " + dy + "] " + msg );
17 } );
18 },
19
20 testDragOffset: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
21 msg = msg ? msg + "." : "";
22
23 var offsetBefore = el.offset(),
24 offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };
25
26 $( el ).one( "dragstop", function( event, ui ) {
27 assert.deepEqual( ui.offset, offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
28 } );
29 },
30
31 testDragHelperOffset: function( assert, el, dx, dy, expectedDX, expectedDY, msg ) {
32 msg = msg ? msg + "." : "";
33
34 var offsetBefore = el.offset(),
35 offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };
36
37 $( el ).one( "dragstop", function( event, ui ) {
38 assert.deepEqual( ui.helper.offset(), offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
39 } );
40 },
41
42 testDrag: function( assert, el, handle, dx, dy, expectedDX, expectedDY, msg ) {
43 this.testDragPosition( assert, el, dx, dy, expectedDX, expectedDY, msg );
44 this.testDragOffset( assert, el, dx, dy, expectedDX, expectedDY, msg );
45
46 $( handle ).simulate( "drag", {
47 dx: dx,
48 dy: dy
49 } );
50 },
51
52 shouldMovePositionButNotOffset: function( assert, el, msg, handle ) {
53 handle = handle || el;
54 this.testDragPosition( assert, el, 100, 100, 100, 100, msg );
55 this.testDragHelperOffset( assert, el, 100, 100, 0, 0, msg );
56
57 $( handle ).simulate( "drag", {
58 dx: 100,
59 dy: 100
60 } );
61 },
62
63 shouldMove: function( assert, el, msg, handle ) {
64 handle = handle || el;
65 this.testDrag( assert, el, handle, 100, 100, 100, 100, msg );
66 },
67
68 shouldNotMove: function( assert, el, msg, handle ) {
69 handle = handle || el;
70 this.testDrag( assert, el, handle, 100, 100, 0, 0, msg );
71 },
72
73 shouldNotDrag: function( assert, el, msg, handle ) {
74 handle = handle || el;
75
76 var newOffset,
77 element = $( el ),
78 beginOffset = element.offset();
79
80 element.on( "dragstop", function() {
81 assert.ok( false, "should not drag " + msg );
82 } );
83
84 $( handle ).simulate( "drag", {
85 dx: 100,
86 dy: 100
87 } );
88
89 newOffset = element.offset();
90
91 // Also assert that draggable did not move, to ensure it isn't just
92 // that drag did not fire and draggable still somehow moved
93 assert.equal( newOffset.left, beginOffset.left, "Offset left should not be different" );
94 assert.equal( newOffset.top, beginOffset.top, "Offset top should not be different" );
95
96 element.off( "dragstop" );
97 },
98
99 setScrollable: function( what, isScrollable ) {
100 var overflow = isScrollable ? "scroll" : "hidden";
101 $( what ).css( { overflow: overflow, overflowX: overflow, overflowY: overflow } );
102 },
103
104 testScroll: function( assert, el, position ) {
105 var oldPosition = $( "#main" ).css( "position" );
106 $( "#main" ).css( { position: position, top: "0px", left: "0px" } );
107 this.shouldMove( assert, el, position + " parent" );
108 $( "#main" ).css( "position", oldPosition );
109 },
110
111 restoreScroll: function( what ) {
112 $( what ).scrollTop( 0 ).scrollLeft( 0 );
113 },
114
115 setScroll: function( what ) {
116 $( what ).scrollTop( 100 ).scrollLeft( 100 );
117 },
118
119 border: function( el, side ) {
120 return parseInt( el.css( "border-" + side + "-width" ), 10 ) || 0;
121 },
122
123 margin: function( el, side ) {
124 return parseInt( el.css( "margin-" + side ), 10 ) || 0;
125 },
126
127 move: function( el, x, y ) {
128 $( el ).simulate( "drag", {
129 dx: x,
130 dy: y
131 } );
132 },
133
134 trackMouseCss: function( el ) {
135 el.on( "drag", function() {
136 el.data( "last_dragged_cursor", $( "body" ).css( "cursor" ) );
137 } );
138 },
139
140 trackAppendedParent: function( el ) {
141
142 // TODO: appendTo is currently ignored if helper is original (see #7044)
143 el.draggable( "option", "helper", "clone" );
144
145 // Get what parent is at time of drag
146 el.on( "drag", function( e, ui ) {
147 el.data( "last_dragged_parent", ui.helper.parent()[ 0 ] );
148 } );
149 }
150 } );
151
152 } );