Skip to content

Commit

Permalink
Convert chart models to TS and export additional modules (apache-supe…
Browse files Browse the repository at this point in the history
…rset#55)

internal: Convert Chart models to ts
  • Loading branch information
kristw authored Dec 11, 2018
1 parent d6d7396 commit f5695f8
Show file tree
Hide file tree
Showing 22 changed files with 381 additions and 216 deletions.
15 changes: 12 additions & 3 deletions packages/superset-ui-chart/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export { default as ChartMetadata } from './models/ChartMetadata';
export { default as ChartPlugin } from './models/ChartPlugin';
export { default as ChartProps } from './models/ChartProps';
export { ChartMetadata, ChartMetadataConfig } from './models/ChartMetadata';
export {
ChartPlugin,
ChartPluginConfig,
BuildQueryFunction,
TransformPropsFunction,
} from './models/ChartPlugin';
export { ChartProps, ChartPropsConfig } from './models/ChartProps';

export { default as createLoadableRenderer } from './components/createLoadableRenderer';
export { default as reactify } from './components/reactify';
Expand All @@ -13,3 +18,7 @@ export { default as getChartMetadataRegistry } from './registries/ChartMetadataR
export {
default as getChartTransformPropsRegistry,
} from './registries/ChartTransformPropsRegistrySingleton';

export { QueryContext, buildQueryContext } from './query/buildQueryContext';
export { DatasourceType, DatasourceKey } from './query/DatasourceKey';
export { FormData } from './query/FormData';
28 changes: 0 additions & 28 deletions packages/superset-ui-chart/src/models/ChartMetadata.js

This file was deleted.

55 changes: 55 additions & 0 deletions packages/superset-ui-chart/src/models/ChartMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
interface LookupTable {
[key: string]: boolean;
}

export interface ChartMetadataConfig {
name: string;
credits?: Array<string>;
description?: string;
show?: boolean;
canBeAnnotationTypes?: Array<string>;
supportedAnnotationTypes?: Array<string>;
thumbnail: string;
}

export class ChartMetadata {
name: string;
credits: Array<string>;
description: string;
show: boolean;
canBeAnnotationTypesLookup: LookupTable;
supportedAnnotationTypes: Array<string>;
thumbnail: string;

constructor(config: ChartMetadataConfig) {
const {
name,
credits = [],
description = '',
show = true,
canBeAnnotationTypes = [],
supportedAnnotationTypes = [],
thumbnail,
} = config;

this.name = name;
this.credits = credits;
this.description = description;
this.show = show;
this.canBeAnnotationTypesLookup = canBeAnnotationTypes.reduce(
(prev: LookupTable, type: string) => {
const lookup = prev;
lookup[type] = true;

return lookup;
},
{},
);
this.supportedAnnotationTypes = supportedAnnotationTypes;
this.thumbnail = thumbnail;
}

canBeAnnotationType(type: string): boolean {
return this.canBeAnnotationTypesLookup[type] || false;
}
}
51 changes: 0 additions & 51 deletions packages/superset-ui-chart/src/models/ChartPlugin.js

This file was deleted.

79 changes: 79 additions & 0 deletions packages/superset-ui-chart/src/models/ChartPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { isRequired, Plugin } from '@superset-ui/core';
import { ChartMetadata } from './ChartMetadata';
import { ChartProps } from './ChartProps';
import { FormData } from '../query/FormData';
import { QueryContext } from '../query/buildQueryContext';
import getChartMetadataRegistry from '../registries/ChartMetadataRegistrySingleton';
import getChartBuildQueryRegistry from '../registries/ChartBuildQueryRegistrySingleton';
import getChartComponentRegistry from '../registries/ChartComponentRegistrySingleton';
import getChartTransformPropsRegistry from '../registries/ChartTransformPropsRegistrySingleton';

const IDENTITY = (x: any) => x;

type PromiseOrValue<T> = Promise<T> | T;
type PromiseOrValueLoader<T> = () => PromiseOrValue<T>;

export type BuildQueryFunction = (formData: FormData) => QueryContext;

export type TransformPropsFunction = (
chartProps: ChartProps,
) => {
[key: string]: any;
};

export interface ChartPluginConfig {
metadata: ChartMetadata;
// use buildQuery for immediate value
buildQuery?: BuildQueryFunction;
// use loadBuildQuery for dynamic import (lazy-loading)
loadBuildQuery?: PromiseOrValueLoader<BuildQueryFunction>;
// use transformProps for immediate value
transformProps?: TransformPropsFunction;
// use loadTransformProps for dynamic import (lazy-loading)
loadTransformProps?: PromiseOrValueLoader<TransformPropsFunction>;
// use Chart for immediate value
Chart?: Function;
// use loadChart for dynamic import (lazy-loading)
loadChart?: PromiseOrValueLoader<Function>;
}

export class ChartPlugin extends Plugin {
metadata: ChartMetadata;
loadBuildQuery?: PromiseOrValueLoader<BuildQueryFunction>;
loadTransformProps: PromiseOrValueLoader<TransformPropsFunction>;
loadChart: PromiseOrValueLoader<Function>;

constructor(config: ChartPluginConfig) {
super();
const {
metadata,
buildQuery,
loadBuildQuery,
transformProps = IDENTITY,
loadTransformProps,
Chart,
loadChart,
} = config;
this.metadata = metadata;
this.loadBuildQuery = loadBuildQuery || (buildQuery ? () => buildQuery : undefined);
this.loadTransformProps = loadTransformProps || (() => transformProps);

if (loadChart) {
this.loadChart = loadChart;
} else if (Chart) {
this.loadChart = () => Chart;
} else {
throw new Error('Chart or loadChart is required');
}
}

register(): ChartPlugin {
const { key = isRequired('config.key') } = this.config;
getChartMetadataRegistry().registerValue(key, this.metadata);
getChartBuildQueryRegistry().registerLoader(key, this.loadBuildQuery);
getChartComponentRegistry().registerLoader(key, this.loadChart);
getChartTransformPropsRegistry().registerLoader(key, this.loadTransformProps);

return this;
}
}
74 changes: 0 additions & 74 deletions packages/superset-ui-chart/src/models/ChartProps.js

This file was deleted.

Loading

0 comments on commit f5695f8

Please sign in to comment.