context.ts 5.4 KB

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