context.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 { Canvas3D } from 'mol-canvas3d/canvas3d';
  7. import { EmptyLoci, Loci } from 'mol-model/loci';
  8. import { Representation } from 'mol-repr/representation';
  9. import { StructureRepresentationRegistry } from 'mol-repr/structure/registry';
  10. import { State, Transform, Transformer } from 'mol-state';
  11. import { Task } from 'mol-task';
  12. import { ColorTheme } from 'mol-theme/color';
  13. import { SizeTheme } from 'mol-theme/size';
  14. import { ThemeRegistryContext } from 'mol-theme/theme';
  15. import { LogEntry } from 'mol-util/log-entry';
  16. import { RxEventHelper } from 'mol-util/rx-event-helper';
  17. import { merge } from 'rxjs';
  18. import { BuiltInPluginBehaviors } from './behavior';
  19. import { PluginCommand, PluginCommands } from './command';
  20. import { PluginSpec } from './spec';
  21. import { PluginState } from './state';
  22. import { TaskManager } from './util/task-manager';
  23. import { Color } from 'mol-util/color';
  24. import { LociLabelEntry, LociLabelManager } from './util/loci-label-manager';
  25. export class PluginContext {
  26. private disposed = false;
  27. private ev = RxEventHelper.create();
  28. private tasks = new TaskManager();
  29. readonly state = new PluginState(this);
  30. readonly commands = new PluginCommand.Manager();
  31. readonly events = {
  32. state: {
  33. cell: {
  34. stateUpdated: merge(this.state.dataState.events.cell.stateUpdated, this.state.behaviorState.events.cell.stateUpdated),
  35. created: merge(this.state.dataState.events.cell.created, this.state.behaviorState.events.cell.created),
  36. removed: merge(this.state.dataState.events.cell.removed, this.state.behaviorState.events.cell.removed),
  37. },
  38. object: {
  39. created: merge(this.state.dataState.events.object.created, this.state.behaviorState.events.object.created),
  40. removed: merge(this.state.dataState.events.object.removed, this.state.behaviorState.events.object.removed),
  41. updated: merge(this.state.dataState.events.object.updated, this.state.behaviorState.events.object.updated)
  42. },
  43. cameraSnapshots: this.state.cameraSnapshots.events,
  44. snapshots: this.state.snapshots.events,
  45. },
  46. log: this.ev<LogEntry>(),
  47. task: this.tasks.events,
  48. labels: {
  49. highlight: this.ev<{ entries: ReadonlyArray<LociLabelEntry> }>()
  50. }
  51. };
  52. readonly lociLabels: LociLabelManager;
  53. readonly structureReprensentation = {
  54. registry: new StructureRepresentationRegistry(),
  55. themeCtx: { colorThemeRegistry: new ColorTheme.Registry(), sizeThemeRegistry: new SizeTheme.Registry() } as ThemeRegistryContext
  56. }
  57. readonly behaviors = {
  58. canvas: {
  59. highlightLoci: this.ev.behavior<{ loci: Loci, repr?: Representation.Any }>({ loci: EmptyLoci }),
  60. selectLoci: this.ev.behavior<{ loci: Loci, repr?: Representation.Any }>({ loci: EmptyLoci }),
  61. },
  62. command: this.commands.behaviour
  63. };
  64. readonly canvas3d: Canvas3D;
  65. initViewer(canvas: HTMLCanvasElement, container: HTMLDivElement) {
  66. try {
  67. (this.canvas3d as Canvas3D) = Canvas3D.create(canvas, container);
  68. this.canvas3d.setProps({ backgroundColor: Color(0xFCFBF9) });
  69. this.canvas3d.animate();
  70. return true;
  71. } catch (e) {
  72. this.log(LogEntry.error('' + e));
  73. console.error(e);
  74. return false;
  75. }
  76. }
  77. log(e: LogEntry) {
  78. this.events.log.next(e);
  79. }
  80. /**
  81. * This should be used in all transform related request so that it could be "spoofed" to allow
  82. * "static" access to resources.
  83. */
  84. async fetch(url: string, type: 'string' | 'binary' = 'string'): Promise<string | Uint8Array> {
  85. const req = await fetch(url, { referrerPolicy: 'origin-when-cross-origin' });
  86. return type === 'string' ? await req.text() : new Uint8Array(await req.arrayBuffer());
  87. }
  88. runTask<T>(task: Task<T>) {
  89. return this.tasks.run(task);
  90. }
  91. dispose() {
  92. if (this.disposed) return;
  93. this.commands.dispose();
  94. this.canvas3d.dispose();
  95. this.ev.dispose();
  96. this.state.dispose();
  97. this.tasks.dispose();
  98. this.disposed = true;
  99. }
  100. private initBuiltInBehavior() {
  101. BuiltInPluginBehaviors.State.registerDefault(this);
  102. BuiltInPluginBehaviors.Representation.registerDefault(this);
  103. BuiltInPluginBehaviors.Camera.registerDefault(this);
  104. merge(this.state.dataState.events.log, this.state.behaviorState.events.log).subscribe(e => this.events.log.next(e));
  105. }
  106. async initBehaviors() {
  107. const tree = this.state.behaviorState.tree.build();
  108. for (const b of this.spec.behaviors) {
  109. tree.toRoot().apply(b.transformer, b.defaultParams || { }, { ref: b.transformer.id });
  110. }
  111. await this.runTask(this.state.behaviorState.update(tree));
  112. }
  113. initDataActions() {
  114. for (const a of this.spec.actions) {
  115. this.state.dataState.actions.add(a.action);
  116. }
  117. }
  118. applyTransform(state: State, a: Transform.Ref, transformer: Transformer, params: any) {
  119. const tree = state.tree.build().to(a).apply(transformer, params);
  120. return PluginCommands.State.Update.dispatch(this, { state, tree });
  121. }
  122. updateTransform(state: State, a: Transform.Ref, params: any) {
  123. const tree = state.build().to(a).update(params);
  124. return PluginCommands.State.Update.dispatch(this, { state, tree });
  125. }
  126. constructor(public spec: PluginSpec) {
  127. this.initBuiltInBehavior();
  128. this.initBehaviors();
  129. this.initDataActions();
  130. this.lociLabels = new LociLabelManager(this);
  131. }
  132. // settings = ;
  133. }