Spaces:
Running
Running
Update interface.js
Browse files- interface.js +98 -136
interface.js
CHANGED
|
@@ -2,179 +2,141 @@
|
|
| 2 |
// interface.js
|
| 3 |
// ==============================
|
| 4 |
|
| 5 |
-
// Store a reference to the <script> tag that loaded this file
|
| 6 |
const currentScriptTag = document.currentScript;
|
| 7 |
|
| 8 |
(async function() {
|
| 9 |
-
// βββ 1. Locate
|
| 10 |
let scriptTag = currentScriptTag;
|
| 11 |
if (!scriptTag) {
|
| 12 |
const scripts = document.getElementsByTagName('script');
|
| 13 |
-
for (let
|
| 14 |
-
if (
|
| 15 |
-
scriptTag =
|
| 16 |
break;
|
| 17 |
}
|
| 18 |
}
|
| 19 |
-
if (!scriptTag && scripts.length
|
| 20 |
-
scriptTag = scripts[scripts.length - 1];
|
| 21 |
-
}
|
| 22 |
}
|
| 23 |
|
| 24 |
const configUrl = scriptTag.getAttribute('data-config');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
let config = {};
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
console.error("Error loading config file:", error);
|
| 32 |
-
return;
|
| 33 |
-
}
|
| 34 |
-
} else {
|
| 35 |
-
console.error("No config file provided. Please set a data-config attribute on the <script> tag.");
|
| 36 |
return;
|
| 37 |
}
|
| 38 |
|
| 39 |
-
// βββ 2.
|
| 40 |
if (config.css_url) {
|
| 41 |
-
const
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
document.head.appendChild(
|
| 45 |
}
|
| 46 |
|
| 47 |
-
// βββ 3.
|
| 48 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
| 49 |
-
|
| 50 |
-
// βββ 4. Compute the aspect ratio (padding-bottom %) ββββββββββββββββββββββββββββ
|
| 51 |
-
let aspectPercent = "100%";
|
| 52 |
-
if (config.aspect) {
|
| 53 |
-
if (config.aspect.includes(":")) {
|
| 54 |
-
const parts = config.aspect.split(":");
|
| 55 |
-
const w = parseFloat(parts[0]);
|
| 56 |
-
const h = parseFloat(parts[1]);
|
| 57 |
-
if (!isNaN(w) && !isNaN(h) && w > 0) {
|
| 58 |
-
aspectPercent = (h / w * 100) + "%";
|
| 59 |
-
}
|
| 60 |
-
} else {
|
| 61 |
-
const aspectValue = parseFloat(config.aspect);
|
| 62 |
-
if (!isNaN(aspectValue) && aspectValue > 0) {
|
| 63 |
-
aspectPercent = (100 / aspectValue) + "%";
|
| 64 |
-
}
|
| 65 |
-
}
|
| 66 |
-
} else {
|
| 67 |
-
const parentContainer = scriptTag.parentNode;
|
| 68 |
-
const containerWidth = parentContainer.offsetWidth;
|
| 69 |
-
const containerHeight = parentContainer.offsetHeight;
|
| 70 |
-
if (containerWidth > 0 && containerHeight > 0) {
|
| 71 |
-
aspectPercent = (containerHeight / containerWidth * 100) + "%";
|
| 72 |
-
}
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
// βββ 5. Create the widget container ββββββββββββββββββββββββββββββββββββββββββββ
|
| 76 |
const widgetContainer = document.createElement('div');
|
| 77 |
-
widgetContainer.id =
|
| 78 |
widgetContainer.classList.add('ply-widget-container');
|
| 79 |
-
|
| 80 |
-
widgetContainer.style.paddingBottom = aspectPercent;
|
| 81 |
-
widgetContainer.setAttribute('data-original-aspect', aspectPercent);
|
| 82 |
|
| 83 |
-
//
|
| 84 |
-
const tooltipsButtonHTML = config.tooltips_url
|
| 85 |
-
? `<button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle">β¦Ώ</button>`
|
| 86 |
-
: '';
|
| 87 |
-
|
| 88 |
-
// Add all inner HTML, including help panel with French lines
|
| 89 |
widgetContainer.innerHTML = `
|
| 90 |
-
<div id="viewer-container-${instanceId}" class="viewer-container">
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
</div>
|
| 102 |
</div>
|
|
|
|
|
|
|
| 103 |
<div id="tooltip-panel" class="tooltip-panel" style="display: none;">
|
| 104 |
-
<
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
<img id="tooltip-image" class="tooltip-image" src="" alt="" style="display: none;" />
|
| 108 |
-
</div>
|
| 109 |
</div>
|
| 110 |
`;
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
const
|
| 116 |
-
const
|
| 117 |
-
const
|
| 118 |
-
const
|
| 119 |
-
const
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
`;
|
| 149 |
-
} else {
|
| 150 |
-
helpTextDiv.innerHTML = `
|
| 151 |
-
- orbitez avec le clic droit<br>
|
| 152 |
-
- zoomez avec la molette<br>
|
| 153 |
-
- dΓ©placez vous avec le clic gauche<br>
|
| 154 |
-
β² RΓ©initialise la camΓ©ra.<br>
|
| 155 |
-
β± Passe en plein Γ©cran.<br>
|
| 156 |
-
${tooltipInstruction}
|
| 157 |
-
`;
|
| 158 |
}
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
| 163 |
|
| 164 |
-
// βββ 7.
|
| 165 |
try {
|
| 166 |
-
console.log('[
|
| 167 |
const viewerModule = await import('./viewer.js');
|
| 168 |
-
console.log('[Viewer-Debug] viewer.js imported, calling initializeViewer()');
|
| 169 |
await viewerModule.initializeViewer(config, instanceId);
|
| 170 |
-
console.log('[Viewer-Debug] initializeViewer() completed');
|
| 171 |
} catch (err) {
|
| 172 |
-
console.error("Failed to load viewer.js or initialize
|
| 173 |
-
return;
|
| 174 |
}
|
| 175 |
|
| 176 |
-
// βββ 8.
|
| 177 |
-
//
|
| 178 |
-
// drag-to-hide, wheel-to-hide, resize listeners, etc., without alteration.)
|
| 179 |
|
| 180 |
})();
|
|
|
|
| 2 |
// interface.js
|
| 3 |
// ==============================
|
| 4 |
|
|
|
|
| 5 |
const currentScriptTag = document.currentScript;
|
| 6 |
|
| 7 |
(async function() {
|
| 8 |
+
// βββ 1. Locate <script> and load config βββββββββββββββββββββββββββββββββββββ
|
| 9 |
let scriptTag = currentScriptTag;
|
| 10 |
if (!scriptTag) {
|
| 11 |
const scripts = document.getElementsByTagName('script');
|
| 12 |
+
for (let s of scripts) {
|
| 13 |
+
if (s.src.includes('interface.js') && s.hasAttribute('data-config')) {
|
| 14 |
+
scriptTag = s;
|
| 15 |
break;
|
| 16 |
}
|
| 17 |
}
|
| 18 |
+
if (!scriptTag && scripts.length) scriptTag = scripts[scripts.length - 1];
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
const configUrl = scriptTag.getAttribute('data-config');
|
| 22 |
+
if (!configUrl) {
|
| 23 |
+
console.error("No data-config on <script>");
|
| 24 |
+
return;
|
| 25 |
+
}
|
| 26 |
let config = {};
|
| 27 |
+
try {
|
| 28 |
+
const resp = await fetch(configUrl);
|
| 29 |
+
config = await resp.json();
|
| 30 |
+
} catch (e) {
|
| 31 |
+
console.error("Failed loading config:", e);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return;
|
| 33 |
}
|
| 34 |
|
| 35 |
+
// βββ 2. Inject CSS if provided βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 36 |
if (config.css_url) {
|
| 37 |
+
const link = document.createElement('link');
|
| 38 |
+
link.rel = 'stylesheet';
|
| 39 |
+
link.href = config.css_url;
|
| 40 |
+
document.head.appendChild(link);
|
| 41 |
}
|
| 42 |
|
| 43 |
+
// βββ 3. Make widget container ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
const widgetContainer = document.createElement('div');
|
| 46 |
+
widgetContainer.id = `ply-widget-container-${instanceId}`;
|
| 47 |
widgetContainer.classList.add('ply-widget-container');
|
| 48 |
+
document.body.appendChild(widgetContainer); // append early so buttons exist
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
// βββ 4. Build inner HTML (buttons + panels) ββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
widgetContainer.innerHTML = `
|
| 52 |
+
<div id="viewer-container-${instanceId}" class="viewer-container"></div>
|
| 53 |
+
|
| 54 |
+
<!-- Controls -->
|
| 55 |
+
<button id="fullscreen-toggle-${instanceId}" class="widget-button fullscreen-toggle">β±</button>
|
| 56 |
+
<button id="help-toggle-${instanceId}" class="widget-button help-toggle">?</button>
|
| 57 |
+
<button id="reset-camera-btn-${instanceId}" class="widget-button reset-camera-btn">β²</button>
|
| 58 |
+
${config.tooltips_url ? `<button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle">β¦Ώ</button>` : ''}
|
| 59 |
+
|
| 60 |
+
<!-- Help panel -->
|
| 61 |
+
<div id="menu-content-${instanceId}" class="menu-content" style="display: block;">
|
| 62 |
+
<span id="help-close-${instanceId}" class="help-close">Γ</span>
|
| 63 |
+
<div class="help-text">
|
| 64 |
+
${(/iPad|iPhone|iPod/.test(navigator.userAgent) ? `
|
| 65 |
+
- Pour vous dΓ©placer, glissez deux doigts.<br>
|
| 66 |
+
- Pour orbiter, un doigt.<br>
|
| 67 |
+
- Pour zoomer, pincez.<br>
|
| 68 |
+
` : `
|
| 69 |
+
- Orbitez avec le clic droit.<br>
|
| 70 |
+
- Zoomez avec la molette.<br>
|
| 71 |
+
- DΓ©placez-vous avec le clic gauche.<br>
|
| 72 |
+
`)}
|
| 73 |
+
${config.tooltips_url ? '- Cliquez sur β¦Ώ pour afficher/masquer les tooltips.<br>' : ''}
|
| 74 |
+
β² RΓ©initialise la camΓ©ra.<br>
|
| 75 |
+
β± Passe en plein Γ©cran.
|
| 76 |
</div>
|
| 77 |
</div>
|
| 78 |
+
|
| 79 |
+
<!-- Tooltip panel -->
|
| 80 |
<div id="tooltip-panel" class="tooltip-panel" style="display: none;">
|
| 81 |
+
<span id="tooltip-close" class="tooltip-close">Γ</span>
|
| 82 |
+
<div id="tooltip-text" class="tooltip-text"></div>
|
| 83 |
+
<img id="tooltip-image" class="tooltip-image" style="display:none" />
|
|
|
|
|
|
|
| 84 |
</div>
|
| 85 |
`;
|
| 86 |
+
|
| 87 |
+
// βββ 5. Grab references **after** DOM insertion ββββββββββββββββββββββββββββββ
|
| 88 |
+
const fullscreenToggle = document.getElementById(`fullscreen-toggle-${instanceId}`);
|
| 89 |
+
const helpToggle = document.getElementById(`help-toggle-${instanceId}`);
|
| 90 |
+
const resetCameraBtn = document.getElementById(`reset-camera-btn-${instanceId}`);
|
| 91 |
+
const tooltipsToggleBtn = document.getElementById(`tooltips-toggle-${instanceId}`);
|
| 92 |
+
const helpCloseBtn = document.getElementById(`help-close-${instanceId}`);
|
| 93 |
+
const menuContent = document.getElementById(`menu-content-${instanceId}`);
|
| 94 |
+
const tooltipCloseBtn = document.getElementById('tooltip-close');
|
| 95 |
+
|
| 96 |
+
// βββ 6. Bind click handlers with debug logs ββββββββββββββββββββββββββββββββ
|
| 97 |
+
fullscreenToggle.addEventListener('click', () => {
|
| 98 |
+
console.log('[UI-Debug] fullscreen clicked');
|
| 99 |
+
// existing fullscreen toggle logicβ¦
|
| 100 |
+
});
|
| 101 |
+
|
| 102 |
+
helpToggle.addEventListener('click', (e) => {
|
| 103 |
+
console.log('[UI-Debug] help clicked');
|
| 104 |
+
e.stopPropagation();
|
| 105 |
+
menuContent.style.display = menuContent.style.display === 'block' ? 'none' : 'block';
|
| 106 |
+
});
|
| 107 |
+
|
| 108 |
+
helpCloseBtn.addEventListener('click', () => {
|
| 109 |
+
console.log('[UI-Debug] help-close clicked');
|
| 110 |
+
menuContent.style.display = 'none';
|
| 111 |
+
});
|
| 112 |
+
|
| 113 |
+
resetCameraBtn.addEventListener('click', () => {
|
| 114 |
+
console.log('[UI-Debug] reset-camera clicked');
|
| 115 |
+
// existing reset-camera logicβ¦
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
+
if (tooltipsToggleBtn) {
|
| 119 |
+
tooltipsToggleBtn.addEventListener('click', () => {
|
| 120 |
+
console.log('[UI-Debug] tooltips-toggle clicked');
|
| 121 |
+
// existing toggle-tooltips logicβ¦
|
| 122 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
|
| 125 |
+
tooltipCloseBtn.addEventListener('click', () => {
|
| 126 |
+
console.log('[UI-Debug] tooltip-close clicked');
|
| 127 |
+
// existing hide-tooltip logicβ¦
|
| 128 |
+
});
|
| 129 |
|
| 130 |
+
// βββ 7. Now load the viewer ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 131 |
try {
|
| 132 |
+
console.log('[UI-Debug] About to import viewer.js');
|
| 133 |
const viewerModule = await import('./viewer.js');
|
|
|
|
| 134 |
await viewerModule.initializeViewer(config, instanceId);
|
|
|
|
| 135 |
} catch (err) {
|
| 136 |
+
console.error("Failed to load viewer.js or initialize:", err);
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
+
// βββ 8. Any additional listeners (wheel, pointermove, etc.) βββββββββββββββββ
|
| 140 |
+
// β¦rest of your existing interface.js codeβ¦
|
|
|
|
| 141 |
|
| 142 |
})();
|