-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fit): allow custom zoom level limits (#1134)
They are optional and default to the same values as before (that is no breaking change), they are limited to values above 0 (zero would lead to infinities, NaNs etc. and I have no idea what negative zoom should mean) and max can't be lower than min. There's visual regression test included.
- Loading branch information
Showing
8 changed files
with
206 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
context("Fit", (): void => { | ||
beforeEach((): void => { | ||
cy.visVisitUniversal( | ||
{ | ||
nodes: [ | ||
{ id: 1, label: "Node 1" }, | ||
{ id: 2, label: "Node 2" }, | ||
{ id: 3, label: "Node 3" }, | ||
{ id: 4, label: "Node 4" }, | ||
{ id: 5, label: "Node 5" }, | ||
], | ||
edges: [ | ||
{ from: 1, to: 3 }, | ||
{ from: 1, to: 2 }, | ||
{ from: 2, to: 4 }, | ||
{ from: 2, to: 5 }, | ||
{ from: 3, to: 3 }, | ||
], | ||
}, | ||
{ requireNewerVersionThan: "8.4.2" } | ||
); | ||
}); | ||
|
||
it("No params", (): void => { | ||
cy.visRun(({ network }): void => { | ||
network.fit(); | ||
}); | ||
cy.visSnapshotOpenedPage("no-params", { moveTo: false }); | ||
}); | ||
|
||
it("High max", (): void => { | ||
cy.visRun(({ network }): void => { | ||
network.fit({ | ||
maxZoomLevel: 20, | ||
}); | ||
}); | ||
cy.visSnapshotOpenedPage("high-max", { moveTo: false }); | ||
}); | ||
|
||
it("Low max", (): void => { | ||
cy.visRun(({ network }): void => { | ||
network.fit({ | ||
maxZoomLevel: 0.2, | ||
}); | ||
}); | ||
cy.visSnapshotOpenedPage("low-max", { moveTo: false }); | ||
}); | ||
|
||
it("Impossible to fit min and max", (): void => { | ||
cy.visRun(({ network }): void => { | ||
network.fit({ | ||
minZoomLevel: 5, | ||
maxZoomLevel: 10, | ||
}); | ||
}); | ||
cy.visSnapshotOpenedPage("impossible-to-fit-min-and-max", { | ||
moveTo: false, | ||
}); | ||
}); | ||
|
||
it("One node", (): void => { | ||
cy.visRun(({ network }): void => { | ||
network.fit({ nodes: [3] }); | ||
}); | ||
cy.visSnapshotOpenedPage("one-node", { moveTo: false }); | ||
}); | ||
|
||
it("One node with custom limits", (): void => { | ||
cy.visRun(({ network }): void => { | ||
network.fit({ | ||
nodes: [3], | ||
minZoomLevel: 6, | ||
maxZoomLevel: 15, | ||
}); | ||
}); | ||
cy.visSnapshotOpenedPage("one-node-with-custom-limits", { moveTo: false }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
type IdType = string | number; | ||
|
||
export interface ViewFitOptions { | ||
nodes: IdType[]; | ||
minZoomLevel: number; | ||
maxZoomLevel: number; | ||
} | ||
|
||
/** | ||
* Validate the fit options, replace missing optional values by defaults etc. | ||
* | ||
* @param rawOptions - The raw options. | ||
* @param allNodeIds - All node ids that will be used if nodes are omitted in | ||
* the raw options. | ||
* | ||
* @returns Options with everything filled in and validated. | ||
*/ | ||
export function normalizeFitOptions( | ||
rawOptions: Partial<ViewFitOptions>, | ||
allNodeIds: IdType[] | ||
): ViewFitOptions { | ||
const options = Object.assign<ViewFitOptions, Partial<ViewFitOptions>>( | ||
{ | ||
nodes: allNodeIds, | ||
minZoomLevel: Number.MIN_VALUE, | ||
maxZoomLevel: 1, | ||
}, | ||
rawOptions ?? {} | ||
); | ||
|
||
if (!Array.isArray(options.nodes)) { | ||
throw new TypeError("Nodes has to be an array of ids."); | ||
} | ||
if (options.nodes.length === 0) { | ||
options.nodes = allNodeIds; | ||
} | ||
|
||
if (!(typeof options.minZoomLevel === "number" && options.minZoomLevel > 0)) { | ||
throw new TypeError("Min zoom level has to be a number higher than zero."); | ||
} | ||
|
||
if ( | ||
!( | ||
typeof options.maxZoomLevel === "number" && | ||
options.minZoomLevel <= options.maxZoomLevel | ||
) | ||
) { | ||
throw new TypeError( | ||
"Max zoom level has to be a number higher than min zoom level." | ||
); | ||
} | ||
|
||
return options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters