Skip to content

Commit e626cf3

Browse files
committed
Update ng-sortable.js
1 parent 5b4737f commit e626cf3

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

ng-sortable.js

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,63 +25,57 @@
2525

2626

2727
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) {
3130
var removed,
3231
nextSibling;
3332

34-
35-
3633
// Export
3734
return {
3835
restrict: 'AC',
39-
scope: { ngSortable: "=?" },
4036
link: function (scope, $el, attrs) {
4137

4238
function getSource(el) {
39+
// var scope = angular.element(el).scope();
4340
var ngRepeat = [].filter.call(el.childNodes, function (node) {
4441
return (
4542
(node.nodeType === 8) &&
4643
(node.nodeValue.indexOf('ngRepeat:') !== -1)
4744
);
4845
})[0];
49-
46+
5047
if (!ngRepeat) {
5148
// Without ng-repeat
5249
return null;
5350
}
54-
51+
5552
// tests: http://jsbin.com/kosubutilo/1/edit?js,output
5653
ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*(?:\(.*?,\s*)?([^\s)]+)[\s)]+in\s+([^\s|]+)/);
57-
54+
5855
var itemExpr = $parse(ngRepeat[1]);
5956
var itemsExpr = $parse(ngRepeat[2]);
60-
57+
6158
return {
62-
item: function (el) {
63-
return itemExpr(angular.element(el).scope());
64-
},
6559
items: function () {
6660
return itemsExpr(scope);
6761
}
6862
};
6963
}
7064

7165
var el = $el[0],
72-
options = angular.extend(scope.ngSortable || {}, ngSortableConfig),
66+
ngSortable = attrs.ngSortable,
67+
options = scope.$eval(ngSortable) || {},
7368
source = getSource(el),
74-
watchers = [],
7569
sortable
7670
;
7771

7872

7973
function _emitEvent(/**Event*/evt, /*Mixed*/item) {
8074
var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1);
8175

82-
/* jshint expr:true */
76+
/* jshint expr:true */
8377
options[name] && options[name]({
84-
model: item || source && source.item(evt.item),
78+
model: item || source && source.items()[evt.newIndex],
8579
models: source && source.items(),
8680
oldIndex: evt.oldIndex,
8781
newIndex: evt.newIndex
@@ -157,29 +151,27 @@
157151
}));
158152

159153
$el.on('$destroy', function () {
160-
angular.forEach(watchers, function (/** Function */unwatch) {
161-
unwatch();
162-
});
163154
sortable.destroy();
164-
watchers = null;
165155
sortable = null;
166156
nextSibling = null;
167157
});
168158

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+
}
179171
}
180-
}
181-
}));
182-
});
172+
});
173+
});
174+
}
183175
}
184176
};
185177
}]);

0 commit comments

Comments
 (0)