Skip to content

Commit

Permalink
Update time graph navigator on absolute range change
Browse files Browse the repository at this point in the history
Add support for absolute range change handlers in unit controller. Make
time graph navigator register one such handler so that it can update
itself when the absolute range changes.

Signed-off-by: Patrick Tasse <[email protected]>
  • Loading branch information
PatrickTasse committed Sep 13, 2024
1 parent 5bb8298 commit 2ea4281
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions timeline-chart/src/layer/time-graph-navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class TimeGraphNavigator extends TimeGraphLayer {

afterAddToContainer() {
this._updateHandler = (): void => this.update();
this.unitController.onAbsoluteRangeChanged(this._updateHandler);
this.unitController.onViewRangeChanged(this._updateHandler);
this.navigatorBackground = new TimeGraphNavigatorBackground(this.unitController, this.stateController);
this.addChild(this.navigatorBackground);
Expand Down Expand Up @@ -59,6 +60,7 @@ export class TimeGraphNavigator extends TimeGraphLayer {

destroy() : void {
if (this.unitController) {
this.unitController.removeAbsoluteRangeChangedHandler(this._updateHandler);
this.unitController.removeViewRangeChangedHandler(this._updateHandler);
this.unitController.removeSelectionRangeChangedHandler(this._updateHandler);
}
Expand Down
32 changes: 31 additions & 1 deletion timeline-chart/src/time-graph-unit-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { TimeGraphRenderController } from "./time-graph-render-controller";

export class TimeGraphUnitController {

protected absoluteRangeChangedHandlers: ((absoluteRange: bigint) => void)[];
protected _absoluteRange: bigint;

protected viewRangeChangedHandlers: ((oldRange: TimelineChart.TimeGraphRange, newRange: TimelineChart.TimeGraphRange) => void)[];
protected _viewRange: TimelineChart.TimeGraphRange;
Expand All @@ -27,15 +29,21 @@ export class TimeGraphUnitController {
numberTranslator?: (theNumber: bigint) => string | undefined;
scaleSteps?: number[]

constructor(public absoluteRange: bigint, viewRange?: TimelineChart.TimeGraphRange) {
constructor(absoluteRange: bigint, viewRange?: TimelineChart.TimeGraphRange) {
this._absoluteRange = absoluteRange;
this._viewRange = viewRange || { start: BigInt(0), end: absoluteRange };

this.absoluteRangeChangedHandlers = [];
this.viewRangeChangedHandlers = [];
this.selectionRangeChangedHandlers = [];

this._renderer = new TimeGraphRenderController();
}

protected handleAbsoluteRangeChange() {
this.absoluteRangeChangedHandlers.forEach(handler => handler(this._absoluteRange));
}

protected handleViewRangeChange(oldRange: TimelineChart.TimeGraphRange) {
this.viewRangeChangedHandlers.forEach(handler => handler(oldRange, this._viewRange));
}
Expand All @@ -44,10 +52,21 @@ export class TimeGraphUnitController {
this.selectionRangeChangedHandlers.forEach(handler => handler(this._selectionRange));
}

onAbsoluteRangeChanged(handler: (absoluteRange: bigint) => void) {
this.absoluteRangeChangedHandlers.push(handler);
}

onViewRangeChanged(handler: (oldRange: TimelineChart.TimeGraphRange, viewRange: TimelineChart.TimeGraphRange) => void) {
this.viewRangeChangedHandlers.push(handler);
}

removeAbsoluteRangeChangedHandler(handler: (absoluteRange: bigint) => void) {
const index = this.absoluteRangeChangedHandlers.indexOf(handler);
if (index > -1) {
this.absoluteRangeChangedHandlers.splice(index, 1);
}
}

removeViewRangeChangedHandler(handler: (oldRange: TimelineChart.TimeGraphRange, viewRange: TimelineChart.TimeGraphRange) => void) {
const index = this.viewRangeChangedHandlers.indexOf(handler);
if (index > -1) {
Expand All @@ -66,6 +85,17 @@ export class TimeGraphUnitController {
}
}

get absoluteRange(): bigint {
return this._absoluteRange;
}

set absoluteRange(absoluteRange: bigint) {
if (this._absoluteRange !== absoluteRange) {
this._absoluteRange = absoluteRange;
this.handleAbsoluteRangeChange();
}
}

get viewRange(): TimelineChart.TimeGraphRange {
return this._viewRange;
}
Expand Down

0 comments on commit 2ea4281

Please sign in to comment.