You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clone_script.tmpl 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <script>
  2. // synchronously set clone button states and urls here to avoid flickering
  3. // on page load. initRepoCloneLink calls this when proto changes.
  4. // this applies the protocol-dependant clone url to all elements with the
  5. // `js-clone-url` and `js-clone-url-vsc` classes.
  6. // TODO: This localStorage setting should be moved to backend user config
  7. // so it's available during rendering, then this inline script can be removed.
  8. (window.updateCloneStates = function() {
  9. const httpsBtn = document.getElementById('repo-clone-https');
  10. const sshBtn = document.getElementById('repo-clone-ssh');
  11. const value = localStorage.getItem('repo-clone-protocol') || 'https';
  12. const isSSH = value === 'ssh' && sshBtn || value !== 'ssh' && !httpsBtn;
  13. if (httpsBtn) {
  14. httpsBtn.textContent = window.origin.split(':')[0].toUpperCase();
  15. httpsBtn.classList.toggle('primary', !isSSH);
  16. httpsBtn.classList.toggle('basic', isSSH);
  17. }
  18. if (sshBtn) {
  19. sshBtn.classList.toggle('primary', isSSH);
  20. sshBtn.classList.toggle('basic', !isSSH);
  21. }
  22. const btn = isSSH ? sshBtn : httpsBtn;
  23. if (!btn) return;
  24. // NOTE: Keep this function in sync with the one in the js folder
  25. function toOriginUrl(urlStr) {
  26. try {
  27. if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
  28. const {origin, protocol, hostname, port} = window.location;
  29. const url = new URL(urlStr, origin);
  30. url.protocol = protocol;
  31. url.hostname = hostname;
  32. url.port = port || (protocol === 'https:' ? '443' : '80');
  33. return url.toString();
  34. }
  35. } catch {}
  36. return urlStr;
  37. }
  38. const link = toOriginUrl(btn.getAttribute('data-link'));
  39. for (const el of document.getElementsByClassName('js-clone-url')) {
  40. el[el.nodeName === 'INPUT' ? 'value' : 'textContent'] = link;
  41. }
  42. for (const el of document.getElementsByClassName('js-clone-url-vsc')) {
  43. el['href'] = 'vscode://vscode.git/clone?url=' + encodeURIComponent(link);
  44. }
  45. })();
  46. </script>