param-definition.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 { shallowEqual } from 'mol-util';
  9. import { Vec2 as Vec2Data, Vec3 as Vec3Data } from 'mol-math/linear-algebra';
  10. import { deepClone } from './object';
  11. export namespace ParamDefinition {
  12. export interface Info {
  13. label?: string,
  14. description?: string,
  15. isHidden?: boolean,
  16. }
  17. function setInfo<T extends Info>(param: T, info?: Info): T {
  18. if (!info) return param;
  19. if (info.description) param.description = info.description;
  20. if (info.label) param.label = info.label;
  21. if (info.isHidden) param.isHidden = info.isHidden;
  22. return param;
  23. }
  24. export interface Base<T> extends Info {
  25. isOptional?: boolean,
  26. defaultValue: T
  27. }
  28. export function makeOptional<T>(p: Base<T>): Base<T | undefined> {
  29. p.isOptional = true;
  30. return p;
  31. }
  32. export interface Value<T> extends Base<T> {
  33. type: 'value'
  34. }
  35. export function Value<T>(defaultValue: T, info?: Info): Value<T> {
  36. return setInfo<Value<T>>({ type: 'value', defaultValue }, info);
  37. }
  38. export interface Select<T extends string | number> extends Base<T> {
  39. type: 'select'
  40. /** array of (value, label) tuples */
  41. options: [T, string][]
  42. }
  43. export function Select<T extends string | number>(defaultValue: T, options: [T, string][], info?: Info): Select<T> {
  44. return setInfo<Select<T>>({ type: 'select', defaultValue, options }, info)
  45. }
  46. export interface ColorScale<T extends string> extends Base<T> {
  47. type: 'color-scale'
  48. /** array of (value, label) tuples */
  49. options: [T, string][]
  50. }
  51. export function ColorScale<T extends string>(defaultValue: T, options: [T, string][], info?: Info): ColorScale<T> {
  52. return setInfo<ColorScale<T>>({ type: 'color-scale', defaultValue, options }, info)
  53. }
  54. export interface MultiSelect<E extends string, T = E[]> extends Base<T> {
  55. type: 'multi-select'
  56. /** array of (value, label) tuples */
  57. options: [E, string][]
  58. }
  59. export function MultiSelect<E extends string, T = E[]>(defaultValue: T, options: [E, string][], info?: Info): MultiSelect<E, T> {
  60. return setInfo<MultiSelect<E, T>>({ type: 'multi-select', defaultValue, options }, info)
  61. }
  62. export interface Boolean extends Base<boolean> {
  63. type: 'boolean'
  64. }
  65. export function Boolean(defaultValue: boolean, info?: Info): Boolean {
  66. return setInfo<Boolean>({ type: 'boolean', defaultValue }, info)
  67. }
  68. export interface Text<T extends string = string> extends Base<T> {
  69. type: 'text'
  70. }
  71. export function Text<T extends string = string>(defaultValue: string = '', info?: Info): Text<T> {
  72. return setInfo<Text<T>>({ type: 'text', defaultValue: defaultValue as any }, info)
  73. }
  74. export interface Color extends Base<ColorData> {
  75. type: 'color'
  76. }
  77. export function Color(defaultValue: ColorData, info?: Info): Color {
  78. return setInfo<Color>({ type: 'color', defaultValue }, info)
  79. }
  80. export interface Vec3 extends Base<Vec3Data> {
  81. type: 'vec3'
  82. }
  83. export function Vec3(defaultValue: Vec3Data, info?: Info): Vec3 {
  84. return setInfo<Vec3>({ type: 'vec3', defaultValue }, info)
  85. }
  86. export interface FileParam extends Base<File> {
  87. type: 'file',
  88. accept?: string
  89. }
  90. export function File(info?: Info & { accept?: string }): FileParam {
  91. const ret = setInfo<FileParam>({ type: 'file', defaultValue: void 0 as any }, info);
  92. if (info && info.accept) ret.accept = info.accept;
  93. return ret;
  94. }
  95. export interface Range {
  96. /** If given treat as a range. */
  97. min?: number
  98. /** If given treat as a range. */
  99. max?: number
  100. /**
  101. * If given treat as a range.
  102. * If an `integer` parse value with parseInt, otherwise use parseFloat.
  103. */
  104. step?: number
  105. }
  106. function setRange<T extends Numeric | Interval>(p: T, range?: { min?: number, max?: number, step?: number }) {
  107. if (!range) return p;
  108. if (typeof range.min !== 'undefined') p.min = range.min;
  109. if (typeof range.max !== 'undefined') p.max = range.max;
  110. if (typeof range.step !== 'undefined') p.step = range.step;
  111. return p;
  112. }
  113. export interface Numeric extends Base<number>, Range {
  114. type: 'number'
  115. }
  116. export function Numeric(defaultValue: number, range?: { min?: number, max?: number, step?: number }, info?: Info): Numeric {
  117. return setInfo<Numeric>(setRange({ type: 'number', defaultValue }, range), info)
  118. }
  119. export interface Interval extends Base<[number, number]>, Range {
  120. type: 'interval'
  121. }
  122. export function Interval(defaultValue: [number, number], range?: { min?: number, max?: number, step?: number }, info?: Info): Interval {
  123. return setInfo<Interval>(setRange({ type: 'interval', defaultValue }, range), info)
  124. }
  125. export interface LineGraph extends Base<Vec2Data[]> {
  126. type: 'line-graph'
  127. }
  128. export function LineGraph(defaultValue: Vec2Data[], info?: Info): LineGraph {
  129. return setInfo<LineGraph>({ type: 'line-graph', defaultValue }, info)
  130. }
  131. export interface Group<T> extends Base<T> {
  132. type: 'group',
  133. params: Params,
  134. isExpanded?: boolean,
  135. isFlat?: boolean
  136. }
  137. export function Group<P extends Params>(params: P, info?: Info & { isExpanded?: boolean, isFlat?: boolean }): Group<Values<P>> {
  138. const ret = setInfo<Group<Values<P>>>({ type: 'group', defaultValue: getDefaultValues(params) as any, params }, info);
  139. if (info && info.isExpanded) ret.isExpanded = info.isExpanded;
  140. if (info && info.isFlat) ret.isFlat = info.isFlat;
  141. return ret;
  142. }
  143. export interface NamedParams<T = any, K = string> { name: K, params: T }
  144. export type NamedParamUnion<P extends Params, K = keyof P> = K extends any ? NamedParams<P[K]['defaultValue'], K> : never
  145. export interface Mapped<T extends NamedParams<any, any>> extends Base<T> {
  146. type: 'mapped',
  147. select: Select<string>,
  148. map(name: string): Any
  149. }
  150. export function Mapped<T>(defaultKey: string, names: [string, string][], map: (name: string) => Any, info?: Info): Mapped<NamedParams<T>> {
  151. return setInfo<Mapped<NamedParams<T>>>({
  152. type: 'mapped',
  153. defaultValue: { name: defaultKey, params: map(defaultKey).defaultValue as any },
  154. select: Select<string>(defaultKey, names, info),
  155. map
  156. }, info);
  157. }
  158. export function MappedStatic<C extends Params>(defaultKey: keyof C, map: C, info?: Info & { options?: [keyof C, string][] }): Mapped<NamedParamUnion<C>> {
  159. const options: [string, string][] = info && info.options
  160. ? info.options as [string, string][]
  161. : Object.keys(map).map(k => [k, k]) as [string, string][];
  162. return setInfo<Mapped<NamedParamUnion<C>>>({
  163. type: 'mapped',
  164. defaultValue: { name: defaultKey, params: map[defaultKey].defaultValue } as any,
  165. select: Select<string>(defaultKey as string, options, info),
  166. map: key => map[key]
  167. }, info);
  168. }
  169. export interface Converted<T, C> extends Base<T> {
  170. type: 'converted',
  171. converted: Any,
  172. /** converts from prop value to display value */
  173. fromValue(v: T): C,
  174. /** converts from display value to prop value */
  175. toValue(v: C): T
  176. }
  177. export function Converted<T, C extends Any>(fromValue: (v: T) => C['defaultValue'], toValue: (v: C['defaultValue']) => T, converted: C): Converted<T, C['defaultValue']> {
  178. return { type: 'converted', defaultValue: toValue(converted.defaultValue), converted, fromValue, toValue };
  179. }
  180. export interface Conditioned<T, P extends Base<T>, C = { [k: string]: P }> extends Base<T> {
  181. type: 'conditioned',
  182. select: Select<string>,
  183. conditionParams: C
  184. conditionForValue(v: T): keyof C
  185. conditionedValue(v: T, condition: keyof C): T,
  186. }
  187. export function Conditioned<T, P extends Base<T>, C = { [k: string]: P }>(defaultValue: T, conditionParams: C, conditionForValue: (v: T) => keyof C, conditionedValue: (v: T, condition: keyof C) => T): Conditioned<T, P, C> {
  188. const options = Object.keys(conditionParams).map(k => [k, k]) as [string, string][];
  189. return { type: 'conditioned', select: Select<string>(conditionForValue(defaultValue) as string, options), defaultValue, conditionParams, conditionForValue, conditionedValue };
  190. }
  191. export type Any = Value<any> | Select<any> | MultiSelect<any> | Boolean | Text | Color | Vec3 | Numeric | FileParam | Interval | LineGraph | ColorScale<any> | Group<any> | Mapped<any> | Converted<any, any> | Conditioned<any, any, any>
  192. export type Params = { [k: string]: Any }
  193. export type Values<T extends Params> = { [k in keyof T]: T[k]['defaultValue'] }
  194. type Optionals<P> = { [K in keyof P]-?: undefined extends P[K] ? K : never }[keyof P]
  195. type NonOptionals<P> = { [K in keyof P]-?: undefined extends P[K] ? never: K }[keyof P]
  196. export type Normalize<P> = Pick<P, NonOptionals<P>> & Partial<Pick<P, Optionals<P>>>
  197. export type For<P> = { [K in keyof P]-?: Base<P[K]> }
  198. export function getDefaultValues<T extends Params>(params: T) {
  199. const d: { [k: string]: any } = {}
  200. for (const k of Object.keys(params)) {
  201. if (params[k].isOptional) continue;
  202. d[k] = params[k].defaultValue;
  203. }
  204. return d as Values<T>;
  205. }
  206. export function clone<P extends Params>(params: P): P {
  207. return deepClone(params)
  208. }
  209. /**
  210. * List of [error text, pathToValue]
  211. * i.e. ['Missing Nested Id', ['group1', 'id']]
  212. */
  213. export type ParamErrors = [string, string | string[]][]
  214. export function validate(params: Params, values: any): ParamErrors | undefined {
  215. // TODO
  216. return void 0;
  217. }
  218. export function areEqual(params: Params, a: any, b: any): boolean {
  219. if (a === b) return true;
  220. if (!a) return !b;
  221. if (!b) return !a;
  222. if (typeof a !== 'object' || typeof b !== 'object') return false;
  223. for (const k of Object.keys(params)) {
  224. if (!isParamEqual(params[k], a[k], b[k])) return false;
  225. }
  226. return true;
  227. }
  228. function isParamEqual(p: Any, a: any, b: any): boolean {
  229. if (a === b) return true;
  230. if (!a) return !b;
  231. if (!b) return !a;
  232. if (p.type === 'group') {
  233. return areEqual(p.params, a, b);
  234. } else if (p.type === 'mapped') {
  235. const u = a as NamedParams, v = b as NamedParams;
  236. if (!u) return !v;
  237. if (!u || !v) return false;
  238. if (u.name !== v.name) return false;
  239. const map = p.map(u.name);
  240. return isParamEqual(map, u.params, v.params);
  241. } else if (p.type === 'multi-select') {
  242. const u = a as MultiSelect<any>['defaultValue'], v = b as MultiSelect<any>['defaultValue'];
  243. if (u.length !== v.length) return false;
  244. if (u.length < 10) {
  245. for (let i = 0, _i = u.length; i < _i; i++) {
  246. if (u[i] === v[i]) continue;
  247. if (v.indexOf(u[i]) < 0) return false;
  248. }
  249. } else {
  250. // TODO: should the value of multiselect be a set?
  251. const vSet = new Set(v);
  252. for (let i = 0, _i = u.length; i < _i; i++) {
  253. if (u[i] === v[i]) continue;
  254. if (!vSet.has(u[i])) return false;
  255. }
  256. }
  257. return true;
  258. } else if (p.type === 'interval') {
  259. return a[0] === b[0] && a[1] === b[1];
  260. } else if (p.type === 'line-graph') {
  261. const u = a as LineGraph['defaultValue'], v = b as LineGraph['defaultValue'];
  262. if (u.length !== v.length) return false;
  263. for (let i = 0, _i = u.length; i < _i; i++) {
  264. if (!Vec2Data.areEqual(u[i], v[i])) return false;
  265. }
  266. return true;
  267. } else if (p.type === 'vec3') {
  268. return Vec3Data.equals(a, b);
  269. } else if (typeof a === 'object' && typeof b === 'object') {
  270. return shallowEqual(a, b);
  271. }
  272. // a === b was checked at the top.
  273. return false;
  274. }
  275. }