index.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 } 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. const DefaultSpec: PluginSpec = {
  21. actions: [
  22. PluginSpec.Action(DownloadStructure),
  23. PluginSpec.Action(OpenStructure),
  24. PluginSpec.Action(CreateComplexRepresentation),
  25. PluginSpec.Action(StateTransforms.Data.Download),
  26. PluginSpec.Action(StateTransforms.Data.ParseCif),
  27. PluginSpec.Action(StateTransforms.Model.StructureAssemblyFromModel),
  28. PluginSpec.Action(StateTransforms.Model.StructureFromModel),
  29. PluginSpec.Action(StateTransforms.Model.ModelFromTrajectory),
  30. PluginSpec.Action(StateTransforms.Representation.StructureRepresentation3D)
  31. ],
  32. behaviors: [
  33. PluginSpec.Behavior(PluginBehaviors.Representation.HighlightLoci),
  34. PluginSpec.Behavior(PluginBehaviors.Representation.SelectLoci),
  35. PluginSpec.Behavior(PluginBehaviors.Representation.DefaultLociLabelProvider),
  36. PluginSpec.Behavior(PluginBehaviors.Camera.FocusLociOnSelect, { minRadius: 20, extraRadius: 4 }),
  37. PluginSpec.Behavior(PluginBehaviors.Animation.StructureAnimation, { rotate: false, rotateValue: 0, explode: false, explodeValue: 0 }),
  38. PluginSpec.Behavior(PluginBehaviors.CustomProps.PDBeStructureQualityReport, { autoAttach: true }),
  39. PluginSpec.Behavior(PluginBehaviors.CustomProps.RCSBAssemblySymmetry, { autoAttach: true }),
  40. ]
  41. }
  42. export function createPlugin(target: HTMLElement): PluginContext {
  43. const ctx = new PluginContext(DefaultSpec);
  44. ReactDOM.render(React.createElement(Plugin, { plugin: ctx }), target);
  45. trySetSnapshot(ctx);
  46. return ctx;
  47. }
  48. async function trySetSnapshot(ctx: PluginContext) {
  49. try {
  50. const snapshotUrl = getParam('snapshot-url', `[^&]+`);
  51. if (!snapshotUrl) return;
  52. await PluginCommands.State.Snapshots.Fetch.dispatch(ctx, { url: snapshotUrl })
  53. } catch (e) {
  54. ctx.log.error('Failed to load snapshot.');
  55. console.warn('Failed to load snapshot', e);
  56. }
  57. }