1
1
'use strict' ;
2
+ /*
3
+ jQuery UI Sortable plugin wrapper
4
+
5
+ @param [ui-sortable] {object} Options to pass to $.fn.sortable() merged onto ui.config
6
+ */
2
7
angular . module ( 'ui.sortable' , [ ] ) . value ( 'uiSortableConfig' , { } ) . directive ( 'uiSortable' , [
3
8
'uiSortableConfig' ,
4
9
'$log' ,
@@ -34,37 +39,47 @@ angular.module('ui.sortable', []).value('uiSortableConfig', {}).directive('uiSor
34
39
element . sortable ( 'refresh' ) ;
35
40
} ;
36
41
callbacks . start = function ( e , ui ) {
42
+ // Save position of dragged item
37
43
ui . item . sortable = { index : ui . item . index ( ) } ;
38
44
} ;
39
45
callbacks . update = function ( e , ui ) {
46
+ // For some reason the reference to ngModel in stop() is wrong
40
47
ui . item . sortable . resort = ngModel ;
41
48
} ;
42
49
callbacks . receive = function ( e , ui ) {
43
50
ui . item . sortable . relocate = true ;
51
+ // if the item still exists (it has not been cancelled)
44
52
if ( 'moved' in ui . item . sortable ) {
53
+ // added item to array into correct position and set up flag
45
54
ngModel . $modelValue . splice ( ui . item . index ( ) , 0 , ui . item . sortable . moved ) ;
46
55
}
47
56
} ;
48
57
callbacks . remove = function ( e , ui ) {
58
+ // copy data into item
49
59
if ( ngModel . $modelValue . length === 1 ) {
50
60
ui . item . sortable . moved = ngModel . $modelValue . splice ( 0 , 1 ) [ 0 ] ;
51
61
} else {
52
62
ui . item . sortable . moved = ngModel . $modelValue . splice ( ui . item . sortable . index , 1 ) [ 0 ] ;
53
63
}
54
64
} ;
55
65
callbacks . stop = function ( e , ui ) {
66
+ // digest all prepared changes
56
67
if ( ui . item . sortable . resort && ! ui . item . sortable . relocate ) {
68
+ // Fetch saved and current position of dropped element
57
69
var end , start ;
58
70
start = ui . item . sortable . index ;
59
71
end = ui . item . index ( ) ;
72
+ // Reorder array and apply change to scope
60
73
ui . item . sortable . resort . $modelValue . splice ( end , 0 , ui . item . sortable . resort . $modelValue . splice ( start , 1 ) [ 0 ] ) ;
61
74
}
62
75
} ;
63
76
scope . $watch ( attrs . uiSortable , function ( newVal ) {
64
77
angular . forEach ( newVal , function ( value , key ) {
65
78
if ( callbacks [ key ] ) {
79
+ // wrap the callback
66
80
value = combineCallbacks ( callbacks [ key ] , value ) ;
67
81
if ( key === 'stop' ) {
82
+ // call apply after stop
68
83
value = combineCallbacks ( value , apply ) ;
69
84
}
70
85
}
@@ -74,10 +89,12 @@ angular.module('ui.sortable', []).value('uiSortableConfig', {}).directive('uiSor
74
89
angular . forEach ( callbacks , function ( value , key ) {
75
90
opts [ key ] = combineCallbacks ( value , opts [ key ] ) ;
76
91
} ) ;
92
+ // call apply after stop
77
93
opts . stop = combineCallbacks ( opts . stop , apply ) ;
78
94
} else {
79
95
log . info ( 'ui.sortable: ngModel not provided!' , element ) ;
80
96
}
97
+ // Create sortable
81
98
element . sortable ( opts ) ;
82
99
}
83
100
} ;
0 commit comments