code.gitea.io/gitea@v1.22.3/templates/repo/clone_script.tmpl (about)

     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  
    14  		if (httpsBtn) {
    15  			httpsBtn.textContent = window.origin.split(':')[0].toUpperCase();
    16  			httpsBtn.classList.toggle('primary', !isSSH);
    17  			httpsBtn.classList.toggle('basic', isSSH);
    18  		}
    19  		if (sshBtn) {
    20  			sshBtn.classList.toggle('primary', isSSH);
    21  			sshBtn.classList.toggle('basic', !isSSH);
    22  		}
    23  
    24  		const btn = isSSH ? sshBtn : httpsBtn;
    25  		if (!btn) return;
    26  
    27  		// NOTE: Keep this function in sync with the one in the js folder
    28  		function toOriginUrl(urlStr) {
    29  			try {
    30  				if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
    31  					const {origin, protocol, hostname, port} = window.location;
    32  					const url = new URL(urlStr, origin);
    33  					url.protocol = protocol;
    34  					url.hostname = hostname;
    35  					url.port = port || (protocol === 'https:' ? '443' : '80');
    36  					return url.toString();
    37  				}
    38  			} catch {}
    39  			return urlStr;
    40  		}
    41  		const link = toOriginUrl(btn.getAttribute('data-link'));
    42  
    43  		for (const el of document.getElementsByClassName('js-clone-url')) {
    44  			el[el.nodeName === 'INPUT' ? 'value' : 'textContent'] = link;
    45  		}
    46  		for (const el of document.getElementsByClassName('js-clone-url-editor')) {
    47  			el.href = el.getAttribute('data-href-template').replace('{url}', encodeURIComponent(link));
    48  		}
    49  	})();
    50  </script>