MikaFil commited on
Commit
8b545a5
·
verified ·
1 Parent(s): 6e742f9

Update interface.js

Browse files
Files changed (1) hide show
  1. interface.js +25 -34
interface.js CHANGED
@@ -1,7 +1,6 @@
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() {
@@ -24,15 +23,12 @@ const currentScriptTag = document.currentScript;
24
  let config = {};
25
  if (configUrl) {
26
  try {
27
- const response = await fetch(configUrl);
28
  config = await response.json();
29
  } catch (error) {
30
- // Silent fail, but log in dev environments
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
 
@@ -72,7 +68,7 @@ const currentScriptTag = document.currentScript;
72
  }
73
  }
74
 
75
- // 5. Create the widget container (no GIF preview, no close button)
76
  const widgetContainer = document.createElement('div');
77
  widgetContainer.id = 'ply-widget-container-' + instanceId;
78
  widgetContainer.classList.add('ply-widget-container');
@@ -80,20 +76,18 @@ const currentScriptTag = document.currentScript;
80
  widgetContainer.style.paddingBottom = aspectPercent;
81
  widgetContainer.setAttribute('data-original-aspect', aspectPercent);
82
 
83
- // Conditionally include the “tooltips-toggle” button only if config.tooltips_url is defined
84
  const tooltipsButtonHTML = config.tooltips_url
85
- ? `<button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle">⦿</button>`
86
  : '';
87
 
88
- // Add the 3D-viewer HTML + tooltip + help HTML (all HTML in a template literal!)
89
  widgetContainer.innerHTML = `
90
  <div id="viewer-container-${instanceId}" class="viewer-container">
91
  <div id="progress-dialog-${instanceId}" class="progress-dialog">
92
  <progress id="progress-indicator-${instanceId}" max="100" value="0"></progress>
93
  </div>
94
- <button id="fullscreen-toggle-${instanceId}" class="widget-button fullscreen-toggle">⇱</button>
95
- <button id="help-toggle-${instanceId}" class="widget-button help-toggle">?</button>
96
- <button id="reset-camera-btn-${instanceId}" class="widget-button reset-camera-btn">
97
  <span class="reset-icon">⟲</span>
98
  </button>
99
  ${tooltipsButtonHTML}
@@ -111,7 +105,7 @@ const currentScriptTag = document.currentScript;
111
  </div>
112
  `;
113
 
114
- // Append the widget container immediately after the <script> tag
115
  scriptTag.parentNode.appendChild(widgetContainer);
116
 
117
  // 6. Grab references to new DOM elements
@@ -123,14 +117,12 @@ const currentScriptTag = document.currentScript;
123
  const tooltipsToggleBtn = document.getElementById('tooltips-toggle-' + instanceId);
124
  const menuContent = document.getElementById('menu-content-' + instanceId);
125
  const helpTextDiv = menuContent.querySelector('.help-text');
 
 
 
 
126
 
127
- // Tooltip panel elements
128
- const tooltipPanel = document.getElementById('tooltip-panel');
129
- const tooltipTextDiv = document.getElementById('tooltip-text');
130
- const tooltipImage = document.getElementById('tooltip-image');
131
- const tooltipCloseBtn = document.getElementById('tooltip-close');
132
-
133
- // 6a. Detect mobile vs. desktop
134
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
135
  const isMobile = isIOS || /Android/i.test(navigator.userAgent);
136
 
@@ -139,9 +131,9 @@ const currentScriptTag = document.currentScript;
139
  ? '- Cliquez sur ⦿ pour afficher/masquer les tooltips.<br>'
140
  : '';
141
 
142
- // Fill help text with instructions plus the two new French lines
143
  if (isMobile) {
144
- helpTextDiv.innerHTML =
145
  '- Pour vous déplacer, glissez deux doigts sur l\'écran.<br>' +
146
  '- Pour orbiter, utilisez un doigt.<br>' +
147
  '- Pour zoomer, pincez avec deux doigts.<br>' +
@@ -149,7 +141,7 @@ const currentScriptTag = document.currentScript;
149
  '- ⟲ Réinitialise la caméra.<br>' +
150
  '- ⇱ Passe en plein écran.<br>';
151
  } else {
152
- helpTextDiv.innerHTML =
153
  '- orbitez avec le clic droit<br>' +
154
  '- zoomez avec la molette<br>' +
155
  '- déplacez vous avec le clic gauche<br>' +
@@ -158,14 +150,12 @@ const currentScriptTag = document.currentScript;
158
  '- ⇱ Passe en plein écran.<br>';
159
  }
160
 
161
- // Ensure instructions panel is visible by default
162
  menuContent.style.display = 'block';
163
  viewerContainerElem.style.display = 'block';
164
 
165
- // Variable to hold the drag-hide listener reference
166
  let dragHide = null;
167
-
168
- // Utilities to hide panels
169
  function hideTooltipPanel() {
170
  if (dragHide) {
171
  viewerContainerElem.removeEventListener('pointermove', dragHide);
@@ -180,17 +170,17 @@ const currentScriptTag = document.currentScript;
180
  // 7. Dynamically load viewer.js
181
  let viewerModule;
182
  try {
 
 
183
  viewerModule = await import('https://mikafil-viewer-gs.static.hf.space/viewer.js');
184
  await viewerModule.initializeViewer(config, instanceId);
185
  } catch (err) {
186
- // Do not break UI, but don't continue
187
  return;
188
  }
189
-
190
  const canvasId = 'canvas-' + instanceId;
191
  const canvasEl = document.getElementById(canvasId);
192
 
193
- // 8. Conditional display of tooltips-toggle button
194
  if (tooltipsToggleBtn) {
195
  if (!config.tooltips_url) {
196
  tooltipsToggleBtn.style.display = 'none';
@@ -226,7 +216,6 @@ const currentScriptTag = document.currentScript;
226
  }
227
  };
228
  }
229
-
230
  function restoreOriginalStyles() {
231
  if (!savedState) return;
232
  const aspectToUse = savedState.widget.paddingBottom;
@@ -260,7 +249,6 @@ const currentScriptTag = document.currentScript;
260
 
261
  savedState = null;
262
  }
263
-
264
  function applyFullscreenStyles() {
265
  widgetContainer.style.position = 'fixed';
266
  widgetContainer.style.top = '0';
@@ -286,7 +274,6 @@ const currentScriptTag = document.currentScript;
286
  fullscreenToggle.textContent = '⇲';
287
  isFullscreen = true;
288
  }
289
-
290
  function enterFullscreen() {
291
  if (!savedState) saveCurrentState();
292
  if (isIOS) {
@@ -304,7 +291,6 @@ const currentScriptTag = document.currentScript;
304
  widgetContainer.classList.add('fake-fullscreen');
305
  }
306
  }
307
-
308
  function exitFullscreen() {
309
  if (document.fullscreenElement === widgetContainer && document.exitFullscreen) {
310
  document.exitFullscreen().catch(() => {});
@@ -340,13 +326,16 @@ const currentScriptTag = document.currentScript;
340
  }
341
  });
342
 
 
343
  if (tooltipsToggleBtn) {
344
  let tooltipsVisible = !!config.showTooltipsDefault;
345
  tooltipsToggleBtn.style.opacity = tooltipsVisible ? '1' : '0.5';
 
346
  tooltipsToggleBtn.addEventListener('click', () => {
347
  hideTooltipPanel();
348
  tooltipsVisible = !tooltipsVisible;
349
  tooltipsToggleBtn.style.opacity = tooltipsVisible ? '1' : '0.5';
 
350
  document.dispatchEvent(new CustomEvent('toggle-tooltips', { detail: { visible: tooltipsVisible } }));
351
  });
352
  }
@@ -371,6 +360,8 @@ const currentScriptTag = document.currentScript;
371
  viewerContainerElem.addEventListener('pointermove', dragHide);
372
  });
