Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

clone_script.tmpl 2.0KB

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-editor')) {
  43. el.href = el.getAttribute('data-href-template').replace('{url}', encodeURIComponent(link));
  44. }
  45. })();
  46. </script>