[Timeline] Invoke $apply from mct-swimlane-drop

...and add some checks to ensure this is necessary, to avoid
triggering a ton of digest cycles while dragging.
This commit is contained in:
Victor Woeltjen 2016-03-21 17:28:42 -07:00
parent ad4c456ca2
commit dd053f7e6e
2 changed files with 27 additions and 6 deletions

View File

@ -97,21 +97,37 @@ define(
function link(scope, element, attrs) {
// Lookup swimlane by evaluating this attribute
function swimlane() {
function lookupSwimlane() {
return scope.$eval(attrs.mctSwimlaneDrop);
}
// Handle dragover
element.on('dragover', function (e) {
dragOver(e, element, swimlane());
var swimlane = lookupSwimlane(),
highlight = swimlane.highlight(),
highlightBottom = swimlane.highlightBottom();
dragOver(e, element, swimlane);
if ((highlightBottom !== swimlane.highlightBottom()) ||
highlight !== swimlane.highlight()) {
scope.$apply();
}
});
// Handle drops
element.on('drop', function (e) {
drop(e, element, swimlane());
drop(e, element, lookupSwimlane());
scope.$apply();
});
// Clear highlights when drag leaves this swimlane
element.on('dragleave', function () {
swimlane().highlight(false);
swimlane().highlightBottom(false);
var swimlane = lookupSwimlane(),
wasHighlighted = swimlane.highlight() ||
swimlane.highlightBottom();
swimlane.highlight(false);
swimlane.highlightBottom(false);
if (wasHighlighted) {
scope.$apply();
}
});
}

View File

@ -55,7 +55,7 @@ define(
'dndService',
['setData', 'getData', 'removeData']
);
mockScope = jasmine.createSpyObj('$scope', ['$eval']);
mockScope = jasmine.createSpyObj('$scope', ['$eval', '$apply']);
mockElement = jasmine.createSpyObj('element', ['on']);
testAttrs = { mctSwimlaneDrop: "mockSwimlane" };
mockSwimlane = jasmine.createSpyObj(
@ -118,6 +118,7 @@ define(
expect(mockSwimlane.highlight).toHaveBeenCalledWith(true);
expect(mockSwimlane.highlightBottom).toHaveBeenCalledWith(false);
expect(mockScope.$apply).toHaveBeenCalled();
});
it("updates bottom highlights on drag over", function () {
@ -128,6 +129,7 @@ define(
expect(mockSwimlane.highlight).toHaveBeenCalledWith(false);
expect(mockSwimlane.highlightBottom).toHaveBeenCalledWith(true);
expect(mockScope.$apply).toHaveBeenCalled();
});
it("respects swimlane's allowDropIn response", function () {
@ -157,12 +159,15 @@ define(
it("notifies swimlane on drop", function () {
handlers.drop(testEvent);
expect(mockSwimlane.drop).toHaveBeenCalledWith('abc', 'someDomainObject');
expect(mockScope.$apply).toHaveBeenCalled();
});
it("clears highlights when drag leaves", function () {
mockSwimlane.highlight.andReturn(true);
handlers.dragleave();
expect(mockSwimlane.highlight).toHaveBeenCalledWith(false);
expect(mockSwimlane.highlightBottom).toHaveBeenCalledWith(false);
expect(mockScope.$apply).toHaveBeenCalled();
});
});
}