transform.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. (a as any)[k] = s;
  68. }
  69. for (const k of Object.keys(a)) {
  70. const s = (b as any)[k], t = (a as any)[k];
  71. if (!!s === !!t) continue;
  72. changed = true;
  73. (a as any)[k] = s;
  74. }
  75. return changed;
  76. }
  77. export interface Options {
  78. ref?: string,
  79. tags?: string | string[],
  80. state?: State,
  81. dependsOn?: Ref[]
  82. }
  83. export function create<T extends StateTransformer>(parent: Ref, transformer: T, params?: StateTransformer.Params<T>, options?: Options): Transform<T> {
  84. const ref = options && options.ref ? options.ref : UUID.create22() as string as Ref;
  85. let tags: string[] | undefined = void 0;
  86. if (options && options.tags) {
  87. tags = typeof options.tags === 'string' ? [options.tags] : options.tags;
  88. if (tags.length === 0) tags = void 0;
  89. else tags.sort();
  90. }
  91. return {
  92. parent,
  93. transformer,
  94. state: (options && options.state) || { },
  95. tags,
  96. ref,
  97. dependsOn: options && options.dependsOn,
  98. params,
  99. version: UUID.create22()
  100. }
  101. }
  102. export function withParams(t: Transform, params: any): Transform {
  103. return { ...t, params, version: UUID.create22() };
  104. }
  105. export function withState(t: Transform, state?: Partial<State>): Transform {
  106. if (!state) return t;
  107. return { ...t, state: { ...t.state, ...state } };
  108. }
  109. export function withTags(t: Transform, newTags?: string | string[]): Transform {
  110. let tags: string[] | undefined = void 0;
  111. if (newTags) {
  112. tags = typeof newTags === 'string' ? [newTags] : newTags;
  113. if (tags.length === 0) tags = void 0;
  114. else tags.sort();
  115. }
  116. return { ...t, tags, version: UUID.create22() };
  117. }
  118. export function withParent(t: Transform, parent: Ref): Transform {
  119. return { ...t, parent, version: UUID.create22() };
  120. }
  121. export function createRoot(state?: State): Transform {
  122. return create(RootRef, StateTransformer.ROOT, {}, { ref: RootRef, state });
  123. }
  124. export function hasTag(t: Transform, tag: string) {
  125. if (!t.tags) return false;
  126. return t.tags.indexOf(tag) >= 0;
  127. }
  128. export function hasTags(t: Transform, tags: string | string[]) {
  129. if (!t.tags) return typeof tags !== 'string' && tags.length === 0;
  130. if (typeof tags === 'string') return hasTag(t, tags);
  131. for (const tag of tags) {
  132. if (t.tags.indexOf(tag) < 0) return false;
  133. }
  134. return true;
  135. }
  136. export interface Serialized {
  137. parent: string,
  138. transformer: string,
  139. params: any,
  140. state?: State,
  141. tags?: string[],
  142. ref: string,
  143. dependsOn?: string[]
  144. version: string
  145. }
  146. function _id(x: any) { return x; }
  147. export function toJSON(t: Transform): Serialized {
  148. const pToJson = t.transformer.definition.customSerialization
  149. ? t.transformer.definition.customSerialization.toJSON
  150. : _id;
  151. let state: any = void 0;
  152. for (const k of Object.keys(t.state)) {
  153. const s = (t.state as any)[k];
  154. if (!s) continue;
  155. if (!state) state = { };
  156. state[k] = true;
  157. }
  158. return {
  159. parent: t.parent,
  160. transformer: t.transformer.id,
  161. params: t.params ? pToJson(t.params) : void 0,
  162. state,
  163. tags: t.tags,
  164. ref: t.ref,
  165. dependsOn: t.dependsOn,
  166. version: t.version
  167. };
  168. }
  169. export function fromJSON(t: Serialized): Transform {
  170. const transformer = StateTransformer.get(t.transformer);
  171. const pFromJson = transformer.definition.customSerialization
  172. ? transformer.definition.customSerialization.toJSON
  173. : _id;
  174. return {
  175. parent: t.parent as Ref,
  176. transformer,
  177. params: t.params ? pFromJson(t.params) : void 0,
  178. state: t.state || { },
  179. tags: t.tags,
  180. ref: t.ref as Ref,
  181. dependsOn: t.dependsOn,
  182. version: t.version
  183. };
  184. }
  185. }