|
1 | 1 | import $ from 'jquery';
|
2 | 2 |
|
3 | 3 | 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