index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 { DownloadStructure, CreateComplexRepresentation, OpenStructure, OpenVolume, DownloadDensity } from './state/actions/basic';
  14. import { StateTransforms } from './state/transforms';
  15. import { PluginBehaviors } from './behavior';
  16. function getParam(name: string, regex: string): string {
  17. let r = new RegExp(`${name}=(${regex})[&]?`, 'i');
  18. return decodeURIComponent(((window.location.search || '').match(r) || [])[1] || '');
  19. }
  20. export const DefaultPluginSpec: PluginSpec = {
  21. actions: [
  22. PluginSpec.Action(DownloadStructure),
  23. PluginSpec.Action(DownloadDensity),
  24. PluginSpec.Action(OpenStructure),
  25. PluginSpec.Action(OpenVolume),
  26. PluginSpec.Action(CreateComplexRepresentation),
  27. PluginSpec.Action(StateTransforms.Data.Download),
  28. PluginSpec.Action(StateTransforms.Data.ParseCif),
  29. PluginSpec.Action(StateTransforms.Data.ParseCcp4),
  30. PluginSpec.Action(StateTransforms.Model.StructureAssemblyFromModel),
  31. PluginSpec.Action(StateTransforms.Model.StructureSymmetryFromModel),
  32. PluginSpec.Action(StateTransforms.Model.StructureFromModel),
  33. PluginSpec.Action(StateTransforms.Model.ModelFromTrajectory),
  34. PluginSpec.Action(StateTransforms.Model.VolumeFromCcp4),
  35. PluginSpec.Action(StateTransforms.Representation.StructureRepresentation3D),
  36. PluginSpec.Action(StateTransforms.Representation.ExplodeStructureRepresentation3D),
  37. PluginSpec.Action(StateTransforms.Representation.VolumeRepresentation3D),
  38. ],
  39. behaviors: [
  40. PluginSpec.Behavior(PluginBehaviors.Representation.HighlightLoci),
  41. PluginSpec.Behavior(PluginBehaviors.Representation.SelectLoci),
  42. PluginSpec.Behavior(PluginBehaviors.Representation.DefaultLociLabelProvider),
  43. PluginSpec.Behavior(PluginBehaviors.Camera.FocusLociOnSelect, { minRadius: 20, extraRadius: 4 }),
  44. PluginSpec.Behavior(PluginBehaviors.Animation.StructureAnimation, { rotate: false, rotateValue: 0, explode: false, explodeValue: 0 }),
  45. PluginSpec.Behavior(PluginBehaviors.Labels.SceneLabels),
  46. PluginSpec.Behavior(PluginBehaviors.CustomProps.PDBeStructureQualityReport, { autoAttach: true }),
  47. PluginSpec.Behavior(PluginBehaviors.CustomProps.RCSBAssemblySymmetry, { autoAttach: true }),
  48. ]
  49. }
  50. export function createPlugin(target: HTMLElement, spec?: PluginSpec): PluginContext {
  51. const ctx = new PluginContext(spec || DefaultPluginSpec);
  52. ReactDOM.render(React.createElement(Plugin, { plugin: ctx }), target);
  53. trySetSnapshot(ctx);
  54. return ctx;
  55. }
  56. async function trySetSnapshot(ctx: PluginContext) {
  57. try {
  58. const snapshotUrl = getParam('snapshot-url', `[^&]+`);
  59. if (!snapshotUrl) return;
  60. await PluginCommands.State.Snapshots.Fetch.dispatch(ctx, { url: snapshotUrl })
  61. } catch (e) {
  62. ctx.log.error('Failed to load snapshot.');
  63. console.warn('Failed to load snapshot', e);
  64. }
  65. }