index.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 { PluginContext } from './context';
  7. import { Plugin } from './ui/plugin'
  8. import * as React from 'react';
  9. import * as ReactDOM from 'react-dom';
  10. import { PluginCommands } from './command';
  11. import { PluginSpec } from './spec';
  12. import { DownloadStructure, CreateComplexRepresentation, OpenStructure } from './state/actions/basic';
  13. import { StateTransforms } from './state/transforms';
  14. import { PluginBehaviors } from './behavior';
  15. function getParam(name: string, regex: string): string {
  16. let r = new RegExp(`${name}=(${regex})[&]?`, 'i');
  17. return decodeURIComponent(((window.location.search || '').match(r) || [])[1] || '');
  18. }
  19. const DefaultSpec: PluginSpec = {
  20. actions: [
  21. PluginSpec.Action(DownloadStructure),
  22. PluginSpec.Action(OpenStructure),
  23. PluginSpec.Action(CreateComplexRepresentation),
  24. PluginSpec.Action(StateTransforms.Data.Download),
  25. PluginSpec.Action(StateTransforms.Data.ParseCif),
  26. PluginSpec.Action(StateTransforms.Model.StructureAssemblyFromModel),
  27. PluginSpec.Action(StateTransforms.Model.StructureFromModel),
  28. PluginSpec.Action(StateTransforms.Model.ModelFromTrajectory),
  29. PluginSpec.Action(StateTransforms.Representation.StructureRepresentation3D)
  30. ],
  31. behaviors: [
  32. PluginSpec.Behavior(PluginBehaviors.Representation.HighlightLoci),
  33. PluginSpec.Behavior(PluginBehaviors.Representation.SelectLoci),
  34. PluginSpec.Behavior(PluginBehaviors.Representation.DefaultLociLabelProvider),
  35. PluginSpec.Behavior(PluginBehaviors.Camera.FocusLociOnSelect, { minRadius: 20, extraRadius: 4 }),
  36. PluginSpec.Behavior(PluginBehaviors.CustomProps.PDBeStructureQualityReport, { autoAttach: true }),
  37. PluginSpec.Behavior(PluginBehaviors.CustomProps.RCSBAssemblySymmetry, { autoAttach: true }),
  38. ]
  39. }
  40. export function createPlugin(target: HTMLElement): PluginContext {
  41. const ctx = new PluginContext(DefaultSpec);
  42. ReactDOM.render(React.createElement(Plugin, { plugin: ctx }), target);
  43. trySetSnapshot(ctx);
  44. return ctx;
  45. }
  46. async function trySetSnapshot(ctx: PluginContext) {
  47. try {
  48. const snapshotUrl = getParam('snapshot-url', `[^&]+`);
  49. if (!snapshotUrl) return;
  50. await PluginCommands.State.Snapshots.Fetch.dispatch(ctx, { url: snapshotUrl })
  51. } catch (e) {
  52. ctx.log.error('Failed to load snapshot.');
  53. console.warn('Failed to load snapshot', e);
  54. }
  55. }