Spaces:
Running
Running
Update interface.js
Browse files- interface.js +40 -18
interface.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
const currentScriptTag = document.currentScript;
|
| 6 |
|
| 7 |
(async function() {
|
| 8 |
-
// βββ 1. Locate the <script> and
|
| 9 |
let scriptTag = currentScriptTag;
|
| 10 |
if (!scriptTag) {
|
| 11 |
const scripts = document.getElementsByTagName('script');
|
|
@@ -23,16 +23,17 @@ const currentScriptTag = document.currentScript;
|
|
| 23 |
console.error("No config file provided. Please set a data-config attribute on the <script> tag.");
|
| 24 |
return;
|
| 25 |
}
|
|
|
|
| 26 |
let config = {};
|
| 27 |
try {
|
| 28 |
const resp = await fetch(configUrl);
|
| 29 |
config = await resp.json();
|
| 30 |
-
} catch (
|
| 31 |
-
console.error("Error loading config file:",
|
| 32 |
return;
|
| 33 |
}
|
| 34 |
|
| 35 |
-
// βββ 2.
|
| 36 |
if (config.css_url) {
|
| 37 |
const linkEl = document.createElement('link');
|
| 38 |
linkEl.rel = "stylesheet";
|
|
@@ -40,36 +41,55 @@ const currentScriptTag = document.currentScript;
|
|
| 40 |
document.head.appendChild(linkEl);
|
| 41 |
}
|
| 42 |
|
| 43 |
-
// βββ 3.
|
| 44 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
| 45 |
|
| 46 |
-
// βββ 4.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
const widgetContainer = document.createElement('div');
|
| 48 |
widgetContainer.id = 'ply-widget-container-' + instanceId;
|
| 49 |
widgetContainer.classList.add('ply-widget-container');
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
// βββ 5. Build inner HTML, including the progress dialog βββββββββββββββββββββ
|
| 53 |
widgetContainer.innerHTML = `
|
| 54 |
<div id="viewer-container-${instanceId}" class="viewer-container">
|
| 55 |
-
<!-- Progress dialog must exist for viewer.js -->
|
| 56 |
<div id="progress-dialog-${instanceId}" class="progress-dialog">
|
| 57 |
<progress id="progress-indicator-${instanceId}" max="100" value="0"></progress>
|
| 58 |
</div>
|
| 59 |
</div>
|
| 60 |
|
| 61 |
-
<!-- Control buttons -->
|
| 62 |
<button id="fullscreen-toggle-${instanceId}" class="widget-button fullscreen-toggle">β±</button>
|
| 63 |
<button id="help-toggle-${instanceId}" class="widget-button help-toggle">?</button>
|
| 64 |
<button id="reset-camera-btn-${instanceId}" class="widget-button reset-camera-btn">β²</button>
|
| 65 |
-
${
|
| 66 |
|
| 67 |
-
<!-- Help panel -->
|
| 68 |
<div id="menu-content-${instanceId}" class="menu-content" style="display: block;">
|
| 69 |
<span id="help-close-${instanceId}" class="help-close">Γ</span>
|
| 70 |
<div class="help-text">
|
| 71 |
${(/iPad|iPhone|iPod/.test(navigator.userAgent) ? `
|
| 72 |
-
- Pour vous dΓ©placer, glissez deux doigts.<br>
|
| 73 |
- Pour orbiter, un doigt.<br>
|
| 74 |
- Pour zoomer, pincez.<br>
|
| 75 |
` : `
|
|
@@ -83,7 +103,6 @@ const currentScriptTag = document.currentScript;
|
|
| 83 |
</div>
|
| 84 |
</div>
|
| 85 |
|
| 86 |
-
<!-- Tooltip panel -->
|
| 87 |
<div id="tooltip-panel" class="tooltip-panel" style="display: none;">
|
| 88 |
<span id="tooltip-close" class="tooltip-close">Γ</span>
|
| 89 |
<div id="tooltip-text" class="tooltip-text"></div>
|
|
@@ -91,10 +110,13 @@ const currentScriptTag = document.currentScript;
|
|
| 91 |
</div>
|
| 92 |
`;
|
| 93 |
|
| 94 |
-
//
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
// βββ 7.
|
| 98 |
try {
|
| 99 |
const viewerModule = await import('./viewer.js');
|
| 100 |
await viewerModule.initializeViewer(config, instanceId);
|
|
@@ -102,6 +124,6 @@ const currentScriptTag = document.currentScript;
|
|
| 102 |
console.error("Failed to load viewer.js or initialize the 3D viewer:", err);
|
| 103 |
}
|
| 104 |
|
| 105 |
-
// βββ 8. (rest of
|
| 106 |
|
| 107 |
})();
|
|
|
|
| 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');
|
|
|
|
| 23 |
console.error("No config file provided. Please set a data-config attribute on the <script> tag.");
|
| 24 |
return;
|
| 25 |
}
|
| 26 |
+
|
| 27 |
let config = {};
|
| 28 |
try {
|
| 29 |
const resp = await fetch(configUrl);
|
| 30 |
config = await resp.json();
|
| 31 |
+
} catch (err) {
|
| 32 |
+
console.error("Error loading config file:", err);
|
| 33 |
return;
|
| 34 |
}
|
| 35 |
|
| 36 |
+
// βββ 2. Conditionally inject CSS βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 37 |
if (config.css_url) {
|
| 38 |
const linkEl = document.createElement('link');
|
| 39 |
linkEl.rel = "stylesheet";
|
|
|
|
| 41 |
document.head.appendChild(linkEl);
|
| 42 |
}
|
| 43 |
|
| 44 |
+
// βββ 3. Unique widget instance ID βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
| 46 |
|
| 47 |
+
// βββ 4. Compute aspect ratio padding ββββββββββββββββββββββββββββββββββββββββββββ
|
| 48 |
+
let aspectPercent = "100%";
|
| 49 |
+
if (config.aspect) {
|
| 50 |
+
if (config.aspect.includes(":")) {
|
| 51 |
+
const [w, h] = config.aspect.split(":").map(parseFloat);
|
| 52 |
+
if (w > 0 && h > 0) aspectPercent = (h / w * 100) + "%";
|
| 53 |
+
} else if (+config.aspect > 0) {
|
| 54 |
+
aspectPercent = (100 / +config.aspect) + "%";
|
| 55 |
+
}
|
| 56 |
+
} else {
|
| 57 |
+
const parent = scriptTag.parentNode;
|
| 58 |
+
if (parent.offsetWidth && parent.offsetHeight) {
|
| 59 |
+
aspectPercent = (parent.offsetHeight / parent.offsetWidth * 100) + "%";
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// βββ 5. Create container & inner HTML ββββββββββββββββββββββββββββββββββββββββββ
|
| 64 |
const widgetContainer = document.createElement('div');
|
| 65 |
widgetContainer.id = 'ply-widget-container-' + instanceId;
|
| 66 |
widgetContainer.classList.add('ply-widget-container');
|
| 67 |
+
widgetContainer.style.height = "0";
|
| 68 |
+
widgetContainer.style.paddingBottom = aspectPercent;
|
| 69 |
+
widgetContainer.setAttribute('data-original-aspect', aspectPercent);
|
| 70 |
+
|
| 71 |
+
// Conditionally include tooltips-toggle only if a URL is provided
|
| 72 |
+
const tooltipBtn = config.tooltips_url
|
| 73 |
+
? `<button id="tooltips-toggle-${instanceId}" class="widget-button tooltips-toggle">β¦Ώ</button>`
|
| 74 |
+
: '';
|
| 75 |
|
|
|
|
| 76 |
widgetContainer.innerHTML = `
|
| 77 |
<div id="viewer-container-${instanceId}" class="viewer-container">
|
|
|
|
| 78 |
<div id="progress-dialog-${instanceId}" class="progress-dialog">
|
| 79 |
<progress id="progress-indicator-${instanceId}" max="100" value="0"></progress>
|
| 80 |
</div>
|
| 81 |
</div>
|
| 82 |
|
|
|
|
| 83 |
<button id="fullscreen-toggle-${instanceId}" class="widget-button fullscreen-toggle">β±</button>
|
| 84 |
<button id="help-toggle-${instanceId}" class="widget-button help-toggle">?</button>
|
| 85 |
<button id="reset-camera-btn-${instanceId}" class="widget-button reset-camera-btn">β²</button>
|
| 86 |
+
${tooltipBtn}
|
| 87 |
|
|
|
|
| 88 |
<div id="menu-content-${instanceId}" class="menu-content" style="display: block;">
|
| 89 |
<span id="help-close-${instanceId}" class="help-close">Γ</span>
|
| 90 |
<div class="help-text">
|
| 91 |
${(/iPad|iPhone|iPod/.test(navigator.userAgent) ? `
|
| 92 |
+
- Pour vous dΓ©placer, glissez deux doigts sur l'Γ©cran.<br>
|
| 93 |
- Pour orbiter, un doigt.<br>
|
| 94 |
- Pour zoomer, pincez.<br>
|
| 95 |
` : `
|
|
|
|
| 103 |
</div>
|
| 104 |
</div>
|
| 105 |
|
|
|
|
| 106 |
<div id="tooltip-panel" class="tooltip-panel" style="display: none;">
|
| 107 |
<span id="tooltip-close" class="tooltip-close">Γ</span>
|
| 108 |
<div id="tooltip-text" class="tooltip-text"></div>
|
|
|
|
| 110 |
</div>
|
| 111 |
`;
|
| 112 |
|
| 113 |
+
// Insert into the page
|
| 114 |
+
scriptTag.parentNode.appendChild(widgetContainer);
|
| 115 |
+
|
| 116 |
+
// βββ 6. Grab elements & hook up listeners βββββββββββββββββββββββββββββββββββββ
|
| 117 |
+
// (all your existing listener code goes here, unchanged)
|
| 118 |
|
| 119 |
+
// βββ 7. Load and initialize PlayCanvas viewer βββββββββββββββββββββββββββββββββ
|
| 120 |
try {
|
| 121 |
const viewerModule = await import('./viewer.js');
|
| 122 |
await viewerModule.initializeViewer(config, instanceId);
|
|
|
|
| 124 |
console.error("Failed to load viewer.js or initialize the 3D viewer:", err);
|
| 125 |
}
|
| 126 |
|
| 127 |
+
// βββ 8. (rest of interface.jsβ¦) βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 128 |
|
| 129 |
})();
|