@@ -6,11 +6,12 @@ describe('uiSortable', function() {
66 beforeEach ( module ( 'ui.sortable' ) ) ;
77 beforeEach ( module ( 'ui.sortable.testHelper' ) ) ;
88
9- var EXTRA_DY_PERCENTAGE , listContent ;
9+ var EXTRA_DY_PERCENTAGE , listContent , listInnerContent ;
1010
1111 beforeEach ( inject ( function ( sortableTestHelper ) {
1212 EXTRA_DY_PERCENTAGE = sortableTestHelper . EXTRA_DY_PERCENTAGE ;
1313 listContent = sortableTestHelper . listContent ;
14+ listInnerContent = sortableTestHelper . listInnerContent ;
1415 } ) ) ;
1516
1617 describe ( 'Multiple sortables related' , function ( ) {
@@ -346,6 +347,193 @@ describe('uiSortable', function() {
346347 } ) ;
347348 } ) ;
348349
350+ it ( 'should work when "helper: function" that returns a list element is used' , function ( ) {
351+ inject ( function ( $compile , $rootScope ) {
352+ var elementTop , elementBottom ;
353+ elementTop = $compile ( '<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsTop"><li ng-repeat="item in itemsTop" id="s-top-{{$index}}" class="sortable-item">{{ item }}</li></ul>' ) ( $rootScope ) ;
354+ elementBottom = $compile ( '<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsBottom"><li ng-repeat="item in itemsBottom" id="s-bottom-{{$index}}" class="sortable-item">{{ item }}</li></ul>' ) ( $rootScope ) ;
355+ $rootScope . $apply ( function ( ) {
356+ $rootScope . itemsTop = [ 'Top One' , 'Top Two' , 'Top Three' ] ;
357+ $rootScope . itemsBottom = [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ;
358+ $rootScope . opts = {
359+ helper : function ( e , item ) {
360+ return item ;
361+ } ,
362+ connectWith : '.cross-sortable'
363+ } ;
364+ } ) ;
365+
366+ host . append ( elementTop ) . append ( elementBottom ) ;
367+
368+ var li1 = elementTop . find ( ':eq(0)' ) ;
369+ var li2 = elementBottom . find ( ':eq(0)' ) ;
370+ var dy = EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) + ( li2 . position ( ) . top - li1 . position ( ) . top ) ;
371+ li1 . simulate ( 'drag' , { dy : dy } ) ;
372+ expect ( $rootScope . itemsTop ) . toEqual ( [ 'Top Two' , 'Top Three' ] ) ;
373+ expect ( $rootScope . itemsBottom ) . toEqual ( [ 'Bottom One' , 'Top One' , 'Bottom Two' , 'Bottom Three' ] ) ;
374+ expect ( $rootScope . itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
375+ expect ( $rootScope . itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
376+
377+ li1 = elementBottom . find ( ':eq(1)' ) ;
378+ li2 = elementTop . find ( ':eq(1)' ) ;
379+ dy = - EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) - ( li1 . position ( ) . top - li2 . position ( ) . top ) ;
380+ li1 . simulate ( 'drag' , { dy : dy } ) ;
381+ expect ( $rootScope . itemsTop ) . toEqual ( [ 'Top Two' , 'Top One' , 'Top Three' ] ) ;
382+ expect ( $rootScope . itemsBottom ) . toEqual ( [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ) ;
383+ expect ( $rootScope . itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
384+ expect ( $rootScope . itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
385+
386+ $ ( elementTop ) . remove ( ) ;
387+ $ ( elementBottom ) . remove ( ) ;
388+ } ) ;
389+ } ) ;
390+
391+ it ( 'should work when "placeholder" and "helper: function" that returns a list element are used' , function ( ) {
392+ inject ( function ( $compile , $rootScope ) {
393+ var elementTop , elementBottom ;
394+ elementTop = $compile ( '<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsTop"><li ng-repeat="item in itemsTop" id="s-top-{{$index}}" class="sortable-item">{{ item }}</li></ul>' ) ( $rootScope ) ;
395+ elementBottom = $compile ( '<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsBottom"><li ng-repeat="item in itemsBottom" id="s-bottom-{{$index}}" class="sortable-item">{{ item }}</li></ul>' ) ( $rootScope ) ;
396+ $rootScope . $apply ( function ( ) {
397+ $rootScope . itemsTop = [ 'Top One' , 'Top Two' , 'Top Three' ] ;
398+ $rootScope . itemsBottom = [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ;
399+ $rootScope . opts = {
400+ helper : function ( e , item ) {
401+ return item ;
402+ } ,
403+ placeholder : 'sortable-item-placeholder' ,
404+ connectWith : '.cross-sortable'
405+ } ;
406+ } ) ;
407+
408+ host . append ( elementTop ) . append ( elementBottom ) ;
409+
410+ var li1 = elementTop . find ( ':eq(0)' ) ;
411+ var li2 = elementBottom . find ( ':eq(0)' ) ;
412+ var dy = EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) + ( li2 . position ( ) . top - li1 . position ( ) . top ) ;
413+ li1 . simulate ( 'drag' , { dy : dy } ) ;
414+ expect ( $rootScope . itemsTop ) . toEqual ( [ 'Top Two' , 'Top Three' ] ) ;
415+ expect ( $rootScope . itemsBottom ) . toEqual ( [ 'Bottom One' , 'Top One' , 'Bottom Two' , 'Bottom Three' ] ) ;
416+ expect ( $rootScope . itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
417+ expect ( $rootScope . itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
418+
419+ li1 = elementBottom . find ( ':eq(1)' ) ;
420+ li2 = elementTop . find ( ':eq(1)' ) ;
421+ dy = - EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) - ( li1 . position ( ) . top - li2 . position ( ) . top ) ;
422+ li1 . simulate ( 'drag' , { dy : dy } ) ;
423+ expect ( $rootScope . itemsTop ) . toEqual ( [ 'Top Two' , 'Top One' , 'Top Three' ] ) ;
424+ expect ( $rootScope . itemsBottom ) . toEqual ( [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ) ;
425+ expect ( $rootScope . itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
426+ expect ( $rootScope . itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
427+
428+ $ ( elementTop ) . remove ( ) ;
429+ $ ( elementBottom ) . remove ( ) ;
430+ } ) ;
431+ } ) ;
432+
433+ it ( 'should update model when sorting between nested sortables' , function ( ) {
434+ inject ( function ( $compile , $rootScope ) {
435+ var elementTree , li1 , li2 , dy ;
436+
437+ elementTree = $compile ( '' . concat (
438+ '<ul ui-sortable="sortableOptions" ng-model="items" class="apps-container outterList" style="float: left;margin-left: 10px;padding-bottom: 10px;">' ,
439+ '<li ng-repeat="item in items">' ,
440+ '<div>' ,
441+ '<span class="itemContent lvl1ItemContent">{{item.text}}</span>' ,
442+ '<ul ui-sortable="sortableOptions" ng-model="item.items" class="apps-container innerList" style="margin-left: 10px;padding-bottom: 10px;">' ,
443+ '<li ng-repeat="i in item.items">' ,
444+ '<span class="itemContent lvl2ItemContent">{{i.text}}</span>' ,
445+ '</li>' ,
446+ '</ul>' ,
447+ '</div>' ,
448+ '</li>' ,
449+ '</ul>' ,
450+ '<div style="clear: both;"></div>' ) ) ( $rootScope ) ;
451+
452+ $rootScope . $apply ( function ( ) {
453+ $rootScope . items = [
454+ {
455+ text : 'Item 1' ,
456+ items : [ ]
457+ } ,
458+ {
459+ text : 'Item 2' ,
460+ items : [
461+ { text : 'Item 2.1' , items : [ ] } ,
462+ { text : 'Item 2.2' , items : [ ] }
463+ ]
464+ }
465+ ] ;
466+
467+ $rootScope . sortableOptions = {
468+ connectWith : '.apps-container'
469+ } ;
470+ } ) ;
471+
472+ host . append ( elementTree ) ;
473+
474+ // this should drag the item out of the list and
475+ // the item should return back to its original position
476+ li1 = elementTree . find ( '.innerList:last' ) . find ( ':last' ) ;
477+ li1 . simulate ( 'drag' , { dx : - 200 , moves : 30 } ) ;
478+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) )
479+ . toEqual ( [ 'Item 1' , 'Item 2' ] ) ;
480+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) )
481+ . toEqual ( listInnerContent ( elementTree , '.lvl1ItemContent' ) ) ;
482+ expect ( $rootScope . items [ 0 ] . items . map ( function ( x ) { return x . text ; } ) )
483+ . toEqual ( [ ] ) ;
484+ expect ( $rootScope . items [ 0 ] . items . map ( function ( x ) { return x . text ; } ) )
485+ . toEqual ( listInnerContent ( elementTree . find ( '.innerList:eq(0)' ) , '.lvl2ItemContent' ) ) ;
486+ expect ( $rootScope . items [ 1 ] . items . map ( function ( x ) { return x . text ; } ) )
487+ . toEqual ( [ 'Item 2.1' , 'Item 2.2' ] ) ;
488+ expect ( $rootScope . items [ 1 ] . items . map ( function ( x ) { return x . text ; } ) )
489+ . toEqual ( listInnerContent ( elementTree . find ( '.innerList:eq(1)' ) , '.lvl2ItemContent' ) ) ;
490+
491+ // this should drag the item from the inner list and
492+ // drop it to the outter list
493+ li1 = elementTree . find ( '.innerList:last' ) . find ( ':last' ) ;
494+ li2 = elementTree . find ( '> li:last' ) ;
495+ dy = EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) + ( li2 . position ( ) . top - li1 . position ( ) . top ) ;
496+ li1 . simulate ( 'drag' , { dy : dy } ) ;
497+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) )
498+ . toEqual ( [ 'Item 1' , 'Item 2.2' , 'Item 2' ] ) ;
499+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) )
500+ . toEqual ( listInnerContent ( elementTree , '.lvl1ItemContent' ) ) ;
501+ expect ( $rootScope . items [ 0 ] . items . map ( function ( x ) { return x . text ; } ) )
502+ . toEqual ( [ ] ) ;
503+ expect ( $rootScope . items [ 0 ] . items . map ( function ( x ) { return x . text ; } ) )
504+ . toEqual ( listInnerContent ( elementTree . find ( '.innerList:eq(0)' ) , '.lvl2ItemContent' ) ) ;
505+ expect ( $rootScope . items [ 1 ] . items . map ( function ( x ) { return x . text ; } ) )
506+ . toEqual ( [ ] ) ;
507+ expect ( $rootScope . items [ 1 ] . items . map ( function ( x ) { return x . text ; } ) )
508+ . toEqual ( listInnerContent ( elementTree . find ( '.innerList:eq(1)' ) , '.lvl2ItemContent' ) ) ;
509+ expect ( $rootScope . items [ 2 ] . items . map ( function ( x ) { return x . text ; } ) )
510+ . toEqual ( [ 'Item 2.1' ] ) ;
511+ expect ( $rootScope . items [ 2 ] . items . map ( function ( x ) { return x . text ; } ) )
512+ . toEqual ( listInnerContent ( elementTree . find ( '.innerList:eq(2)' ) , '.lvl2ItemContent' ) ) ;
513+
514+ // this should drag the item from the outter list and
515+ // drop it to the inner list
516+ li1 = elementTree . find ( '> li:first' ) ;
517+ li2 = elementTree . find ( '.innerList:last' ) . find ( ':last' ) ;
518+ dy = - EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) + ( li2 . position ( ) . top - li1 . position ( ) . top ) ;
519+ li1 . simulate ( 'drag' , { dy : dy } ) ;
520+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) )
521+ . toEqual ( [ 'Item 2.2' , 'Item 2' ] ) ;
522+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) )
523+ . toEqual ( listInnerContent ( elementTree , '.lvl1ItemContent' ) ) ;
524+ expect ( $rootScope . items [ 0 ] . items . map ( function ( x ) { return x . text ; } ) )
525+ . toEqual ( [ ] ) ;
526+ expect ( $rootScope . items [ 0 ] . items . map ( function ( x ) { return x . text ; } ) )
527+ . toEqual ( listInnerContent ( elementTree . find ( '.innerList:eq(0)' ) , '.lvl2ItemContent' ) ) ;
528+ expect ( $rootScope . items [ 1 ] . items . map ( function ( x ) { return x . text ; } ) )
529+ . toEqual ( [ 'Item 1' , 'Item 2.1' ] ) ;
530+ expect ( $rootScope . items [ 1 ] . items . map ( function ( x ) { return x . text ; } ) )
531+ . toEqual ( listInnerContent ( elementTree . find ( '.innerList:eq(1)' ) , '.lvl2ItemContent' ) ) ;
532+
533+ $ ( elementTree ) . remove ( ) ;
534+ } ) ;
535+ } ) ;
536+
349537 } ) ;
350538
351539} ) ;
0 commit comments