param-definition.ts 13 KB

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