bilca commited on
Commit
945a2d4
Β·
verified Β·
1 Parent(s): a513587

Update interface.js

Browse files
Files changed (1) hide show
  1. interface.js +84 -101
interface.js CHANGED
@@ -47,10 +47,7 @@ const currentScriptTag = document.currentScript;
47
  // ─── 3. Generate a unique instanceId for this widget ───────────────────────────
48
  const instanceId = Math.random().toString(36).substr(2, 8);
49
 
50
- // ─── 4. Determine whether aspect was provided in config ────────────────────────
51
- const hasConfigAspect = !!config.aspect;
52
-
53
- // ─── 5. Compute the aspect ratio (padding-bottom %) ────────────────────────────
54
  let aspectPercent = "100%";
55
  if (config.aspect) {
56
  if (config.aspect.includes(":")) {
@@ -67,24 +64,23 @@ const currentScriptTag = document.currentScript;
67
  }
68
  }
69
  } else {
70
- // No config.aspect β†’ compute from parent container
71
  const parentContainer = scriptTag.parentNode;
72
- const containerWidth = parentContainer.clientWidth;
73
- const containerHeight = parentContainer.clientHeight;
74
  if (containerWidth > 0 && containerHeight > 0) {
75
  aspectPercent = (containerHeight / containerWidth * 100) + "%";
76
  }
77
  }
78
 
79
- // ─── 6. Create the widget container (no GIF preview, no close button) ───────────
80
  const widgetContainer = document.createElement('div');
81
  widgetContainer.id = 'ply-widget-container-' + instanceId;
82
  widgetContainer.classList.add('ply-widget-container');
83
- widgetContainer.style.height = "0"; // Height is controlled via padding-bottom
84
  widgetContainer.style.paddingBottom = aspectPercent;
85
  widgetContainer.setAttribute('data-original-aspect', aspectPercent);
86
 
