Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented #427 - Implement an API to make a visualizer default #428

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/visualizationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare type VisualizerConstructor = new (
*/
export class VisualizationManager {
static alternativesVisualizer: any = undefined;
static vizualizers: { [index: string]: Array<VisualizerConstructor> } = {};
static vizualizers: { [index: string]: Array<{ ctor: VisualizerConstructor, index: number }> } = {};
/**
* Registers a visualizer for a specified question type.
*
Expand All @@ -24,14 +24,15 @@ export class VisualizationManager {
*/
public static registerVisualizer(
questionType: string,
constructor: VisualizerConstructor
constructor: VisualizerConstructor,
index = Number.MAX_VALUE
) {
let visualizers = VisualizationManager.vizualizers[questionType];
if (!visualizers) {
visualizers = [];
VisualizationManager.vizualizers[questionType] = visualizers;
}
visualizers.push(constructor);
visualizers.push({ ctor: constructor, index });
}
/**
* Unregisters a visualizer for a specified question type.
Expand All @@ -46,12 +47,19 @@ export class VisualizationManager {
questionTypes = Object.keys(VisualizationManager.vizualizers);
}
questionTypes.forEach(qType => {
let visualizers = VisualizationManager.vizualizers[qType];
if (!!visualizers) {
let index = visualizers.indexOf(constructor);
if (index !== -1) {
visualizers.splice(index, 1);
if(constructor) {
let visualizers = VisualizationManager.vizualizers[qType];
if (!!visualizers) {
const vDescr = visualizers.filter(v => v.ctor === constructor)[0];
if(!!vDescr) {
let index = visualizers.indexOf(vDescr);
if (index !== -1) {
visualizers.splice(index, 1);
}
}
}
} else {
VisualizationManager.vizualizers[qType] = [];
}
});
}
Expand All @@ -69,11 +77,13 @@ export class VisualizationManager {
public static getVisualizersByType(
questionType: string
): VisualizerConstructor[] {
let visualizers = VisualizationManager.vizualizers[questionType];
if (!visualizers) {
let vDescrs = VisualizationManager.vizualizers[questionType];
if (!vDescrs) {
return [VisualizerBase];
}
return visualizers;
vDescrs = [].concat(vDescrs);
vDescrs.sort((v1, v2) => v1.index - v2.index);
return vDescrs.map(v => v.ctor);
}
/**
* Returns a constructor for an alternative visualizer selector.
Expand Down
38 changes: 37 additions & 1 deletion tests/visualizationManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ test("unregister visualizer", () => {

expect(textVizualizers.length).toBe(1);
expect(textVizualizers[0]).toBe(Text);

VisualizationManager.registerVisualizer("text", WordCloud);
});

test("unregister visualizer for all question types", () => {
expect(VisualizationManager.vizualizers).toMatchObject({});

const text = Text; // need to trigger VisualizationManager.registerVisualizer("text", Text);
const wordCloud = WordCloud; // need to trigger VisualizationManager.registerVisualizer("text", WordCloud);
VisualizationManager.registerVisualizer("text", WordCloud);

let textVizualizers = VisualizationManager.getVisualizersByType("text");
let commentVizualizers = VisualizationManager.getVisualizersByType("comment");
Expand Down Expand Up @@ -81,4 +82,39 @@ test("unregister visualizer for all question types", () => {

expect(multipletextVizualizers.length).toBe(1);
expect(multipletextVizualizers[0]).toBe(Text);

VisualizationManager.registerVisualizer("text", WordCloud);
VisualizationManager.registerVisualizer("comment", WordCloud);
VisualizationManager.registerVisualizer("multipletext", WordCloud);
});

test("visualizers default order", () => {
VisualizationManager.registerVisualizer("test", { name: "v1" } as any);
VisualizationManager.registerVisualizer("test", { name: "v2" } as any);
VisualizationManager.registerVisualizer("test", { name: "v3" } as any);

let testVizualizers = VisualizationManager.getVisualizersByType("test");
expect(testVizualizers.length).toBe(3);
expect(testVizualizers[0].name).toBe("v1");
expect(testVizualizers[2].name).toBe("v3");

VisualizationManager.unregisterVisualizer("test", undefined as any);
testVizualizers = VisualizationManager.getVisualizersByType("test");
expect(testVizualizers.length).toBe(0);
});

test("visualizers set order", () => {
VisualizationManager.registerVisualizer("test", { name: "v1" } as any);
VisualizationManager.registerVisualizer("test", { name: "v2" } as any);
VisualizationManager.registerVisualizer("test", { name: "v3" } as any, 0);

let testVizualizers = VisualizationManager.getVisualizersByType("test");
expect(testVizualizers.length).toBe(3);
expect(testVizualizers[0].name).toBe("v3");
expect(testVizualizers[1].name).toBe("v1");
expect(testVizualizers[2].name).toBe("v2");

VisualizationManager.unregisterVisualizer("test", undefined as any);
testVizualizers = VisualizationManager.getVisualizersByType("test");
expect(testVizualizers.length).toBe(0);
});
Loading