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