transform.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { StateTransformer } from './transformer';
  7. import { UUID } from '../mol-util';
  8. export { Transform as StateTransform };
  9. interface Transform<T extends StateTransformer = StateTransformer> {
  10. readonly parent: Transform.Ref,
  11. readonly transformer: T,
  12. readonly state: Transform.State,
  13. readonly tags?: string[],
  14. readonly ref: Transform.Ref,
  15. /**
  16. * Sibling-like dependency
  17. * Do NOT make a cell dependent on its ancestor.
  18. */
  19. readonly dependsOn?: Transform.Ref[],
  20. readonly params?: StateTransformer.Params<T>,
  21. readonly version: string
  22. }
  23. namespace Transform {
  24. export type Ref = string
  25. export type Transformer<T extends Transform> = T extends Transform<infer S> ? S : never
  26. export const RootRef = '-=root=-' as Ref;
  27. export interface State {
  28. // is the cell shown in the UI
  29. isGhost?: boolean,
  30. // can the corresponding be deleted by the user.
  31. isLocked?: boolean,
  32. // is the representation associated with the cell hidden
  33. isHidden?: boolean,
  34. // is the tree node collapsed?
  35. isCollapsed?: boolean
  36. }
  37. export function areStatesEqual(a: State, b: State) {
  38. return !!a.isHidden !== !!b.isHidden || !!a.isCollapsed !== !!b.isCollapsed
  39. || !!a.isGhost !== !!b.isGhost || !!a.isLocked !== !!b.isLocked;
  40. }
  41. export function isStateChange(a: State, b?: Partial<State>) {
  42. if (!b) return false;
  43. if (typeof b.isCollapsed !== 'undefined' && a.isCollapsed !== b.isCollapsed) return true;
  44. if (typeof b.isHidden !== 'undefined' && a.isHidden !== b.isHidden) return true;
  45. if (typeof b.isGhost !== 'undefined' && a.isGhost !== b.isGhost) return true;
  46. if (typeof b.isLocked !== 'undefined' && a.isLocked !== b.isLocked) return true;
  47. return false;
  48. }
  49. export function assignState(a: State, b?: Partial<State>): boolean {
  50. if (!b) return false;
  51. let changed = false;
  52. for (const k of Object.keys(b)) {
  53. const s = (b as any)[k], t = (a as any)[k];
  54. if (!!s === !!t) continue;
  55. changed = true;
  56. (a as any)[k] = s;
  57. }
  58. return changed;
  59. }
  60. export function syncState(a: State, b?: Partial<State>): boolean {
  61. if (!b) return false;
  62. let changed = false;
  63. for (const k of Object.keys(b)) {
  64. const s = (b as any)[k], t = (a as any)[k];
  65. if (!!s === !!t) continue;
  66. changed = true;
  67. if (s !== void 0) {
  68. (a as any)[k] = s;
  69. } else {
  70. delete (a as any)[k];
  71. }
  72. }
  73. for (const k of Object.keys(a)) {
  74. const s = (b as any)[k], t = (a as any)[k];
  75. if (!!s === !!t) continue;
  76. changed = true;
  77. if (s !== void 0) {
  78. (a as any)[k] = s;
  79. } else {
  80. delete (a as any)[k];
  81. }
  82. }
  83. return changed;
  84. }
  85. export interface Options {
  86. ref?: string,
  87. tags?: string | string[],
  88. state?: State,
  89. dependsOn?: Ref[]
  90. }
  91. export function create<T extends StateTransformer>(parent: Ref, transformer: T, params?: StateTransformer.Params<T>, options?: Options): Transform<T> {
  92. const ref = options && options.ref ? options.ref : UUID.create22() as string as Ref;
  93. let tags: string[] | undefined = void 0;
  94. if (options && options.tags) {
  95. tags = typeof options.tags === 'string' ? [options.tags] : options.tags;
  96. if (tags.length === 0) tags = void 0;
  97. else tags.sort();
  98. }
  99. return {
  100. parent,
  101. transformer,
  102. state: options?.state || { },
  103. tags,
  104. ref,
  105. dependsOn: options && options.dependsOn,
  106. params,
  107. version: UUID.create22()
  108. };
  109. }
  110. export function withParams(t: Transform, params: any): Transform {
  111. return { ...t, params, version: UUID.create22() };
  112. }
  113. export function withState(t: Transform, state?: Partial<State>): Transform {
  114. if (!state) return t;
  115. return { ...t, state: { ...t.state, ...state } };
  116. }
  117. export function withTags(t: Transform, newTags?: string | string[]): Transform {
  118. let tags: string[] | undefined = void 0;
  119. if (newTags) {
  120. tags = typeof newTags === 'string' ? [newTags] : newTags;
  121. if (tags.length === 0) tags = void 0;
  122. else tags.sort();
  123. }
  124. return { ...t, tags, version: UUID.create22() };
  125. }
  126. export function withParent(t: Transform, parent: Ref): Transform {
  127. return { ...t, parent, version: UUID.create22() };
  128. }
  129. export function createRoot(state?: State): Transform {
  130. return create(RootRef, StateTransformer.ROOT, {}, { ref: RootRef, state });
  131. }
  132. export function hasTag(t: Transform, tag: string) {
  133. if (!t.tags) return false;
  134. return t.tags.indexOf(tag) >= 0;
  135. }
  136. export function hasTags(t: Transform, tags: string | string[]) {
  137. if (!t.tags) return typeof tags !== 'string' && tags.length === 0;
  138. if (typeof tags === 'string') return hasTag(t, tags);
  139. for (const tag of tags) {
  140. if (t.tags.indexOf(tag) < 0) return false;
  141. }
  142. return true;
  143. }
  144. export interface Serialized {
  145. parent: string,
  146. transformer: string,
  147. params: any,
  148. state?: State,
  149. tags?: string[],
  150. isDecorator?: boolean,
  151. ref: string,
  152. dependsOn?: string[]
  153. version: string
  154. }
  155. function _id(x: any) { return x; }
  156. export function toJSON(t: Transform): Serialized {
  157. const pToJson = t.transformer.definition.customSerialization
  158. ? t.transformer.definition.customSerialization.toJSON
  159. : _id;
  160. let state: any = void 0;
  161. for (const k of Object.keys(t.state)) {
  162. const s = (t.state as any)[k];
  163. if (!s) continue;
  164. if (!state) state = { };
  165. state[k] = true;
  166. }
  167. return {
  168. parent: t.parent,
  169. transformer: t.transformer.id,
  170. params: t.params ? pToJson(t.params) : void 0,
  171. state,
  172. tags: t.tags,
  173. ref: t.ref,
  174. dependsOn: t.dependsOn,
  175. version: t.version
  176. };
  177. }
  178. export function fromJSON(t: Serialized): Transform {
  179. const transformer = StateTransformer.get(t.transformer);
  180. const pFromJson = transformer.definition.customSerialization
  181. ? transformer.definition.customSerialization.fromJSON
  182. : _id;
  183. return {
  184. parent: t.parent as Ref,
  185. transformer,
  186. params: t.params ? pFromJson(t.params) : void 0,
  187. state: t.state || { },
  188. tags: t.tags,
  189. ref: t.ref as Ref,
  190. dependsOn: t.dependsOn,
  191. version: t.version
  192. };
  193. }
  194. }