Spaces:
Running
Running
Update interface.js
Browse files- interface.js +31 -35
interface.js
CHANGED
|
@@ -1,24 +1,11 @@
|
|
| 1 |
// interface.js
|
| 2 |
// ==============================
|
| 3 |
-
// Robust version with iOS/Safari debug alerts
|
| 4 |
-
// ==============================
|
| 5 |
|
|
|
|
| 6 |
const currentScriptTag = document.currentScript;
|
| 7 |
|
| 8 |
-
function isIOSorSafari() {
|
| 9 |
-
const ua = navigator.userAgent;
|
| 10 |
-
return (
|
| 11 |
-
/iPad|iPhone|iPod/.test(ua) ||
|
| 12 |
-
(/Safari/.test(ua) && !/Chrome/.test(ua) && !/Android/.test(ua))
|
| 13 |
-
);
|
| 14 |
-
}
|
| 15 |
-
function logAndAlert(msg) {
|
| 16 |
-
console.log(msg);
|
| 17 |
-
if (isIOSorSafari()) alert(msg);
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
(async function() {
|
| 21 |
-
// βββ 1. Locate the <script> and read data-config
|
| 22 |
let scriptTag = currentScriptTag;
|
| 23 |
if (!scriptTag) {
|
| 24 |
const scripts = document.getElementsByTagName('script');
|
|
@@ -33,21 +20,18 @@ function logAndAlert(msg) {
|
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
-
// Defensive fetch with CORS for config
|
| 37 |
const configUrl = scriptTag.getAttribute('data-config');
|
| 38 |
let config = {};
|
| 39 |
if (configUrl) {
|
| 40 |
try {
|
| 41 |
-
|
| 42 |
-
const response = await fetch(configUrl, {mode: "cors"});
|
| 43 |
config = await response.json();
|
| 44 |
-
logAndAlert("[interface.js] Config loaded");
|
| 45 |
} catch (error) {
|
| 46 |
-
|
| 47 |
return;
|
| 48 |
}
|
| 49 |
} else {
|
| 50 |
-
|
| 51 |
return;
|
| 52 |
}
|
| 53 |
|
|
@@ -100,6 +84,7 @@ function logAndAlert(msg) {
|
|
| 100 |
? <button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle">β¦Ώ</button>
|
| 101 |
: '';
|
| 102 |
|
|
|
|
| 103 |
widgetContainer.innerHTML =
|
| 104 |
<div id="viewer-container-${instanceId}" class="viewer-container">
|
| 105 |
<div id="progress-dialog-${instanceId}" class="progress-dialog">
|
|
@@ -125,6 +110,7 @@ function logAndAlert(msg) {
|
|
| 125 |
</div>
|
| 126 |
;
|
| 127 |
|
|
|
|
| 128 |
scriptTag.parentNode.appendChild(widgetContainer);
|
| 129 |
|
| 130 |
// βββ 6. Grab references to new DOM elements ββββββββββββββββββββββββββββββββββ
|
|
@@ -136,10 +122,12 @@ function logAndAlert(msg) {
|
|
| 136 |
const tooltipsToggleBtn = document.getElementById('tooltips-toggle-' + instanceId);
|
| 137 |
const menuContent = document.getElementById('menu-content-' + instanceId);
|
| 138 |
const helpTextDiv = menuContent.querySelector('.help-text');
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
const
|
| 142 |
-
const
|
|
|
|
|
|
|
| 143 |
|
| 144 |
// βββ 6a. Detect mobile vs. desktop ββββββββββββββββββββββββββββββββββββββββββββ
|
| 145 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
@@ -150,6 +138,7 @@ function logAndAlert(msg) {
|
|
| 150 |
? '- Cliquez sur β¦Ώ pour afficher/masquer les tooltips.<br>'
|
| 151 |
: '';
|
| 152 |
|
|
|
|
| 153 |
if (isMobile) {
|
| 154 |
helpTextDiv.innerHTML =
|
| 155 |
- Pour vous dΓ©placer, glissez deux doigts sur l'Γ©cran.<br>
|
|
@@ -170,10 +159,14 @@ function logAndAlert(msg) {
|
|
| 170 |
;
|
| 171 |
}
|
| 172 |
|
|
|
|
| 173 |
menuContent.style.display = 'block';
|
| 174 |
viewerContainerElem.style.display = 'block';
|
| 175 |
|
|
|
|
| 176 |
let dragHide = null;
|
|
|
|
|
|
|
| 177 |
function hideTooltipPanel() {
|
| 178 |
if (dragHide) {
|
| 179 |
viewerContainerElem.removeEventListener('pointermove', dragHide);
|
|
@@ -181,41 +174,42 @@ function logAndAlert(msg) {
|
|
| 181 |
}
|
| 182 |
tooltipPanel.style.display = 'none';
|
| 183 |
}
|
| 184 |
-
function hideHelpPanel() {
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
// βββ 7. Dynamically load viewer.js
|
| 187 |
let viewerModule;
|
| 188 |
try {
|
| 189 |
-
logAndAlert("[interface.js] Loading viewer.js...");
|
| 190 |
viewerModule = await import('https://mikafil-viewer-gs.static.hf.space/viewer.js');
|
| 191 |
await viewerModule.initializeViewer(config, instanceId);
|
| 192 |
-
logAndAlert("[interface.js] viewer.js initialized.");
|
| 193 |
} catch (err) {
|
| 194 |
-
|
| 195 |
return;
|
| 196 |
}
|
| 197 |
|
| 198 |
const canvasId = 'canvas-' + instanceId;
|
| 199 |
const canvasEl = document.getElementById(canvasId);
|
| 200 |
|
| 201 |
-
// βββ 8. Conditional display of tooltips-toggle button
|
| 202 |
if (tooltipsToggleBtn) {
|
| 203 |
if (!config.tooltips_url) {
|
| 204 |
tooltipsToggleBtn.style.display = 'none';
|
| 205 |
} else {
|
| 206 |
-
|
| 207 |
-
fetch(config.tooltips_url, {mode:"cors"})
|
| 208 |
.then(resp => { if (!resp.ok) tooltipsToggleBtn.style.display = 'none'; })
|
| 209 |
.catch(() => { tooltipsToggleBtn.style.display = 'none'; });
|
| 210 |
}
|
| 211 |
}
|
| 212 |
|
| 213 |
-
// βββ 9. Fullscreen / state-preservation logic
|
| 214 |
let isFullscreen = false;
|
| 215 |
let savedState = null;
|
| 216 |
|
| 217 |
function saveCurrentState() {
|
| 218 |
if (isFullscreen) return;
|
|
|
|
|
|
|
| 219 |
const originalAspect = widgetContainer.getAttribute('data-original-aspect') || aspectPercent;
|
| 220 |
savedState = {
|
| 221 |
widget: {
|
|
@@ -266,6 +260,7 @@ function logAndAlert(msg) {
|
|
| 266 |
viewerContainerElem.clientHeight
|
| 267 |
);
|
| 268 |
}
|
|
|
|
| 269 |
savedState = null;
|
| 270 |
}
|
| 271 |
|
|
@@ -322,7 +317,7 @@ function logAndAlert(msg) {
|
|
| 322 |
isFullscreen = false;
|
| 323 |
}
|
| 324 |
|
| 325 |
-
// βββ 10. Hook up event listeners
|
| 326 |
fullscreenToggle.addEventListener('click', () => {
|
| 327 |
hideTooltipPanel();
|
| 328 |
isFullscreen ? exitFullscreen() : enterFullscreen();
|
|
@@ -398,6 +393,7 @@ function logAndAlert(msg) {
|
|
| 398 |
}
|
| 399 |
});
|
| 400 |
|
|
|
|
| 401 |
setTimeout(() => {
|
| 402 |
saveCurrentState();
|
| 403 |
document.dispatchEvent(new CustomEvent('toggle-tooltips', { detail: { visible: !!config.showTooltipsDefault } }));
|
|
|
|
| 1 |
// interface.js
|
| 2 |
// ==============================
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
// Store a reference to the <script> tag that loaded this file
|
| 5 |
const currentScriptTag = document.currentScript;
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
(async function() {
|
| 8 |
+
// βββ 1. Locate the <script> and read data-config βββββββββββββββββββββββββββββββ
|
| 9 |
let scriptTag = currentScriptTag;
|
| 10 |
if (!scriptTag) {
|
| 11 |
const scripts = document.getElementsByTagName('script');
|
|
|
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
|
|
|
| 23 |
const configUrl = scriptTag.getAttribute('data-config');
|
| 24 |
let config = {};
|
| 25 |
if (configUrl) {
|
| 26 |
try {
|
| 27 |
+
const response = await fetch(configUrl);
|
|
|
|
| 28 |
config = await response.json();
|
|
|
|
| 29 |
} catch (error) {
|
| 30 |
+
console.error("Error loading config file:", error);
|
| 31 |
return;
|
| 32 |
}
|
| 33 |
} else {
|
| 34 |
+
console.error("No config file provided. Please set a data-config attribute on the <script> tag.");
|
| 35 |
return;
|
| 36 |
}
|
| 37 |
|
|
|
|
| 84 |
? <button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle">β¦Ώ</button>
|
| 85 |
: '';
|
| 86 |
|
| 87 |
+
// Add the 3D-viewer HTML + tooltip + help HTML
|
| 88 |
widgetContainer.innerHTML =
|
| 89 |
<div id="viewer-container-${instanceId}" class="viewer-container">
|
| 90 |
<div id="progress-dialog-${instanceId}" class="progress-dialog">
|
|
|
|
| 110 |
</div>
|
| 111 |
;
|
| 112 |
|
| 113 |
+
// Append the widget container immediately after the <script> tag
|
| 114 |
scriptTag.parentNode.appendChild(widgetContainer);
|
| 115 |
|
| 116 |
// βββ 6. Grab references to new DOM elements ββββββββββββββββββββββββββββββββββ
|
|
|
|
| 122 |
const tooltipsToggleBtn = document.getElementById('tooltips-toggle-' + instanceId);
|
| 123 |
const menuContent = document.getElementById('menu-content-' + instanceId);
|
| 124 |
const helpTextDiv = menuContent.querySelector('.help-text');
|
| 125 |
+
|
| 126 |
+
// Tooltip panel elements
|
| 127 |
+
const tooltipPanel = document.getElementById('tooltip-panel');
|
| 128 |
+
const tooltipTextDiv = document.getElementById('tooltip-text');
|
| 129 |
+
const tooltipImage = document.getElementById('tooltip-image');
|
| 130 |
+
const tooltipCloseBtn = document.getElementById('tooltip-close');
|
| 131 |
|
| 132 |
// βββ 6a. Detect mobile vs. desktop ββββββββββββββββββββββββββββββββββββββββββββ
|
| 133 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
|
|
| 138 |
? '- Cliquez sur β¦Ώ pour afficher/masquer les tooltips.<br>'
|
| 139 |
: '';
|
| 140 |
|
| 141 |
+
// Fill help text with instructions plus the two new French lines
|
| 142 |
if (isMobile) {
|
| 143 |
helpTextDiv.innerHTML =
|
| 144 |
- Pour vous dΓ©placer, glissez deux doigts sur l'Γ©cran.<br>
|
|
|
|
| 159 |
;
|
| 160 |
}
|
| 161 |
|
| 162 |
+
// Ensure instructions panel is visible by default
|
| 163 |
menuContent.style.display = 'block';
|
| 164 |
viewerContainerElem.style.display = 'block';
|
| 165 |
|
| 166 |
+
// Variable to hold the drag-hide listener reference
|
| 167 |
let dragHide = null;
|
| 168 |
+
|
| 169 |
+
// Utilities to hide panels
|
| 170 |
function hideTooltipPanel() {
|
| 171 |
if (dragHide) {
|
| 172 |
viewerContainerElem.removeEventListener('pointermove', dragHide);
|
|
|
|
| 174 |
}
|
| 175 |
tooltipPanel.style.display = 'none';
|
| 176 |
}
|
| 177 |
+
function hideHelpPanel() {
|
| 178 |
+
menuContent.style.display = 'none';
|
| 179 |
+
}
|
| 180 |
|
| 181 |
+
// βββ 7. Dynamically load viewer.js βββββββββββββββββββββββββββββββββββββββββ
|
| 182 |
let viewerModule;
|
| 183 |
try {
|
|
|
|
| 184 |
viewerModule = await import('https://mikafil-viewer-gs.static.hf.space/viewer.js');
|
| 185 |
await viewerModule.initializeViewer(config, instanceId);
|
|
|
|
| 186 |
} catch (err) {
|
| 187 |
+
console.error("Failed to load viewer.js or initialize the 3D viewer:", err);
|
| 188 |
return;
|
| 189 |
}
|
| 190 |
|
| 191 |
const canvasId = 'canvas-' + instanceId;
|
| 192 |
const canvasEl = document.getElementById(canvasId);
|
| 193 |
|
| 194 |
+
// βββ 8. Conditional display of tooltips-toggle button βββββββββββββββββββββββββ
|
| 195 |
if (tooltipsToggleBtn) {
|
| 196 |
if (!config.tooltips_url) {
|
| 197 |
tooltipsToggleBtn.style.display = 'none';
|
| 198 |
} else {
|
| 199 |
+
fetch(config.tooltips_url)
|
|
|
|
| 200 |
.then(resp => { if (!resp.ok) tooltipsToggleBtn.style.display = 'none'; })
|
| 201 |
.catch(() => { tooltipsToggleBtn.style.display = 'none'; });
|
| 202 |
}
|
| 203 |
}
|
| 204 |
|
| 205 |
+
// βββ 9. Fullscreen / state-preservation logic βββββββββββββββββββββββββββββββ
|
| 206 |
let isFullscreen = false;
|
| 207 |
let savedState = null;
|
| 208 |
|
| 209 |
function saveCurrentState() {
|
| 210 |
if (isFullscreen) return;
|
| 211 |
+
const computedWidget = window.getComputedStyle(widgetContainer);
|
| 212 |
+
const computedViewer = window.getComputedStyle(viewerContainerElem);
|
| 213 |
const originalAspect = widgetContainer.getAttribute('data-original-aspect') || aspectPercent;
|
| 214 |
savedState = {
|
| 215 |
widget: {
|
|
|
|
| 260 |
viewerContainerElem.clientHeight
|
| 261 |
);
|
| 262 |
}
|
| 263 |
+
|
| 264 |
savedState = null;
|
| 265 |
}
|
| 266 |
|
|
|
|
| 317 |
isFullscreen = false;
|
| 318 |
}
|
| 319 |
|
| 320 |
+
// βββ 10. Hook up event listeners βββββββββββββββββββββββββββββββββββββββββββ
|
| 321 |
fullscreenToggle.addEventListener('click', () => {
|
| 322 |
hideTooltipPanel();
|
| 323 |
isFullscreen ? exitFullscreen() : enterFullscreen();
|
|
|
|
| 393 |
}
|
| 394 |
});
|
| 395 |
|
| 396 |
+
// Save βinitial stateβ after a brief delay
|
| 397 |
setTimeout(() => {
|
| 398 |
saveCurrentState();
|
| 399 |
document.dispatchEvent(new CustomEvent('toggle-tooltips', { detail: { visible: !!config.showTooltipsDefault } }));
|