forgejo/web_src/js/features/repo-common.js
wxiaoguang d32af84a10
Refactor hiding-methods, remove jQuery show/hide, remove .hide class, remove inline style=display:none (#22950)
Close #22847

This PR:

* introduce Gitea's own `showElem` and related functions
* remove jQuery show/hide
* remove .hide class
* remove inline style=display:none 

From now on:

do not use:
* "[hidden]" attribute: it's too weak, can not be applied to an element
with "display: flex"
* ".hidden" class: it has been polluted by Fomantic UI in many cases
* inline style="display: none": it's difficult to tweak
* jQuery's show/hide/toggle: it can not show/hide elements with
"display: xxx !important"

only use:
* this ".gt-hidden" class
* showElem/hideElem/toggleElem functions in "utils/dom.js"

cc: @silverwind , this is the all-in-one PR
2023-02-19 12:06:14 +08:00

109 lines
3 KiB
JavaScript

import $ from 'jquery';
import {hideElem, showElem, toggleElem} from '../utils/dom.js';
const {csrfToken} = window.config;
function getArchive($target, url, first) {
$.ajax({
url,
type: 'POST',
data: {
_csrf: csrfToken,
},
complete(xhr) {
if (xhr.status === 200) {
if (!xhr.responseJSON) {
// XXX Shouldn't happen?
$target.closest('.dropdown').children('i').removeClass('loading');
return;
}
if (!xhr.responseJSON.complete) {
$target.closest('.dropdown').children('i').addClass('loading');
// Wait for only three quarters of a second initially, in case it's
// quickly archived.
setTimeout(() => {
getArchive($target, url, false);
}, first ? 750 : 2000);
} else {
// We don't need to continue checking.
$target.closest('.dropdown').children('i').removeClass('loading');
window.location.href = url;
}
}
},
});
}
export function initRepoArchiveLinks() {
$('.archive-link').on('click', function (event) {
event.preventDefault();
const url = $(this).attr('href');
if (!url) return;
getArchive($(event.target), url, true);
});
}
export function initRepoCloneLink() {
const $repoCloneSsh = $('#repo-clone-ssh');
const $repoCloneHttps = $('#repo-clone-https');
const $inputLink = $('#repo-clone-url');
if ((!$repoCloneSsh.length && !$repoCloneHttps.length) || !$inputLink.length) {
return;
}
// restore animation after first init
setTimeout(() => {
$repoCloneSsh.removeClass('gt-no-transition');
$repoCloneHttps.removeClass('gt-no-transition');
}, 100);
$repoCloneSsh.on('click', () => {
localStorage.setItem('repo-clone-protocol', 'ssh');
window.updateCloneStates();
});
$repoCloneHttps.on('click', () => {
localStorage.setItem('repo-clone-protocol', 'https');
window.updateCloneStates();
});
$inputLink.on('focus', () => {
$inputLink.select();
});
}
export function initRepoCommonBranchOrTagDropdown(selector) {
$(selector).each(function () {
const $dropdown = $(this);
$dropdown.find('.reference.column').on('click', function () {
hideElem($dropdown.find('.scrolling.reference-list-menu'));
showElem($($(this).data('target')));
return false;
});
});
}
export function initRepoCommonFilterSearchDropdown(selector) {
const $dropdown = $(selector);
$dropdown.dropdown({
fullTextSearch: 'exact',
selectOnKeydown: false,
onChange(_text, _value, $choice) {
if ($choice.attr('data-url')) {
window.location.href = $choice.attr('data-url');
}
},
message: {noResults: $dropdown.attr('data-no-results')},
});
}
export function initRepoCommonLanguageStats() {
// Language stats
if ($('.language-stats').length > 0) {
$('.language-stats').on('click', (e) => {
e.preventDefault();
toggleElem($('.language-stats-details, .repository-menu'));
});
}
}