373
 
 
 
374
  if (canvasEl) {
375
  canvasEl.addEventListener('wheel', hideTooltipPanel, { passive: true });
376
  }
 
1
  // interface.js
2
  // ==============================
3
 
 
4
  const currentScriptTag = document.currentScript;
5
 
6
  (async function() {
 
23
  let config = {};
24
  if (configUrl) {
25
  try {
26
+ const response = await fetch(configUrl, { cache: 'no-store' }); // iOS fix: avoid cache/cors issues
27
  config = await response.json();
28
  } catch (error) {
 
 
29
  return;
30
  }
31
  } else {
 
32
  return;
33
  }
34
 
 
68
  }
69
  }
70
 
71
+ // 5. Create the widget container
72
  const widgetContainer = document.createElement('div');
73
  widgetContainer.id = 'ply-widget-container-' + instanceId;
74
  widgetContainer.classList.add('ply-widget-container');
 
76
  widgetContainer.style.paddingBottom = aspectPercent;
77
  widgetContainer.setAttribute('data-original-aspect', aspectPercent);
78
 
 
79
  const tooltipsButtonHTML = config.tooltips_url
80
+ ? `<button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle" aria-label="Tooltips">⦿</button>`
81
  : '';
82
 
 
83
  widgetContainer.innerHTML = `
84
  <div id="viewer-container-${instanceId}" class="viewer-container">
85
  <div id="progress-dialog-${instanceId}" class="progress-dialog">
86
  <progress id="progress-indicator-${instanceId}" max="100" value="0"></progress>
87
  </div>
88
+ <button id="fullscreen-toggle-${instanceId}" class="widget-button fullscreen-toggle" aria-label="Fullscreen">⇱</button>
89
+ <button id="help-toggle-${instanceId}" class="widget-button help-toggle" aria-label="Help">?</button>
90
+ <button id="reset-camera-btn-${instanceId}" class="widget-button reset-camera-btn" aria-label="Reset Camera">
91
  <span class="reset-icon">⟲</span>
92
  </button>
93
  ${tooltipsButtonHTML}
 
105
  </div>
106
  `;
107
 
108
+ // Append the widget container after the <script> tag
109
  scriptTag.parentNode.appendChild(widgetContainer);
110
 
