code.gitea.io/gitea@v1.21.7/web_src/js/features/repo-common.js (about) 1 import $ from 'jquery'; 2 import {hideElem, showElem} from '../utils/dom.js'; 3 4 const {csrfToken} = window.config; 5 6 function getArchive($target, url, first) { 7 $.ajax({ 8 url, 9 type: 'POST', 10 data: { 11 _csrf: csrfToken, 12 }, 13 complete(xhr) { 14 if (xhr.status === 200) { 15 if (!xhr.responseJSON) { 16 // XXX Shouldn't happen? 17 $target.closest('.dropdown').children('i').removeClass('loading'); 18 return; 19 } 20 21 if (!xhr.responseJSON.complete) { 22 $target.closest('.dropdown').children('i').addClass('loading'); 23 // Wait for only three quarters of a second initially, in case it's 24 // quickly archived. 25 setTimeout(() => { 26 getArchive($target, url, false); 27 }, first ? 750 : 2000); 28 } else { 29 // We don't need to continue checking. 30 $target.closest('.dropdown').children('i').removeClass('loading'); 31 window.location.href = url; 32 } 33 } 34 }, 35 }); 36 } 37 38 export function initRepoArchiveLinks() { 39 $('.archive-link').on('click', function (event) { 40 event.preventDefault(); 41 const url = $(this).attr('href'); 42 if (!url) return; 43 getArchive($(event.target), url, true); 44 }); 45 } 46 47 export function initRepoCloneLink() { 48 const $repoCloneSsh = $('#repo-clone-ssh'); 49 const $repoCloneHttps = $('#repo-clone-https'); 50 const $inputLink = $('#repo-clone-url'); 51 52 if ((!$repoCloneSsh.length && !$repoCloneHttps.length) || !$inputLink.length) { 53 return; 54 } 55 56 $repoCloneSsh.on('click', () => { 57 localStorage.setItem('repo-clone-protocol', 'ssh'); 58 window.updateCloneStates(); 59 }); 60 $repoCloneHttps.on('click', () => { 61 localStorage.setItem('repo-clone-protocol', 'https'); 62 window.updateCloneStates(); 63 }); 64 65 $inputLink.on('focus', () => { 66 $inputLink.trigger('select'); 67 }); 68 } 69 70 export function initRepoCommonBranchOrTagDropdown(selector) { 71 $(selector).each(function () { 72 const $dropdown = $(this); 73 $dropdown.find('.reference.column').on('click', function () { 74 hideElem($dropdown.find('.scrolling.reference-list-menu')); 75 showElem($($(this).data('target'))); 76 return false; 77 }); 78 }); 79 } 80 81 export function initRepoCommonFilterSearchDropdown(selector) { 82 const $dropdown = $(selector); 83 $dropdown.dropdown({ 84 fullTextSearch: 'exact', 85 selectOnKeydown: false, 86 onChange(_text, _value, $choice) { 87 if ($choice.attr('data-url')) { 88 window.location.href = $choice.attr('data-url'); 89 } 90 }, 91 message: {noResults: $dropdown.attr('data-no-results')}, 92 }); 93 }