objects.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 { CifFile } from 'mol-io/reader/cif';
  7. import { Model as _Model, Structure as _Structure } from 'mol-model/structure';
  8. import { VolumeData } from 'mol-model/volume';
  9. import { PluginBehavior } from 'mol-plugin/behavior/behavior';
  10. import { Representation } from 'mol-repr/representation';
  11. import { StructureRepresentation } from 'mol-repr/structure/representation';
  12. import { VolumeRepresentation } from 'mol-repr/volume/representation';
  13. import { StateObject, Transformer } from 'mol-state';
  14. export type TypeClass = 'root' | 'data' | 'prop'
  15. export namespace PluginStateObject {
  16. export type Any = StateObject<any, TypeInfo>
  17. export type TypeClass = 'Root' | 'Group' | 'Data' | 'Object' | 'Representation3D' | 'Behavior'
  18. export interface TypeInfo { name: string, typeClass: TypeClass }
  19. export const Create = StateObject.factory<TypeInfo>();
  20. export function isRepresentation3D(o?: Any): o is StateObject<Representation.Any, TypeInfo> {
  21. return !!o && o.type.typeClass === 'Representation3D';
  22. }
  23. export function isBehavior(o?: Any): o is StateObject<PluginBehavior, TypeInfo> {
  24. return !!o && o.type.typeClass === 'Behavior';
  25. }
  26. export function CreateRepresentation3D<T extends Representation.Any>(type: { name: string }) {
  27. return Create<T>({ ...type, typeClass: 'Representation3D' })
  28. }
  29. export function CreateBehavior<T extends PluginBehavior>(type: { name: string }) {
  30. return Create<T>({ ...type, typeClass: 'Behavior' })
  31. }
  32. export class Root extends Create({ name: 'Root', typeClass: 'Root' }) { }
  33. export class Group extends Create({ name: 'Group', typeClass: 'Group' }) { }
  34. export namespace Data {
  35. export class String extends Create<string>({ name: 'String Data', typeClass: 'Data', }) { }
  36. export class Binary extends Create<Uint8Array>({ name: 'Binary Data', typeClass: 'Data' }) { }
  37. // TODO
  38. // export class MultipleRaw extends Create<{
  39. // [key: string]: { type: 'String' | 'Binary', data: string | Uint8Array }
  40. // }>({ name: 'Data', typeClass: 'Data', shortName: 'MD', description: 'Multiple Keyed Data.' }) { }
  41. }
  42. export namespace Format {
  43. export class Json extends Create<any>({ name: 'JSON Data', typeClass: 'Data' }) { }
  44. export class Cif extends Create<CifFile>({ name: 'CIF File', typeClass: 'Data' }) { }
  45. }
  46. export namespace Molecule {
  47. export class Trajectory extends Create<ReadonlyArray<_Model>>({ name: 'Trajectory', typeClass: 'Object' }) { }
  48. export class Model extends Create<_Model>({ name: 'Model', typeClass: 'Object' }) { }
  49. export class Structure extends Create<_Structure>({ name: 'Structure', typeClass: 'Object' }) { }
  50. export class Representation3D extends CreateRepresentation3D<StructureRepresentation<any>>({ name: 'Structure 3D' }) { }
  51. }
  52. export namespace Volume {
  53. export class Data extends Create<VolumeData>({ name: 'Volume Data', typeClass: 'Object' }) { }
  54. export class Representation3D extends CreateRepresentation3D<VolumeRepresentation<any>>({ name: 'Volume 3D' }) { }
  55. }
  56. }
  57. export namespace PluginStateTransform {
  58. export const CreateBuiltIn = Transformer.factory('ms-plugin');
  59. export const BuiltIn = Transformer.builderFactory('ms-plugin');
  60. }