index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { RcsbFv3DCustomInterface} from "../RcsbFv3D/RcsbFv3DCustom";
  2. import { DebugUtil } from "./tmdet-extension/debug-utils";
  3. import { createFeatureViewerConfing } from "./FeatureViewConfig";
  4. import { fetchDescriptor, registerRegionDescriptorData } from "./UniTmpHelper";
  5. import { TmFv3DCustom } from "./tmdet-viewer/TmFv3DCustom";
  6. import { updateSiteColors } from "./tmdet-extension/tmdet-color-theme";
  7. import { createRcsbFeatureViewer, TmFv1DElement } from "./tmdet-viewer/TmFv1DComponent";
  8. import { RcsbFvDOMConstants } from "../RcsbFvConstants/RcsbFvConstants";
  9. customElements.define("tm-saguaro-1d", TmFv1DElement);
  10. function createRcsb3DViewer(configParams: any) {
  11. document.addEventListener("DOMContentLoaded", (event) => {
  12. (async function(event) {
  13. updateSiteColors(configParams.side1);
  14. DebugUtil.enableLog();
  15. const panel3dConfig = await createConfig(configParams);
  16. DebugUtil.log('Panel config', JSON.stringify(panel3dConfig));
  17. const panel3d = new TmFv3DCustom(panel3dConfig);
  18. panel3d.render();
  19. const sequenceViewerContainerId = panel3dConfig.sequencePanelConfig.config.customViewContainerId;
  20. if (sequenceViewerContainerId) {
  21. const destinationContainer = `${configParams.elementId}_${RcsbFvDOMConstants.PFV_APP_ID}`;
  22. console.log(sequenceViewerContainerId, destinationContainer);
  23. watchNodeChanges(destinationContainer, () => arrange(sequenceViewerContainerId, destinationContainer));
  24. }
  25. })();
  26. });
  27. }
  28. export function arrange(childId: string, parentId: string) {
  29. const child = document.getElementById(childId);
  30. if (!child) {
  31. console.warn(`DOM node '${childId}' not found`);
  32. return;
  33. }
  34. document.getElementById(parentId)?.appendChild(child);
  35. child.style.visibility = "visible";
  36. }
  37. async function watchNodeChanges(elementId: string, action: () => void, retryCount: number = 5) {
  38. const targetNode = document.getElementById(elementId);
  39. if (!targetNode) {
  40. console.warn(`DOM node not found with id '${elementId}'`);
  41. if (retryCount > 0) {
  42. console.warn("Retrying later");
  43. setTimeout(() => {
  44. console.log(`Running watchNodeChanges again with '${elementId}'`);
  45. watchNodeChanges(elementId, action, retryCount - 1);
  46. }, 500);
  47. }
  48. return;
  49. }
  50. action();
  51. }
  52. async function createConfig(configParams: any): Promise<RcsbFv3DCustomInterface> {
  53. const pdbEntry = configParams.pdbEntry;
  54. const labelAsymId = configParams.labelAsymId;
  55. const params: any = {
  56. title: configParams.title,
  57. subtitle: configParams.subtitle,
  58. side1: configParams.side1,
  59. htpEntry: configParams.htpEntry,
  60. pdbEntry: configParams.pdbEntry,
  61. structureUrl: configParams.structureUrl!,
  62. labelAsymId: labelAsymId,
  63. pdbtmEntry: `${pdbEntry}_${labelAsymId}`
  64. };
  65. if (pdbEntry) {
  66. console.log('annotation data:', configParams.annotationData);
  67. await registerRegionDescriptorData(configParams.annotationData, params.side1);
  68. }
  69. if (configParams.configData) {
  70. console.log('config data:', configParams.configData);
  71. params.fvConfigData = await fetchDescriptor(configParams.configData);
  72. }
  73. const { sequenceConfig, molstarConfig } = await createFeatureViewerConfing(params);
  74. const panel3dConfig: RcsbFv3DCustomInterface = {
  75. elementId: configParams.elementId,
  76. sequencePanelConfig: sequenceConfig,
  77. structurePanelConfig: molstarConfig
  78. };
  79. panel3dConfig.sequencePanelConfig.config.customViewContainerId = configParams.customViewContainerId;
  80. molstarConfig.structureViewerConfig.viewerProps.layoutShowSequence = true;
  81. (molstarConfig as any).loadConfig.loadParams.params = {
  82. preset: { modelIndex: 0 }
  83. }
  84. return panel3dConfig;
  85. }
  86. export {
  87. DebugUtil,
  88. createRcsb3DViewer,
  89. createRcsbFeatureViewer,
  90. TmFv1DElement
  91. };