context.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. import { ajaxGet } from 'mol-util/data-source';
  26. import { CustomPropertyRegistry } from './util/custom-prop-registry';
  27. import { VolumeRepresentationRegistry } from 'mol-repr/volume/registry';
  28. import { PLUGIN_VERSION } from './version';
  29. export class PluginContext {
  30. private disposed = false;
  31. private ev = RxEventHelper.create();
  32. private tasks = new TaskManager();
  33. readonly state = new PluginState(this);
  34. readonly commands = new PluginCommand.Manager();
  35. readonly events = {
  36. state: {
  37. cell: {
  38. stateUpdated: merge(this.state.dataState.events.cell.stateUpdated, this.state.behaviorState.events.cell.stateUpdated),
  39. created: merge(this.state.dataState.events.cell.created, this.state.behaviorState.events.cell.created),
  40. removed: merge(this.state.dataState.events.cell.removed, this.state.behaviorState.events.cell.removed),
  41. },
  42. object: {
  43. created: merge(this.state.dataState.events.object.created, this.state.behaviorState.events.object.created),
  44. removed: merge(this.state.dataState.events.object.removed, this.state.behaviorState.events.object.removed),
  45. updated: merge(this.state.dataState.events.object.updated, this.state.behaviorState.events.object.updated)
  46. },
  47. cameraSnapshots: this.state.cameraSnapshots.events,
  48. snapshots: this.state.snapshots.events,
  49. },
  50. log: this.ev<LogEntry>(),
  51. task: this.tasks.events,
  52. labels: {
  53. highlight: this.ev<{ entries: ReadonlyArray<LociLabelEntry> }>()
  54. },
  55. canvad3d: {
  56. settingsUpdated: this.ev()
  57. }
  58. };
  59. readonly behaviors = {
  60. canvas: {
  61. highlightLoci: this.ev.behavior<{ loci: Loci, repr?: Representation.Any }>({ loci: EmptyLoci }),
  62. selectLoci: this.ev.behavior<{ loci: Loci, repr?: Representation.Any }>({ loci: EmptyLoci }),
  63. },
  64. command: this.commands.behaviour
  65. };
  66. readonly canvas3d: Canvas3D;
  67. readonly lociLabels: LociLabelManager;
  68. readonly structureRepresentation = {
  69. registry: new StructureRepresentationRegistry(),
  70. themeCtx: { colorThemeRegistry: ColorTheme.createRegistry(), sizeThemeRegistry: SizeTheme.createRegistry() } as ThemeRegistryContext
  71. }
  72. readonly volumeRepresentation = {
  73. registry: new VolumeRepresentationRegistry(),
  74. themeCtx: { colorThemeRegistry: ColorTheme.createRegistry(), sizeThemeRegistry: SizeTheme.createRegistry() } as ThemeRegistryContext
  75. }
  76. readonly customModelProperties = new CustomPropertyRegistry();
  77. initViewer(canvas: HTMLCanvasElement, container: HTMLDivElement) {
  78. try {
  79. (this.canvas3d as Canvas3D) = Canvas3D.create(canvas, container);
  80. PluginCommands.Canvas3D.SetSettings.dispatch(this, { settings: { backgroundColor: Color(0xFCFBF9) } });
  81. this.canvas3d.animate();
  82. return true;
  83. } catch (e) {
  84. this.log.error('' + e);
  85. console.error(e);
  86. return false;
  87. }
  88. }
  89. readonly log = {
  90. entry: (e: LogEntry) => this.events.log.next(e),
  91. error: (msg: string) => this.events.log.next(LogEntry.error(msg)),
  92. message: (msg: string) => this.events.log.next(LogEntry.message(msg)),
  93. info: (msg: string) => this.events.log.next(LogEntry.info(msg)),
  94. warn: (msg: string) => this.events.log.next(LogEntry.warning(msg)),
  95. };
  96. /**
  97. * This should be used in all transform related request so that it could be "spoofed" to allow
  98. * "static" access to resources.
  99. */
  100. fetch(url: string, type: 'string' | 'binary' = 'string', body?: string): Task<string | Uint8Array> {
  101. return ajaxGet({ url, type, body });
  102. // const req = await fetch(url, { referrerPolicy: 'origin-when-cross-origin' });
  103. // return type === 'string' ? await req.text() : new Uint8Array(await req.arrayBuffer());
  104. }
  105. runTask<T>(task: Task<T>) {
  106. return this.tasks.run(task);
  107. }
  108. dispose() {
  109. if (this.disposed) return;
  110. this.commands.dispose();
  111. this.canvas3d.dispose();
  112. this.ev.dispose();
  113. this.state.dispose();
  114. this.tasks.dispose();
  115. this.disposed = true;
  116. }
  117. private initBuiltInBehavior() {
  118. BuiltInPluginBehaviors.State.registerDefault(this);
  119. BuiltInPluginBehaviors.Representation.registerDefault(this);
  120. BuiltInPluginBehaviors.Camera.registerDefault(this);
  121. BuiltInPluginBehaviors.Misc.registerDefault(this);
  122. merge(this.state.dataState.events.log, this.state.behaviorState.events.log).subscribe(e => this.events.log.next(e));
  123. }
  124. async initBehaviors() {
  125. const tree = this.state.behaviorState.tree.build();
  126. for (const b of this.spec.behaviors) {
  127. tree.toRoot().apply(b.transformer, b.defaultParams, { ref: b.transformer.id });
  128. }
  129. await this.runTask(this.state.behaviorState.update(tree, true));
  130. }
  131. initDataActions() {
  132. for (const a of this.spec.actions) {
  133. this.state.dataState.actions.add(a.action);
  134. }
  135. }
  136. applyTransform(state: State, a: Transform.Ref, transformer: Transformer, params: any) {
  137. const tree = state.tree.build().to(a).apply(transformer, params);
  138. return PluginCommands.State.Update.dispatch(this, { state, tree });
  139. }
  140. updateTransform(state: State, a: Transform.Ref, params: any) {
  141. const tree = state.build().to(a).update(params);
  142. return PluginCommands.State.Update.dispatch(this, { state, tree });
  143. }
  144. constructor(public spec: PluginSpec) {
  145. this.initBuiltInBehavior();
  146. this.initBehaviors();
  147. this.initDataActions();
  148. this.lociLabels = new LociLabelManager(this);
  149. // TODO: find a better solution for this.
  150. setTimeout(() => this.log.message(`Mol* Plugin ${PLUGIN_VERSION}`), 500);
  151. }
  152. // settings = ;
  153. }