87
- // Add the 3D-viewer HTML + β¦Ώ toggle + tooltip markup + help panel container
88
  widgetContainer.innerHTML = `
89
  <div id="viewer-container-${instanceId}" class="viewer-container">
90
  <div id="progress-dialog-${instanceId}" class="progress-dialog">
@@ -110,7 +106,7 @@ const currentScriptTag = document.currentScript;
110
  // Append the widget container immediately after the <script> tag
111
  scriptTag.parentNode.appendChild(widgetContainer);
112
 
113
- // ─── 7. Grab references to new DOM elements ──────────────────────────────────
114
  const viewerContainerElem = document.getElementById('viewer-container-' + instanceId);
115
  const fullscreenToggle = document.getElementById('fullscreen-toggle-' + instanceId);
116
  const helpToggle = document.getElementById('help-toggle-' + instanceId);
@@ -127,36 +123,21 @@ const currentScriptTag = document.currentScript;
127
  // Device detection for help text
128
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
129
  const isMobile = isIOS || /Android/i.test(navigator.userAgent);
130
-
131
- // Populate instructions panel with close button + text, and show it by default
132
- const instructionsText = isMobile
133
- ? `
134
- <div style="margin-top: 16px;">
135
- - Pour vous dΓ©placer, glissez deux doigts sur l'Γ©cran.<br>
136
- - Pour orbiter, utilisez un doigt.<br>
137
- - Pour zoomer, pincez avec deux doigts.<br>
138
- - Cliquez sur β¦Ώ pour afficher/masquer les points d’information.
139
- </div>
140
- `
141
- : `
142
- <div style="margin-top: 16px;">
143
- - orbitez avec le clic droit<br>
144
- - zoomez avec la molette<br>
145
- - dΓ©placez vous avec le clic gauche<br>
146
- - Cliquez sur β¦Ώ pour afficher/masquer les points d’information.
147
- </div>
148
  `;
149
- menuContent.innerHTML = `
150
- <button id="help-close-${instanceId}" class="help-close">Γ—</button>
151
- <div>${instructionsText}</div>
152
- `;
153
- menuContent.style.display = 'block'; // open by default
154
-
155
- // Listener for the βœ• inside help panel
156
- const helpCloseBtn = document.getElementById(`help-close-${instanceId}`);
157
- helpCloseBtn.addEventListener('click', () => {
158
- menuContent.style.display = 'none';
159
- });
160
 
161
  viewerContainerElem.style.display = 'block';
162
 
@@ -165,14 +146,16 @@ const currentScriptTag = document.currentScript;
165
 
166
  // Utility: hide tooltip and remove drag-hide listener
167
  function hideTooltip() {
 
168
  if (dragHide) {
169
  viewerContainerElem.removeEventListener('pointermove', dragHide);
170
  dragHide = null;
171
  }
 
172
  tooltipDiv.style.display = 'none';
173
  }
174
 
175
- // ─── 8. Dynamically load viewer.js ─────────────────────────────────────────
176
  let viewerModule;
177
  try {
178
  viewerModule = await import('./viewer.js');
@@ -186,6 +169,23 @@ const currentScriptTag = document.currentScript;
186
  const canvasId = 'canvas-' + instanceId;
187
  const canvasEl = document.getElementById(canvasId);
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  // ─── 9. Fullscreen / state-preservation logic ───────────────────────────────
190
  let isFullscreen = false;
191
  let savedState = null;
@@ -201,21 +201,21 @@ const currentScriptTag = document.currentScript;
201
 
202
  savedState = {
203
  widget: {
204
- position: widgetContainer.style.position,
205
- top: widgetContainer.style.top,
206
- left: widgetContainer.style.left,
207
- width: widgetContainer.style.width,
208
- height: widgetContainer.style.height,
209
- maxWidth: widgetContainer.style.maxWidth,
210
- maxHeight: widgetContainer.style.maxHeight,
211
  paddingBottom: widgetContainer.style.paddingBottom || originalAspect,
212
- margin: widgetContainer.style.margin,
213
  aspectPercent: originalAspect,
214
  calculatedAspect: calculatedRatio,
215
- computedWidth: computedWidget.width,
216
- computedHeight: computedWidget.height,
217
- offsetWidth: containerWidth,
218
- offsetHeight: containerHeight
219
  },
220
  viewer: {
221
  borderRadius: viewerContainerElem.style.borderRadius,
@@ -228,19 +228,16 @@ const currentScriptTag = document.currentScript;
228
 
229
  function restoreOriginalStyles() {
230
  if (!savedState) return;
231
- let aspectToUse;
232
-
233
- if (hasConfigAspect) {
 
 
 
 
234
  aspectToUse = savedState.widget.aspectPercent;
235
- } else {
236
- const parentContainer = widgetContainer.parentNode;
237
- const containerWidth = parentContainer.clientWidth;
238
- const containerHeight = parentContainer.clientHeight;
239
- if (containerWidth > 0 && containerHeight > 0) {
240
- aspectToUse = (containerHeight / containerWidth * 100) + "%";
241
- } else {
242
- aspectToUse = savedState.widget.aspectPercent;
243
- }
244
  }
245
 
246
  widgetContainer.style.position = savedState.widget.position || "";
@@ -251,7 +248,6 @@ const currentScriptTag = document.currentScript;
251
  widgetContainer.style.maxWidth = savedState.widget.maxWidth || "";
252
  widgetContainer.style.maxHeight = savedState.widget.maxHeight || "";
253
  widgetContainer.style.paddingBottom= aspectToUse;
254
- widgetContainer.setAttribute('data-original-aspect', aspectToUse);
255
  widgetContainer.style.margin = savedState.widget.margin || "";
256
  widgetContainer.style.border = savedState.widget.border || "";
257
  widgetContainer.style.borderRadius = savedState.widget.borderRadius|| "";
@@ -268,16 +264,12 @@ const currentScriptTag = document.currentScript;
268
  viewerContainerElem.style.borderRadius= savedState.viewer.borderRadius || "";
269
  viewerContainerElem.style.border = savedState.viewer.border || "";
270
 
271
- // Resize PlayCanvas immediately to container dims
272
  if (viewerModule.app) {
273
- const vw = viewerContainerElem.clientWidth;
274
- const vh = viewerContainerElem.clientHeight;
275
- viewerModule.app.resizeCanvas(vw, vh);
276
  }
277
 
278
- // Reset fullscreen button icon back to β€œβ‡±β€
279
- fullscreenToggle.textContent = '⇱';
280
-
281
  savedState = null;
282
  }
283
 
@@ -422,17 +414,19 @@ const currentScriptTag = document.currentScript;
422
  }
423
  });
424
 
425
- // β¦Ώ toggle button
426
  let pointsVisible = !!config.showPointsDefault;
427
- if (!pointsVisible) {
428
- pointsToggleBtn.style.opacity = '0.5';
 
 
 
 
 
 
 
 
429
  }
430
- pointsToggleBtn.addEventListener('click', () => {
431
- hideTooltip();
432
- pointsVisible = !pointsVisible;
433
- pointsToggleBtn.style.opacity = pointsVisible ? '1' : '0.5';
434
- document.dispatchEvent(new CustomEvent('toggle-points', { detail: { visible: pointsVisible } }));
435
- });
436
 
437
  // Close tooltip on button click
438
  tooltipCloseBtn.addEventListener('click', () => {
@@ -477,26 +471,14 @@ const currentScriptTag = document.currentScript;
477
  }
478
  });
479
 
480
- // Window resize β†’ adjust aspectRatios and resize PlayCanvas
481
  window.addEventListener('resize', () => {
482
- if (!hasConfigAspect && !isFullscreen) {
483
- const parentContainer = widgetContainer.parentNode;
484
- const cw = parentContainer.clientWidth;
485
- const ch = parentContainer.clientHeight;
486
- if (cw > 0 && ch > 0) {
487
- const newPercent = (ch / cw * 100) + "%";
488
- widgetContainer.style.paddingBottom = newPercent;
489
- widgetContainer.setAttribute('data-original-aspect', newPercent);
490
- }
491
- }
492
- if (viewerModule.app) {
493
- if (isFullscreen) {
494
- viewerModule.app.resizeCanvas(window.innerWidth, window.innerHeight);
495
- } else {
496
- const vcw = viewerContainerElem.clientWidth;
497
- const vch = viewerContainerElem.clientHeight;
498
- viewerModule.app.resizeCanvas(vcw, vch);
499
- }
500
  }
501
  });
502
 
@@ -505,4 +487,5 @@ const currentScriptTag = document.currentScript;
505
  saveCurrentState();
506
  document.dispatchEvent(new CustomEvent('toggle-points', { detail: { visible: pointsVisible } }));
507
  }, 200);
 
508
  })();
 
47
  // ─── 3. Generate a unique instanceId for this widget ───────────────────────────
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(":")) {
 
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 (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');
79
+ widgetContainer.style.height = "0";
80
  widgetContainer.style.paddingBottom = aspectPercent;
81
  widgetContainer.setAttribute('data-original-aspect', aspectPercent);
82
 
83
+ // Add the 3D-viewer HTML + πŸ“ toggle + tooltip markup
84
  widgetContainer.innerHTML = `
