MikaFil commited on
Commit
5abede3
Β·
verified Β·
1 Parent(s): 2e7c4cc

Update interface.js

Browse files
Files changed (1) hide show
  1. interface.js +34 -32
interface.js CHANGED
@@ -1,13 +1,24 @@
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
- // ─── 1. Locate the <script> and read data-config ───────────────────────────────
11
  let scriptTag = currentScriptTag;
12
  if (!scriptTag) {
13
  const scripts = document.getElementsByTagName('script');
@@ -22,18 +33,21 @@ const currentScriptTag = document.currentScript;
22
  }
23
  }
24
 
 
25
  const configUrl = scriptTag.getAttribute('data-config');
26
  let config = {};
27
  if (configUrl) {
28
  try {
29
- const response = await fetch(configUrl);
 
30
  config = await response.json();
 
31
  } catch (error) {
32
- console.error("Error loading config file:", error);
33
  return;
34
  }
35
  } else {
36
- console.error("No config file provided. Please set a data-config attribute on the <script> tag.");
37
  return;
38
  }
39
 
@@ -86,7 +100,6 @@ const currentScriptTag = document.currentScript;
86
  ? `<button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle">β¦Ώ</button>`
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">
@@ -112,7 +125,6 @@ const currentScriptTag = document.currentScript;
112
  </div>
113
  `;
114
 
115
- // Append the widget container immediately after the <script> tag
116
  scriptTag.parentNode.appendChild(widgetContainer);
117
 
118
  // ─── 6. Grab references to new DOM elements ──────────────────────────────────
@@ -124,12 +136,10 @@ const currentScriptTag = document.currentScript;
124
  const tooltipsToggleBtn = document.getElementById('tooltips-toggle-' + instanceId);
125
  const menuContent = document.getElementById('menu-content-' + instanceId);
126
  const helpTextDiv = menuContent.querySelector('.help-text');
127
-
128
- // Tooltip panel elements
129
- const tooltipPanel = document.getElementById('tooltip-panel');
130
- const tooltipTextDiv = document.getElementById('tooltip-text');
131
- const tooltipImage = document.getElementById('tooltip-image');
132
- const tooltipCloseBtn = document.getElementById('tooltip-close');
133
 
134
  // ─── 6a. Detect mobile vs. desktop ────────────────────────────────────────────
135
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
@@ -140,7 +150,6 @@ const currentScriptTag = document.currentScript;
140
  ? '- Cliquez sur β¦Ώ pour afficher/masquer les tooltips.<br>'
141
  : '';
142
 
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>
@@ -161,14 +170,10 @@ const currentScriptTag = document.currentScript;
161
  `;
162
  }
163
 
164
- // Ensure instructions panel is visible by default
165
  menuContent.style.display = 'block';
166
  viewerContainerElem.style.display = 'block';
167
 
168
- // Variable to hold the drag-hide listener reference
169
  let dragHide = null;
170
-
171
- // Utilities to hide panels
172
  function hideTooltipPanel() {
173
  if (dragHide) {
174
  viewerContainerElem.removeEventListener('pointermove', dragHide);
@@ -176,42 +181,41 @@ const currentScriptTag = document.currentScript;
176
  }
177
  tooltipPanel.style.display = 'none';
178
  }
179
- function hideHelpPanel() {
180
- menuContent.style.display = 'none';
181
- }
182
 
183
- // ─── 7. Dynamically load viewer.js ─────────────────────────────────────────
184
  let viewerModule;
185
  try {
 
186
  viewerModule = await import('https://mikafil-viewer-gs.static.hf.space/viewer.js');
187
  await viewerModule.initializeViewer(config, instanceId);
 
188
  } catch (err) {
189
- console.error("Failed to load viewer.js or initialize the 3D viewer:", err);
190
  return;
191
  }
192
 
193
  const canvasId = 'canvas-' + instanceId;
194
  const canvasEl = document.getElementById(canvasId);
195
 
196
- // ─── 8. Conditional display of tooltips-toggle button ─────────────────────────
197
  if (tooltipsToggleBtn) {
198
  if (!config.tooltips_url) {
199
  tooltipsToggleBtn.style.display = 'none';
200
  } else {
201
- fetch(config.tooltips_url)
 
202
  .then(resp => { if (!resp.ok) tooltipsToggleBtn.style.display = 'none'; })
203
  .catch(() => { tooltipsToggleBtn.style.display = 'none'; });
204
  }
205
  }
206
 
207
- // ─── 9. Fullscreen / state-preservation logic ───────────────────────────────
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: {
@@ -262,7 +266,6 @@ const currentScriptTag = document.currentScript;
262
  viewerContainerElem.clientHeight
263
  );
264
  }
265
-
266
  savedState = null;
267
  }
268
 
@@ -319,7 +322,7 @@ const currentScriptTag = document.currentScript;
319
  isFullscreen = false;
320
  }
321
 
322
- // ─── 10. Hook up event listeners ───────────────────────────────────────────
323
  fullscreenToggle.addEventListener('click', () => {
324
  hideTooltipPanel();
325
  isFullscreen ? exitFullscreen() : enterFullscreen();
@@ -395,7 +398,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
+ // 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
  }
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
+ logAndAlert("[interface.js] Fetching config: " + configUrl);
42
+ const response = await fetch(configUrl, {mode: "cors"});
43
  config = await response.json();
44
+ logAndAlert("[interface.js] Config loaded");
45
  } catch (error) {
46
+ logAndAlert("Error loading config file: " + error);
47
  return;
48
  }
49
  } else {
50
+ logAndAlert("No config file provided. Please set a data-config attribute on the <script> tag.");
51
  return;
52
  }
53
 
 
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
  </div>
126
  `;
127
 
 
128
  scriptTag.parentNode.appendChild(widgetContainer);
129
 
130
  // ─── 6. Grab references to new DOM elements ──────────────────────────────────
 
136
  const tooltipsToggleBtn = document.getElementById('tooltips-toggle-' + instanceId);
137
  const menuContent = document.getElementById('menu-content-' + instanceId);
138
  const helpTextDiv = menuContent.querySelector('.help-text');
139
+ const tooltipPanel = document.getElementById('tooltip-panel');
140
+ const tooltipTextDiv = document.getElementById('tooltip-text');
141
+ const tooltipImage = document.getElementById('tooltip-image');
142
+ const tooltipCloseBtn = document.getElementById('tooltip-close');
 
 
143
 
144
  // ─── 6a. Detect mobile vs. desktop ────────────────────────────────────────────
145
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
 
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
  `;
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
  }
182
  tooltipPanel.style.display = 'none';
183
  }
184
+ function hideHelpPanel() { menuContent.style.display = 'none'; }
 
 
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
+ logAndAlert("Failed to load viewer.js or initialize the 3D viewer: " + err);
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
+ // Defensive CORS for tooltips.json
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
  viewerContainerElem.clientHeight
267
  );
268
  }
 
269
  savedState = null;
270
  }
271
 
 
322
  isFullscreen = false;
323
  }
324
 
325
+ // ─── 10. Hook up event listeners ──────────────
326
  fullscreenToggle.addEventListener('click', () => {
327
  hideTooltipPanel();
328
  isFullscreen ? exitFullscreen() : enterFullscreen();
 
398
  }
399
  });
400
 
 
401
  setTimeout(() => {
402
  saveCurrentState();
403
  document.dispatchEvent(new CustomEvent('toggle-tooltips', { detail: { visible: !!config.showTooltipsDefault } }));