111
  // 6. Grab references to new DOM elements
 
117
  const tooltipsToggleBtn = document.getElementById('tooltips-toggle-' + instanceId);
118
  const menuContent = document.getElementById('menu-content-' + instanceId);
119
  const helpTextDiv = menuContent.querySelector('.help-text');
120
+ const tooltipPanel = document.getElementById('tooltip-panel');
121
+ const tooltipTextDiv = document.getElementById('tooltip-text');
122
+ const tooltipImage = document.getElementById('tooltip-image');
123
+ const tooltipCloseBtn = document.getElementById('tooltip-close');
124
 
125
+ // 6a. Device detection
 
 
 
 
 
 
126
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
127
  const isMobile = isIOS || /Android/i.test(navigator.userAgent);
128
 
 
131
  ? '- Cliquez sur ⦿ pour afficher/masquer les tooltips.<br>'
132
  : '';
133
 
134
+ // Fill help text with instructions plus French lines
135
  if (isMobile) {
136
+ helpTextDiv.innerHTML =
137
  '- Pour vous déplacer, glissez deux doigts sur l\'écran.<br>' +
138
  '- Pour orbiter, utilisez un doigt.<br>' +
139
  '- Pour zoomer, pincez avec deux doigts.<br>' +
 
141
  '- ⟲ Réinitialise la caméra.<br>' +
142
  '- ⇱ Passe en plein écran.<br>';
143
  } else {
144
+ helpTextDiv.innerHTML =
145
  '- orbitez avec le clic droit<br>' +
146
  '- zoomez avec la molette<br>' +
147
  '- déplacez vous avec le clic gauche<br>' +
 
150
  '- ⇱ Passe en plein écran.<br>';
151
  }
152
 
153
+ // Show help panel by default
154
  menuContent.style.display = 'block';
155
  viewerContainerElem.style.display = 'block';
156
 
157
+ // Drag-to-hide reference
158
  let dragHide = null;
 
 
159
  function hideTooltipPanel() {
160
  if (dragHide) {
161
  viewerContainerElem.removeEventListener('pointermove', dragHide);
 
170
  // 7. Dynamically load viewer.js
171
  let viewerModule;
172
  try {
173
+ // --- iOS model loading fix: set up custom fetch to ensure PLY loads as ArrayBuffer, not text ---
174
+ window.PLY_FORCE_ARRAYBUFFER = true;
175
  viewerModule = await import('https://mikafil-viewer-gs.static.hf.space/viewer.js');
176
  await viewerModule.initializeViewer(config, instanceId);
177
  } catch (err) {
 
178
  return;
179
  }
 
180
  const canvasId = 'canvas-' + instanceId;
181
  const canvasEl = document.getElementById(canvasId);
182
 
183
+ // 8. Tooltips button display: robust logic
184
  if (tooltipsToggleBtn) {
185
  if (!config.tooltips_url) {
186
  tooltipsToggleBtn.style.display = 'none';
 
216
  }
217
  };
218
  }
 
219
  function restoreOriginalStyles() {
220
  if (!savedState) return;
221
  const aspectToUse = savedState.widget.paddingBottom;
 
249
 
250
  savedState = null;
251
  }
 
252
  function applyFullscreenStyles() {
253
  widgetContainer.style.position = 'fixed';
254
  widgetContainer.style.top = '0';
 
274
  fullscreenToggle.textContent = '⇲';
275
  isFullscreen = true;
276
  }
 
277
  function enterFullscreen() {
278
  if (!savedState) saveCurrentState();
279
  if (isIOS) {
 
291
  widgetContainer.classList.add('fake-fullscreen');
292
  }
293
  }
 
294
  function exitFullscreen() {
295
  if (document.fullscreenElement === widgetContainer && document.exitFullscreen) {
296
  document.exitFullscreen().catch(() => {});
 
326
  }
327
  });
328
 
329
+ // Tooltips toggle: robust UI state
330
  if (tooltipsToggleBtn) {
331
  let tooltipsVisible = !!config.showTooltipsDefault;
332
  tooltipsToggleBtn.style.opacity = tooltipsVisible ? '1' : '0.5';
333
+ tooltipsToggleBtn.setAttribute('aria-pressed', tooltipsVisible ? 'true' : 'false');
334
  tooltipsToggleBtn.addEventListener('click', () => {
335
  hideTooltipPanel();
336
  tooltipsVisible = !tooltipsVisible;
337
  tooltipsToggleBtn.style.opacity = tooltipsVisible ? '1' : '0.5';
338
+ tooltipsToggleBtn.setAttribute('aria-pressed', tooltipsVisible ? 'true' : 'false');
339
  document.dispatchEvent(new CustomEvent('toggle-tooltips', { detail: { visible: tooltipsVisible } }));
340
  });
341
  }
 
360
  viewerContainerElem.addEventListener('pointermove', dragHide);
361
  });
362
 
363
+ document.addEventListener('hide-tooltip', hideTooltipPanel);
364
+
365
  if (canvasEl) {
366
  canvasEl.addEventListener('wheel', hideTooltipPanel, { passive: true });
367
  }