2022-07-31 20:29:55 +02:00
|
|
|
<script>
|
|
|
|
// synchronously set clone button states and urls here to avoid flickering
|
|
|
|
// on page load. initRepoCloneLink calls this when proto changes.
|
2022-09-09 08:38:51 +02:00
|
|
|
// this applies the protocol-dependant clone url to all elements with the
|
|
|
|
// `js-clone-url` and `js-clone-url-vsc` classes.
|
2022-07-31 20:29:55 +02:00
|
|
|
// 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;
|
|
|
|
|
2023-04-07 09:31:41 +02:00
|
|
|
if (httpsBtn) {
|
|
|
|
httpsBtn.textContent = window.origin.split(':')[0].toUpperCase();
|
2023-05-29 14:45:22 +02:00
|
|
|
httpsBtn.classList.toggle('primary', !isSSH);
|
|
|
|
httpsBtn.classList.toggle('basic', isSSH);
|
|
|
|
}
|
|
|
|
if (sshBtn) {
|
|
|
|
sshBtn.classList.toggle('primary', isSSH);
|
|
|
|
sshBtn.classList.toggle('basic', !isSSH);
|
2023-04-07 09:31:41 +02:00
|
|
|
}
|
2022-07-31 20:29:55 +02:00
|
|
|
|
|
|
|
const btn = isSSH ? sshBtn : httpsBtn;
|
|
|
|
if (!btn) return;
|
|
|
|
|
2024-02-28 16:04:04 +01:00
|
|
|
// 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;
|
2023-02-09 10:29:13 +01:00
|
|
|
}
|
2024-02-28 16:04:04 +01:00
|
|
|
const link = toOriginUrl(btn.getAttribute('data-link'));
|
|
|
|
|
2022-07-31 20:29:55 +02:00
|
|
|
for (const el of document.getElementsByClassName('js-clone-url')) {
|
|
|
|
el[el.nodeName === 'INPUT' ? 'value' : 'textContent'] = link;
|
|
|
|
}
|
2024-02-24 14:12:17 +01:00
|
|
|
for (const el of document.getElementsByClassName('js-clone-url-editor')) {
|
|
|
|
el.href = el.getAttribute('data-href-template').replace('{url}', encodeURIComponent(link));
|
2022-09-09 08:38:51 +02:00
|
|
|
}
|
2022-07-31 20:29:55 +02:00
|
|
|
})();
|
|
|
|
</script>
|