|
25 | 25 |
|
26 | 26 |
|
27 | 27 | angular.module('ng-sortable', []) |
28 | | - .constant('ngSortableVersion', '0.3.7') |
29 | | - .constant('ngSortableConfig', {}) |
30 | | - .directive('ngSortable', ['$parse', 'ngSortableConfig', function ($parse, ngSortableConfig) { |
| 28 | + .constant('version', '0.3.7') |
| 29 | + .directive('ngSortable', ['$parse', function ($parse) { |
31 | 30 | var removed, |
32 | 31 | nextSibling; |
33 | 32 |
|
34 | | - |
35 | | - |
36 | 33 | // Export |
37 | 34 | return { |
38 | 35 | restrict: 'AC', |
39 | | - scope: { ngSortable: "=?" }, |
40 | 36 | link: function (scope, $el, attrs) { |
41 | 37 |
|
42 | 38 | function getSource(el) { |
| 39 | + // var scope = angular.element(el).scope(); |
43 | 40 | var ngRepeat = [].filter.call(el.childNodes, function (node) { |
44 | 41 | return ( |
45 | 42 | (node.nodeType === 8) && |
46 | 43 | (node.nodeValue.indexOf('ngRepeat:') !== -1) |
47 | 44 | ); |
48 | 45 | })[0]; |
49 | | - |
| 46 | + |
50 | 47 | if (!ngRepeat) { |
51 | 48 | // Without ng-repeat |
52 | 49 | return null; |
53 | 50 | } |
54 | | - |
| 51 | + |
55 | 52 | // tests: http://jsbin.com/kosubutilo/1/edit?js,output |
56 | 53 | ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*(?:\(.*?,\s*)?([^\s)]+)[\s)]+in\s+([^\s|]+)/); |
57 | | - |
| 54 | + |
58 | 55 | var itemExpr = $parse(ngRepeat[1]); |
59 | 56 | var itemsExpr = $parse(ngRepeat[2]); |
60 | | - |
| 57 | + |
61 | 58 | return { |
62 | | - item: function (el) { |
63 | | - return itemExpr(angular.element(el).scope()); |
64 | | - }, |
65 | 59 | items: function () { |
66 | 60 | return itemsExpr(scope); |
67 | 61 | } |
68 | 62 | }; |
69 | 63 | } |
70 | 64 |
|
71 | 65 | var el = $el[0], |
72 | | - options = angular.extend(scope.ngSortable || {}, ngSortableConfig), |
| 66 | + ngSortable = attrs.ngSortable, |
| 67 | + options = scope.$eval(ngSortable) || {}, |
73 | 68 | source = getSource(el), |
74 | | - watchers = [], |
75 | 69 | sortable |
76 | 70 | ; |
77 | 71 |
|
78 | 72 |
|
79 | 73 | function _emitEvent(/**Event*/evt, /*Mixed*/item) { |
80 | 74 | var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1); |
81 | 75 |
|
82 | | - /* jshint expr:true */ |
| 76 | + /* jshint expr:true */ |
83 | 77 | options[name] && options[name]({ |
84 | | - model: item || source && source.item(evt.item), |
| 78 | + model: item || source && source.items()[evt.newIndex], |
85 | 79 | models: source && source.items(), |
86 | 80 | oldIndex: evt.oldIndex, |
87 | 81 | newIndex: evt.newIndex |
|
157 | 151 | })); |
158 | 152 |
|
159 | 153 | $el.on('$destroy', function () { |
160 | | - angular.forEach(watchers, function (/** Function */unwatch) { |
161 | | - unwatch(); |
162 | | - }); |
163 | 154 | sortable.destroy(); |
164 | | - watchers = null; |
165 | 155 | sortable = null; |
166 | 156 | nextSibling = null; |
167 | 157 | }); |
168 | 158 |
|
169 | | - angular.forEach([ |
170 | | - 'sort', 'disabled', 'draggable', 'handle', 'animation', |
171 | | - 'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort' |
172 | | - ], function (name) { |
173 | | - watchers.push(scope.$watch('ngSortable.' + name, function (value) { |
174 | | - if (value !== void 0) { |
175 | | - options[name] = value; |
176 | | - |
177 | | - if (!/^on[A-Z]/.test(name)) { |
178 | | - sortable.option(name, value); |
| 159 | + if (ngSortable && !/{|}/.test(ngSortable)) { // todo: ugly |
| 160 | + angular.forEach([ |
| 161 | + 'sort', 'disabled', 'draggable', 'handle', 'animation', |
| 162 | + 'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort' |
| 163 | + ], function (name) { |
| 164 | + scope.$watch(ngSortable + '.' + name, function (value) { |
| 165 | + if (value !== void 0) { |
| 166 | + options[name] = value; |
| 167 | + |
| 168 | + if (!/^on[A-Z]/.test(name)) { |
| 169 | + sortable.option(name, value); |
| 170 | + } |
179 | 171 | } |
180 | | - } |
181 | | - })); |
182 | | - }); |
| 172 | + }); |
| 173 | + }); |
| 174 | + } |
183 | 175 | } |
184 | 176 | }; |
185 | 177 | }]); |
|
0 commit comments