1
+ 'use strict' ;
2
+
3
+ describe ( 'uiSortable' , function ( ) {
4
+
5
+ // Ensure the sortable angular module is loaded
6
+ beforeEach ( module ( 'ui.sortable' ) ) ;
7
+ beforeEach ( module ( 'ui.sortable.testHelper' ) ) ;
8
+
9
+ var EXTRA_DY_PERCENTAGE , listContent ;
10
+
11
+ beforeEach ( inject ( function ( sortableTestHelper ) {
12
+ EXTRA_DY_PERCENTAGE = sortableTestHelper . EXTRA_DY_PERCENTAGE ;
13
+ listContent = sortableTestHelper . listContent ;
14
+ } ) ) ;
15
+
16
+ describe ( 'Callbacks related' , function ( ) {
17
+
18
+ var host ;
19
+
20
+ beforeEach ( inject ( function ( ) {
21
+ host = $ ( '<div id="test-host"></div>' ) ;
22
+ $ ( 'body' ) . append ( host ) ;
23
+ } ) ) ;
24
+
25
+ afterEach ( function ( ) {
26
+ host . remove ( ) ;
27
+ host = null ;
28
+ } ) ;
29
+
30
+ it ( 'should cancel sorting of node "Two"' , function ( ) {
31
+ inject ( function ( $compile , $rootScope ) {
32
+ var element ;
33
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
34
+ $rootScope . $apply ( function ( ) {
35
+ $rootScope . opts = {
36
+ update : function ( e , ui ) {
37
+ if ( ui . item . scope ( ) . item === 'Two' ) {
38
+ ui . item . sortable . cancel ( ) ;
39
+ }
40
+ }
41
+ } ;
42
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
43
+ } ) ;
44
+
45
+ host . append ( element ) ;
46
+
47
+ var li = element . find ( ':eq(1)' ) ;
48
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
49
+ li . simulate ( 'drag' , { dy : dy } ) ;
50
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
51
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
52
+
53
+ li = element . find ( ':eq(0)' ) ;
54
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
55
+ li . simulate ( 'drag' , { dy : dy } ) ;
56
+ expect ( $rootScope . items ) . toEqual ( [ 'Two' , 'Three' , 'One' ] ) ;
57
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
58
+
59
+ li = element . find ( ':eq(2)' ) ;
60
+ dy = - ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
61
+ li . simulate ( 'drag' , { dy : dy } ) ;
62
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
63
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
64
+
65
+ $ ( element ) . remove ( ) ;
66
+ } ) ;
67
+ } ) ;
68
+
69
+ it ( 'should cancel sorting of nodes that contain "Two"' , function ( ) {
70
+ inject ( function ( $compile , $rootScope ) {
71
+ var elementTop , elementBottom ;
72
+ elementTop = $compile ( '<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsTop"><li ng-repeat="item in itemsTop" id="s-top-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
73
+ elementBottom = $compile ( '<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsBottom"><li ng-repeat="item in itemsBottom" id="s-bottom-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
74
+ $rootScope . $apply ( function ( ) {
75
+ $rootScope . itemsTop = [ 'Top One' , 'Top Two' , 'Top Three' ] ;
76
+ $rootScope . itemsBottom = [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ;
77
+ $rootScope . opts = {
78
+ connectWith : '.cross-sortable' ,
79
+ update : function ( e , ui ) {
80
+ if ( ui . item . scope ( ) &&
81
+ ( typeof ui . item . scope ( ) . item === 'string' ) &&
82
+ ui . item . scope ( ) . item . indexOf ( 'Two' ) >= 0 ) {
83
+ ui . item . sortable . cancel ( ) ;
84
+ }
85
+ }
86
+ } ;
87
+ } ) ;
88
+
89
+ host . append ( elementTop ) . append ( elementBottom ) ;
90
+
91
+ var li1 = elementTop . find ( ':eq(1)' ) ;
92
+ var li2 = elementBottom . find ( ':eq(0)' ) ;
93
+ var dy = EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) + ( li2 . position ( ) . top - li1 . position ( ) . top ) ;
94
+ li1 . simulate ( 'drag' , { dy : dy } ) ;
95
+ expect ( $rootScope . itemsTop ) . toEqual ( [ 'Top One' , 'Top Two' , 'Top Three' ] ) ;
96
+ expect ( $rootScope . itemsBottom ) . toEqual ( [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ) ;
97
+ expect ( $rootScope . itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
98
+ expect ( $rootScope . itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
99
+
100
+ li1 = elementBottom . find ( ':eq(1)' ) ;
101
+ li2 = elementTop . find ( ':eq(1)' ) ;
102
+ dy = - EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) - ( li1 . position ( ) . top - li2 . position ( ) . top ) ;
103
+ li1 . simulate ( 'drag' , { dy : dy } ) ;
104
+ expect ( $rootScope . itemsTop ) . toEqual ( [ 'Top One' , 'Top Two' , 'Top Three' ] ) ;
105
+ expect ( $rootScope . itemsBottom ) . toEqual ( [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ) ;
106
+ expect ( $rootScope . itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
107
+ expect ( $rootScope . itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
108
+
109
+ li1 = elementTop . find ( ':eq(0)' ) ;
110
+ li2 = elementBottom . find ( ':eq(0)' ) ;
111
+ dy = EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) + ( li2 . position ( ) . top - li1 . position ( ) . top ) ;
112
+ li1 . simulate ( 'drag' , { dy : dy } ) ;
113
+ expect ( $rootScope . itemsTop ) . toEqual ( [ 'Top Two' , 'Top Three' ] ) ;
114
+ expect ( $rootScope . itemsBottom ) . toEqual ( [ 'Bottom One' , 'Top One' , 'Bottom Two' , 'Bottom Three' ] ) ;
115
+ expect ( $rootScope . itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
116
+ expect ( $rootScope . itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
117
+
118
+ li1 = elementBottom . find ( ':eq(1)' ) ;
119
+ li2 = elementTop . find ( ':eq(1)' ) ;
120
+ dy = - EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) - ( li1 . position ( ) . top - li2 . position ( ) . top ) ;
121
+ li1 . simulate ( 'drag' , { dy : dy } ) ;
122
+ expect ( $rootScope . itemsTop ) . toEqual ( [ 'Top Two' , 'Top One' , 'Top Three' ] ) ;
123
+ expect ( $rootScope . itemsBottom ) . toEqual ( [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ) ;
124
+ expect ( $rootScope . itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
125
+ expect ( $rootScope . itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
126
+
127
+ $ ( elementTop ) . remove ( ) ;
128
+ $ ( elementBottom ) . remove ( ) ;
129
+ } ) ;
130
+ } ) ;
131
+
132
+ it ( 'should update model from update() callback' , function ( ) {
133
+ inject ( function ( $compile , $rootScope ) {
134
+ var element , logsElement ;
135
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
136
+ logsElement = $compile ( '<ul ng-model="logs"><li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li></ul>' ) ( $rootScope ) ;
137
+ $rootScope . $apply ( function ( ) {
138
+ $rootScope . opts = {
139
+ update : function ( e , ui ) {
140
+ $rootScope . logs . push ( 'Moved element ' + ui . item . scope ( ) . item ) ;
141
+ }
142
+ } ;
143
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
144
+ $rootScope . logs = [ ] ;
145
+ } ) ;
146
+
147
+ host . append ( element ) . append ( logsElement ) ;
148
+
149
+ var li = element . find ( ':eq(1)' ) ;
150
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
151
+ li . simulate ( 'drag' , { dy : dy } ) ;
152
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Three' , 'Two' ] ) ;
153
+ expect ( $rootScope . logs ) . toEqual ( [ 'Moved element Two' ] ) ;
154
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
155
+ expect ( $rootScope . logs ) . toEqual ( listContent ( logsElement ) ) ;
156
+
157
+ $ ( element ) . remove ( ) ;
158
+ $ ( logsElement ) . remove ( ) ;
159
+ } ) ;
160
+ } ) ;
161
+
162
+ // ensure scope.apply() is called after a stop() callback
163
+ it ( 'should update model from stop() callback' , function ( ) {
164
+ inject ( function ( $compile , $rootScope ) {
165
+ var element , logsElement ;
166
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
167
+ logsElement = $compile ( '<ul ng-model="logs"><li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li></ul>' ) ( $rootScope ) ;
168
+ $rootScope . $apply ( function ( ) {
169
+ $rootScope . opts = {
170
+ stop : function ( e , ui ) {
171
+ $rootScope . logs . push ( 'Moved element ' + ui . item . scope ( ) . item ) ;
172
+ }
173
+ } ;
174
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
175
+ $rootScope . logs = [ ] ;
176
+ } ) ;
177
+
178
+ host . append ( element ) . append ( logsElement ) ;
179
+
180
+ var li = element . find ( ':eq(1)' ) ;
181
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
182
+ li . simulate ( 'drag' , { dy : dy } ) ;
183
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Three' , 'Two' ] ) ;
184
+ expect ( $rootScope . logs ) . toEqual ( [ 'Moved element Two' ] ) ;
185
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
186
+ expect ( $rootScope . logs ) . toEqual ( listContent ( logsElement ) ) ;
187
+
188
+ $ ( element ) . remove ( ) ;
189
+ $ ( logsElement ) . remove ( ) ;
190
+ } ) ;
191
+ } ) ;
192
+
193
+ } ) ;
194
+
195
+ } ) ;
0 commit comments