entity.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. */
  6. import { readFileAs, readUrlAs } from 'mol-util/read'
  7. import { idFactory } from 'mol-util/id-factory'
  8. import { StateContext } from './context';
  9. import { getFileInfo } from 'mol-util/file-info';
  10. import { CifFile, CifFrame } from 'mol-io/reader/cif';
  11. import { mmCIF_Database } from 'mol-io/reader/cif/schema/mmcif';
  12. import { Model, Structure } from 'mol-model/structure';
  13. import { StructureRepresentation } from 'mol-geo/representation/structure';
  14. import { SpacefillProps } from 'mol-geo/representation/structure/spacefill';
  15. import { BallAndStickProps } from 'mol-geo/representation/structure/ball-and-stick';
  16. import { DistanceRestraintProps } from 'mol-geo/representation/structure/distance-restraint';
  17. import { CartoonProps } from 'mol-geo/representation/structure/cartoon';
  18. import { BackboneProps } from 'mol-geo/representation/structure/backbone';
  19. import { CarbohydrateProps } from 'mol-geo/representation/structure/carbohydrate';
  20. const getNextId = idFactory(1)
  21. export interface StateEntity<T, K extends string> {
  22. id: number
  23. kind: K
  24. value: T
  25. }
  26. export namespace StateEntity {
  27. export function create<T, K extends string>(ctx: StateContext, kind: K, value: T): StateEntity<T, K> {
  28. const entity = { id: getNextId(), kind, value }
  29. ctx.entities.add(entity)
  30. ctx.change.next(ctx.change.getValue() + 1)
  31. return entity
  32. }
  33. }
  34. export type AnyEntity = StateEntity<any, any>
  35. export type NullEntity = StateEntity<null, 'null'>
  36. export const NullEntity: NullEntity = { id: -1, kind: 'null', value: null }
  37. export const RootEntity: StateEntity<null, 'root'> = { id: 0, kind: 'root', value: null }
  38. export interface UrlProps {
  39. url: string
  40. name: string
  41. type: string
  42. getData: () => Promise<string | Uint8Array>
  43. }
  44. export type UrlEntity = StateEntity<UrlProps, 'url'>
  45. export namespace UrlEntity {
  46. export function ofUrl(ctx: StateContext, url: string, isBinary?: boolean): UrlEntity {
  47. const { name, ext: type, compressed, binary } = getFileInfo(url)
  48. return StateEntity.create(ctx, 'url', {
  49. url, name, type,
  50. getData: () => readUrlAs(url, isBinary || !!compressed || binary)
  51. })
  52. }
  53. }
  54. export interface FileProps {
  55. name: string
  56. type: string
  57. getData: () => Promise<string | Uint8Array>
  58. }
  59. export type FileEntity = StateEntity<FileProps, 'file'>
  60. export namespace FileEntity {
  61. export function ofFile(ctx: StateContext, file: File, isBinary?: boolean): FileEntity {
  62. const { name, ext: type, compressed, binary } = getFileInfo(file)
  63. return StateEntity.create(ctx, 'file', {
  64. name, type,
  65. getData: () => readFileAs(file, isBinary || !!compressed || binary)
  66. })
  67. }
  68. }
  69. export interface DataProps {
  70. type: string
  71. data: string | Uint8Array
  72. }
  73. export type DataEntity = StateEntity<DataProps, 'data'>
  74. export namespace DataEntity {
  75. export function ofData<T>(ctx: StateContext, data: string | Uint8Array, type: string): DataEntity {
  76. return StateEntity.create(ctx, 'data', {
  77. type,
  78. data
  79. })
  80. }
  81. }
  82. export type CifEntity = StateEntity<CifFile, 'cif'>
  83. export namespace CifEntity {
  84. export function ofCifFile(ctx: StateContext, file: CifFile): CifEntity {
  85. return StateEntity.create(ctx, 'cif', file)
  86. }
  87. }
  88. export type MmcifEntity = StateEntity<{ db: mmCIF_Database, frame: CifFrame }, 'mmcif'>
  89. export namespace MmcifEntity {
  90. export function ofMmcifDb(ctx: StateContext, mmCif: { db: mmCIF_Database, frame: CifFrame }): MmcifEntity {
  91. return StateEntity.create(ctx, 'mmcif', mmCif)
  92. }
  93. }
  94. export type ModelEntity = StateEntity<ReadonlyArray<Model>, 'model'>
  95. export namespace ModelEntity {
  96. export function ofModels(ctx: StateContext, models: ReadonlyArray<Model>): ModelEntity {
  97. return StateEntity.create(ctx, 'model', models)
  98. }
  99. }
  100. export type StructureEntity = StateEntity<Structure, 'structure'>
  101. export namespace StructureEntity {
  102. export function ofStructure(ctx: StateContext, structure: Structure): StructureEntity {
  103. return StateEntity.create(ctx, 'structure', structure)
  104. }
  105. }
  106. export type SpacefillEntity = StateEntity<StructureRepresentation<SpacefillProps>, 'spacefill'>
  107. export namespace SpacefillEntity {
  108. export function ofRepr(ctx: StateContext, repr: StructureRepresentation<SpacefillProps>): SpacefillEntity {
  109. return StateEntity.create(ctx, 'spacefill', repr )
  110. }
  111. }
  112. export type BallAndStickEntity = StateEntity<StructureRepresentation<BallAndStickProps>, 'ballandstick'>
  113. export namespace BallAndStickEntity {
  114. export function ofRepr(ctx: StateContext, repr: StructureRepresentation<BallAndStickProps>): BallAndStickEntity {
  115. return StateEntity.create(ctx, 'ballandstick', repr )
  116. }
  117. }
  118. export type DistanceRestraintEntity = StateEntity<StructureRepresentation<DistanceRestraintProps>, 'distancerestraint'>
  119. export namespace DistanceRestraintEntity {
  120. export function ofRepr(ctx: StateContext, repr: StructureRepresentation<DistanceRestraintProps>): DistanceRestraintEntity {
  121. return StateEntity.create(ctx, 'distancerestraint', repr )
  122. }
  123. }
  124. export type BackboneEntity = StateEntity<StructureRepresentation<BackboneProps>, 'backbone'>
  125. export namespace BackboneEntity {
  126. export function ofRepr(ctx: StateContext, repr: StructureRepresentation<BackboneProps>): BackboneEntity {
  127. return StateEntity.create(ctx, 'backbone', repr )
  128. }
  129. }
  130. export type CartoonEntity = StateEntity<StructureRepresentation<CartoonProps>, 'cartoon'>
  131. export namespace CartoonEntity {
  132. export function ofRepr(ctx: StateContext, repr: StructureRepresentation<CartoonProps>): CartoonEntity {
  133. return StateEntity.create(ctx, 'cartoon', repr )
  134. }
  135. }
  136. export type CarbohydrateEntity = StateEntity<StructureRepresentation<CarbohydrateProps>, 'carbohydrate'>
  137. export namespace CarbohydrateEntity {
  138. export function ofRepr(ctx: StateContext, repr: StructureRepresentation<CarbohydrateProps>): CarbohydrateEntity {
  139. return StateEntity.create(ctx, 'carbohydrate', repr )
  140. }
  141. }