|
11 | 11 | querySpot, |
12 | 12 | removeFailedSite, |
13 | 13 | } from "@samply/lens"; |
| 14 | + import { base } from "$app/paths"; |
14 | 15 | import { env } from "$env/dynamic/public"; |
15 | 16 | import { onMount } from "svelte"; |
16 | 17 | import { v4 as uuidv4 } from "uuid"; |
| 18 | + import { |
| 19 | + cloneLensOptions, |
| 20 | + loadOptionsWithDirectoryBiobankNames, |
| 21 | + } from "$lib/directoryBiobankNames"; |
17 | 22 | import optionsProd from "./config/options.json"; |
18 | 23 | import optionsTest from "./config/options-test.json"; |
19 | 24 | import optionsAcceptance from "./config/options-acceptance.json"; |
|
77 | 82 | scrollToResults(); |
78 | 83 | }); |
79 | 84 |
|
80 | | - onMount(async () => { |
| 85 | + const setupResultTableNameTooltips = () => { |
| 86 | + let animationFrameId: number | undefined; |
| 87 | + let tableObserver: MutationObserver | undefined; |
| 88 | +
|
| 89 | + const updateTooltips = (resultTable: HTMLElement) => { |
| 90 | + const rows = resultTable.shadowRoot?.querySelectorAll<HTMLElement>( |
| 91 | + '[part~="lens-result-table-item-body-row"]', |
| 92 | + ); |
| 93 | +
|
| 94 | + rows?.forEach((row) => { |
| 95 | + const cells = row.querySelectorAll<HTMLElement>( |
| 96 | + '[part~="lens-result-table-item-body-cell"]', |
| 97 | + ); |
| 98 | + const siteCell = cells[1]; |
| 99 | + const siteName = siteCell?.textContent?.replace(/\s+/g, " ").trim(); |
| 100 | +
|
| 101 | + if (!siteCell || !siteName) { |
| 102 | + return; |
| 103 | + } |
| 104 | +
|
| 105 | + siteCell.title = siteName; |
| 106 | + siteCell |
| 107 | + .querySelector<HTMLElement>( |
| 108 | + '[part~="lens-result-table-item-body-cell-link"]', |
| 109 | + ) |
| 110 | + ?.setAttribute("title", siteName); |
| 111 | + }); |
| 112 | + }; |
| 113 | +
|
| 114 | + const observeResultTable = () => { |
| 115 | + const resultTable = |
| 116 | + document.querySelector<HTMLElement>("lens-result-table"); |
| 117 | +
|
| 118 | + if (!resultTable?.shadowRoot) { |
| 119 | + animationFrameId = window.requestAnimationFrame(observeResultTable); |
| 120 | + return; |
| 121 | + } |
| 122 | +
|
| 123 | + updateTooltips(resultTable); |
| 124 | + tableObserver = new MutationObserver(() => updateTooltips(resultTable)); |
| 125 | + tableObserver.observe(resultTable.shadowRoot, { |
| 126 | + childList: true, |
| 127 | + subtree: true, |
| 128 | + characterData: true, |
| 129 | + }); |
| 130 | + }; |
| 131 | +
|
| 132 | + observeResultTable(); |
| 133 | +
|
| 134 | + return () => { |
| 135 | + tableObserver?.disconnect(); |
| 136 | + if (animationFrameId) { |
| 137 | + window.cancelAnimationFrame(animationFrameId); |
| 138 | + } |
| 139 | + }; |
| 140 | + }; |
| 141 | +
|
| 142 | + const initializeLens = async () => { |
81 | 143 | // Set the options based on the environment |
82 | | - let options: LensOptions = optionsProd; |
| 144 | + let optionsSource: LensOptions = optionsProd; |
83 | 145 | if (env.PUBLIC_ENVIRONMENT === "test") { |
84 | | - options = optionsTest; |
| 146 | + optionsSource = optionsTest; |
85 | 147 | } else if (env.PUBLIC_ENVIRONMENT === "acceptance") { |
86 | | - options = optionsAcceptance; |
| 148 | + optionsSource = optionsAcceptance; |
87 | 149 | } |
| 150 | +
|
| 151 | + let options = cloneLensOptions(optionsSource); |
| 152 | +
|
88 | 153 | if (env.PUBLIC_SPOT_URL) { |
89 | | - options.spotUrl = env.PUBLIC_SPOT_URL; |
| 154 | + options = { |
| 155 | + ...options, |
| 156 | + spotUrl: env.PUBLIC_SPOT_URL, |
| 157 | + }; |
90 | 158 | } |
91 | 159 |
|
92 | 160 | // Check if prism URL parameter is set |
|
105 | 173 | }; |
106 | 174 | } |
107 | 175 |
|
| 176 | + options = await loadOptionsWithDirectoryBiobankNames( |
| 177 | + options, |
| 178 | + `${base}/api/directory-biobank-names`, |
| 179 | + ); |
| 180 | +
|
108 | 181 | setOptions(options); |
109 | 182 |
|
110 | 183 | // Set the catalogue |
|
113 | 186 | // Wait for the search bar to initialize (load query from URL) before sending the initial query. |
114 | 187 | // Using setTimeout to ensure the custom element's onMount has completed. |
115 | 188 | setTimeout(() => sendQuery(), 0); |
| 189 | + }; |
| 190 | +
|
| 191 | + onMount(() => { |
| 192 | + const cleanupResultTableNameTooltips = setupResultTableNameTooltips(); |
| 193 | + void initializeLens(); |
| 194 | +
|
| 195 | + return cleanupResultTableNameTooltips; |
116 | 196 | }); |
117 | 197 |
|
118 | 198 | let results: HTMLElement; |
|
0 commit comments