| 1 |
define( [ |
| 2 |
"qunit", |
| 3 |
"jquery", |
| 4 |
"lib/helper", |
| 5 |
"./helper", |
| 6 |
"ui/widgets/sortable", |
| 7 |
"ui/widgets/draggable" |
| 8 |
], function( QUnit, $, helper, testHelper ) { |
| 9 |
"use strict"; |
| 10 |
|
| 11 |
QUnit.module( "sortable: events", { afterEach: helper.moduleAfterEach } ); |
| 12 |
|
| 13 |
QUnit.test( "start", function( assert ) { |
| 14 |
assert.expect( 7 ); |
| 15 |
|
| 16 |
var hash; |
| 17 |
$( "#sortable" ).sortable( { |
| 18 |
start: function( e, ui ) { |
| 19 |
hash = ui; |
| 20 |
} |
| 21 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 22 |
dy: 10 |
| 23 |
} ); |
| 24 |
|
| 25 |
assert.ok( hash, "start event triggered" ); |
| 26 |
assert.ok( hash.helper, "UI hash includes: helper" ); |
| 27 |
assert.ok( hash.placeholder, "UI hash includes: placeholder" ); |
| 28 |
assert.ok( hash.item, "UI hash includes: item" ); |
| 29 |
assert.ok( !hash.sender, "UI hash does not include: sender" ); |
| 30 |
|
| 31 |
|
| 32 |
assert.ok( "position" in hash, "UI hash includes: position" ); |
| 33 |
assert.ok( "offset" in hash, "UI hash includes: offset" ); |
| 34 |
} ); |
| 35 |
|
| 36 |
QUnit.test( "sort", function( assert ) { |
| 37 |
assert.expect( 7 ); |
| 38 |
|
| 39 |
var hash; |
| 40 |
$( "#sortable" ).sortable( { |
| 41 |
sort: function( e, ui ) { |
| 42 |
hash = ui; |
| 43 |
} |
| 44 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 45 |
dy: 10 |
| 46 |
} ); |
| 47 |
|
| 48 |
assert.ok( hash, "sort event triggered" ); |
| 49 |
assert.ok( hash.helper, "UI hash includes: helper" ); |
| 50 |
assert.ok( hash.placeholder, "UI hash includes: placeholder" ); |
| 51 |
assert.ok( hash.position && ( "top" in hash.position && "left" in hash.position ), "UI hash includes: position" ); |
| 52 |
assert.ok( hash.offset && ( hash.offset.top && hash.offset.left ), "UI hash includes: offset" ); |
| 53 |
assert.ok( hash.item, "UI hash includes: item" ); |
| 54 |
assert.ok( !hash.sender, "UI hash does not include: sender" ); |
| 55 |
|
| 56 |
} ); |
| 57 |
|
| 58 |
QUnit.test( "change", function( assert ) { |
| 59 |
assert.expect( 8 ); |
| 60 |
|
| 61 |
var hash; |
| 62 |
$( "#sortable" ).sortable( { |
| 63 |
change: function( e, ui ) { |
| 64 |
hash = ui; |
| 65 |
} |
| 66 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 67 |
dx: 1, |
| 68 |
dy: 1 |
| 69 |
} ); |
| 70 |
|
| 71 |
assert.ok( !hash, "1px drag, change event should not be triggered" ); |
| 72 |
|
| 73 |
$( "#sortable" ).sortable( { |
| 74 |
change: function( e, ui ) { |
| 75 |
hash = ui; |
| 76 |
} |
| 77 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 78 |
dy: 22 |
| 79 |
} ); |
| 80 |
|
| 81 |
assert.ok( hash, "change event triggered" ); |
| 82 |
assert.ok( hash.helper, "UI hash includes: helper" ); |
| 83 |
assert.ok( hash.placeholder, "UI hash includes: placeholder" ); |
| 84 |
assert.ok( hash.position && ( "top" in hash.position && "left" in hash.position ), "UI hash includes: position" ); |
| 85 |
assert.ok( hash.offset && ( hash.offset.top && hash.offset.left ), "UI hash includes: offset" ); |
| 86 |
assert.ok( hash.item, "UI hash includes: item" ); |
| 87 |
assert.ok( !hash.sender, "UI hash does not include: sender" ); |
| 88 |
|
| 89 |
} ); |
| 90 |
|
| 91 |
QUnit.test( "beforeStop", function( assert ) { |
| 92 |
assert.expect( 7 ); |
| 93 |
|
| 94 |
var hash; |
| 95 |
$( "#sortable" ).sortable( { |
| 96 |
beforeStop: function( e, ui ) { |
| 97 |
hash = ui; |
| 98 |
} |
| 99 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 100 |
dy: 20 |
| 101 |
} ); |
| 102 |
|
| 103 |
assert.ok( hash, "beforeStop event triggered" ); |
| 104 |
assert.ok( hash.helper, "UI hash includes: helper" ); |
| 105 |
assert.ok( hash.placeholder, "UI hash includes: placeholder" ); |
| 106 |
assert.ok( hash.position && ( "top" in hash.position && "left" in hash.position ), "UI hash includes: position" ); |
| 107 |
assert.ok( hash.offset && ( hash.offset.top && hash.offset.left ), "UI hash includes: offset" ); |
| 108 |
assert.ok( hash.item, "UI hash includes: item" ); |
| 109 |
assert.ok( !hash.sender, "UI hash does not include: sender" ); |
| 110 |
|
| 111 |
} ); |
| 112 |
|
| 113 |
QUnit.test( "stop", function( assert ) { |
| 114 |
assert.expect( 7 ); |
| 115 |
|
| 116 |
var hash; |
| 117 |
$( "#sortable" ).sortable( { |
| 118 |
stop: function( e, ui ) { |
| 119 |
hash = ui; |
| 120 |
} |
| 121 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 122 |
dy: 20 |
| 123 |
} ); |
| 124 |
|
| 125 |
assert.ok( hash, "stop event triggered" ); |
| 126 |
assert.ok( !hash.helper, "UI should not include: helper" ); |
| 127 |
assert.ok( hash.placeholder, "UI hash includes: placeholder" ); |
| 128 |
assert.ok( hash.position && ( "top" in hash.position && "left" in hash.position ), "UI hash includes: position" ); |
| 129 |
assert.ok( hash.offset && ( hash.offset.top && hash.offset.left ), "UI hash includes: offset" ); |
| 130 |
assert.ok( hash.item, "UI hash includes: item" ); |
| 131 |
assert.ok( !hash.sender, "UI hash does not include: sender" ); |
| 132 |
|
| 133 |
} ); |
| 134 |
|
| 135 |
QUnit.test( "update", function( assert ) { |
| 136 |
assert.expect( 8 ); |
| 137 |
|
| 138 |
var hash; |
| 139 |
$( "#sortable" ).sortable( { |
| 140 |
update: function( e, ui ) { |
| 141 |
hash = ui; |
| 142 |
} |
| 143 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 144 |
dx: 1, |
| 145 |
dy: 1 |
| 146 |
} ); |
| 147 |
|
| 148 |
assert.ok( !hash, "1px drag, update event should not be triggered" ); |
| 149 |
|
| 150 |
$( "#sortable" ).sortable( { |
| 151 |
update: function( e, ui ) { |
| 152 |
hash = ui; |
| 153 |
} |
| 154 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 155 |
dy: 22 |
| 156 |
} ); |
| 157 |
|
| 158 |
assert.ok( hash, "update event triggered" ); |
| 159 |
assert.ok( !hash.helper, "UI hash should not include: helper" ); |
| 160 |
assert.ok( hash.placeholder, "UI hash includes: placeholder" ); |
| 161 |
assert.ok( hash.position && ( "top" in hash.position && "left" in hash.position ), "UI hash includes: position" ); |
| 162 |
assert.ok( hash.offset && ( hash.offset.top && hash.offset.left ), "UI hash includes: offset" ); |
| 163 |
assert.ok( hash.item, "UI hash includes: item" ); |
| 164 |
assert.ok( !hash.sender, "UI hash does not include: sender" ); |
| 165 |
|
| 166 |
} ); |
| 167 |
|
| 168 |
QUnit.test( "#3019: Stop fires too early", function( assert ) { |
| 169 |
assert.expect( 2 ); |
| 170 |
|
| 171 |
var helper = null, |
| 172 |
el = $( "#sortable" ).sortable( { |
| 173 |
stop: function( event, ui ) { |
| 174 |
helper = ui.helper; |
| 175 |
} |
| 176 |
} ); |
| 177 |
|
| 178 |
testHelper.sort( assert, $( "li", el )[ 0 ], 0, 44, 2, "Dragging the sortable" ); |
| 179 |
assert.equal( helper, null, "helper should be false" ); |
| 180 |
|
| 181 |
} ); |
| 182 |
|
| 183 |
QUnit.test( "#4752: link event firing on sortable with connect list", function( assert ) { |
| 184 |
assert.expect( 10 ); |
| 185 |
|
| 186 |
var fired = {}, |
| 187 |
hasFired = function( type ) { |
| 188 |
return ( type in fired ) && ( true === fired[ type ] ); |
| 189 |
}; |
| 190 |
|
| 191 |
$( "#sortable" ).clone().attr( "id", "sortable2" ).insertAfter( "#sortable" ); |
| 192 |
|
| 193 |
$( "#qunit-fixture ul" ).sortable( { |
| 194 |
connectWith: "#qunit-fixture ul", |
| 195 |
change: function() { |
| 196 |
fired.change = true; |
| 197 |
}, |
| 198 |
receive: function() { |
| 199 |
fired.receive = true; |
| 200 |
}, |
| 201 |
remove: function() { |
| 202 |
fired.remove = true; |
| 203 |
} |
| 204 |
} ); |
| 205 |
|
| 206 |
$( "#qunit-fixture ul" ).on( "click.ui-sortable-test", function() { |
| 207 |
fired.click = true; |
| 208 |
} ); |
| 209 |
|
| 210 |
$( "#sortable li" ).eq( 0 ).simulate( "click" ); |
| 211 |
assert.ok( !hasFired( "change" ), "Click only, change event should not have fired" ); |
| 212 |
assert.ok( hasFired( "click" ), "Click event should have fired" ); |
| 213 |
|
| 214 |
|
| 215 |
fired = {}; |
| 216 |
$( "#sortable li" ).eq( 0 ).simulate( "drag", { dx: 0, dy: 40 } ); |
| 217 |
assert.ok( hasFired( "change" ), "40px drag, change event should have fired" ); |
| 218 |
assert.ok( !hasFired( "receive" ), "Receive event should not have fired" ); |
| 219 |
assert.ok( !hasFired( "remove" ), "Remove event should not have fired" ); |
| 220 |
assert.ok( !hasFired( "click" ), "Click event should not have fired" ); |
| 221 |
|
| 222 |
|
| 223 |
fired = {}; |
| 224 |
$( "#sortable li" ).eq( 0 ).simulate( "drag", { dx: 0, dy: 150 } ); |
| 225 |
assert.ok( hasFired( "change" ), "150px drag, change event should have fired" ); |
| 226 |
assert.ok( hasFired( "receive" ), "Receive event should have fired" ); |
| 227 |
assert.ok( hasFired( "remove" ), "Remove event should have fired" ); |
| 228 |
assert.ok( !hasFired( "click" ), "Click event should not have fired" ); |
| 229 |
} ); |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
QUnit.test( "over", function( assert ) { |
| 242 |
assert.expect( 8 ); |
| 243 |
|
| 244 |
var hash, |
| 245 |
overCount = 0; |
| 246 |
|
| 247 |
$( "#sortable" ).sortable( { |
| 248 |
over: function( e, ui ) { |
| 249 |
hash = ui; |
| 250 |
overCount++; |
| 251 |
} |
| 252 |
} ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 253 |
dy: 20 |
| 254 |
} ); |
| 255 |
|
| 256 |
assert.ok( hash, "over event triggered" ); |
| 257 |
assert.ok( hash.helper, "UI includes: helper" ); |
| 258 |
assert.ok( hash.placeholder, "UI hash includes: placeholder" ); |
| 259 |
assert.ok( hash.position && ( "top" in hash.position && "left" in hash.position ), "UI hash includes: position" ); |
| 260 |
assert.ok( hash.offset && ( hash.offset.top && hash.offset.left ), "UI hash includes: offset" ); |
| 261 |
assert.ok( hash.item, "UI hash includes: item" ); |
| 262 |
assert.ok( hash.sender, "UI hash includes: sender" ); |
| 263 |
assert.equal( overCount, 1, "over fires only once" ); |
| 264 |
} ); |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
QUnit.test( "over, fires with draggable connected to sortable", function( assert ) { |
| 269 |
assert.expect( 3 ); |
| 270 |
|
| 271 |
var hash, |
| 272 |
overCount = 0, |
| 273 |
item = $( "<div></div>" ).text( "6" ).insertAfter( "#sortable" ); |
| 274 |
|
| 275 |
item.draggable( { |
| 276 |
connectToSortable: "#sortable" |
| 277 |
} ); |
| 278 |
$( ".connectWith" ).sortable( { |
| 279 |
connectWith: ".connectWith", |
| 280 |
over: function( event, ui ) { |
| 281 |
hash = ui; |
| 282 |
overCount++; |
| 283 |
} |
| 284 |
} ); |
| 285 |
|
| 286 |
item.simulate( "drag", { |
| 287 |
dy: -20 |
| 288 |
} ); |
| 289 |
|
| 290 |
assert.ok( hash, "over event triggered" ); |
| 291 |
assert.ok( !hash.sender, "UI should not include: sender" ); |
| 292 |
assert.equal( overCount, 1, "over fires only once" ); |
| 293 |
} ); |
| 294 |
|
| 295 |
QUnit.test( "over, with connected sortable", function( assert ) { |
| 296 |
assert.expect( 3 ); |
| 297 |
|
| 298 |
var hash, |
| 299 |
overCount = 0; |
| 300 |
|
| 301 |
$( ".connectWith" ).sortable( { |
| 302 |
connectWith: ".connectWith" |
| 303 |
} ); |
| 304 |
$( "#sortable2" ).on( "sortover", function( event, ui ) { |
| 305 |
hash = ui; |
| 306 |
overCount++; |
| 307 |
} ); |
| 308 |
$( "#sortable" ).find( "li" ).eq( 0 ).simulate( "drag", { |
| 309 |
dy: 102 |
| 310 |
} ); |
| 311 |
|
| 312 |
assert.ok( hash, "over event triggered" ); |
| 313 |
assert.equal( hash.sender[ 0 ], $( " #sortable" )[ 0 ], "UI includes: sender" ); |
| 314 |
assert.equal( overCount, 1, "over fires only once" ); |
| 315 |
} ); |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
QUnit.test( "out, with connected sortable", function( assert ) { |
| 324 |
assert.expect( 2 ); |
| 325 |
|
| 326 |
var hash, |
| 327 |
outCount = 0; |
| 328 |
|
| 329 |
$( ".connectWith" ).sortable( { |
| 330 |
connectWith: ".connectWith" |
| 331 |
} ); |
| 332 |
$( "#sortable" ).on( "sortout", function( event, ui ) { |
| 333 |
hash = ui; |
| 334 |
outCount++; |
| 335 |
} ); |
| 336 |
$( "#sortable" ).find( "li" ).last().simulate( "drag", { |
| 337 |
dy: 40 |
| 338 |
} ); |
| 339 |
|
| 340 |
assert.ok( hash, "out event triggered" ); |
| 341 |
assert.equal( outCount, 1, "out fires only once" ); |
| 342 |
} ); |
| 343 |
|
| 344 |
QUnit.test( "repeated out & over between connected sortables", function( assert ) { |
| 345 |
assert.expect( 2 ); |
| 346 |
|
| 347 |
var outCount = 0, |
| 348 |
overCount = 0; |
| 349 |
|
| 350 |
$( ".connectWith" ).sortable( { |
| 351 |
connectWith: ".connectWith", |
| 352 |
over: function() { |
| 353 |
overCount++; |
| 354 |
}, |
| 355 |
out: function( event, ui ) { |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if ( !ui.helper ) { |
| 360 |
outCount++; |
| 361 |
} |
| 362 |
} |
| 363 |
} ); |
| 364 |
$( "#sortable" ).find( "li" ).last().simulate( "drag", { |
| 365 |
dy: 40 |
| 366 |
} ).simulate( "drag", { |
| 367 |
dy: -40 |
| 368 |
} ); |
| 369 |
|
| 370 |
assert.equal( outCount, 2, "out fires twice" ); |
| 371 |
assert.equal( overCount, 4, "over fires four times" ); |
| 372 |
} ); |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
} ); |