param-definition.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { Color as ColorData } from './color';
  8. import { shallowClone } from 'mol-util';
  9. export namespace ParamDefinition {
  10. export interface Base<T> {
  11. label: string
  12. description: string
  13. defaultValue: T
  14. }
  15. export interface GenericValue<T> extends Base<T> {
  16. type: 'generic-value'
  17. }
  18. export function GenericValue<T>(label: string, description: string, defaultValue: T): GenericValue<T> {
  19. return { type: 'generic-value', label, description, defaultValue }
  20. }
  21. export interface Select<T extends string> extends Base<T> {
  22. type: 'select'
  23. /** array of (value, label) tuples */
  24. options: [T, string][]
  25. }
  26. export function Select<T extends string>(label: string, description: string, defaultValue: T, options: [T, string][]): Select<T> {
  27. return { type: 'select', label, description, defaultValue, options }
  28. }
  29. export interface MultiSelect<E extends string, T = E[]> extends Base<T> {
  30. type: 'multi-select'
  31. /** array of (value, label) tuples */
  32. options: [E, string][]
  33. }
  34. export function MultiSelect<E extends string, T = E[]>(label: string, description: string, defaultValue: T, options: [E, string][]): MultiSelect<E, T> {
  35. return { type: 'multi-select', label, description, defaultValue, options }
  36. }
  37. export interface Boolean extends Base<boolean> {
  38. type: 'boolean'
  39. }
  40. export function Boolean(label: string, description: string, defaultValue: boolean): Boolean {
  41. return { type: 'boolean', label, description, defaultValue }
  42. }
  43. export interface Range extends Base<number> {
  44. type: 'range'
  45. min: number
  46. max: number
  47. /** if an `integer` parse value with parseInt, otherwise use parseFloat */
  48. step: number
  49. }
  50. export function Range(label: string, description: string, defaultValue: number, min: number, max: number, step: number): Range {
  51. return { type: 'range', label, description, defaultValue, min, max, step }
  52. }
  53. export interface Text extends Base<string> {
  54. type: 'text'
  55. }
  56. export function Text(label: string, description: string, defaultValue: string = ''): Text {
  57. return { type: 'text', label, description, defaultValue }
  58. }
  59. export interface Color extends Base<ColorData> {
  60. type: 'color'
  61. }
  62. export function Color(label: string, description: string, defaultValue: ColorData): Color {
  63. return { type: 'color', label, description, defaultValue }
  64. }
  65. export interface Numeric extends Base<number> {
  66. type: 'number'
  67. min: number
  68. max: number
  69. /** if an `integer` parse value with parseInt, otherwise use parseFloat */
  70. step: number
  71. }
  72. export function Numeric(label: string, description: string, defaultValue: number, min: number, max: number, step: number): Numeric {
  73. return { type: 'number', label, description, defaultValue, min, max, step }
  74. }
  75. export interface Interval extends Base<[number, number]> {
  76. type: 'interval'
  77. }
  78. export function Interval(label: string, description: string, defaultValue: [number, number]): Interval {
  79. return { type: 'interval', label, description, defaultValue }
  80. }
  81. export interface Group<T extends { [key: string]: any }> extends Base<T> {
  82. type: 'group',
  83. params: T
  84. }
  85. export function Group<T extends { [key: string]: any }>(label: string, description: string, params: T): Group<T> {
  86. return { type: 'group', label, description, defaultValue: getDefaultValues(params), params };
  87. }
  88. export interface Mapped<T> extends Base<{ name: string, params: T }> {
  89. type: 'mapped',
  90. select: Select<string>,
  91. map(name: string): Params
  92. }
  93. export function Mapped<T>(label: string, description: string, defaultKey: string, names: [string, string][], map: Mapped<T>['map']): Mapped<T> {
  94. return {
  95. type: 'mapped',
  96. label,
  97. description,
  98. defaultValue: { name: defaultKey, params: getDefaultValues(map(defaultKey)) as any },
  99. select: Select<string>(label, description, defaultKey, names),
  100. map
  101. };
  102. }
  103. export type Any = Select<any> | MultiSelect<any> | Boolean | Range | Text | Color | Numeric | Interval | Group<any> | Mapped<any>
  104. export type Params = { [k: string]: Any }
  105. export type Values<T extends Params> = { [k in keyof T]: T[k]['defaultValue'] }
  106. export function getDefaultValues<T extends Params>(params: T) {
  107. const d: { [k: string]: any } = {}
  108. Object.keys(params).forEach(k => d[k] = params[k].defaultValue)
  109. return d as Values<T>
  110. }
  111. export function clone<P extends Params>(params: P): P {
  112. return shallowClone(params)
  113. }
  114. /**
  115. * List of [error text, pathToValue]
  116. * i.e. ['Missing Nested Id', ['group1', 'id']]
  117. */
  118. export type ParamErrors = [string, string | string[]][]
  119. export interface Provider<A = any, P = any, Ctx = any> {
  120. /** Check the parameters and return a list of errors if the are not valid. */
  121. default?(a: A, globalCtx: Ctx): P,
  122. /** Specify default control descriptors for the parameters */
  123. definition?(a: A, globalCtx: Ctx): { [K in keyof P]?: Any },
  124. /** Check the parameters and return a list of errors if the are not valid. */
  125. validate?(params: P, a: A, globalCtx: unknown): ParamErrors | undefined,
  126. /** Optional custom parameter equality. Use shallow structural equal by default. */
  127. areEqual?(oldParams: P, newParams: P): boolean
  128. }
  129. }