code.gitea.io/gitea@v1.22.3/web_src/js/features/repo-common.js (about) 1 import $ from 'jquery'; 2 import {hideElem, queryElems, showElem} from '../utils/dom.js'; 3 import {POST} from '../modules/fetch.js'; 4 import {showErrorToast} from '../modules/toast.js'; 5 import {sleep} from '../utils.js'; 6 7 async function onDownloadArchive(e) { 8 e.preventDefault(); 9 // there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list 10 const el = e.target.closest('a.archive-link[href]'); 11 const targetLoading = el.closest('.ui.dropdown') ?? el; 12 targetLoading.classList.add('is-loading', 'loading-icon-2px'); 13 try { 14 for (let tryCount = 0; ;tryCount++) { 15 const response = await POST(el.href); 16 if (!response.ok) throw new Error(`Invalid server response: ${response.status}`); 17 18 const data = await response.json(); 19 if (data.complete) break; 20 await sleep(Math.min((tryCount + 1) * 750, 2000)); 21 } 22 window.location.href = el.href; // the archive is ready, start real downloading 23 } catch (e) { 24 console.error(e); 25 showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500}); 26 } finally { 27 targetLoading.classList.remove('is-loading', 'loading-icon-2px'); 28 } 29 } 30 31 export function initRepoArchiveLinks() { 32 queryElems('a.archive-link[href]', (el) => el.addEventListener('click', onDownloadArchive)); 33 } 34 35 export function initRepoCloneLink() { 36 const $repoCloneSsh = $('#repo-clone-ssh'); 37 const $repoCloneHttps = $('#repo-clone-https'); 38 const $inputLink = $('#repo-clone-url'); 39 40 if ((!$repoCloneSsh.length && !$repoCloneHttps.length) || !$inputLink.length) { 41 return; 42 } 43 44 $repoCloneSsh.on('click', () => { 45 localStorage.setItem('repo-clone-protocol', 'ssh'); 46 window.updateCloneStates(); 47 }); 48 $repoCloneHttps.on('click', () => { 49 localStorage.setItem('repo-clone-protocol', 'https'); 50 window.updateCloneStates(); 51 }); 52 53 $inputLink.on('focus', () => { 54 $inputLink.trigger('select'); 55 }); 56 } 57 58 export function initRepoCommonBranchOrTagDropdown(selector) { 59 $(selector).each(function () { 60 const $dropdown = $(this); 61 $dropdown.find('.reference.column').on('click', function () { 62 hideElem($dropdown.find('.scrolling.reference-list-menu')); 63 showElem($($(this).data('target'))); 64 return false; 65 }); 66 }); 67 } 68 69 export function initRepoCommonFilterSearchDropdown(selector) { 70 const $dropdown = $(selector); 71 if (!$dropdown.length) return; 72 73 $dropdown.dropdown({ 74 fullTextSearch: 'exact', 75 selectOnKeydown: false, 76 onChange(_text, _value, $choice) { 77 if ($choice[0].getAttribute('data-url')) { 78 window.location.href = $choice[0].getAttribute('data-url'); 79 } 80 }, 81 message: {noResults: $dropdown[0].getAttribute('data-no-results')}, 82 }); 83 }