2015-04-06 16:42:49 +00:00
|
|
|
define( [
|
2016-04-03 16:21:57 +00:00
|
|
|
"qunit",
|
2015-04-06 16:42:49 +00:00
|
|
|
"jquery",
|
2020-05-16 07:16:24 +00:00
|
|
|
"lib/helper",
|
2015-07-15 01:59:40 +00:00
|
|
|
"ui/widgets/draggable"
|
2020-05-16 07:16:24 +00:00
|
|
|
], function( QUnit, $, helper ) {
|
2021-06-06 22:58:12 +00:00
|
|
|
"use strict";
|
2009-02-01 00:25:58 +00:00
|
|
|
|
2013-01-12 05:33:45 +00:00
|
|
|
var element;
|
2009-02-01 00:25:58 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
QUnit.module( "draggable: events", {
|
|
|
|
beforeEach: function() {
|
2015-08-24 12:59:44 +00:00
|
|
|
element = $( "<div>" ).appendTo( "#qunit-fixture" );
|
2013-01-12 05:33:45 +00:00
|
|
|
},
|
2016-04-03 16:21:57 +00:00
|
|
|
afterEach: function() {
|
2015-08-24 12:59:44 +00:00
|
|
|
element.draggable( "destroy" );
|
2020-05-16 07:16:24 +00:00
|
|
|
return helper.moduleAfterEach.apply( this, arguments );
|
2013-01-12 05:33:45 +00:00
|
|
|
}
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2009-02-01 00:25:58 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
QUnit.test( "callbacks occurrence count", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2009-02-01 00:25:58 +00:00
|
|
|
|
2012-11-03 20:17:16 +00:00
|
|
|
var start = 0,
|
|
|
|
stop = 0,
|
2013-01-12 05:33:45 +00:00
|
|
|
dragc = 0;
|
|
|
|
|
2015-08-24 12:59:44 +00:00
|
|
|
element.draggable( {
|
2013-01-12 05:33:45 +00:00
|
|
|
start: function() {
|
|
|
|
start++;
|
|
|
|
},
|
|
|
|
drag: function() {
|
|
|
|
dragc++;
|
|
|
|
},
|
|
|
|
stop: function() {
|
|
|
|
stop++;
|
|
|
|
}
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2013-01-12 05:33:45 +00:00
|
|
|
|
|
|
|
element.simulate( "drag", {
|
2012-12-09 02:27:37 +00:00
|
|
|
dx: 10,
|
|
|
|
dy: 10
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2009-02-01 00:25:58 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
assert.equal( start, 1, "start callback should happen exactly once" );
|
|
|
|
assert.equal( dragc, 3, "drag callback should happen exactly once per mousemove" );
|
|
|
|
assert.equal( stop, 1, "stop callback should happen exactly once" );
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2009-02-01 00:25:58 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
QUnit.test( "stopping the start callback", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2012-11-03 20:17:16 +00:00
|
|
|
var start = 0,
|
|
|
|
stop = 0,
|
2013-01-12 05:33:45 +00:00
|
|
|
dragc = 0;
|
|
|
|
|
2015-08-24 12:59:44 +00:00
|
|
|
element.draggable( {
|
2013-01-12 05:33:45 +00:00
|
|
|
start: function() {
|
|
|
|
start++;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
drag: function() {
|
|
|
|
dragc++;
|
|
|
|
},
|
|
|
|
stop: function() {
|
|
|
|
stop++;
|
|
|
|
}
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2013-01-12 05:33:45 +00:00
|
|
|
|
|
|
|
element.simulate( "drag", {
|
2012-12-09 02:27:37 +00:00
|
|
|
dx: 10,
|
|
|
|
dy: 10
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
assert.equal( start, 1, "start callback should happen exactly once" );
|
|
|
|
assert.equal( dragc, 0, "drag callback should not happen at all" );
|
|
|
|
assert.equal( stop, 0, "stop callback should not happen if there wasnt even a start" );
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
QUnit.test( "stopping the drag callback", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2012-11-03 20:17:16 +00:00
|
|
|
var start = 0,
|
|
|
|
stop = 0,
|
2013-01-12 05:33:45 +00:00
|
|
|
dragc = 0;
|
|
|
|
|
2015-08-24 12:59:44 +00:00
|
|
|
element.draggable( {
|
2013-01-12 05:33:45 +00:00
|
|
|
start: function() {
|
|
|
|
start++;
|
|
|
|
},
|
|
|
|
drag: function() {
|
|
|
|
dragc++;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
stop: function() {
|
|
|
|
stop++;
|
|
|
|
}
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2013-01-12 05:33:45 +00:00
|
|
|
|
|
|
|
element.simulate( "drag", {
|
2012-12-09 02:27:37 +00:00
|
|
|
dx: 10,
|
|
|
|
dy: 10
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
assert.equal( start, 1, "start callback should happen exactly once" );
|
|
|
|
assert.equal( stop, 1, "stop callback should happen, as we need to actively stop the drag" );
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
QUnit.test( "stopping the stop callback", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2015-08-24 12:59:44 +00:00
|
|
|
element.draggable( {
|
2012-12-26 13:08:48 +00:00
|
|
|
helper: "clone",
|
2013-01-12 05:33:45 +00:00
|
|
|
stop: function() {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2013-01-12 05:33:45 +00:00
|
|
|
element.simulate( "drag", {
|
2012-12-09 02:27:37 +00:00
|
|
|
dx: 10,
|
|
|
|
dy: 10
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
assert.ok( element.draggable( "instance" ).helper, "the clone should not be deleted if the stop callback is stopped" );
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2014-08-17 01:27:16 +00:00
|
|
|
|
|
|
|
// http://bugs.jqueryui.com/ticket/6884
|
|
|
|
// Draggable: ui.offset.left differs between the "start" and "drag" hooks
|
2016-04-03 16:21:57 +00:00
|
|
|
QUnit.test( "position and offset in hash is consistent between start, drag, and stop", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2014-08-17 01:27:16 +00:00
|
|
|
|
|
|
|
var startPos, startOffset, dragPos, dragOffset, stopPos, stopOffset;
|
|
|
|
|
|
|
|
element = $( "<div style='margin: 2px;'></div>" ).appendTo( "#qunit-fixture" );
|
|
|
|
|
2015-08-24 12:59:44 +00:00
|
|
|
element.draggable( {
|
2014-08-17 01:27:16 +00:00
|
|
|
start: function( event, ui ) {
|
|
|
|
startPos = ui.position;
|
|
|
|
startOffset = ui.offset;
|
|
|
|
},
|
|
|
|
drag: function( event, ui ) {
|
|
|
|
dragPos = ui.position;
|
|
|
|
dragOffset = ui.offset;
|
|
|
|
},
|
|
|
|
stop: function( event, ui ) {
|
|
|
|
stopPos = ui.position;
|
|
|
|
stopOffset = ui.offset;
|
|
|
|
}
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2014-08-17 01:27:16 +00:00
|
|
|
|
|
|
|
element.simulate( "drag", {
|
|
|
|
dx: 10,
|
|
|
|
dy: 10,
|
|
|
|
moves: 1
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2014-08-17 01:27:16 +00:00
|
|
|
startPos.left += 10;
|
|
|
|
startPos.top += 10;
|
|
|
|
startOffset.left += 10;
|
|
|
|
startOffset.top += 10;
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2016-04-03 16:21:57 +00:00
|
|
|
assert.deepEqual( startPos, dragPos, "start position equals drag position plus distance" );
|
|
|
|
assert.deepEqual( dragPos, stopPos, "drag position equals stop position" );
|
|
|
|
assert.deepEqual( startOffset, dragOffset, "start offset equals drag offset plus distance" );
|
|
|
|
assert.deepEqual( dragOffset, stopOffset, "drag offset equals stop offset" );
|
2015-08-24 12:59:44 +00:00
|
|
|
} );
|
2010-01-28 16:35:59 +00:00
|
|
|
|
2015-04-06 16:42:49 +00:00
|
|
|
} );
|