index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Copyright (c) 2018-2020 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 '../../mol-util/polyfill';
  8. import { createPlugin, DefaultPluginSpec } from '../../mol-plugin';
  9. import './index.html';
  10. import './favicon.ico';
  11. import { PluginContext } from '../../mol-plugin/context';
  12. import { PluginCommands } from '../../mol-plugin/commands';
  13. import { PluginSpec } from '../../mol-plugin/spec';
  14. import { DownloadStructure } from '../../mol-plugin-state/actions/structure';
  15. import { PluginConfig } from '../../mol-plugin/config';
  16. import { CellPack } from '../../extensions/cellpack';
  17. import { RCSBAssemblySymmetry, RCSBValidationReport } from '../../extensions/rcsb';
  18. require('mol-plugin-ui/skin/light.scss');
  19. function getParam(name: string, regex: string): string {
  20. let r = new RegExp(`${name}=(${regex})[&]?`, 'i');
  21. return decodeURIComponent(((window.location.search || '').match(r) || [])[1] || '');
  22. }
  23. const hideControls = getParam('hide-controls', `[^&]+`) === '1';
  24. function init() {
  25. const spec: PluginSpec = {
  26. actions: [...DefaultPluginSpec.actions],
  27. behaviors: [
  28. ...DefaultPluginSpec.behaviors,
  29. PluginSpec.Behavior(CellPack),
  30. PluginSpec.Behavior(RCSBAssemblySymmetry),
  31. PluginSpec.Behavior(RCSBValidationReport),
  32. ],
  33. animations: [...DefaultPluginSpec.animations || []],
  34. customParamEditors: DefaultPluginSpec.customParamEditors,
  35. layout: {
  36. initial: {
  37. isExpanded: true,
  38. showControls: !hideControls
  39. },
  40. controls: {
  41. ...DefaultPluginSpec.layout && DefaultPluginSpec.layout.controls
  42. }
  43. },
  44. config: DefaultPluginSpec.config
  45. };
  46. spec.config?.set(PluginConfig.Viewport.ShowExpand, false);
  47. const plugin = createPlugin(document.getElementById('app')!, spec);
  48. trySetSnapshot(plugin);
  49. tryLoadFromUrl(plugin);
  50. }
  51. async function trySetSnapshot(ctx: PluginContext) {
  52. try {
  53. const snapshotUrl = getParam('snapshot-url', `[^&]+`);
  54. const snapshotId = getParam('snapshot-id', `[^&]+`);
  55. if (!snapshotUrl && !snapshotId) return;
  56. // TODO parametrize the server
  57. const url = snapshotId
  58. ? `https://webchem.ncbr.muni.cz/molstar-state/get/${snapshotId}`
  59. : snapshotUrl;
  60. await PluginCommands.State.Snapshots.Fetch(ctx, { url });
  61. } catch (e) {
  62. ctx.log.error('Failed to load snapshot.');
  63. console.warn('Failed to load snapshot', e);
  64. }
  65. }
  66. async function tryLoadFromUrl(ctx: PluginContext) {
  67. const url = getParam('loadFromURL', '[^&]+').trim();
  68. try {
  69. if (!url) return;
  70. let format = 'cif', isBinary = false;
  71. switch (getParam('loadFromURLFormat', '[a-z]+').toLocaleLowerCase().trim()) {
  72. case 'pdb': format = 'pdb'; break;
  73. case 'mmbcif': isBinary = true; break;
  74. }
  75. const params = DownloadStructure.createDefaultParams(void 0 as any, ctx);
  76. return ctx.runTask(ctx.state.data.applyAction(DownloadStructure, {
  77. source: {
  78. name: 'url',
  79. params: {
  80. url,
  81. format: format as any,
  82. isBinary,
  83. options: params.source.params.options,
  84. }
  85. }
  86. }));
  87. } catch (e) {
  88. ctx.log.error(`Failed to load from URL (${url})`);
  89. console.warn(`Failed to load from URL (${url})`, e);
  90. }
  91. }
  92. init();