Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/angular-dragdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,26 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
callback = objExtract.callback,
constructor = objExtract.constructor,
args = [event, ui].concat(objExtract.args);

// call either $scoped method i.e. $scope.dropCallback or constructor's method i.e. this.dropCallback.
// Removing scope.$apply call that was performance intensive (especially onDrag) and does not require it
// always. So call it within the callback if needed.
return (scope[callback] || scope[constructor][callback]).apply(scope, args);

return (scope[constructor] && scope[constructor][callback]
? scope[constructor][callback]
: scope[callback]
).apply((scope[constructor] || scope), args);

function extract(callbackName) {
var atStartBracket = callbackName.indexOf('(') !== -1 ? callbackName.indexOf('(') : callbackName.length,
atEndBracket = callbackName.lastIndexOf(')') !== -1 ? callbackName.lastIndexOf(')') : callbackName.length,
args = callbackName.substring(atStartBracket + 1, atEndBracket), // matching function arguments inside brackets
constructor = callbackName.indexOf('.') !== -1 ? callbackName.substr(0, callbackName.indexOf('.')) : null; // matching a string upto a dot to check ctrl as syntax
constructor = scope[constructor] && typeof scope[constructor].constructor === 'function' ? constructor : null;
//constructor = scope[constructor] && typeof scope[constructor].constructor === 'function' ? constructor : null;

return {
callback: callbackName.substring(constructor && constructor.length + 1 || 0, atStartBracket),
args: $.map(args && args.split(',') || [], function(item) { return [$parse(item)(scope)]; }),
constructor: constructor
constructor: scope[constructor] && typeof scope[constructor].constructor === 'function' ? constructor : null
}
}
};
Expand Down Expand Up @@ -194,7 +197,7 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
toPos.top+= $toEl.outerHeight(true);
}
} else {
// Angular v1.2 uses ng-hide to hide an element
// Angular v1.2 uses ng-hide to hide an element
// so we've to remove it in order to grab its position
if (hadNgHideCls) $toEl.removeClass('ng-hide');
toPos = $toEl.css({'visibility': 'hidden', 'display': 'block'})[dropSettings.containment || 'offset']();
Expand Down Expand Up @@ -399,7 +402,7 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti

killWatcher = scope.$watch(function() { return scope.$eval(attrs.drop); }, updateDroppable);
updateDroppable();

element.on('$destroy', function() {
element.droppable({disabled: true}).droppable('destroy');
});
Expand Down
39 changes: 38 additions & 1 deletion test/spec/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('Service: ngDragDropService', function() {
scope.filterIt = function() {
return orderByFilter(scope.list, 'title');
};

expect(ngDragDropService.fixIndex(scope, {index: 1, applyFilter: 'filterIt'}, scope.list)).toBe(0);
expect(ngDragDropService.fixIndex(scope, {index: 0, applyFilter: 'filterIt'}, scope.list)).toBe(1);

Expand Down Expand Up @@ -200,4 +200,41 @@ describe('Service: ngDragDropService', function() {
timeout.flush();
expect(scope.list.map(function(item) { return item.title; }).join('')).toBe('NNLIIE');
});

describe("controller as support", function() {
var Controller;
beforeEach(function() {
Controller = (function(){
var ctrl = function(){
this.message = "ctrl";
};
ctrl.prototype.onDrop = function() {
return this.message;
};
return ctrl;
})();
scope.vm = new Controller();
scope.message = "scope";
scope.onDrop = function() {
return this.message;
}
});

it('should use controller method when prefixed with constructor', function() {
var callbackResult = ngDragDropService.callEventCallback(scope, "vm.onDrop");
expect(callbackResult).toEqual("ctrl");
});

it('should use scope method when not prefixed with constructor', function() {
var callbackResult = ngDragDropService.callEventCallback(scope, "onDrop");
expect(callbackResult).toEqual("scope");
});

it('should use scope method when prefix does not exist on scope', function() {
var callbackResult = ngDragDropService.callEventCallback(scope, "this.onDrop");
expect(callbackResult).toEqual("scope");
callbackResult = ngDragDropService.callEventCallback(scope, "self.onDrop");
expect(callbackResult).toEqual("scope");
})
});
});