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