mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-08-22 07:31:52 +02:00
Ported the function as-is and added comments so we don't forget about this in the future. Fixes: https://github.com/go-gitea/gitea/issues/29462 (cherry picked from commit 82405f808d7b50c3580f26e5ca645e2ed6d284ab)
54 lines
2.1 KiB
Go HTML Template
54 lines
2.1 KiB
Go HTML Template
<script>
|
|
// synchronously set clone button states and urls here to avoid flickering
|
|
// on page load. initRepoCloneLink calls this when proto changes.
|
|
// this applies the protocol-dependant clone url to all elements with the
|
|
// `js-clone-url` and `js-clone-url-vsc` classes.
|
|
// TODO: This localStorage setting should be moved to backend user config
|
|
// so it's available during rendering, then this inline script can be removed.
|
|
(window.updateCloneStates = function() {
|
|
const httpsBtn = document.getElementById('repo-clone-https');
|
|
const sshBtn = document.getElementById('repo-clone-ssh');
|
|
const value = localStorage.getItem('repo-clone-protocol') || 'https';
|
|
const isSSH = value === 'ssh' && sshBtn || value !== 'ssh' && !httpsBtn;
|
|
|
|
if (httpsBtn) {
|
|
httpsBtn.textContent = window.origin.split(':')[0].toUpperCase();
|
|
httpsBtn.classList.toggle('primary', !isSSH);
|
|
httpsBtn.classList.toggle('basic', isSSH);
|
|
}
|
|
if (sshBtn) {
|
|
sshBtn.classList.toggle('primary', isSSH);
|
|
sshBtn.classList.toggle('basic', !isSSH);
|
|
}
|
|
|
|
const btn = isSSH ? sshBtn : httpsBtn;
|
|
if (!btn) return;
|
|
|
|
// NOTE: Keep this function in sync with the one in the js folder
|
|
function toOriginUrl(urlStr) {
|
|
try {
|
|
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
|
|
const {origin, protocol, hostname, port} = window.location;
|
|
const url = new URL(urlStr, origin);
|
|
url.protocol = protocol;
|
|
url.hostname = hostname;
|
|
url.port = port || (protocol === 'https:' ? '443' : '80');
|
|
return url.toString();
|
|
}
|
|
} catch {}
|
|
return urlStr;
|
|
}
|
|
const link = toOriginUrl(btn.getAttribute('data-link'));
|
|
|
|
for (const el of document.getElementsByClassName('js-clone-url')) {
|
|
el[el.nodeName === 'INPUT' ? 'value' : 'textContent'] = link;
|
|
}
|
|
for (const el of document.getElementsByClassName('js-clone-url-vsc')) {
|
|
el['href'] = 'vscode://vscode.git/clone?url=' + encodeURIComponent(link);
|
|
}
|
|
for (const el of document.getElementsByClassName('js-clone-url-vscodium')) {
|
|
el['href'] = 'vscodium://vscode.git/clone?url=' + encodeURIComponent(link);
|
|
}
|
|
})();
|
|
</script>
|