[Toolbar] set selection initially in fixed controller and toolbar... (#1994)

* [Toolbar] set selection initially in fixed controller and toolbar...

... to make the add button appear in the toolbar when a fixed position is created.

Remove selection change listener on destroy.

Start a digest cycle when handling selection in toolbar to avoid delays in toolbar.

Fixes #1991, #1987

* fixed checkstyle and lint errors

* Fix tests

* Update comment
This commit is contained in:
Pegah Sarram 2018-04-20 08:45:29 -07:00 committed by Pete Richards
parent 78a5ace18d
commit 75ae5ab3bb
6 changed files with 61 additions and 34 deletions

View File

@ -88,12 +88,6 @@ define(
commit("Changes from toolbar."); commit("Changes from toolbar.");
} }
} }
// Avoid attaching scope to this;
// http://errors.angularjs.org/1.2.26/ng/cpws
this.setSelection = function (s) {
scope.selection = s;
};
this.clearExposedToolbar = function () { this.clearExposedToolbar = function () {
// Clear exposed toolbar state (if any) // Clear exposed toolbar state (if any)
if (attrs.toolbar) { if (attrs.toolbar) {
@ -110,6 +104,7 @@ define(
this.toolbar = undefined; this.toolbar = undefined;
this.toolbarObject = {}; this.toolbarObject = {};
this.openmct = openmct; this.openmct = openmct;
this.scope = scope;
// If this representation exposes a toolbar, set up watches // If this representation exposes a toolbar, set up watches
// to synchronize with it. // to synchronize with it.
@ -130,26 +125,23 @@ define(
// Represent a domain object using this definition // Represent a domain object using this definition
EditToolbarRepresenter.prototype.represent = function (representation) { EditToolbarRepresenter.prototype.represent = function (representation) {
// Get the newest toolbar definition from the view // Get the newest toolbar definition from the view
var definition = (representation || {}).toolbar || {}, var definition = (representation || {}).toolbar || {};
self = this;
// Initialize toolbar (expose object to parent scope) // If we have been asked to expose toolbar state...
function initialize(def) { if (this.attrs.toolbar) {
// If we have been asked to expose toolbar state... // Initialize toolbar object
if (self.attrs.toolbar) { this.toolbar = new EditToolbar(definition, this.commit);
// Initialize toolbar object // Ensure toolbar state is exposed
self.toolbar = new EditToolbar(def, self.commit); this.exposeToolbar();
// Ensure toolbar state is exposed
self.exposeToolbar();
}
} }
// Expose the toolbar object to the parent scope // Add toolbar selection to scope.
initialize(definition); this.scope.selection = new EditToolbarSelection(
// Create a selection scope this.scope,
this.setSelection(new EditToolbarSelection(this.openmct)); this.openmct
// Initialize toolbar to an empty selection );
this.updateSelection([]); // Initialize toolbar to current selection
this.updateSelection(this.scope.selection.all());
}; };
// Destroy; remove toolbar object from parent scope // Destroy; remove toolbar object from parent scope

View File

@ -38,24 +38,37 @@ define(
* @memberof platform/commonUI/edit * @memberof platform/commonUI/edit
* @constructor * @constructor
*/ */
function EditToolbarSelection(openmct) { function EditToolbarSelection($scope, openmct) {
this.selection = [{}]; this.selection = [{}];
this.selecting = false; this.selecting = false;
this.selectedObj = undefined; this.selectedObj = undefined;
this.openmct = openmct;
var self = this;
openmct.selection.on('change', function (selection) { function setSelection(selection) {
var selected = selection[0]; var selected = selection[0];
if (selected && selected.context.toolbar) { if (selected && selected.context.toolbar) {
this.select(selected.context.toolbar); self.select(selected.context.toolbar);
} else { } else {
this.deselect(); self.deselect();
} }
if (selected && selected.context.viewProxy) { if (selected && selected.context.viewProxy) {
this.proxy(selected.context.viewProxy); self.proxy(selected.context.viewProxy);
} }
}.bind(this));
setTimeout(function () {
$scope.$apply();
});
}
$scope.$on("$destroy", function () {
self.openmct.selection.off('change', setSelection);
});
this.openmct.selection.on('change', setSelection);
setSelection(this.openmct.selection.get());
} }
/** /**

View File

@ -36,7 +36,7 @@ define(
beforeEach(function () { beforeEach(function () {
mockScope = jasmine.createSpyObj( mockScope = jasmine.createSpyObj(
'$scope', '$scope',
['$on', '$watch', '$watchCollection', "commit"] ['$on', '$watch', '$watchCollection', "commit", "$apply"]
); );
mockElement = {}; mockElement = {};
testAttrs = { toolbar: 'testToolbar' }; testAttrs = { toolbar: 'testToolbar' };

View File

@ -30,7 +30,8 @@ define(
otherElement, otherElement,
selection, selection,
mockSelection, mockSelection,
mockOpenMCT; mockOpenMCT,
mockScope;
beforeEach(function () { beforeEach(function () {
testProxy = { someKey: "some value" }; testProxy = { someKey: "some value" };
@ -46,7 +47,12 @@ define(
mockOpenMCT = { mockOpenMCT = {
selection: mockSelection selection: mockSelection
}; };
selection = new EditToolbarSelection(mockOpenMCT); mockScope = jasmine.createSpyObj('$scope', [
'$on',
'$apply'
]);
selection = new EditToolbarSelection(mockScope, mockOpenMCT);
selection.proxy(testProxy); selection.proxy(testProxy);
}); });
@ -103,6 +109,20 @@ define(
expect(selection.all()).toEqual([testProxy]); expect(selection.all()).toEqual([testProxy]);
}); });
it("cleans up selection on scope destroy", function () {
expect(mockScope.$on).toHaveBeenCalledWith(
'$destroy',
jasmine.any(Function)
);
mockScope.$on.mostRecentCall.args[1]();
expect(mockOpenMCT.selection.off).toHaveBeenCalledWith(
'change',
jasmine.any(Function)
);
});
}); });
} }
); );

View File

@ -315,6 +315,8 @@ define(
this.openmct.time.on("bounds", updateDisplayBounds); this.openmct.time.on("bounds", updateDisplayBounds);
this.openmct.selection.on('change', setSelection); this.openmct.selection.on('change', setSelection);
this.$element.on('click', this.bypassSelection.bind(this)); this.$element.on('click', this.bypassSelection.bind(this));
setSelection(this.openmct.selection.get());
} }
/** /**

View File

@ -201,7 +201,7 @@ define(
'off', 'off',
'get' 'get'
]); ]);
mockSelection.get.andCallThrough(); mockSelection.get.andReturn([]);
mockOpenMCT = { mockOpenMCT = {
time: mockConductor, time: mockConductor,
@ -596,7 +596,7 @@ define(
expect(controller.getSelectedElementStyle()).not.toEqual(oldStyle); expect(controller.getSelectedElementStyle()).not.toEqual(oldStyle);
}); });
it("cleans up slection on scope destroy", function () { it("cleans up selection on scope destroy", function () {
expect(mockScope.$on).toHaveBeenCalledWith( expect(mockScope.$on).toHaveBeenCalledWith(
'$destroy', '$destroy',
jasmine.any(Function) jasmine.any(Function)