Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit ef5862c

Browse files
committed
feat(sortable): change attr callback implementation w/ meta-programming
1 parent b6d8bda commit ef5862c

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

src/sortable.js

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ angular.module('ui.sortable', [])
1818
scope: {
1919
ngModel:'=',
2020
uiSortable:'=',
21-
receive:'&uiSortableReceive',//Expression bindings from html.
22-
remove:'&uiSortableRemove',
21+
////Expression bindings from html.
22+
create:'&uiSortableCreate',
23+
// helper:'&uiSortableHelper',
2324
start:'&uiSortableStart',
24-
stop:'&uiSortableStop',
25+
activate:'&uiSortableActivate',
26+
// sort:'&uiSortableSort',
27+
// change:'&uiSortableChange',
28+
// over:'&uiSortableOver',
29+
// out:'&uiSortableOut',
30+
beforeStop:'&uiSortableBeforeStop',
2531
update:'&uiSortableUpdate',
26-
received :'&uiSortableReceived'
32+
remove:'&uiSortableRemove',
33+
receive:'&uiSortableReceive',
34+
deactivate:'&uiSortableDeactivate',
35+
stop:'&uiSortableStop'
2736
},
2837
link: function(scope, element, attrs, ngModel) {
2938
var savedNodes;
@@ -131,41 +140,9 @@ angular.module('ui.sortable', [])
131140
opts[key] = patchSortableOption(key, value);
132141
return;
133142
}
134-
135-
136-
/*
137-
* If user defines ui-sortable-callback for @key and callback based option at the same time the html based ui-sortable-callback expression will be selected. (Overridden)
138-
* If user defined ui-sortable-callback for @key but callback expression is empty then option based @key callback will be selected.
139-
* If user not defines a option for @key callback then html based ui-sortable-callback will be just selected.
140-
* If user just defines option for @key callback then it will be attached.
141-
* */
142-
var attrKey = 'uiSortable'+key.substring(0,1).toUpperCase()+key.substring(1);
143-
if(scope[key]!==undefined && scope[key] instanceof Function && attrs[attrKey] !== undefined && attrs[attrKey].length > 0) {
144-
145-
var expression = scope[key]; //Scope variable can be changed on fly.
146-
var receivedFunct = scope.received;
147-
var expressionCapsule = function() {
148-
try {
149-
150-
expression.apply(0,arguments); //Sends all of arguments to callBack function.
151-
152-
if(receivedFunct instanceof Function) {
153-
receivedFunct.apply(0,arguments);
154-
}
155-
156-
}
157-
catch(err) {
158-
159-
}
160-
161-
}
162-
163-
value = patchSortableOption(key, expressionCapsule);
164-
}
165-
else {
166-
value = patchSortableOption(key, value);
167-
}
168-
143+
144+
value = patchSortableOption(key, value);
145+
169146
if (!optsDiff) {
170147
optsDiff = {};
171148
}
@@ -255,11 +232,19 @@ angular.module('ui.sortable', [])
255232
};
256233

257234
var callbacks = {
258-
receive: null,
259-
remove: null,
235+
create: null,
260236
start: null,
261-
stop: null,
262-
update: null
237+
activate: null,
238+
// sort: null,
239+
// change: null,
240+
// over: null,
241+
// out: null,
242+
beforeStop: null,
243+
update: null,
244+
remove: null,
245+
receive: null,
246+
deactivate: null,
247+
stop: null
263248
};
264249

265250
var wrappers = {
@@ -471,6 +456,21 @@ angular.module('ui.sortable', [])
471456
}
472457
};
473458

459+
// setup attribute handlers
460+
angular.forEach(callbacks, function(value, key) {
461+
callbacks[key] = combineCallbacks(callbacks[key],
462+
function () {
463+
var attrHandler = scope[key];
464+
var attrHandlerFn;
465+
if (typeof attrHandler === 'function' &&
466+
('uiSortable' + key.substring(0,1).toUpperCase() + key.substring(1)).length &&
467+
typeof (attrHandlerFn = attrHandler()) === 'function') {
468+
attrHandlerFn.apply(this, arguments);
469+
}
470+
});
471+
});
472+
473+
474474
wrappers.helper = function (inner) {
475475
if (inner && typeof inner === 'function') {
476476
return function (e, item) {

0 commit comments

Comments
 (0)