From 776586ae25fcd8a1f9c781f995ec63465f496dad Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 12 May 2016 20:23:33 -0700 Subject: [PATCH] [New Edit Mode] #634 Removed edit concerns from MctRepresentation --- .../regions/src/InspectorController.js | 8 +++- .../regions/test/InspectorControllerSpec.js | 39 +++++++++++++++++-- .../representation/src/MCTRepresentation.js | 29 ++------------ .../test/MCTRepresentationSpec.js | 15 ------- 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/platform/commonUI/regions/src/InspectorController.js b/platform/commonUI/regions/src/InspectorController.js index 32b0e1903d..89b88d4b15 100644 --- a/platform/commonUI/regions/src/InspectorController.js +++ b/platform/commonUI/regions/src/InspectorController.js @@ -32,7 +32,8 @@ define( */ function InspectorController($scope, policyService) { var domainObject = $scope.domainObject, - typeCapability = domainObject.getCapability('type'); + typeCapability = domainObject.getCapability('type'), + statusListener; /** * Filters region parts to only those allowed by region policies @@ -50,6 +51,11 @@ define( $scope.regions = filterRegions(typeCapability.getDefinition().inspector || new InspectorRegion()); } + statusListener = domainObject.getCapability("status").listen(setRegions); + $scope.$on("$destroy", function () { + statusListener(); + }); + setRegions(); } diff --git a/platform/commonUI/regions/test/InspectorControllerSpec.js b/platform/commonUI/regions/test/InspectorControllerSpec.js index ad3243c348..9d815e2851 100644 --- a/platform/commonUI/regions/test/InspectorControllerSpec.js +++ b/platform/commonUI/regions/test/InspectorControllerSpec.js @@ -30,6 +30,8 @@ define( mockTypeCapability, mockTypeDefinition, mockPolicyService, + mockStatusCapability, + capabilities = {}, controller; beforeEach(function(){ @@ -47,19 +49,29 @@ define( 'getDefinition' ]); mockTypeCapability.getDefinition.andReturn(mockTypeDefinition); + capabilities.type = mockTypeCapability; + + mockStatusCapability = jasmine.createSpyObj('statusCapability', [ + 'listen' + ]); + capabilities.status = mockStatusCapability; mockDomainObject = jasmine.createSpyObj('domainObject', [ 'getCapability' ]); - mockDomainObject.getCapability.andReturn(mockTypeCapability); + mockDomainObject.getCapability.andCallFake(function (name) { + return capabilities[name]; + }); mockPolicyService = jasmine.createSpyObj('policyService', [ 'allow' ]); - mockScope = { - domainObject: mockDomainObject - }; + mockScope = jasmine.createSpyObj('$scope', + ['$on'] + ); + + mockScope.domainObject = mockDomainObject; }); it("filters out regions disallowed by region policy", function() { @@ -73,6 +85,25 @@ define( controller = new InspectorController(mockScope, mockPolicyService); expect(mockScope.regions.length).toBe(2); }); + + it("Responds to status changes", function() { + mockPolicyService.allow.andReturn(true); + controller = new InspectorController(mockScope, mockPolicyService); + expect(mockScope.regions.length).toBe(2); + expect(mockStatusCapability.listen).toHaveBeenCalled(); + mockPolicyService.allow.andReturn(false); + mockStatusCapability.listen.mostRecentCall.args[0](); + expect(mockScope.regions.length).toBe(0); + }); + + it("Unregisters status listener", function() { + var mockListener = jasmine.createSpy('listener'); + mockStatusCapability.listen.andReturn(mockListener); + controller = new InspectorController(mockScope, mockPolicyService); + expect(mockScope.$on).toHaveBeenCalledWith("$destroy", jasmine.any(Function)); + mockScope.$on.mostRecentCall.args[1](); + expect(mockListener).toHaveBeenCalled(); + }); }); } ); diff --git a/platform/representation/src/MCTRepresentation.js b/platform/representation/src/MCTRepresentation.js index bcaa90e4e7..331139b793 100644 --- a/platform/representation/src/MCTRepresentation.js +++ b/platform/representation/src/MCTRepresentation.js @@ -88,7 +88,6 @@ define( toClear = [], // Properties to clear out of scope on change counter = 0, couldRepresent = false, - couldEdit = false, lastIdPath = [], lastKey, statusListener, @@ -137,14 +136,13 @@ define( }); } - function unchanged(canRepresent, canEdit, idPath, key) { + function unchanged(canRepresent, idPath, key) { return (canRepresent === couldRepresent) && (key === lastKey) && (idPath.length === lastIdPath.length) && idPath.every(function (id, i) { return id === lastIdPath[i]; - }) && - (canEdit === couldEdit); + }); } function getIdPath(domainObject) { @@ -168,11 +166,10 @@ define( representation = lookup($scope.key, domainObject), uses = ((representation || {}).uses || []), canRepresent = !!(representation && domainObject), - canEdit = !!(domainObject && domainObject.hasCapability('editor') && domainObject.getCapability('editor').inEditContext()), idPath = getIdPath(domainObject), key = $scope.key; - if (unchanged(canRepresent, canEdit, idPath, key)) { + if (unchanged(canRepresent, idPath, key)) { return; } @@ -201,7 +198,6 @@ define( // To allow simplified change detection next time around couldRepresent = canRepresent; lastIdPath = idPath; - couldEdit = canEdit; lastKey = key; // Populate scope with fields associated with the current @@ -240,25 +236,6 @@ define( // (to a different object) $scope.$watch("domainObject", refresh); - function listenForStatusChange(object) { - if (statusListener) { - statusListener(); - } - statusListener = object.getCapability("status").listen(refresh); - } - - /** - * Add a listener to the object for status changes. - */ - $scope.$watch("domainObject", function (domainObject, oldDomainObject) { - if (domainObject !== oldDomainObject){ - listenForStatusChange(domainObject); - } - }); - if ($scope.domainObject) { - listenForStatusChange($scope.domainObject); - } - // Finally, also update when there is a new version of that // same domain object; these changes should be tracked in the // model's "modified" field, by the mutation capability. diff --git a/platform/representation/test/MCTRepresentationSpec.js b/platform/representation/test/MCTRepresentationSpec.js index 52d6c70d55..b5e78fec2a 100644 --- a/platform/representation/test/MCTRepresentationSpec.js +++ b/platform/representation/test/MCTRepresentationSpec.js @@ -253,21 +253,6 @@ define( expect(mockScope.testCapability).toBeUndefined(); }); - it("registers a status change listener", function () { - mockScope.$watch.calls[2].args[1](mockDomainObject); - expect(mockStatusCapability.listen).toHaveBeenCalled(); - }); - - it("unlistens for status change on scope destruction", function () { - var mockUnlistener = jasmine.createSpy("unlisten"); - mockStatusCapability.listen.andReturn(mockUnlistener); - mockScope.$watch.calls[2].args[1](mockDomainObject); - expect(mockStatusCapability.listen).toHaveBeenCalled(); - - mockScope.$on.calls[1].args[1](); - expect(mockUnlistener).toHaveBeenCalled(); - }); - describe("when a domain object has been observed", function () { var mockContext, mockContext2,