From d026bc2134f3342fe5955e9c09ce047b3591f194 Mon Sep 17 00:00:00 2001 From: Andrew Henry Date: Wed, 12 Dec 2018 17:17:49 -0800 Subject: [PATCH 1/2] Show edit only if view is editable. Rename editable to canEdit --- .../commonUI/edit/src/policies/EditActionPolicy.js | 7 ++++--- src/adapter/views/LegacyViewProvider.js | 4 +++- src/plugins/displayLayout/plugin.js | 3 +++ .../flexibleLayout/flexibleLayoutViewProvider.js | 3 +++ .../src/views/SummaryWidgetViewProvider.js | 4 +++- src/plugins/tabs/tabs.js | 3 +++ .../telemetryTable/TelemetryTableViewProvider.js | 12 ++++++------ src/ui/components/layout/BrowseBar.vue | 10 +++++++++- src/ui/registries/ViewRegistry.js | 12 ++++++++++++ 9 files changed, 46 insertions(+), 12 deletions(-) diff --git a/platform/commonUI/edit/src/policies/EditActionPolicy.js b/platform/commonUI/edit/src/policies/EditActionPolicy.js index 2d6dd56b37..d898609bb4 100644 --- a/platform/commonUI/edit/src/policies/EditActionPolicy.js +++ b/platform/commonUI/edit/src/policies/EditActionPolicy.js @@ -41,6 +41,7 @@ define( * @private */ EditActionPolicy.prototype.countEditableViews = function (context) { + console.trace('countEditableViews'); var domainObject = context.domainObject, count = 0, type, views; @@ -65,10 +66,10 @@ define( }); function isEditable(view) { - if (typeof view.editable === Function) { - return view.editable(domainObject.useCapability('adapter')); + if (typeof view.canEdit === Function) { + return view.canEdit(domainObject.useCapability('adapter')); } else { - return view.editable === true; + return view.canEdit === true; } } diff --git a/src/adapter/views/LegacyViewProvider.js b/src/adapter/views/LegacyViewProvider.js index 686175f820..517b9a921c 100644 --- a/src/adapter/views/LegacyViewProvider.js +++ b/src/adapter/views/LegacyViewProvider.js @@ -21,7 +21,9 @@ define([ name: legacyView.name, cssClass: legacyView.cssClass, description: legacyView.description, - editable: legacyView.editable, + canEdit: function () { + return legacyView.editable === true; + }, canView: function (domainObject) { if (!domainObject || !domainObject.identifier) { return false; diff --git a/src/plugins/displayLayout/plugin.js b/src/plugins/displayLayout/plugin.js index 397d3c0b67..a50d6bf2df 100644 --- a/src/plugins/displayLayout/plugin.js +++ b/src/plugins/displayLayout/plugin.js @@ -33,6 +33,9 @@ export default function () { canView: function (domainObject) { return domainObject.type === 'layout'; }, + canEdit: function (domainObject) { + return domainObject.type === 'layout'; + }, view: function (domainObject) { let component; return { diff --git a/src/plugins/flexibleLayout/flexibleLayoutViewProvider.js b/src/plugins/flexibleLayout/flexibleLayoutViewProvider.js index 7dd7dcfcde..f981ba54a9 100644 --- a/src/plugins/flexibleLayout/flexibleLayoutViewProvider.js +++ b/src/plugins/flexibleLayout/flexibleLayoutViewProvider.js @@ -35,6 +35,9 @@ define([ canView: function (domainObject) { return domainObject.type === 'flexible-layout'; }, + canEdit: function (domainObject) { + return domainObject.type === 'flexible-layout'; + }, view: function (domainObject) { let component; diff --git a/src/plugins/summaryWidget/src/views/SummaryWidgetViewProvider.js b/src/plugins/summaryWidget/src/views/SummaryWidgetViewProvider.js index 61a9fb470d..77a9a2f814 100644 --- a/src/plugins/summaryWidget/src/views/SummaryWidgetViewProvider.js +++ b/src/plugins/summaryWidget/src/views/SummaryWidgetViewProvider.js @@ -20,6 +20,9 @@ define([ canView: function (domainObject) { return domainObject.type === 'summary-widget'; }, + canEdit: function (domainObject) { + return domainObject.type === 'summary-widget'; + }, view: function (domainObject) { var statusService = openmct.$injector.get('statusService'); var objectId = objectUtils.makeKeyString(domainObject.identifier); @@ -32,7 +35,6 @@ define([ return new SummaryWidgetView(domainObject, openmct); } }, - editable: true, priority: function (domainObject) { if (domainObject.type === 'summary-widget') { return Number.MAX_VALUE; diff --git a/src/plugins/tabs/tabs.js b/src/plugins/tabs/tabs.js index e896e7acd6..1b40f3f848 100644 --- a/src/plugins/tabs/tabs.js +++ b/src/plugins/tabs/tabs.js @@ -35,6 +35,9 @@ define([ canView: function (domainObject) { return domainObject.type === 'tabs'; }, + canEdit: function (domainObject) { + return domainObject.type === 'tabs'; + }, view: function (domainObject) { let component; diff --git a/src/plugins/telemetryTable/TelemetryTableViewProvider.js b/src/plugins/telemetryTable/TelemetryTableViewProvider.js index 58acc86d7e..b96c1efe53 100644 --- a/src/plugins/telemetryTable/TelemetryTableViewProvider.js +++ b/src/plugins/telemetryTable/TelemetryTableViewProvider.js @@ -36,13 +36,13 @@ define([ key: 'table', name: 'Telemetry Table', cssClass: 'icon-tabular-realtime', - editable: function(domainObject) { - return domainObject.type === 'table'; - }, - canView: function (domainObject) { + canView(domainObject) { return domainObject.type === 'table' || domainObject.hasOwnProperty('telemetry'); }, - view: function (domainObject) { + canEdit(domainObject) { + return domainObject.type === 'table'; + }, + view(domainObject) { let csvExporter = new CSVExporter.default(); let table = new TelemetryTable(domainObject, openmct); let component; @@ -67,7 +67,7 @@ define([ } } }, - priority: function () { + priority() { return 1; } } diff --git a/src/ui/components/layout/BrowseBar.vue b/src/ui/components/layout/BrowseBar.vue index 8416513a42..3ddc9e8427 100644 --- a/src/ui/components/layout/BrowseBar.vue +++ b/src/ui/components/layout/BrowseBar.vue @@ -40,7 +40,7 @@
- +
@@ -119,6 +119,14 @@ return {} } return objectType.definition; + }, + isViewEditable() { + let currentViewKey = this.currentView.key; + if (currentViewKey !== undefined) { + let currentViewProvider = this.openmct.objectViews.getByProviderKey(currentViewKey); + return currentViewProvider.canEdit && currentViewProvider.canEdit(this.domainObject); + } + return false; } }, mounted: function () { diff --git a/src/ui/registries/ViewRegistry.js b/src/ui/registries/ViewRegistry.js index bebde51c7e..480bb79867 100644 --- a/src/ui/registries/ViewRegistry.js +++ b/src/ui/registries/ViewRegistry.js @@ -170,6 +170,18 @@ define([], function () { * otherwise 'false'. */ + /** + * An optional function that defines whether or not this view can be used to edit a given object. + * If not provided, will default to `false` and the view will not support editing. + * + * @method canEdit + * @memberof module:openmct.ViewProvider# + * @param {module:openmct.DomainObject} domainObject the domain object + * to be edited + * @returns {boolean} 'true' if the view can be used to edit the provided object, + * otherwise 'false'. + */ + /** * Optional method determining the priority of a given view. If this * function is not defined on a view provider, then a default priority From 3122168b0e40820697d18e5ba7f407044268109a Mon Sep 17 00:00:00 2001 From: Andrew Henry Date: Thu, 13 Dec 2018 17:11:39 -0800 Subject: [PATCH 2/2] Removed unused legacy code --- platform/commonUI/edit/bundle.js | 6 - .../edit/src/policies/EditActionPolicy.js | 112 -------------- .../edit/src/policies/EditableViewPolicy.js | 49 ------- .../test/policies/EditActionPolicySpec.js | 138 ------------------ .../test/policies/EditableViewPolicySpec.js | 79 ---------- 5 files changed, 384 deletions(-) delete mode 100644 platform/commonUI/edit/src/policies/EditActionPolicy.js delete mode 100644 platform/commonUI/edit/src/policies/EditableViewPolicy.js delete mode 100644 platform/commonUI/edit/test/policies/EditActionPolicySpec.js delete mode 100644 platform/commonUI/edit/test/policies/EditableViewPolicySpec.js diff --git a/platform/commonUI/edit/bundle.js b/platform/commonUI/edit/bundle.js index d421c4bec8..54a4809743 100644 --- a/platform/commonUI/edit/bundle.js +++ b/platform/commonUI/edit/bundle.js @@ -32,7 +32,6 @@ define([ "./src/actions/SaveAndStopEditingAction", "./src/actions/SaveAsAction", "./src/actions/CancelAction", - "./src/policies/EditActionPolicy", "./src/policies/EditPersistableObjectsPolicy", "./src/representers/EditRepresenter", "./src/capabilities/EditorCapability", @@ -64,7 +63,6 @@ define([ SaveAndStopEditingAction, SaveAsAction, CancelAction, - EditActionPolicy, EditPersistableObjectsPolicy, EditRepresenter, EditorCapability, @@ -225,10 +223,6 @@ define([ } ], "policies": [ - { - "category": "action", - "implementation": EditActionPolicy - }, { "category": "action", "implementation": EditPersistableObjectsPolicy, diff --git a/platform/commonUI/edit/src/policies/EditActionPolicy.js b/platform/commonUI/edit/src/policies/EditActionPolicy.js deleted file mode 100644 index d898609bb4..0000000000 --- a/platform/commonUI/edit/src/policies/EditActionPolicy.js +++ /dev/null @@ -1,112 +0,0 @@ -/***************************************************************************** - * Open MCT, Copyright (c) 2014-2018, United States Government - * as represented by the Administrator of the National Aeronautics and Space - * Administration. All rights reserved. - * - * Open MCT is licensed under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * Open MCT includes source code licensed under additional open source - * licenses. See the Open Source Licenses file (LICENSES.md) included with - * this source code distribution or the Licensing information page available - * at runtime from the About dialog for additional information. - *****************************************************************************/ - -define( - [], - function () { - - /** - * Policy controlling when the `edit` and/or `properties` actions - * can appear as applicable actions of the `view-control` category - * (shown as buttons in the top-right of browse mode.) - * @memberof platform/commonUI/edit - * @constructor - * @implements {Policy.} - */ - function EditActionPolicy(policyService) { - this.policyService = policyService; - } - - /** - * Get a count of views which are not flagged as non-editable. - * @private - */ - EditActionPolicy.prototype.countEditableViews = function (context) { - console.trace('countEditableViews'); - var domainObject = context.domainObject, - count = 0, - type, views; - - if (!domainObject) { - return count; - } - - type = domainObject.getCapability('type'); - views = domainObject.useCapability('view'); - - - // A view is editable unless explicitly flagged as not - (views || []).forEach(function (view) { - if (isEditable(view) || - (view.key === 'plot' && type.getKey() === 'telemetry.panel') || - (view.key === 'table' && type.getKey() === 'table') || - (view.key === 'rt-table' && type.getKey() === 'rttable') - ) { - count++; - } - }); - - function isEditable(view) { - if (typeof view.canEdit === Function) { - return view.canEdit(domainObject.useCapability('adapter')); - } else { - return view.canEdit === true; - } - } - - return count; - }; - - /** - * Checks whether the domain object is currently being edited. If - * so, the edit action is not applicable. - * @param context - * @returns {*|boolean} - */ - function isEditing(context) { - var domainObject = (context || {}).domainObject; - return domainObject && - domainObject.hasCapability('editor') && - domainObject.getCapability('editor').isEditContextRoot(); - } - - EditActionPolicy.prototype.allow = function (action, context) { - var key = action.getMetadata().key, - category = (context || {}).category; - - // Restrict 'edit' to cases where there are editable - // views (similarly, restrict 'properties' to when - // the converse is true), and where the domain object is not - // already being edited. - if (key === 'edit') { - return this.countEditableViews(context) > 0 && !isEditing(context); - } else if (key === 'properties' && category === 'view-control') { - return this.countEditableViews(context) < 1 && !isEditing(context); - } - - // Like all policies, allow by default. - return true; - }; - - return EditActionPolicy; - } -); diff --git a/platform/commonUI/edit/src/policies/EditableViewPolicy.js b/platform/commonUI/edit/src/policies/EditableViewPolicy.js deleted file mode 100644 index a3869b0211..0000000000 --- a/platform/commonUI/edit/src/policies/EditableViewPolicy.js +++ /dev/null @@ -1,49 +0,0 @@ -/***************************************************************************** - * Open MCT, Copyright (c) 2014-2018, United States Government - * as represented by the Administrator of the National Aeronautics and Space - * Administration. All rights reserved. - * - * Open MCT is licensed under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * Open MCT includes source code licensed under additional open source - * licenses. See the Open Source Licenses file (LICENSES.md) included with - * this source code distribution or the Licensing information page available - * at runtime from the About dialog for additional information. - *****************************************************************************/ - -define( - [], - function () { - - /** - * Policy controlling which views should be visible in Edit mode. - * @memberof platform/commonUI/edit - * @constructor - * @implements {Policy.} - */ - function EditableViewPolicy() { - } - - EditableViewPolicy.prototype.allow = function (view, domainObject) { - // If a view is flagged as non-editable, only allow it - // while we're not in Edit mode. - if ((view || {}).editable === false) { - return !(domainObject.hasCapability('editor') && domainObject.getCapability('editor').inEditContext()); - } - - // Like all policies, allow by default. - return true; - }; - - return EditableViewPolicy; - } -); diff --git a/platform/commonUI/edit/test/policies/EditActionPolicySpec.js b/platform/commonUI/edit/test/policies/EditActionPolicySpec.js deleted file mode 100644 index 2ac5fbf96d..0000000000 --- a/platform/commonUI/edit/test/policies/EditActionPolicySpec.js +++ /dev/null @@ -1,138 +0,0 @@ -/***************************************************************************** - * Open MCT, Copyright (c) 2014-2018, United States Government - * as represented by the Administrator of the National Aeronautics and Space - * Administration. All rights reserved. - * - * Open MCT is licensed under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * Open MCT includes source code licensed under additional open source - * licenses. See the Open Source Licenses file (LICENSES.md) included with - * this source code distribution or the Licensing information page available - * at runtime from the About dialog for additional information. - *****************************************************************************/ - -define( - ["../../src/policies/EditActionPolicy"], - function (EditActionPolicy) { - - describe("The Edit action policy", function () { - var editableView, - nonEditableView, - testViews, - testContext, - mockDomainObject, - mockEditAction, - mockPropertiesAction, - mockTypeCapability, - mockEditorCapability, - capabilities, - plotView, - policy; - - beforeEach(function () { - mockDomainObject = jasmine.createSpyObj( - 'domainObject', - [ - 'useCapability', - 'hasCapability', - 'getCapability' - ] - ); - mockEditorCapability = jasmine.createSpyObj('editorCapability', ['isEditContextRoot']); - mockTypeCapability = jasmine.createSpyObj('type', ['getKey']); - capabilities = { - 'editor': mockEditorCapability, - 'type': mockTypeCapability - }; - - mockEditAction = jasmine.createSpyObj('edit', ['getMetadata']); - mockPropertiesAction = jasmine.createSpyObj('edit', ['getMetadata']); - - mockDomainObject.getCapability.and.callFake(function (capability) { - return capabilities[capability]; - }); - mockDomainObject.hasCapability.and.callFake(function (capability) { - return !!capabilities[capability]; - }); - - editableView = { editable: true }; - nonEditableView = { editable: false }; - plotView = { key: "plot", editable: false }; - testViews = []; - - mockDomainObject.useCapability.and.callFake(function (c) { - // Provide test views, only for the view capability - return c === 'view' && testViews; - }); - - mockEditAction.getMetadata.and.returnValue({ key: 'edit' }); - mockPropertiesAction.getMetadata.and.returnValue({ key: 'properties' }); - - testContext = { - domainObject: mockDomainObject, - category: 'view-control' - }; - - policy = new EditActionPolicy(); - }); - - it("allows the edit action when there are editable views", function () { - testViews = [editableView]; - expect(policy.allow(mockEditAction, testContext)).toBe(true); - }); - - it("allows the edit properties action when there are no editable views", function () { - testViews = [nonEditableView, nonEditableView]; - expect(policy.allow(mockPropertiesAction, testContext)).toBe(true); - }); - - it("disallows the edit action when there are no editable views", function () { - testViews = [nonEditableView, nonEditableView]; - expect(policy.allow(mockEditAction, testContext)).toBe(false); - }); - - it("disallows the edit properties action when there are" + - " editable views", function () { - testViews = [editableView]; - expect(policy.allow(mockPropertiesAction, testContext)).toBe(false); - }); - - it("disallows the edit action when object is already being" + - " edited", function () { - testViews = [editableView]; - mockEditorCapability.isEditContextRoot.and.returnValue(true); - expect(policy.allow(mockEditAction, testContext)).toBe(false); - }); - - it("allows editing of panels in plot view", function () { - testViews = [plotView]; - mockTypeCapability.getKey.and.returnValue('telemetry.panel'); - - expect(policy.allow(mockEditAction, testContext)).toBe(true); - }); - - it("disallows editing of plot view when object not a panel type", function () { - testViews = [plotView]; - mockTypeCapability.getKey.and.returnValue('something.else'); - - expect(policy.allow(mockEditAction, testContext)).toBe(false); - }); - - - it("allows the edit properties outside of the 'view-control' category", function () { - testViews = [nonEditableView]; - testContext.category = "something-else"; - expect(policy.allow(mockPropertiesAction, testContext)).toBe(true); - }); - }); - } -); diff --git a/platform/commonUI/edit/test/policies/EditableViewPolicySpec.js b/platform/commonUI/edit/test/policies/EditableViewPolicySpec.js deleted file mode 100644 index 094c5ba6b1..0000000000 --- a/platform/commonUI/edit/test/policies/EditableViewPolicySpec.js +++ /dev/null @@ -1,79 +0,0 @@ -/***************************************************************************** - * Open MCT, Copyright (c) 2014-2018, United States Government - * as represented by the Administrator of the National Aeronautics and Space - * Administration. All rights reserved. - * - * Open MCT is licensed under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * Open MCT includes source code licensed under additional open source - * licenses. See the Open Source Licenses file (LICENSES.md) included with - * this source code distribution or the Licensing information page available - * at runtime from the About dialog for additional information. - *****************************************************************************/ - -define( - ["../../src/policies/EditableViewPolicy"], - function (EditableViewPolicy) { - - describe("The editable view policy", function () { - var mockDomainObject, - testMode, - policy; - - beforeEach(function () { - testMode = true; // Act as if we're in Edit mode by default - mockDomainObject = jasmine.createSpyObj( - 'domainObject', - ['hasCapability', 'getCapability'] - ); - mockDomainObject.getCapability.and.returnValue({ - inEditContext: function () { - return true; - } - }); - mockDomainObject.hasCapability.and.callFake(function (c) { - return (c === 'editor') && testMode; - }); - - policy = new EditableViewPolicy(); - }); - - it("disallows views in edit mode that are flagged as non-editable", function () { - expect(policy.allow({ editable: false }, mockDomainObject)) - .toBeFalsy(); - }); - - it("allows views in edit mode that are flagged as editable", function () { - expect(policy.allow({ editable: true }, mockDomainObject)) - .toBeTruthy(); - }); - - it("allows any view outside of edit mode", function () { - var testViews = [ - { editable: false }, - { editable: true }, - { someKey: "some value" } - ]; - testMode = false; // Act as if we're not in Edit mode - - testViews.forEach(function (testView) { - expect(policy.allow(testView, mockDomainObject)).toBeTruthy(); - }); - }); - - it("treats views with no defined 'editable' property as editable", function () { - expect(policy.allow({ someKey: "some value" }, mockDomainObject)) - .toBeTruthy(); - }); - }); - } -);