85
  <div id="viewer-container-${instanceId}" class="viewer-container">
86
  <div id="progress-dialog-${instanceId}" class="progress-dialog">
 
106
  // Append the widget container immediately after the <script> tag
107
  scriptTag.parentNode.appendChild(widgetContainer);
108
 
109
+ // ─── 6. Grab references to new DOM elements ──────────────────────────────────
110
  const viewerContainerElem = document.getElementById('viewer-container-' + instanceId);
111
  const fullscreenToggle = document.getElementById('fullscreen-toggle-' + instanceId);
112
  const helpToggle = document.getElementById('help-toggle-' + instanceId);
 
123
  // Device detection for help text
124
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
125
  const isMobile = isIOS || /Android/i.test(navigator.userAgent);
126
+ if (isMobile) {
127
+ menuContent.innerHTML = `
128
+ - Pour vous dΓ©placer, glissez deux doigts sur l'Γ©cran.<br>
129
+ - Pour orbiter, utilisez un doigt.<br>
130
+ - Pour zoomer, pincez avec deux doigts.<br>
131
+ - Cliquez sur β¦Ώ pour afficher/masquer les points d’information.
 
 
 
 
 
 
 
 
 
 
 
 
132
  `;
133
+ } else {
134
+ menuContent.innerHTML = `
135
+ - orbitez avec le clic droit<br>
136
+ - zoomez avec la molette<br>
137
+ - dΓ©placez vous avec le clic gauche<br>
138
+ - Cliquez sur β¦Ώ pour afficher/masquer les points d’information.
139
+ `;
140
+ }
 
 
 
141
 
142
  viewerContainerElem.style.display = 'block';
143
 
 
146
 
147
  // Utility: hide tooltip and remove drag-hide listener
148
  function hideTooltip() {
149
+ // Remove drag-hide listener if present
150
  if (dragHide) {
151
  viewerContainerElem.removeEventListener('pointermove', dragHide);
152
  dragHide = null;
153
  }
154
+ // Hide tooltip DOM
155
  tooltipDiv.style.display = 'none';
156
  }
157
 
158
+ // ─── 7. Dynamically load viewer.js ─────────────────────────────────────────
159
  let viewerModule;
