-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Extend jQuery with customized show/hide/toggle #22884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,78 @@ | ||
import $ from 'jquery'; | ||
|
||
window.$ = window.jQuery = $; | ||
|
||
// to remove the calls to "getComputedStyle", it requires that there is no element with "display: none" in its own CSS classes | ||
// for example: `.popup { display: none; ... } <div class="popup">` should be rewritten to `.popup { ... } <div class="popup gt-hidden">` | ||
function getComputedStyleProperty(el, prop) { | ||
const cs = el ? window.getComputedStyle(el) : null; | ||
return cs ? cs[prop] : null; | ||
} | ||
|
||
const defaultDisplayMap = {}; | ||
function getDefaultDisplay(el) { | ||
let display = defaultDisplayMap[el.nodeName]; | ||
if (display) return display; | ||
|
||
const temp = el.ownerDocument.body.appendChild(el.ownerDocument.createElement(el.nodeName)); | ||
display = getComputedStyleProperty(el, 'display'); | ||
temp.parentNode.removeChild(temp); | ||
|
||
display = display === 'none' ? 'block' : display; | ||
defaultDisplayMap[el.nodeName] = display; | ||
return display; | ||
} | ||
|
||
function showHide(elements, show) { | ||
for (const el of elements) { | ||
if (!el || !el.classList) continue; | ||
if (show) { | ||
// at the moment, there are various hiding-methods in Gitea | ||
// in the future, after they are all refactored by "gt-hidden" class, we can remove all others | ||
el.removeAttribute('hidden'); | ||
el.classList.remove('hide', 'gt-hidden'); | ||
if (el.style.display === 'none') el.style.removeProperty('display'); | ||
|
||
if (getComputedStyleProperty(el, 'display') === 'none') { | ||
// after removing all "hidden" related classes/attributes, if the element is still hidden, | ||
// maybe it already has another class with "display: none", so we need to set the "display: xxx" to its style | ||
el.style.display = getDefaultDisplay(el); | ||
} | ||
} else { | ||
el.classList.add('gt-hidden'); | ||
} | ||
} | ||
return elements; | ||
} | ||
|
||
function warnDeprecated(_fn) { | ||
if (!window.config?.runModeIsProd) { | ||
// TODO: not sure whether it is really necessary to deprecated these methods now | ||
// It seems better to introduce Gitea's own helper methods to replace jQuery, instead of adding/removing the class again and again | ||
// console.warn(`jQuery.${fn}() is deprecated`); | ||
} | ||
} | ||
|
||
window.jQuery.fn.extend({ | ||
show () { | ||
warnDeprecated('show'); | ||
return showHide(this, true); | ||
}, | ||
hide () { | ||
warnDeprecated('hide'); | ||
return showHide(this, false); | ||
}, | ||
toggle (state) { | ||
warnDeprecated('toggle'); | ||
if (typeof state === 'boolean') { | ||
return showHide(this, state); | ||
} | ||
return this.each(function () { | ||
if (getComputedStyleProperty(this, 'display') === 'none') { | ||
$(this).show(); | ||
} else { | ||
$(this).hide(); | ||
} | ||
}); | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1803,16 +1803,9 @@ footer { | |
} | ||
} | ||
|
||
// TODO: refactor to use ".gt-hidden" instead (a simple search&replace should do the trick) | ||
.hide { | ||
display: none; | ||
|
||
&.show-outdated { | ||
display: none !important; | ||
} | ||
|
||
&.hide-outdated { | ||
display: none !important; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most hacky part has been removed. |
||
} | ||
|
||
.center:not(.popup) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,6 @@ | |
.show-outdated, | ||
.hide-outdated { | ||
&:extend(.unselectable); | ||
display: block !important; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another hacky part, it is also removed. |
||
|
||
&:hover { | ||
text-decoration: underline; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.