Skip to content

Commit 309d8bf

Browse files
committed
fix
1 parent b6d7722 commit 309d8bf

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

web_src/js/jquery.js

+71
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
11
import $ from 'jquery';
22

33
window.$ = window.jQuery = $;
4+
5+
function getComputedStyleProperty(el, prop) {
6+
const cs = el ? window.getComputedStyle(el) : null;
7+
return cs ? cs[prop] : null;
8+
}
9+
10+
const defaultDisplayMap = {};
11+
function getDefaultDisplay(el) {
12+
let display = defaultDisplayMap[el.nodeName];
13+
if (display) return display;
14+
15+
const temp = el.ownerDocument.body.appendChild(el.ownerDocument.createElement(el.nodeName));
16+
display = getComputedStyleProperty(el, 'display');
17+
temp.parentNode.removeChild(temp);
18+
19+
display = display === 'none' ? 'block' : display;
20+
defaultDisplayMap[el.nodeName] = display;
21+
return display;
22+
}
23+
24+
function showHide(elements, show) {
25+
for (const el of elements) {
26+
if (!el || !el.classList) continue;
27+
if (show) {
28+
// at the moment, there are various hiding-methods in Gitea
29+
// in the future, after they are all refactored by "gt-hidden" class, we can remove all others
30+
el.removeAttribute('hidden');
31+
el.classList.remove('hide', 'gt-hidden');
32+
if (el.style.display === 'none') el.style.removeProperty('display');
33+
34+
if (getComputedStyleProperty(el, 'display') === 'none') {
35+
// after removing all "hidden" related classes/attributes, if the element is still hidden,
36+
// maybe it already has another class with "display: none", so we need to set the "display: xxx" to its style
37+
el.style.display = getDefaultDisplay(el);
38+
}
39+
} else {
40+
el.classList.add('gt-hidden');
41+
}
42+
}
43+
return elements;
44+
}
45+
46+
function warnDeprecated(fn) {
47+
if (!window.config?.runModeIsProd) {
48+
console.warn(`jQuery.${fn}() is deprecated, add/remove Gitea specialized helper "gt-hidden" class instead`);
49+
}
50+
}
51+
52+
window.jQuery.fn.extend({
53+
show () {
54+
warnDeprecated('show');
55+
return showHide(this, true);
56+
},
57+
hide () {
58+
warnDeprecated('hide');
59+
return showHide(this, false);
60+
},
61+
toggle (state) {
62+
warnDeprecated('toggle');
63+
if (typeof state === 'boolean') {
64+
return showHide(this, state);
65+
}
66+
return this.each(function () {
67+
if (getComputedStyleProperty(this, 'display') === 'none') {
68+
$(this).show();
69+
} else {
70+
$(this).hide();
71+
}
72+
});
73+
}
74+
});

0 commit comments

Comments
 (0)