[Inspector] Listen for mutation and refresh composition

...so that elements pool is updated when selected object's composition changes. Fixes #1869
This commit is contained in:
Pegah Sarram 2018-01-19 00:17:27 -08:00 committed by Victor Woeltjen
parent 0363d0e8ad
commit d4e3e6689c
2 changed files with 69 additions and 10 deletions

View File

@ -52,8 +52,20 @@ define(
} }
function setSelection(selection) { function setSelection(selection) {
self.scope.selection = selection; if (!selection[0]) {
self.refreshComposition(selection); return;
}
if (self.mutationListener) {
self.mutationListener();
delete self.mutationListener;
}
var domainObject = selection[0].context.oldItem;
self.refreshComposition(domainObject);
self.mutationListener = domainObject.getCapability('mutation')
.listen(self.refreshComposition.bind(self, domainObject));
} }
$scope.filterBy = filterBy; $scope.filterBy = filterBy;
@ -70,16 +82,11 @@ define(
/** /**
* Gets the composition for the selected object and populates the scope with it. * Gets the composition for the selected object and populates the scope with it.
* *
* @param selection the selection object * @param domainObject the selected object
* @private * @private
*/ */
ElementsController.prototype.refreshComposition = function (selection) { ElementsController.prototype.refreshComposition = function (domainObject) {
if (!selection[0]) { var selectedObjectComposition = domainObject.useCapability('composition');
return;
}
var selected = selection[0].context.oldItem;
var selectedObjectComposition = selected && selected.useCapability('composition');
if (selectedObjectComposition) { if (selectedObjectComposition) {
selectedObjectComposition.then(function (composition) { selectedObjectComposition.then(function (composition) {

View File

@ -29,9 +29,25 @@ define(
var mockScope, var mockScope,
mockOpenMCT, mockOpenMCT,
mockSelection, mockSelection,
mockDomainObject,
mockMutationCapability,
mockUnlisten,
selectable = [],
controller; controller;
beforeEach(function () { beforeEach(function () {
mockUnlisten = jasmine.createSpy('unlisten');
mockMutationCapability = jasmine.createSpyObj("mutationCapability", [
"listen"
]);
mockMutationCapability.listen.andReturn(mockUnlisten);
mockDomainObject = jasmine.createSpyObj("domainObject", [
"getCapability",
"useCapability"
]);
mockDomainObject.useCapability.andCallThrough();
mockDomainObject.getCapability.andReturn(mockMutationCapability);
mockScope = jasmine.createSpyObj("$scope", ['$on']); mockScope = jasmine.createSpyObj("$scope", ['$on']);
mockSelection = jasmine.createSpyObj("selection", [ mockSelection = jasmine.createSpyObj("selection", [
'on', 'on',
@ -43,6 +59,14 @@ define(
selection: mockSelection selection: mockSelection
}; };
selectable[0] = {
context: {
oldItem: mockDomainObject
}
};
spyOn(ElementsController.prototype, 'refreshComposition');
controller = new ElementsController(mockScope, mockOpenMCT); controller = new ElementsController(mockScope, mockOpenMCT);
}); });
@ -75,6 +99,34 @@ define(
expect(objects.filter(mockScope.searchElements).length).toBe(4); expect(objects.filter(mockScope.searchElements).length).toBe(4);
}); });
it("refreshes composition on selection", function () {
mockOpenMCT.selection.on.mostRecentCall.args[1](selectable);
expect(ElementsController.prototype.refreshComposition).toHaveBeenCalledWith(mockDomainObject);
});
it("listens on mutation and refreshes composition", function () {
mockOpenMCT.selection.on.mostRecentCall.args[1](selectable);
expect(mockDomainObject.getCapability).toHaveBeenCalledWith('mutation');
expect(mockMutationCapability.listen).toHaveBeenCalled();
expect(ElementsController.prototype.refreshComposition.calls.length).toBe(1);
mockMutationCapability.listen.mostRecentCall.args[0](mockDomainObject);
expect(ElementsController.prototype.refreshComposition.calls.length).toBe(2);
});
it("cleans up mutation listener when selection changes", function () {
mockOpenMCT.selection.on.mostRecentCall.args[1](selectable);
expect(mockMutationCapability.listen).toHaveBeenCalled();
mockOpenMCT.selection.on.mostRecentCall.args[1](selectable);
expect(mockUnlisten).toHaveBeenCalled();
});
}); });
} }
); );