index.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright (c) 2018-2019 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 { createPlugin, DefaultPluginSpec } from '../../mol-plugin';
  8. import './index.html'
  9. import { PluginContext } from '../../mol-plugin/context';
  10. import { PluginCommands } from '../../mol-plugin/command';
  11. import { PluginSpec } from '../../mol-plugin/spec';
  12. import { CreateJoleculeState } from './extensions/jolecule';
  13. import { LoadCellPackModel } from './extensions/cellpack/model';
  14. import { StructureFromCellpack } from './extensions/cellpack/state';
  15. require('mol-plugin/skin/light.scss')
  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 hideControls = getParam('hide-controls', `[^&]+`) === '1';
  21. function init() {
  22. const spec: PluginSpec = {
  23. actions: [
  24. ...DefaultPluginSpec.actions,
  25. PluginSpec.Action(CreateJoleculeState),
  26. PluginSpec.Action(LoadCellPackModel),
  27. PluginSpec.Action(StructureFromCellpack),
  28. ],
  29. behaviors: [...DefaultPluginSpec.behaviors],
  30. animations: [...DefaultPluginSpec.animations || []],
  31. customParamEditors: DefaultPluginSpec.customParamEditors,
  32. layout: {
  33. initial: {
  34. isExpanded: true,
  35. showControls: !hideControls
  36. },
  37. controls: {
  38. ...DefaultPluginSpec.layout && DefaultPluginSpec.layout.controls
  39. }
  40. }
  41. };
  42. const plugin = createPlugin(document.getElementById('app')!, spec);
  43. trySetSnapshot(plugin);
  44. }
  45. async function trySetSnapshot(ctx: PluginContext) {
  46. try {
  47. const snapshotUrl = getParam('snapshot-url', `[^&]+`);
  48. const snapshotId = getParam('snapshot-id', `[^&]+`);
  49. if (!snapshotUrl && !snapshotId) return;
  50. // TODO parametrize the server
  51. const url = snapshotId
  52. ? `https://webchem.ncbr.muni.cz/molstar-state/get/${snapshotId}`
  53. : snapshotUrl;
  54. await PluginCommands.State.Snapshots.Fetch.dispatch(ctx, { url })
  55. } catch (e) {
  56. ctx.log.error('Failed to load snapshot.');
  57. console.warn('Failed to load snapshot', e);
  58. }
  59. }
  60. init();