6
6
@param [ui-sortable] {object} Options to pass to $.fn.sortable() merged onto ui.config
7
7
*/
8
8
angular . module ( 'ui.sortable' , [ ] )
9
- . value ( 'uiSortableConfig' , { } )
10
- . directive ( 'uiSortable' , [ 'uiSortableConfig' , '$log' ,
11
- function ( uiSortableConfig , log ) {
12
- return {
13
- require : '?ngModel' ,
14
- link : function ( scope , element , attrs , ngModel ) {
15
-
16
- function combineCallbacks ( first , second ) {
17
- if ( second && ( typeof second === 'function' ) ) {
18
- return function ( e , ui ) {
19
- first ( e , ui ) ;
20
- second ( e , ui ) ;
21
- } ;
22
- }
23
- return first ;
24
- }
25
-
26
- var opts = { } ;
27
-
28
- var callbacks = {
29
- receive : null ,
30
- remove :null ,
31
- start :null ,
32
- stop :null ,
33
- update :null
34
- } ;
9
+ . value ( 'uiSortableConfig' , { } )
10
+ . directive ( 'uiSortable' , [ 'uiSortableConfig' , '$log' ,
11
+ function ( uiSortableConfig , log ) {
12
+ return {
13
+ require : '?ngModel' ,
14
+ link : function ( scope , element , attrs , ngModel ) {
15
+
16
+ function combineCallbacks ( first , second ) {
17
+ if ( second && ( typeof second === 'function' ) ) {
18
+ return function ( e , ui ) {
19
+ first ( e , ui ) ;
20
+ second ( e , ui ) ;
21
+ } ;
22
+ }
23
+ return first ;
24
+ }
35
25
36
- var apply = function ( e , ui ) {
37
- if ( ui . item . sortable . resort || ui . item . sortable . relocate ) {
38
- scope . $apply ( ) ;
39
- }
40
- } ;
26
+ var opts = { } ;
41
27
42
- angular . extend ( opts , uiSortableConfig ) ;
28
+ var callbacks = {
29
+ receive : null ,
30
+ remove : null ,
31
+ start : null ,
32
+ stop : null ,
33
+ update : null
34
+ } ;
43
35
44
- if ( ngModel ) {
36
+ var apply = function ( e , ui ) {
37
+ if ( ui . item . sortable . resort || ui . item . sortable . relocate ) {
38
+ scope . $apply ( ) ;
39
+ }
40
+ } ;
45
41
46
- ngModel . $render = function ( ) {
47
- element . sortable ( 'refresh' ) ;
48
- } ;
42
+ angular . extend ( opts , uiSortableConfig ) ;
49
43
50
- callbacks . start = function ( e , ui ) {
51
- // Save position of dragged item
52
- ui . item . sortable = { index : ui . item . index ( ) } ;
53
- } ;
44
+ if ( ngModel ) {
54
45
55
- callbacks . update = function ( e , ui ) {
56
- // For some reason the reference to ngModel in stop() is wrong
57
- ui . item . sortable . resort = ngModel ;
58
- } ;
46
+ ngModel . $render = function ( ) {
47
+ element . sortable ( 'refresh' ) ;
48
+ } ;
59
49
60
- callbacks . receive = function ( e , ui ) {
61
- ui . item . sortable . relocate = true ;
62
- // added item to array into correct position and set up flag
63
- ngModel . $modelValue . splice ( ui . item . index ( ) , 0 , ui . item . sortable . moved ) ;
50
+ callbacks . start = function ( e , ui ) {
51
+ // Save position of dragged item
52
+ ui . item . sortable = {
53
+ index : ui . item . index ( )
64
54
} ;
55
+ } ;
65
56
66
- callbacks . remove = function ( e , ui ) {
67
- // copy data into item
68
- if ( ngModel . $modelValue . length === 1 ) {
69
- ui . item . sortable . moved = ngModel . $modelValue . splice ( 0 , 1 ) [ 0 ] ;
70
- } else {
71
- ui . item . sortable . moved = ngModel . $modelValue . splice ( ui . item . sortable . index , 1 ) [ 0 ] ;
72
- }
73
- } ;
57
+ callbacks . update = function ( e , ui ) {
58
+ // For some reason the reference to ngModel in stop() is wrong
59
+ ui . item . sortable . resort = ngModel ;
60
+ } ;
74
61
75
- callbacks . stop = function ( e , ui ) {
76
- // digest all prepared changes
77
- if ( ui . item . sortable . resort && ! ui . item . sortable . relocate ) {
62
+ callbacks . receive = function ( e , ui ) {
63
+ ui . item . sortable . relocate = true ;
64
+ // added item to array into correct position and set up flag
65
+ ngModel . $modelValue . splice ( ui . item . index ( ) , 0 , ui . item . sortable . moved ) ;
66
+ } ;
78
67
79
- // Fetch saved and current position of dropped element
80
- var end , start ;
81
- start = ui . item . sortable . index ;
82
- end = ui . item . index ( ) ;
68
+ callbacks . remove = function ( e , ui ) {
69
+ // copy data into item
70
+ if ( ngModel . $modelValue . length === 1 ) {
71
+ ui . item . sortable . moved = ngModel . $modelValue . splice ( 0 , 1 ) [ 0 ] ;
72
+ } else {
73
+ ui . item . sortable . moved = ngModel . $modelValue . splice ( ui . item . sortable . index , 1 ) [ 0 ] ;
74
+ }
75
+ } ;
83
76
84
- // Reorder array and apply change to scope
85
- ui . item . sortable . resort . $modelValue . splice ( end , 0 , ui . item . sortable . resort . $modelValue . splice ( start , 1 ) [ 0 ] ) ;
77
+ callbacks . stop = function ( e , ui ) {
78
+ // digest all prepared changes
79
+ if ( ui . item . sortable . resort && ! ui . item . sortable . relocate ) {
86
80
87
- }
88
- } ;
81
+ // Fetch saved and current position of dropped element
82
+ var end , start ;
83
+ start = ui . item . sortable . index ;
84
+ end = ui . item . index ( ) ;
89
85
90
- scope . $watch ( attrs . uiSortable , function ( newVal ) {
91
- angular . forEach ( newVal , function ( value , key ) {
86
+ // Reorder array and apply change to scope
87
+ ui . item . sortable . resort . $modelValue . splice ( end , 0 , ui . item . sortable . resort . $modelValue . splice ( start , 1 ) [ 0 ] ) ;
92
88
93
- if ( callbacks [ key ] ) {
94
- // wrap the callback
95
- value = combineCallbacks ( callbacks [ key ] , value ) ;
89
+ }
90
+ } ;
96
91
97
- if ( key === 'stop' ) {
98
- // call apply after stop
99
- value = combineCallbacks ( value , apply ) ;
100
- }
101
- }
92
+ scope . $watch ( attrs . uiSortable , function ( newVal ) {
93
+ angular . forEach ( newVal , function ( value , key ) {
102
94
103
- element . sortable ( 'option' , key , value ) ;
104
- } ) ;
105
- } , true ) ;
95
+ if ( callbacks [ key ] ) {
96
+ // wrap the callback
97
+ value = combineCallbacks ( callbacks [ key ] , value ) ;
106
98
107
- angular . forEach ( callbacks , function ( value , key ) {
99
+ if ( key === 'stop' ) {
100
+ // call apply after stop
101
+ value = combineCallbacks ( value , apply ) ;
102
+ }
103
+ }
108
104
109
- opts [ key ] = combineCallbacks ( value , opts [ key ] ) ;
105
+ element . sortable ( 'option' , key , value ) ;
110
106
} ) ;
107
+ } , true ) ;
111
108
112
- // call apply after stop
113
- opts . stop = combineCallbacks ( opts . stop , apply ) ;
109
+ angular . forEach ( callbacks , function ( value , key ) {
114
110
115
- } else {
116
- log . info ( 'ui.sortable: ngModel not provided!' , element ) ;
117
- }
111
+ opts [ key ] = combineCallbacks ( value , opts [ key ] ) ;
112
+ } ) ;
113
+
114
+ // call apply after stop
115
+ opts . stop = combineCallbacks ( opts . stop , apply ) ;
118
116
119
- // Create sortable
120
- element . sortable ( opts ) ;
117
+ } else {
118
+ log . info ( 'ui.sortable: ngModel not provided!' , element ) ;
121
119
}
122
- } ;
123
- }
124
- ] ) ;
120
+
121
+ // Create sortable
122
+ element . sortable ( opts ) ;
123
+ }
124
+ } ;
125
+ }
126
+ ] ) ;
0 commit comments