mirror of
https://github.com/nasa/openmct.git
synced 2025-06-22 09:08:43 +00:00
Decouple removal of independent time context for a view from refreshing context for other views (#6334)
* Decouple removing the context for a view from refreshing the context of views to get their upstream contexts * Add test for clicking on an item in the elements pool for a preview * Add test to ensure independent time context for items with no parents works as expected
This commit is contained in:
@ -32,14 +32,18 @@ class IndependentTimeContext extends TimeContext {
|
||||
this.openmct = openmct;
|
||||
this.unlisteners = [];
|
||||
this.globalTimeContext = globalTimeContext;
|
||||
this.upstreamTimeContext = undefined;
|
||||
// We always start with the global time context.
|
||||
// This upstream context will be undefined when an independent time context is added later.
|
||||
this.upstreamTimeContext = this.globalTimeContext;
|
||||
this.objectPath = objectPath;
|
||||
this.refreshContext = this.refreshContext.bind(this);
|
||||
this.resetContext = this.resetContext.bind(this);
|
||||
this.removeIndependentContext = this.removeIndependentContext.bind(this);
|
||||
|
||||
this.refreshContext();
|
||||
|
||||
this.globalTimeContext.on('refreshContext', this.refreshContext);
|
||||
this.globalTimeContext.on('removeOwnContext', this.removeIndependentContext);
|
||||
}
|
||||
|
||||
bounds(newBounds) {
|
||||
@ -202,10 +206,16 @@ class IndependentTimeContext extends TimeContext {
|
||||
}
|
||||
|
||||
getUpstreamContext() {
|
||||
// If a view has an independent context, don't return an upstream context
|
||||
// Be aware that when a new independent time context is created, we assign the global context as default
|
||||
if (this.hasOwnContext()) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let timeContext = this.globalTimeContext;
|
||||
this.objectPath.some((item, index) => {
|
||||
const key = this.openmct.objects.makeKeyString(item.identifier);
|
||||
//first index is the view object itself
|
||||
// we're only interested in parents, not self, so index > 0
|
||||
const itemContext = this.globalTimeContext.independentContexts.get(key);
|
||||
if (index > 0 && itemContext && itemContext.hasOwnContext()) {
|
||||
//upstream time context
|
||||
@ -219,6 +229,43 @@ class IndependentTimeContext extends TimeContext {
|
||||
|
||||
return timeContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the time context of a view to follow any upstream time contexts as necessary (defaulting to the global context)
|
||||
* This needs to be separate from refreshContext
|
||||
*/
|
||||
removeIndependentContext(viewKey) {
|
||||
const key = this.openmct.objects.makeKeyString(this.objectPath[0].identifier);
|
||||
if (viewKey && key === viewKey) {
|
||||
//this is necessary as the upstream context gets reassigned after this
|
||||
this.stopFollowingTimeContext();
|
||||
|
||||
let timeContext = this.globalTimeContext;
|
||||
|
||||
this.objectPath.some((item, index) => {
|
||||
const objectKey = this.openmct.objects.makeKeyString(item.identifier);
|
||||
// we're only interested in any parents, not self, so index > 0
|
||||
const itemContext = this.globalTimeContext.independentContexts.get(objectKey);
|
||||
if (index > 0 && itemContext && itemContext.hasOwnContext()) {
|
||||
//upstream time context
|
||||
timeContext = itemContext;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
this.upstreamTimeContext = timeContext;
|
||||
|
||||
this.followTimeContext();
|
||||
|
||||
// Emit bounds so that views that are changing context get the upstream bounds
|
||||
this.emit('bounds', this.bounds());
|
||||
// now that the view's context is set, tell others to check theirs in case they were following this view's context.
|
||||
this.globalTimeContext.emit('refreshContext', viewKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default IndependentTimeContext;
|
||||
|
@ -149,7 +149,7 @@ class TimeAPI extends GlobalTimeContext {
|
||||
|
||||
return () => {
|
||||
//follow any upstream time context
|
||||
this.emit('refreshContext');
|
||||
this.emit('removeOwnContext', key);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -117,22 +117,58 @@ describe("The Independent Time API", function () {
|
||||
});
|
||||
|
||||
it("uses an object's independent time context if the parent doesn't have one", () => {
|
||||
const domainObjectKey2 = `${domainObjectKey}-2`;
|
||||
const domainObjectKey3 = `${domainObjectKey}-3`;
|
||||
let timeContext = api.getContextForView([{
|
||||
identifier: {
|
||||
namespace: '',
|
||||
key: domainObjectKey
|
||||
}
|
||||
}, {
|
||||
}]);
|
||||
let timeContext2 = api.getContextForView([{
|
||||
identifier: {
|
||||
namespace: '',
|
||||
key: 'blah'
|
||||
key: domainObjectKey2
|
||||
}
|
||||
}]);
|
||||
let timeContext3 = api.getContextForView([{
|
||||
identifier: {
|
||||
namespace: '',
|
||||
key: domainObjectKey3
|
||||
}
|
||||
}]);
|
||||
// all bounds follow global time context
|
||||
expect(timeContext.bounds()).toEqual(bounds);
|
||||
expect(timeContext2.bounds()).toEqual(bounds);
|
||||
expect(timeContext3.bounds()).toEqual(bounds);
|
||||
// only first item has own context
|
||||
let destroyTimeContext = api.addIndependentContext(domainObjectKey, independentBounds);
|
||||
expect(timeContext.bounds()).toEqual(independentBounds);
|
||||
expect(timeContext2.bounds()).toEqual(bounds);
|
||||
expect(timeContext3.bounds()).toEqual(bounds);
|
||||
// first and second item have own context
|
||||
let destroyTimeContext2 = api.addIndependentContext(domainObjectKey2, independentBounds);
|
||||
expect(timeContext.bounds()).toEqual(independentBounds);
|
||||
expect(timeContext2.bounds()).toEqual(independentBounds);
|
||||
expect(timeContext3.bounds()).toEqual(bounds);
|
||||
// all items have own time context
|
||||
let destroyTimeContext3 = api.addIndependentContext(domainObjectKey3, independentBounds);
|
||||
expect(timeContext.bounds()).toEqual(independentBounds);
|
||||
expect(timeContext2.bounds()).toEqual(independentBounds);
|
||||
expect(timeContext3.bounds()).toEqual(independentBounds);
|
||||
//remove own contexts one at a time - should revert to global time context
|
||||
destroyTimeContext();
|
||||
expect(timeContext.bounds()).toEqual(bounds);
|
||||
expect(timeContext2.bounds()).toEqual(independentBounds);
|
||||
expect(timeContext3.bounds()).toEqual(independentBounds);
|
||||
destroyTimeContext2();
|
||||
expect(timeContext.bounds()).toEqual(bounds);
|
||||
expect(timeContext2.bounds()).toEqual(bounds);
|
||||
expect(timeContext3.bounds()).toEqual(independentBounds);
|
||||
destroyTimeContext3();
|
||||
expect(timeContext.bounds()).toEqual(bounds);
|
||||
expect(timeContext2.bounds()).toEqual(bounds);
|
||||
expect(timeContext3.bounds()).toEqual(bounds);
|
||||
});
|
||||
|
||||
it("Allows setting of valid bounds", function () {
|
||||
|
Reference in New Issue
Block a user