forked from apache-superset/superset-ui-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert chart models to TS and export additional modules (apache-supe…
…rset#55) internal: Convert Chart models to ts
- Loading branch information
Showing
22 changed files
with
381 additions
and
216 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 was deleted.
Oops, something went wrong.
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,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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.