|
@@ -2,90 +2,90 @@
|
|
|
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
|
|
|
*
|
|
|
* @author Alexander Rose <alexander.rose@weirdbyte.de>
|
|
|
+ * @author David Sehnal <david.sehnal@gmail.com>
|
|
|
*/
|
|
|
|
|
|
-import { Color } from './color';
|
|
|
+import { Color as ColorData } from './color';
|
|
|
|
|
|
export namespace ParamDefinition {
|
|
|
- export interface BaseParam<T> {
|
|
|
+ export interface Base<T> {
|
|
|
label: string
|
|
|
description: string
|
|
|
defaultValue: T
|
|
|
}
|
|
|
|
|
|
- // TODO: is this really needed?
|
|
|
- // export interface ValueParam<T> extends BaseParam<T> {
|
|
|
- // type: 'value'
|
|
|
- // }
|
|
|
- // export function ValueParam<T>(label: string, description: string, defaultValue: T): ValueParam<T> {
|
|
|
- // return { type: 'value', label, description, defaultValue }
|
|
|
- // }
|
|
|
+ export interface GenericValue<T> extends Base<T> {
|
|
|
+ type: 'generic-value'
|
|
|
+ }
|
|
|
+ export function GenericValue<T>(label: string, description: string, defaultValue: T): GenericValue<T> {
|
|
|
+ return { type: 'generic-value', label, description, defaultValue }
|
|
|
+ }
|
|
|
|
|
|
- export interface SelectParam<T extends string> extends BaseParam<T> {
|
|
|
+ export interface Select<T extends string> extends Base<T> {
|
|
|
type: 'select'
|
|
|
- /** array of (value, label) tupels */
|
|
|
+ /** array of (value, label) tuples */
|
|
|
options: [T, string][]
|
|
|
}
|
|
|
- export function SelectParam<T extends string>(label: string, description: string, defaultValue: T, options: [T, string][]): SelectParam<T> {
|
|
|
+ export function Select<T extends string>(label: string, description: string, defaultValue: T, options: [T, string][]): Select<T> {
|
|
|
return { type: 'select', label, description, defaultValue, options }
|
|
|
}
|
|
|
|
|
|
- export interface MultiSelectParam<E extends string, T = E[]> extends BaseParam<T> {
|
|
|
+ export interface MultiSelect<E extends string, T = E[]> extends Base<T> {
|
|
|
type: 'multi-select'
|
|
|
- /** array of (value, label) tupels */
|
|
|
+ /** array of (value, label) tuples */
|
|
|
options: [E, string][]
|
|
|
}
|
|
|
- export function MultiSelectParam<E extends string, T = E[]>(label: string, description: string, defaultValue: T, options: [E, string][]): MultiSelectParam<E, T> {
|
|
|
+ export function MultiSelect<E extends string, T = E[]>(label: string, description: string, defaultValue: T, options: [E, string][]): MultiSelect<E, T> {
|
|
|
return { type: 'multi-select', label, description, defaultValue, options }
|
|
|
}
|
|
|
|
|
|
- export interface BooleanParam extends BaseParam<boolean> {
|
|
|
+ export interface Boolean extends Base<boolean> {
|
|
|
type: 'boolean'
|
|
|
}
|
|
|
- export function BooleanParam(label: string, description: string, defaultValue: boolean): BooleanParam {
|
|
|
+ export function Boolean(label: string, description: string, defaultValue: boolean): Boolean {
|
|
|
return { type: 'boolean', label, description, defaultValue }
|
|
|
}
|
|
|
|
|
|
- export interface RangeParam extends BaseParam<number> {
|
|
|
+ export interface Range extends Base<number> {
|
|
|
type: 'range'
|
|
|
min: number
|
|
|
max: number
|
|
|
/** if an `integer` parse value with parseInt, otherwise use parseFloat */
|
|
|
step: number
|
|
|
}
|
|
|
- export function RangeParam(label: string, description: string, defaultValue: number, min: number, max: number, step: number): RangeParam {
|
|
|
+ export function Range(label: string, description: string, defaultValue: number, min: number, max: number, step: number): Range {
|
|
|
return { type: 'range', label, description, defaultValue, min, max, step }
|
|
|
}
|
|
|
|
|
|
- export interface TextParam extends BaseParam<string> {
|
|
|
+ export interface Text extends Base<string> {
|
|
|
type: 'text'
|
|
|
}
|
|
|
- export function TextParam(label: string, description: string, defaultValue: string): TextParam {
|
|
|
+ export function Text(label: string, description: string, defaultValue: string = ''): Text {
|
|
|
return { type: 'text', label, description, defaultValue }
|
|
|
}
|
|
|
|
|
|
- export interface ColorParam extends BaseParam<Color> {
|
|
|
+ export interface Color extends Base<ColorData> {
|
|
|
type: 'color'
|
|
|
}
|
|
|
- export function ColorParam(label: string, description: string, defaultValue: Color): ColorParam {
|
|
|
+ export function Color(label: string, description: string, defaultValue: ColorData): Color {
|
|
|
return { type: 'color', label, description, defaultValue }
|
|
|
}
|
|
|
|
|
|
- export interface NumberParam extends BaseParam<number> {
|
|
|
+ export interface Numeric extends Base<number> {
|
|
|
type: 'number'
|
|
|
min: number
|
|
|
max: number
|
|
|
/** if an `integer` parse value with parseInt, otherwise use parseFloat */
|
|
|
step: number
|
|
|
}
|
|
|
- export function NumberParam(label: string, description: string, defaultValue: number, min: number, max: number, step: number): NumberParam {
|
|
|
+ export function Numeric(label: string, description: string, defaultValue: number, min: number, max: number, step: number): Numeric {
|
|
|
return { type: 'number', label, description, defaultValue, min, max, step }
|
|
|
}
|
|
|
|
|
|
- export type Param = /* ValueParam<any> | */ SelectParam<any> | MultiSelectParam<any> | BooleanParam | RangeParam | TextParam | ColorParam | NumberParam
|
|
|
- export type Params = { [k: string]: Param }
|
|
|
+ export type Any = /* ValueParam<any> | */ Select<any> | MultiSelect<any> | Boolean | Range | Text | Color | Numeric
|
|
|
+ export type Params = { [k: string]: Any }
|
|
|
|
|
|
- export function paramDefaultValues<T extends Params>(params: T) {
|
|
|
+ export function getDefaultValues<T extends Params>(params: T) {
|
|
|
const d: { [k: string]: any } = {}
|
|
|
Object.keys(params).forEach(k => d[k] = params[k].defaultValue)
|
|
|
return d as { [k in keyof T]: T[k]['defaultValue'] }
|