index.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { PluginContext } from './context';
  8. import { Plugin } from './ui/plugin'
  9. import * as React from 'react';
  10. import * as ReactDOM from 'react-dom';
  11. import { PluginCommands } from './command';
  12. import { PluginSpec } from './spec';
  13. import { StateTransforms } from './state/transforms';
  14. import { PluginBehaviors } from './behavior';
  15. import { AnimateModelIndex } from './state/animation/built-in';
  16. import { StateActions } from './state/actions';
  17. function getParam(name: string, regex: string): string {
  18. let r = new RegExp(`${name}=(${regex})[&]?`, 'i');
  19. return decodeURIComponent(((window.location.search || '').match(r) || [])[1] || '');
  20. }
  21. export const DefaultPluginSpec: PluginSpec = {
  22. actions: [
  23. PluginSpec.Action(StateActions.Structure.DownloadStructure),
  24. PluginSpec.Action(StateActions.Volume.DownloadDensity),
  25. PluginSpec.Action(StateActions.DataFormat.OpenFile),
  26. PluginSpec.Action(StateActions.Structure.CreateComplexRepresentation),
  27. PluginSpec.Action(StateActions.Structure.EnableModelCustomProps),
  28. PluginSpec.Action(StateActions.Volume.InitVolumeStreaming),
  29. PluginSpec.Action(StateTransforms.Data.Download),
  30. PluginSpec.Action(StateTransforms.Data.ParseCif),
  31. PluginSpec.Action(StateTransforms.Data.ParseCcp4),
  32. PluginSpec.Action(StateTransforms.Data.ParseDsn6),
  33. PluginSpec.Action(StateTransforms.Model.TrajectoryFromMmCif),
  34. PluginSpec.Action(StateTransforms.Model.TrajectoryFromPDB),
  35. PluginSpec.Action(StateTransforms.Model.StructureAssemblyFromModel),
  36. PluginSpec.Action(StateTransforms.Model.StructureSymmetryFromModel),
  37. PluginSpec.Action(StateTransforms.Model.StructureFromModel),
  38. PluginSpec.Action(StateTransforms.Model.ModelFromTrajectory),
  39. PluginSpec.Action(StateTransforms.Volume.VolumeFromCcp4),
  40. PluginSpec.Action(StateTransforms.Representation.StructureRepresentation3D),
  41. PluginSpec.Action(StateTransforms.Representation.ExplodeStructureRepresentation3D),
  42. PluginSpec.Action(StateTransforms.Representation.VolumeRepresentation3D),
  43. ],
  44. behaviors: [
  45. PluginSpec.Behavior(PluginBehaviors.Representation.HighlightLoci),
  46. PluginSpec.Behavior(PluginBehaviors.Representation.SelectLoci),
  47. PluginSpec.Behavior(PluginBehaviors.Representation.DefaultLociLabelProvider),
  48. PluginSpec.Behavior(PluginBehaviors.Camera.FocusLociOnSelect, { minRadius: 20, extraRadius: 4 }),
  49. PluginSpec.Behavior(PluginBehaviors.Animation.StructureAnimation, { rotate: false, rotateValue: 0, explode: false, explodeValue: 0 }),
  50. PluginSpec.Behavior(PluginBehaviors.Labels.SceneLabels),
  51. PluginSpec.Behavior(PluginBehaviors.CustomProps.PDBeStructureQualityReport, { autoAttach: true }),
  52. PluginSpec.Behavior(PluginBehaviors.CustomProps.RCSBAssemblySymmetry, { autoAttach: true }),
  53. ],
  54. animations: [
  55. AnimateModelIndex
  56. ]
  57. }
  58. export function createPlugin(target: HTMLElement, spec?: PluginSpec): PluginContext {
  59. const ctx = new PluginContext(spec || DefaultPluginSpec);
  60. ReactDOM.render(React.createElement(Plugin, { plugin: ctx }), target);
  61. trySetSnapshot(ctx);
  62. return ctx;
  63. }
  64. async function trySetSnapshot(ctx: PluginContext) {
  65. try {
  66. const snapshotUrl = getParam('snapshot-url', `[^&]+`);
  67. if (!snapshotUrl) return;
  68. await PluginCommands.State.Snapshots.Fetch.dispatch(ctx, { url: snapshotUrl })
  69. } catch (e) {
  70. ctx.log.error('Failed to load snapshot.');
  71. console.warn('Failed to load snapshot', e);
  72. }
  73. }