Debian Codegraph @

tests/unit/draggable/core.js

raw symbols SCIP
1 define( [
2 "qunit",
3 "jquery",
4 "lib/helper",
5 "./helper",
6 "ui/widgets/draggable",
7 "ui/widgets/droppable",
8 "ui/widgets/resizable"
9 ], function( QUnit, $, helper, testHelper ) {
10 "use strict";
11
12 QUnit.module( "draggable: core", { afterEach: helper.moduleAfterEach } );
13
14 QUnit.test( "element types", function( assert ) {
15 var typeNames = (
16 "p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form" +
17 ",table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr" +
18 ",acronym,code,samp,kbd,var,img,hr" +
19 ",input,button,label,select,iframe"
20 ).split( "," );
21
22 assert.expect( typeNames.length * 2 );
23
24 $.each( typeNames, function( i ) {
25 var offsetBefore, offsetAfter,
26 typeName = typeNames[ i ],
27 el = $( document.createElement( typeName ) ).appendTo( "#qunit-fixture" );
28
29 if ( typeName === "table" ) {
30 el.append( "<tr><td>content</td></tr>" );
31 }
32
33 el.draggable( { cancel: "" } );
34 offsetBefore = el.offset();
35 el.simulate( "drag", {
36 dx: 50,
37 dy: 50
38 } );
39 offsetAfter = el.offset();
40
41 // Support: Firefox, Chrome
42 // There are some rounding errors, so we can't say equal, we have to settle for close enough
43 assert.close( offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + "> left" );
44 assert.close( offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + "> top" );
45 el.draggable( "destroy" );
46 el.remove();
47 } );
48 } );
49
50 QUnit.test( "No options, relative", function( assert ) {
51 assert.expect( 2 );
52 testHelper.shouldMove( assert, $( "#draggable1" ).draggable(), "no options, relative" );
53 } );
54
55 QUnit.test( "No options, absolute", function( assert ) {
56 assert.expect( 2 );
57 testHelper.shouldMove( assert, $( "#draggable2" ).draggable(), "no options, absolute" );
58 } );
59
60 QUnit.test( "resizable handle with complex markup (#8756 / #8757)", function( assert ) {
61 assert.expect( 2 );
62
63 $( "#draggable1" )
64 .append(
65 $( "<div>" )
66 .addClass( "ui-resizable-handle ui-resizable-w" )
67 .append( $( "<div>" ) )
68 );
69
70 var handle = $( ".ui-resizable-w div" ),
71 target = $( "#draggable1" ).draggable().resizable( { handles: "all" } );
72
73 // Todo: fix resizable so it doesn't require a mouseover
74 handle.simulate( "mouseover" ).simulate( "drag", { dx: -50 } );
75 assert.equal( target.width(), 250, "compare width" );
76
77 // Todo: fix resizable so it doesn't require a mouseover
78 handle.simulate( "mouseover" ).simulate( "drag", { dx: 50 } );
79 assert.equal( target.width(), 200, "compare width" );
80 } );
81
82 QUnit.test( "#8269: Removing draggable element on drop", function( assert ) {
83 assert.expect( 2 );
84
85 var element = $( "#draggable1" ).wrap( "<div id='wrapper' />" ).draggable( {
86 stop: function() {
87 assert.ok( true, "stop still called despite element being removed from DOM on drop" );
88 }
89 } ),
90 dropOffset = $( "#droppable" ).offset();
91
92 $( "#droppable" ).droppable( {
93 drop: function() {
94 $( "#wrapper" ).remove();
95 assert.ok( true, "element removed from DOM on drop" );
96 }
97 } );
98
99 element.simulate( "drag", {
100 handle: "corner",
101 x: dropOffset.left,
102 y: dropOffset.top
103 } );
104 } );
105
106 // https://bugs.jqueryui.com/ticket/7778
107 // drag element breaks in IE8 when its content is replaced onmousedown
108 QUnit.test( "Stray mousemove after mousedown still drags", function( assert ) {
109 assert.expect( 2 );
110
111 var element = $( "#draggable1" ).draggable( { scroll: false } );
112
113 // In IE8, when content is placed under the mouse (e.g. when draggable content is replaced
114 // on mousedown), mousemove is triggered on those elements even though the mouse hasn't moved.
115 element.on( "mousedown", function() {
116 $( document ).simulate( "mousemove", { button: -1 } );
117 } );
118
119 testHelper.shouldMove( assert, element, "element is draggable" );
120 } );
121
122 QUnit.test( "#6258: not following mouse when scrolled and using overflow-y: scroll", function( assert ) {
123 assert.expect( 2 );
124
125 var element = $( "#draggable1" ).draggable( {
126 stop: function( event, ui ) {
127 assert.equal( ui.position.left, 1, "left position is correct despite overflow on HTML" );
128 assert.equal( ui.position.top, 1, "top position is correct despite overflow on HTML" );
129 $( "html" )
130 .css( "overflow-y", oldOverflowY )
131 .css( "overflow-x", oldOverflowX )
132 .scrollTop( 0 )
133 .scrollLeft( 0 );
134 }
135 } ),
136 oldOverflowY = $( "html" ).css( "overflow-y" ),
137 oldOverflowX = $( "html" ).css( "overflow-x" );
138
139 testHelper.forceScrollableWindow();
140
141 $( "html" )
142 .css( "overflow-y", "scroll" )
143 .css( "overflow-x", "scroll" )
144 .scrollTop( 300 )
145 .scrollLeft( 300 );
146
147 element.simulate( "drag", {
148 dx: 1,
149 dy: 1,
150 moves: 1
151 } );
152 } );
153
154 QUnit.test( "#9315: jumps down with offset of scrollbar", function( assert ) {
155 assert.expect( 2 );
156
157 var element = $( "#draggable2" ).draggable( {
158 stop: function( event, ui ) {
159 assert.equal( ui.position.left, 11, "left position is correct when position is absolute" );
160 assert.equal( ui.position.top, 11, "top position is correct when position is absolute" );
161 $( "html" ).scrollTop( 0 ).scrollLeft( 0 );
162 }
163 } );
164
165 testHelper.forceScrollableWindow();
166
167 $( "html" ).scrollTop( 300 ).scrollLeft( 300 );
168
169 element.simulate( "drag", {
170 dx: 1,
171 dy: 1,
172 moves: 1
173 } );
174 } );
175
176 QUnit.test( "scroll offset with fixed ancestors", function( assert ) {
177 assert.expect( 2 );
178
179 var startValue = 300,
180 element = $( "#draggable1" )
181
182 // https://bugs.jqueryui.com/ticket/5009
183 // scroll not working with parent's position fixed
184 .wrap( "<div id='wrapper' />" )
185
186 // https://bugs.jqueryui.com/ticket/9612
187 // abspos elements inside of fixed elements moving away from the mouse when scrolling
188 .wrap( "<div id='wrapper2' />" )
189 .draggable( {
190 drag: function() {
191 startValue += 100;
192 $( document ).scrollTop( startValue ).scrollLeft( startValue );
193 },
194 stop: function( event, ui ) {
195 assert.equal( ui.position.left, 10, "left position is correct when parent position is fixed" );
196 assert.equal( ui.position.top, 10, "top position is correct when parent position is fixed" );
197 $( document ).scrollTop( 0 ).scrollLeft( 0 );
198 }
199 } );
200
201 testHelper.forceScrollableWindow();
202
203 $( "#wrapper" ).css( "position", "fixed" );
204 $( "#wrapper2" ).css( "position", "absolute" );
205
206 element.simulate( "drag", {
207 dx: 10,
208 dy: 10,
209 moves: 3
210 } );
211 } );
212
213 $( [ "hidden", "auto", "scroll" ] ).each( function() {
214 var overflow = this;
215
216 // https://bugs.jqueryui.com/ticket/9379 - position bug in scrollable div
217 // https://bugs.jqueryui.com/ticket/10147 - Wrong position in a parent with "overflow: hidden"
218 QUnit.test( "position in scrollable parent with overflow: " + overflow, function( assert ) {
219 assert.expect( 2 );
220
221 $( "#qunit-fixture" ).html( "<div id='outer'><div id='inner'></div><div id='dragged'>a</div></div>" );
222 $( "#inner" ).css( { position: "absolute", width: "500px", height: "500px" } );
223 $( "#outer" ).css( { position: "absolute", width: "300px", height: "300px" } );
224 $( "#dragged" ).css( { width: "10px", height: "10px" } );
225
226 var moves = 3,
227 startValue = 0,
228 dragDelta = 20,
229 delta = 100,
230
231 // We scroll after each drag event, so subtract 1 from number of moves for expected
232 expected = delta + ( ( moves - 1 ) * dragDelta ),
233 element = $( "#dragged" ).draggable( {
234 drag: function() {
235 startValue += dragDelta;
236 $( "#outer" ).scrollTop( startValue ).scrollLeft( startValue );
237 },
238 stop: function( event, ui ) {
239 assert.equal( ui.position.left, expected, "left position is correct when grandparent is scrolled" );
240 assert.equal( ui.position.top, expected, "top position is correct when grandparent is scrolled" );
241 }
242 } );
243
244 $( "#outer" ).css( "overflow", overflow );
245
246 element.simulate( "drag", {
247 dy: delta,
248 dx: delta,
249 moves: moves
250 } );
251 } );
252 } );
253
254 QUnit.test( "#5727: draggable from iframe", function( assert ) {
255 assert.expect( 1 );
256
257 var iframeBody, draggable1,
258 iframe = $( "<iframe />" ).appendTo( "#qunit-fixture" ),
259 iframeDoc = ( iframe[ 0 ].contentWindow || iframe[ 0 ].contentDocument ).document;
260
261 iframeDoc.write( "<!doctype html><html><body>" );
262 iframeDoc.close();
263
264 iframeBody = $( iframeDoc.body ).append( "<div style='width: 2px; height: 2px;' />" );
265 draggable1 = iframeBody.find( "div" );
266
267 draggable1.draggable();
268
269 assert.equal( draggable1.closest( iframeBody ).length, 1 );
270
271 // TODO: fix draggable within an IFRAME to fire events on the element properly
272 // and these testHelper.shouldMove relies on events for testing
273 //testHelper.shouldMove( assert, draggable1, "draggable from an iframe" );
274 } );
275
276 QUnit.test( "#8399: A draggable should become the active element after you are finished interacting with it, but not before.", function( assert ) {
277 assert.expect( 2 );
278
279 var element = $( "<a href='#'>link</a>" ).appendTo( "#qunit-fixture" ).draggable();
280
281 $( document ).one( "mousemove", function() {
282 assert.notStrictEqual( document.activeElement, element.get( 0 ), "moving a draggable anchor did not make it the active element" );
283 } );
284
285 testHelper.move( element, 50, 50 );
286
287 assert.strictEqual( document.activeElement, element.get( 0 ), "finishing moving a draggable anchor made it the active element" );
288 } );
289
290 QUnit.test( "blur behavior - handle is main element", function( assert ) {
291 var ready = assert.async();
292 assert.expect( 3 );
293
294 var element = $( "#draggable1" ).draggable(),
295 focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );
296
297 testHelper.onFocus( focusElement, function() {
298 assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );
299
300 testHelper.move( focusElement, 1, 1 );
301
302 // https://bugs.jqueryui.com/ticket/10527
303 // Draggable: Can't select option in modal dialog (IE8)
304 assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused after mousing down on itself" );
305
306 testHelper.move( element, 50, 50 );
307
308 // https://bugs.jqueryui.com/ticket/4261
309 // active element should blur when mousing down on a draggable
310 assert.notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" );
311 ready();
312 } );
313 } );
314
315 QUnit.test( "blur behavior - descendant of handle", function( assert ) {
316 var ready = assert.async();
317 assert.expect( 2 );
318
319 var element = $( "#draggable2" ).draggable( { handle: "span" } ),
320
321 // The handle is a descendant, but we also want to grab a descendant of the handle
322 handle = element.find( "span em" ),
323 focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );
324
325 testHelper.onFocus( focusElement, function() {
326 assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );
327
328 testHelper.move( handle, 50, 50 );
329
330 // Elements outside of the handle should blur (#12472, #14905)
331 assert.notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" );
332 ready();
333 } );
334 } );
335
336 QUnit.test( "blur behavior - off handle", function( assert ) {
337 var ready = assert.async();
338 assert.expect( 3 );
339
340 var element = $( "#draggable2" ).draggable( { handle: "span" } ),
341 focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );
342
343 // Mock $.ui.trigger with a spy
344 var _trigger = $.fn.trigger;
345 var blurCalledCount = 0;
346 $.fn.trigger = function( eventName ) {
347 if ( eventName === "blur" ) {
348 blurCalledCount++;
349 }
350 };
351
352 testHelper.onFocus( focusElement, function() {
353 assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );
354
355 testHelper.move( element, 1, 1 );
356 assert.strictEqual( blurCalledCount, 0, "draggable doesn't blur when mousing down off handle" );
357
358 testHelper.move( element.find( "span" ), 1, 1 );
359 assert.strictEqual( blurCalledCount, 1, "draggable blurs when mousing down on handle" );
360
361 // Restore trigger
362 $.fn.trigger = _trigger;
363
364 ready();
365 } );
366 } );
367
368 QUnit.test( "ui-draggable-handle assigned to appropriate element", function( assert ) {
369 assert.expect( 5 );
370
371 var p = $( "<p>" ).appendTo( "#qunit-fixture" ),
372 element = $( "<div><p></p></div>" ).appendTo( "#qunit-fixture" ).draggable();
373 assert.hasClasses( element, "ui-draggable-handle" );
374
375 element.draggable( "option", "handle", "p" );
376 assert.lacksClasses( element, "ui-draggable-handle" );
377 assert.hasClasses( element.find( "p" ), "ui-draggable-handle",
378 "ensure handle class name is constrained within the draggble (#10212)" );
379 assert.lacksClasses( p, "ui-draggable-handle" );
380
381 element.draggable( "destroy" );
382 assert.lacksClasses( element.find( "p" ), "ui-draggable-handle" );
383 } );
384
385 QUnit.test( "ui-draggable-handle managed correctly in nested draggables", function( assert ) {
386 assert.expect( 4 );
387 var parent = $( "<div><div></div></div>" ).draggable().appendTo( "#qunit-fixture" ),
388 child = parent.find( "div" ).draggable();
389
390 assert.hasClasses( parent, "ui-draggable-handle", "parent has class name on init" );
391 assert.hasClasses( child, "ui-draggable-handle", "child has class name on init" );
392
393 parent.draggable( "destroy" );
394 assert.lacksClasses( parent, "ui-draggable-handle", "parent loses class name on destroy" );
395 assert.hasClasses( child, "ui-draggable-handle", "child retains class name on destroy" );
396 } );
397
398 QUnit.test( "does not stop propagation to window", function( assert ) {
399 assert.expect( 1 );
400 var element = $( "#draggable1" ).draggable();
401
402 var handler = function() {
403 assert.ok( true, "mouseup propagates to window" );
404 };
405 $( window ).on( "mouseup", handler );
406
407 element.simulate( "drag", {
408 dx: 10,
409 dy: 10
410 } );
411
412 $( window ).off( "mouseup", handler );
413 } );
414
415 } );