160
  try {
161
  viewerModule = await import('./viewer.js');
 
169
  const canvasId = 'canvas-' + instanceId;
170
  const canvasEl = document.getElementById(canvasId);
171
 
172
+ // ─── 8. Conditional display of points-toggle button ─────────────────────────
173
+ if (!config.points_url) {
174
+ // No points_url provided β†’ hide button
175
+ pointsToggleBtn.style.display = 'none';
176
+ } else {
177
+ // Verify that points.json is reachable
178
+ fetch(config.points_url)
179
+ .then(resp => {
180
+ if (!resp.ok) {
181
+ pointsToggleBtn.style.display = 'none';
182
+ }
183
+ })
184
+ .catch(() => {
185
+ pointsToggleBtn.style.display = 'none';
186
+ });
187
+ }
188
+
189
  // ─── 9. Fullscreen / state-preservation logic ───────────────────────────────
190
  let isFullscreen = false;
191
  let savedState = null;
 
201
 
202
  savedState = {
203
  widget: {
204
+ position: widgetContainer.style.position,
205
+ top: widgetContainer.style.top,
206
+ left: widgetContainer.style.left,
207
+ width: widgetContainer.style.width,
208
+ height: widgetContainer.style.height,
209
+ maxWidth: widgetContainer.style.maxWidth,
210
+ maxHeight: widgetContainer.style.maxHeight,
211
  paddingBottom: widgetContainer.style.paddingBottom || originalAspect,
212
+ margin: widgetContainer.style.margin,
213
  aspectPercent: originalAspect,
214
  calculatedAspect: calculatedRatio,
215
+ computedWidth: computedWidget.width,
216
+ computedHeight: computedWidget.height,
217
+ offsetWidth: containerWidth,
218
+ offsetHeight: containerHeight
219
  },
220
  viewer: {
221
  borderRadius: viewerContainerElem.style.borderRadius,
 
228
 
229
  function restoreOriginalStyles() {
230
  if (!savedState) return;
231
+ let aspectToUse = aspectPercent;
232
+ if (savedState.widget.offsetWidth && savedState.widget.offsetHeight) {
233
+ const actualRatio = (savedState.widget.offsetHeight / savedState.widget.offsetWidth * 100) + '%';
234
+ aspectToUse = actualRatio;
235
+ } else if (savedState.widget.calculatedAspect) {
236
+ aspectToUse = savedState.widget.calculatedAspect;
237
+ } else if (savedState.widget.aspectPercent) {
238
  aspectToUse = savedState.widget.aspectPercent;
239
+ } else if (savedState.widget.paddingBottom) {
240
+ aspectToUse = savedState.widget.paddingBottom;
 
 
 
 
 
 
 
241
  }
242
 
243
  widgetContainer.style.position = savedState.widget.position || "";
 
248
  widgetContainer.style.maxWidth = savedState.widget.maxWidth || "";
249
  widgetContainer.style.maxHeight = savedState.widget.maxHeight || "";
250
  widgetContainer.style.paddingBottom= aspectToUse;
 
251
  widgetContainer.style.margin = savedState.widget.margin || "";
252
  widgetContainer.style.border = savedState.widget.border || "";
253
  widgetContainer.style.borderRadius = savedState.widget.borderRadius|| "";
 
264
  viewerContainerElem.style.borderRadius= savedState.viewer.borderRadius || "";
265
  viewerContainerElem.style.border = savedState.viewer.border || "";
266
 
 
267
  if (viewerModule.app) {
268
+ const cw = viewerContainerElem.clientWidth;
269
+ const ch = viewerContainerElem.clientHeight;
270
+ viewerModule.app.resizeCanvas(cw, ch);
271
  }
272
 
 
 
 
273
  savedState = null;
274
  }
275
 
 
414
  }
415
  });
416
 
417
+ // πŸ“ toggle button (β¦Ώ)
418
  let pointsVisible = !!config.showPointsDefault;
419
+ if (pointsToggleBtn.style.display !== 'none') {
420
+ if (!pointsVisible) {
421
+ pointsToggleBtn.style.opacity = '0.5';
422
+ }
423
+ pointsToggleBtn.addEventListener('click', () => {
424
+ hideTooltip();
425
+ pointsVisible = !pointsVisible;
426
+ pointsToggleBtn.style.opacity = pointsVisible ? '1' : '0.5';
427
+ document.dispatchEvent(new CustomEvent('toggle-points', { detail: { visible: pointsVisible } }));
428
+ });
429
  }
 
 
 
 
 
 
430
 
431
  // Close tooltip on button click
432
  tooltipCloseBtn.addEventListener('click', () => {
 
471
  }
472
  });
473
 
474
+ // Window resize β†’ resize PlayCanvas canvas
475
  window.addEventListener('resize', () => {
476
+ if (isFullscreen && viewerModule.app) {
477
+ viewerModule.app.resizeCanvas(window.innerWidth, window.innerHeight);
478
+ } else if (viewerModule.app) {
479
+ const cw = viewerContainerElem.clientWidth;
480
+ const ch = viewerContainerElem.clientHeight;
481
+ viewerModule.app.resizeCanvas(cw, ch);
 
 
 
 
 
 
 
 
 
 
 
 
482
  }
483
  });
484
 
 
487
  saveCurrentState();
488
  document.dispatchEvent(new CustomEvent('toggle-points', { detail: { visible: pointsVisible } }));
489
  }, 200);
490
+
491
  })();