@@ -12,6 +12,12 @@ describe('uiSortable', function() {
12
12
return list . children ( ) . map ( function ( ) { return this . innerHTML ; } ) . toArray ( ) ;
13
13
}
14
14
return [ ] ;
15
+ } ,
16
+ toEqualListInnerContent : function ( list ) {
17
+ if ( list && list . length ) {
18
+ return list . children ( ) . map ( function ( ) { return $ ( this ) . find ( '.itemContent' ) . html ( ) ; } ) . toArray ( ) ;
19
+ }
20
+ return [ ] ;
15
21
}
16
22
} ) ;
17
23
} ) ;
@@ -232,6 +238,116 @@ describe('uiSortable', function() {
232
238
} ) ;
233
239
} ) ;
234
240
241
+ it ( 'should work when "handle" option is used' , function ( ) {
242
+ inject ( function ( $compile , $rootScope ) {
243
+ var element ;
244
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}"><span class="handle">H</span> <span class="itemContent">{{ item }}</span></li></ul>' ) ( $rootScope ) ;
245
+ $rootScope . $apply ( function ( ) {
246
+ $rootScope . opts = {
247
+ handle : '.handle'
248
+ } ;
249
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
250
+ } ) ;
251
+
252
+ host . append ( element ) ;
253
+
254
+ var li = element . find ( 'li:eq(1)' ) ;
255
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
256
+ li . find ( '.handle' ) . simulate ( 'drag' , { dy : dy } ) ;
257
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Three' , 'Two' ] ) ;
258
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
259
+
260
+ li = element . find ( 'li:eq(1)' ) ;
261
+ dy = - ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
262
+ li . find ( '.handle' ) . simulate ( 'drag' , { dy : dy } ) ;
263
+ expect ( $rootScope . items ) . toEqual ( [ 'Three' , 'One' , 'Two' ] ) ;
264
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
265
+
266
+ $ ( element ) . remove ( ) ;
267
+ } ) ;
268
+ } ) ;
269
+
270
+ it ( 'should properly remove elements after a sorting' , function ( ) {
271
+ inject ( function ( $compile , $rootScope ) {
272
+ var element ;
273
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}"><span class="handle">H</span> <span class="itemContent">{{ item }}</span> <button type="button" class="removeButton" ng-click="remove(item, $index)">X</button></li></ul>' ) ( $rootScope ) ;
274
+ $rootScope . $apply ( function ( ) {
275
+ $rootScope . opts = {
276
+ handle : '.handle'
277
+ } ;
278
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
279
+
280
+ $rootScope . remove = function ( item , itemIndex ) {
281
+ $rootScope . items . splice ( itemIndex , 1 ) ;
282
+ } ;
283
+ } ) ;
284
+
285
+ host . append ( element ) ;
286
+
287
+ var li = element . find ( 'li:eq(1)' ) ;
288
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
289
+ li . find ( '.handle' ) . simulate ( 'drag' , { dy : dy } ) ;
290
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Three' , 'Two' ] ) ;
291
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
292
+
293
+ li = element . find ( 'li:eq(1)' ) ;
294
+ li . find ( '.removeButton' ) . click ( ) ;
295
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' ] ) ;
296
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
297
+
298
+ li = element . find ( 'li:eq(0)' ) ;
299
+ dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
300
+ li . find ( '.handle' ) . simulate ( 'drag' , { dy : dy } ) ;
301
+ expect ( $rootScope . items ) . toEqual ( [ 'Two' , 'One' ] ) ;
302
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
303
+
304
+ li = element . find ( 'li:eq(0)' ) ;
305
+ li . find ( '.removeButton' ) . click ( ) ;
306
+ expect ( $rootScope . items ) . toEqual ( [ 'One' ] ) ;
307
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
308
+
309
+ $ ( element ) . remove ( ) ;
310
+ } ) ;
311
+ } ) ;
312
+
313
+ it ( 'should properly remove elements after a drag is reverted' , function ( ) {
314
+ inject ( function ( $compile , $rootScope ) {
315
+ var element ;
316
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}"><span class="handle">H</span> <span class="itemContent">{{ item }}</span> <button type="button" class="removeButton" ng-click="remove(item, $index)">X</button></li></ul>' ) ( $rootScope ) ;
317
+ $rootScope . $apply ( function ( ) {
318
+ $rootScope . opts = {
319
+ handle : '.handle'
320
+ } ;
321
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
322
+
323
+ $rootScope . remove = function ( item , itemIndex ) {
324
+ $rootScope . items . splice ( itemIndex , 1 ) ;
325
+ } ;
326
+ } ) ;
327
+
328
+ host . append ( element ) ;
329
+
330
+ var li = element . find ( 'li:eq(0)' ) ;
331
+ var dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
332
+ li . find ( '.handle' ) . simulate ( 'dragAndRevert' , { dy : dy } ) ;
333
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
334
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
335
+
336
+ li = element . find ( 'li:eq(0)' ) ;
337
+ li . find ( '.removeButton' ) . click ( ) ;
338
+ expect ( $rootScope . items ) . toEqual ( [ 'Two' , 'Three' ] ) ;
339
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
340
+
341
+ li = element . find ( 'li:eq(0)' ) ;
342
+ dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
343
+ li . find ( '.handle' ) . simulate ( 'drag' , { dy : dy } ) ;
344
+ expect ( $rootScope . items ) . toEqual ( [ 'Three' , 'Two' ] ) ;
345
+ expect ( $rootScope . items ) . toEqualListInnerContent ( element ) ;
346
+
347
+ $ ( element ) . remove ( ) ;
348
+ } ) ;
349
+ } ) ;
350
+
235
351
} ) ;
236
352
237
353
describe ( 'Multiple sortables related' , function ( ) {
0 commit comments