2026-04-22 22:04:28 +01:00
|
|
|
<script>
|
2026-04-24 08:30:56 +01:00
|
|
|
// Copyright 2026 The Forgejo Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
//
|
2026-04-24 15:45:31 +01:00
|
|
|
// ifcurl — inject "View in 3D" button on .ifc file view and history pages.
|
2026-04-22 22:04:28 +01:00
|
|
|
//
|
|
|
|
|
// Deploy to: /etc/forgejo/templates/custom/footer.tmpl
|
|
|
|
|
//
|
2026-04-24 15:45:31 +01:00
|
|
|
// Uses commit-hash hrefs (always unambiguous) to construct ifc:// URLs.
|
2026-04-22 22:04:28 +01:00
|
|
|
(function () {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2026-04-23 20:48:36 +01:00
|
|
|
var VIEWER_BASE = "/assets/viewer.html";
|
2026-04-22 22:04:28 +01:00
|
|
|
|
|
|
|
|
// Parse /owner/repo/src/commit/{40hex}/{treepath}
|
|
|
|
|
// Returns {repoPath, hash, treePath} or null.
|
|
|
|
|
function parseCommitHref(href) {
|
|
|
|
|
var m = href.match(/^\/([^/]+\/[^/]+)\/src\/commit\/([0-9a-f]{40})\/(.+)$/i);
|
|
|
|
|
if (!m) return null;
|
|
|
|
|
return { repoPath: m[1], hash: m[2], treePath: m[3] };
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 15:45:31 +01:00
|
|
|
function makeViewerLink(info, label, cls) {
|
|
|
|
|
var host = window.location.host;
|
|
|
|
|
var ifcUrl = "ifc://" + host + "/" + info.repoPath
|
|
|
|
|
+ "@" + info.hash
|
|
|
|
|
+ "?path=" + encodeURIComponent(info.treePath);
|
|
|
|
|
var a = document.createElement("a");
|
|
|
|
|
a.href = VIEWER_BASE + "?url=" + encodeURIComponent(ifcUrl);
|
|
|
|
|
a.target = "_blank";
|
|
|
|
|
a.rel = "noopener noreferrer";
|
|
|
|
|
a.className = cls || "ui mini basic button";
|
|
|
|
|
a.textContent = label;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
2026-04-22 22:04:28 +01:00
|
|
|
|
2026-04-24 15:45:31 +01:00
|
|
|
function init() {
|
|
|
|
|
var host = window.location.host;
|
2026-04-22 22:04:28 +01:00
|
|
|
|
2026-04-24 15:45:31 +01:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
// Case 1: single file view — permalink button in .file-header-right.
|
|
|
|
|
// -----------------------------------------------------------------------
|
2026-04-22 22:04:28 +01:00
|
|
|
var btnGroup = document.querySelector(".file-header-right .ui.buttons");
|
2026-04-24 15:45:31 +01:00
|
|
|
if (btnGroup) {
|
|
|
|
|
// Permalink href or current URL when already at a commit.
|
|
|
|
|
var a = document.querySelector('a[href*="/src/commit/"]');
|
|
|
|
|
var href = a ? a.getAttribute("href") : window.location.pathname;
|
|
|
|
|
var info = parseCommitHref(href);
|
|
|
|
|
if (info && info.treePath.toLowerCase().endsWith(".ifc")) {
|
|
|
|
|
btnGroup.appendChild(makeViewerLink(info, "View in 3D"));
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-22 22:04:28 +01:00
|
|
|
|
2026-04-24 15:45:31 +01:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
// Case 2: file history page — inject a "3D" link next to each commit's
|
|
|
|
|
// browse-file link (/src/commit/{hash}/{treepath}).
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
document.querySelectorAll('a[href*="/src/commit/"]').forEach(function(a) {
|
|
|
|
|
var info = parseCommitHref(a.getAttribute("href"));
|
|
|
|
|
if (!info || !info.treePath.toLowerCase().endsWith(".ifc")) return;
|
|
|
|
|
var btn = makeViewerLink(info, "3D");
|
|
|
|
|
btn.style.marginLeft = "4px";
|
|
|
|
|
a.insertAdjacentElement("afterend", btn);
|
|
|
|
|
});
|
2026-04-22 22:04:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (document.readyState === "loading") {
|
|
|
|
|
document.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
} else {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
</script>
|