diff options
author | Daniel Baumann <daniel@debian.org> | 2024-10-18 20:33:49 +0200 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-12-12 23:57:56 +0100 |
commit | e68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch) | |
tree | 97775d6c13b0f416af55314eb6a89ef792474615 /templates/repo/clone_script.tmpl | |
parent | Initial commit. (diff) | |
download | forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip |
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'templates/repo/clone_script.tmpl')
-rw-r--r-- | templates/repo/clone_script.tmpl | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/templates/repo/clone_script.tmpl b/templates/repo/clone_script.tmpl new file mode 100644 index 0000000..40dae76 --- /dev/null +++ b/templates/repo/clone_script.tmpl @@ -0,0 +1,50 @@ +<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-editor')) { + el.href = el.getAttribute('data-href-template').replace('{url}', encodeURIComponent(link)); + } + })(); +</script> |