objects.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { CifFile } from '../../mol-io/reader/cif';
  8. import { PlyFile } from '../../mol-io/reader/ply/schema';
  9. import { Model as _Model, Structure as _Structure, StructureElement } from '../../mol-model/structure';
  10. import { VolumeData } from '../../mol-model/volume';
  11. import { PluginBehavior } from '../../mol-plugin/behavior/behavior';
  12. import { Representation } from '../../mol-repr/representation';
  13. import { StructureRepresentation, StructureRepresentationState } from '../../mol-repr/structure/representation';
  14. import { VolumeRepresentation } from '../../mol-repr/volume/representation';
  15. import { StateObject, StateTransformer } from '../../mol-state';
  16. import { Ccp4File } from '../../mol-io/reader/ccp4/schema';
  17. import { Dsn6File } from '../../mol-io/reader/dsn6/schema';
  18. import { ShapeRepresentation } from '../../mol-repr/shape/representation';
  19. import { Shape as _Shape } from '../../mol-model/shape';
  20. import { ShapeProvider } from '../../mol-model/shape/provider';
  21. export type TypeClass = 'root' | 'data' | 'prop'
  22. export namespace PluginStateObject {
  23. export type Any = StateObject<any, TypeInfo>
  24. export type TypeClass = 'Root' | 'Group' | 'Data' | 'Object' | 'Representation3D' | 'Behavior'
  25. export interface TypeInfo { name: string, typeClass: TypeClass }
  26. export const Create = StateObject.factory<TypeInfo>();
  27. export function isRepresentation3D(o?: Any): o is StateObject<Representation3DData<Representation.Any>, TypeInfo> {
  28. return !!o && o.type.typeClass === 'Representation3D';
  29. }
  30. export function isBehavior(o?: Any): o is StateObject<PluginBehavior, TypeInfo> {
  31. return !!o && o.type.typeClass === 'Behavior';
  32. }
  33. export interface Representation3DData<T extends Representation.Any, S extends StateObject = StateObject> { repr: T, source: S }
  34. export function CreateRepresentation3D<T extends Representation.Any, S extends StateObject = StateObject>(type: { name: string }) {
  35. return Create<Representation3DData<T, S>>({ ...type, typeClass: 'Representation3D' });
  36. }
  37. export function CreateBehavior<T extends PluginBehavior>(type: { name: string }) {
  38. return Create<T>({ ...type, typeClass: 'Behavior' })
  39. }
  40. export class Root extends Create({ name: 'Root', typeClass: 'Root' }) { }
  41. export class Group extends Create({ name: 'Group', typeClass: 'Group' }) { }
  42. export namespace Data {
  43. export class String extends Create<string>({ name: 'String Data', typeClass: 'Data', }) { }
  44. export class Binary extends Create<Uint8Array>({ name: 'Binary Data', typeClass: 'Data' }) { }
  45. export type BlobEntry = { id: string } &
  46. ( { kind: 'string', data: string }
  47. | { kind: 'binary', data: Uint8Array })
  48. export type BlobData = BlobEntry[]
  49. export class Blob extends Create<BlobData>({ name: 'Data Blob', typeClass: 'Data' }) { }
  50. }
  51. export namespace Format {
  52. export class Json extends Create<any>({ name: 'JSON Data', typeClass: 'Data' }) { }
  53. export class Cif extends Create<CifFile>({ name: 'CIF File', typeClass: 'Data' }) { }
  54. export class Ply extends Create<PlyFile>({ name: 'PLY File', typeClass: 'Data' }) { }
  55. export class Ccp4 extends Create<Ccp4File>({ name: 'CCP4/MRC/MAP File', typeClass: 'Data' }) { }
  56. export class Dsn6 extends Create<Dsn6File>({ name: 'DSN6/BRIX File', typeClass: 'Data' }) { }
  57. export type BlobEntry = { id: string } &
  58. ( { kind: 'json', data: unknown }
  59. | { kind: 'string', data: string }
  60. | { kind: 'binary', data: Uint8Array }
  61. | { kind: 'cif', data: CifFile }
  62. | { kind: 'ccp4', data: Ccp4File }
  63. | { kind: 'dsn6', data: Dsn6File }
  64. | { kind: 'ply', data: PlyFile }
  65. // For non-build in extensions
  66. | { kind: 'custom', data: unknown, tag: string })
  67. export type BlobData = BlobEntry[]
  68. export class Blob extends Create<BlobData>({ name: 'Format Blob', typeClass: 'Data' }) { }
  69. }
  70. export namespace Molecule {
  71. export class Trajectory extends Create<ReadonlyArray<_Model>>({ name: 'Trajectory', typeClass: 'Object' }) { }
  72. export class Model extends Create<_Model>({ name: 'Model', typeClass: 'Object' }) { }
  73. export class Structure extends Create<_Structure>({ name: 'Structure', typeClass: 'Object' }) { }
  74. export namespace Structure {
  75. export class Representation3D extends CreateRepresentation3D<StructureRepresentation<any> | ShapeRepresentation<any, any, any>, Structure>({ name: 'Structure 3D' }) { }
  76. export interface Representation3DStateData {
  77. source: Representation3D,
  78. /** used to restore state when the obj is removed */
  79. initialState: Partial<StructureRepresentationState>,
  80. state: Partial<StructureRepresentationState>,
  81. info?: unknown
  82. }
  83. export class Representation3DState extends Create<Representation3DStateData>({ name: 'Structure 3D State', typeClass: 'Object' }) { }
  84. export interface SelectionEntry { key: string, groupId?: string, loci: StructureElement.Loci }
  85. export class Selections extends Create<ReadonlyArray<SelectionEntry>>({ name: 'Selections', typeClass: 'Object' }) {}
  86. }
  87. }
  88. export namespace Volume {
  89. export class Data extends Create<VolumeData>({ name: 'Volume Data', typeClass: 'Object' }) { }
  90. export class Representation3D extends CreateRepresentation3D<VolumeRepresentation<any>>({ name: 'Volume 3D' }) { }
  91. }
  92. export namespace Shape {
  93. export class Provider extends Create<ShapeProvider<any, any, any>>({ name: 'Shape Provider', typeClass: 'Object' }) { }
  94. export class Representation3D extends CreateRepresentation3D<ShapeRepresentation<any, any, any>>({ name: 'Shape 3D' }) { }
  95. }
  96. }
  97. export namespace PluginStateTransform {
  98. export const CreateBuiltIn = StateTransformer.factory('ms-plugin');
  99. export const BuiltIn = StateTransformer.builderFactory('ms-plugin');
  100. }