Spaces:
Running
Running
Update interface.js
Browse files- interface.js +28 -35
interface.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
// interface.js
|
| 2 |
// ==============================
|
| 3 |
-
// interface.js
|
| 4 |
-
// ==============================
|
| 5 |
|
| 6 |
// Store a reference to the <script> tag that loaded this file
|
| 7 |
const currentScriptTag = document.currentScript;
|
| 8 |
|
| 9 |
(async function() {
|
| 10 |
-
//
|
| 11 |
let scriptTag = currentScriptTag;
|
| 12 |
if (!scriptTag) {
|
| 13 |
const scripts = document.getElementsByTagName('script');
|
|
@@ -37,7 +35,7 @@ const currentScriptTag = document.currentScript;
|
|
| 37 |
return;
|
| 38 |
}
|
| 39 |
|
| 40 |
-
//
|
| 41 |
if (config.css_url) {
|
| 42 |
const linkEl = document.createElement('link');
|
| 43 |
linkEl.rel = "stylesheet";
|
|
@@ -45,10 +43,10 @@ const currentScriptTag = document.currentScript;
|
|
| 45 |
document.head.appendChild(linkEl);
|
| 46 |
}
|
| 47 |
|
| 48 |
-
//
|
| 49 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
| 50 |
|
| 51 |
-
//
|
| 52 |
let aspectPercent = "100%";
|
| 53 |
if (config.aspect) {
|
| 54 |
if (config.aspect.includes(":")) {
|
|
@@ -73,7 +71,7 @@ const currentScriptTag = document.currentScript;
|
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
-
//
|
| 77 |
const widgetContainer = document.createElement('div');
|
| 78 |
widgetContainer.id = 'ply-widget-container-' + instanceId;
|
| 79 |
widgetContainer.classList.add('ply-widget-container');
|
|
@@ -83,11 +81,11 @@ const currentScriptTag = document.currentScript;
|
|
| 83 |
|
| 84 |
// Conditionally include the βtooltips-toggleβ button only if config.tooltips_url is defined
|
| 85 |
const tooltipsButtonHTML = config.tooltips_url
|
| 86 |
-
?
|
| 87 |
: '';
|
| 88 |
|
| 89 |
-
// Add the 3D-viewer HTML + tooltip + help HTML
|
| 90 |
-
widgetContainer.innerHTML =
|
| 91 |
<div id="viewer-container-${instanceId}" class="viewer-container">
|
| 92 |
<div id="progress-dialog-${instanceId}" class="progress-dialog">
|
| 93 |
<progress id="progress-indicator-${instanceId}" max="100" value="0"></progress>
|
|
@@ -110,12 +108,12 @@ const currentScriptTag = document.currentScript;
|
|
| 110 |
<img id="tooltip-image" class="tooltip-image" src="" alt="" style="display: none;" />
|
| 111 |
</div>
|
| 112 |
</div>
|
| 113 |
-
|
| 114 |
|
| 115 |
// Append the widget container immediately after the <script> tag
|
| 116 |
scriptTag.parentNode.appendChild(widgetContainer);
|
| 117 |
|
| 118 |
-
//
|
| 119 |
const viewerContainerElem = document.getElementById('viewer-container-' + instanceId);
|
| 120 |
const fullscreenToggle = document.getElementById('fullscreen-toggle-' + instanceId);
|
| 121 |
const helpToggle = document.getElementById('help-toggle-' + instanceId);
|
|
@@ -131,7 +129,7 @@ const currentScriptTag = document.currentScript;
|
|
| 131 |
const tooltipImage = document.getElementById('tooltip-image');
|
| 132 |
const tooltipCloseBtn = document.getElementById('tooltip-close');
|
| 133 |
|
| 134 |
-
//
|
| 135 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 136 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 137 |
|
|
@@ -143,22 +141,20 @@ const currentScriptTag = document.currentScript;
|
|
| 143 |
// Fill help text with instructions plus the two new French lines
|
| 144 |
if (isMobile) {
|
| 145 |
helpTextDiv.innerHTML =
|
| 146 |
-
- Pour vous dΓ©placer, glissez deux doigts sur l'Γ©cran.<br>
|
| 147 |
-
- Pour orbiter, utilisez un doigt.<br>
|
| 148 |
-
- Pour zoomer, pincez avec deux doigts.<br>
|
| 149 |
-
|
| 150 |
-
- β² RΓ©initialise la camΓ©ra.<br>
|
| 151 |
-
- β± Passe en plein Γ©cran.<br>
|
| 152 |
-
;
|
| 153 |
} else {
|
| 154 |
helpTextDiv.innerHTML =
|
| 155 |
-
- orbitez avec le clic droit<br>
|
| 156 |
-
- zoomez avec la molette<br>
|
| 157 |
-
- dΓ©placez vous avec le clic gauche<br>
|
| 158 |
-
|
| 159 |
-
- β² RΓ©initialise la camΓ©ra.<br>
|
| 160 |
-
- β± Passe en plein Γ©cran.<br>
|
| 161 |
-
;
|
| 162 |
}
|
| 163 |
|
| 164 |
// Ensure instructions panel is visible by default
|
|
@@ -180,7 +176,7 @@ const currentScriptTag = document.currentScript;
|
|
| 180 |
menuContent.style.display = 'none';
|
| 181 |
}
|
| 182 |
|
| 183 |
-
//
|
| 184 |
let viewerModule;
|
| 185 |
try {
|
| 186 |
viewerModule = await import('https://mikafil-viewer-gs.static.hf.space/viewer.js');
|
|
@@ -193,7 +189,7 @@ const currentScriptTag = document.currentScript;
|
|
| 193 |
const canvasId = 'canvas-' + instanceId;
|
| 194 |
const canvasEl = document.getElementById(canvasId);
|
| 195 |
|
| 196 |
-
//
|
| 197 |
if (tooltipsToggleBtn) {
|
| 198 |
if (!config.tooltips_url) {
|
| 199 |
tooltipsToggleBtn.style.display = 'none';
|
|
@@ -204,14 +200,12 @@ const currentScriptTag = document.currentScript;
|
|
| 204 |
}
|
| 205 |
}
|
| 206 |
|
| 207 |
-
//
|
| 208 |
let isFullscreen = false;
|
| 209 |
let savedState = null;
|
| 210 |
|
| 211 |
function saveCurrentState() {
|
| 212 |
if (isFullscreen) return;
|
| 213 |
-
const computedWidget = window.getComputedStyle(widgetContainer);
|
| 214 |
-
const computedViewer = window.getComputedStyle(viewerContainerElem);
|
| 215 |
const originalAspect = widgetContainer.getAttribute('data-original-aspect') || aspectPercent;
|
| 216 |
savedState = {
|
| 217 |
widget: {
|
|
@@ -319,7 +313,7 @@ const currentScriptTag = document.currentScript;
|
|
| 319 |
isFullscreen = false;
|
| 320 |
}
|
| 321 |
|
| 322 |
-
//
|
| 323 |
fullscreenToggle.addEventListener('click', () => {
|
| 324 |
hideTooltipPanel();
|
| 325 |
isFullscreen ? exitFullscreen() : enterFullscreen();
|
|
@@ -360,7 +354,7 @@ const currentScriptTag = document.currentScript;
|
|
| 360 |
|
| 361 |
document.addEventListener('tooltip-selected', (evt) => {
|
| 362 |
const { title, description, imgUrl } = evt.detail;
|
| 363 |
-
tooltipTextDiv.innerHTML =
|
| 364 |
if (imgUrl) {
|
| 365 |
tooltipImage.src = imgUrl;
|
| 366 |
tooltipImage.style.display = 'block';
|
|
@@ -395,7 +389,6 @@ const currentScriptTag = document.currentScript;
|
|
| 395 |
}
|
| 396 |
});
|
| 397 |
|
| 398 |
-
// Save βinitial stateβ after a brief delay
|
| 399 |
setTimeout(() => {
|
| 400 |
saveCurrentState();
|
| 401 |
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');
|
|
|
|
| 35 |
return;
|
| 36 |
}
|
| 37 |
|
| 38 |
+
// 2. If config.css_url is provided, inject a <link> to that CSS
|
| 39 |
if (config.css_url) {
|
| 40 |
const linkEl = document.createElement('link');
|
| 41 |
linkEl.rel = "stylesheet";
|
|
|
|
| 43 |
document.head.appendChild(linkEl);
|
| 44 |
}
|
| 45 |
|
| 46 |
+
// 3. Generate a unique instanceId for this widget
|
| 47 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
| 48 |
|
| 49 |
+
// 4. Compute the aspect ratio (padding-bottom %)
|
| 50 |
let aspectPercent = "100%";
|
| 51 |
if (config.aspect) {
|
| 52 |
if (config.aspect.includes(":")) {
|
|
|
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
+
// 5. Create the widget container (no GIF preview, no close button)
|
| 75 |
const widgetContainer = document.createElement('div');
|
| 76 |
widgetContainer.id = 'ply-widget-container-' + instanceId;
|
| 77 |
widgetContainer.classList.add('ply-widget-container');
|
|
|
|
| 81 |
|
| 82 |
// Conditionally include the βtooltips-toggleβ button only if config.tooltips_url is defined
|
| 83 |
const tooltipsButtonHTML = config.tooltips_url
|
| 84 |
+
? `<button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle">β¦Ώ</button>`
|
| 85 |
: '';
|
| 86 |
|
| 87 |
+
// Add the 3D-viewer HTML + tooltip + help HTML (all HTML in a template literal!)
|
| 88 |
+
widgetContainer.innerHTML = `
|
| 89 |
<div id="viewer-container-${instanceId}" class="viewer-container">
|
| 90 |
<div id="progress-dialog-${instanceId}" class="progress-dialog">
|
| 91 |
<progress id="progress-indicator-${instanceId}" max="100" value="0"></progress>
|
|
|
|
| 108 |
<img id="tooltip-image" class="tooltip-image" src="" alt="" style="display: none;" />
|
| 109 |
</div>
|
| 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
|
| 117 |
const viewerContainerElem = document.getElementById('viewer-container-' + instanceId);
|
| 118 |
const fullscreenToggle = document.getElementById('fullscreen-toggle-' + instanceId);
|
| 119 |
const helpToggle = document.getElementById('help-toggle-' + instanceId);
|
|
|
|
| 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;
|
| 134 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 135 |
|
|
|
|
| 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>' +
|
| 145 |
+
'- Pour orbiter, utilisez un doigt.<br>' +
|
| 146 |
+
'- Pour zoomer, pincez avec deux doigts.<br>' +
|
| 147 |
+
tooltipInstruction +
|
| 148 |
+
'- β² RΓ©initialise la camΓ©ra.<br>' +
|
| 149 |
+
'- β± Passe en plein Γ©cran.<br>';
|
|
|
|
| 150 |
} else {
|
| 151 |
helpTextDiv.innerHTML =
|
| 152 |
+
'- orbitez avec le clic droit<br>' +
|
| 153 |
+
'- zoomez avec la molette<br>' +
|
| 154 |
+
'- dΓ©placez vous avec le clic gauche<br>' +
|
| 155 |
+
tooltipInstruction +
|
| 156 |
+
'- β² RΓ©initialise la camΓ©ra.<br>' +
|
| 157 |
+
'- β± Passe en plein Γ©cran.<br>';
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
// Ensure instructions panel is visible by default
|
|
|
|
| 176 |
menuContent.style.display = 'none';
|
| 177 |
}
|
| 178 |
|
| 179 |
+
// 7. Dynamically load viewer.js
|
| 180 |
let viewerModule;
|
| 181 |
try {
|
| 182 |
viewerModule = await import('https://mikafil-viewer-gs.static.hf.space/viewer.js');
|
|
|
|
| 189 |
const canvasId = 'canvas-' + instanceId;
|
| 190 |
const canvasEl = document.getElementById(canvasId);
|
| 191 |
|
| 192 |
+
// 8. Conditional display of tooltips-toggle button
|
| 193 |
if (tooltipsToggleBtn) {
|
| 194 |
if (!config.tooltips_url) {
|
| 195 |
tooltipsToggleBtn.style.display = 'none';
|
|
|
|
| 200 |
}
|
| 201 |
}
|
| 202 |
|
| 203 |
+
// 9. Fullscreen / state-preservation logic
|
| 204 |
let isFullscreen = false;
|
| 205 |
let savedState = null;
|
| 206 |
|
| 207 |
function saveCurrentState() {
|
| 208 |
if (isFullscreen) return;
|
|
|
|
|
|
|
| 209 |
const originalAspect = widgetContainer.getAttribute('data-original-aspect') || aspectPercent;
|
| 210 |
savedState = {
|
| 211 |
widget: {
|
|
|
|
| 313 |
isFullscreen = false;
|
| 314 |
}
|
| 315 |
|
| 316 |
+
// 10. Hook up event listeners
|
| 317 |
fullscreenToggle.addEventListener('click', () => {
|
| 318 |
hideTooltipPanel();
|
| 319 |
isFullscreen ? exitFullscreen() : enterFullscreen();
|
|
|
|
| 354 |
|
| 355 |
document.addEventListener('tooltip-selected', (evt) => {
|
| 356 |
const { title, description, imgUrl } = evt.detail;
|
| 357 |
+
tooltipTextDiv.innerHTML = `<strong>${title}</strong><br>${description}`;
|
| 358 |
if (imgUrl) {
|
| 359 |
tooltipImage.src = imgUrl;
|
| 360 |
tooltipImage.style.display = 'block';
|
|
|
|
| 389 |
}
|
| 390 |
});
|
| 391 |
|
|
|
|
| 392 |
setTimeout(() => {
|
| 393 |
saveCurrentState();
|
| 394 |
document.dispatchEvent(new CustomEvent('toggle-tooltips', { detail: { visible: !!config.showTooltipsDefault } }));
|