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