Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | define( [ |
| 2 | "../core", |
| 3 | "../data/var/dataPriv", |
| 4 | "../css/var/isHiddenWithinTree" |
| 5 | ], function( jQuery, dataPriv, isHiddenWithinTree ) { |
| 6 | |
| 7 | "use strict"; |
| 8 | |
| 9 | var defaultDisplayMap = {}; |
| 10 | |
| 11 | function getDefaultDisplay( elem ) { |
| 12 | var temp, |
| 13 | doc = elem.ownerDocument, |
| 14 | nodeName = elem.nodeName, |
| 15 | display = defaultDisplayMap[ nodeName ]; |
| 16 | |
| 17 | if ( display ) { |
| 18 | return display; |
| 19 | } |
| 20 | |
| 21 | temp = doc.body.appendChild( doc.createElement( nodeName ) ); |
| 22 | display = jQuery.css( temp, "display" ); |
| 23 | |
| 24 | temp.parentNode.removeChild( temp ); |
| 25 | |
| 26 | if ( display === "none" ) { |
| 27 | display = "block"; |
| 28 | } |
| 29 | defaultDisplayMap[ nodeName ] = display; |
| 30 | |
| 31 | return display; |
| 32 | } |
| 33 | |
| 34 | function showHide( elements, show ) { |
| 35 | var display, elem, |
| 36 | values = [], |
| 37 | index = 0, |
| 38 | length = elements.length; |
| 39 | |
| 40 | // Determine new display value for elements that need to change |
| 41 | for ( ; index < length; index++ ) { |
| 42 | elem = elements[ index ]; |
| 43 | if ( !elem.style ) { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | display = elem.style.display; |
| 48 | if ( show ) { |
| 49 | |
| 50 | // Since we force visibility upon cascade-hidden elements, an immediate (and slow) |
| 51 | // check is required in this first loop unless we have a nonempty display value (either |
| 52 | // inline or about-to-be-restored) |
| 53 | if ( display === "none" ) { |
| 54 | values[ index ] = dataPriv.get( elem, "display" ) || null; |
| 55 | if ( !values[ index ] ) { |
| 56 | elem.style.display = ""; |
| 57 | } |
| 58 | } |
| 59 | if ( elem.style.display === "" && isHiddenWithinTree( elem ) ) { |
| 60 | values[ index ] = getDefaultDisplay( elem ); |
| 61 | } |
| 62 | } else { |
| 63 | if ( display !== "none" ) { |
| 64 | values[ index ] = "none"; |
| 65 | |
| 66 | // Remember what we're overwriting |
| 67 | dataPriv.set( elem, "display", display ); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Set the display of the elements in a second loop to avoid constant reflow |
| 73 | for ( index = 0; index < length; index++ ) { |
| 74 | if ( values[ index ] != null ) { |
| 75 | elements[ index ].style.display = values[ index ]; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return elements; |
| 80 | } |
| 81 | |
| 82 | jQuery.fn.extend( { |
| 83 | show: function() { |
| 84 | return showHide( this, true ); |
| 85 | }, |
| 86 | hide: function() { |
| 87 | return showHide( this ); |
| 88 | }, |
| 89 | toggle: function( state ) { |
| 90 | if ( typeof state === "boolean" ) { |
| 91 | return state ? this.show() : this.hide(); |
| 92 | } |
| 93 | |
| 94 | return this.each( function() { |
| 95 | if ( isHiddenWithinTree( this ) ) { |
| 96 | jQuery( this ).show(); |
| 97 | } else { |
| 98 | jQuery( this ).hide(); |
| 99 | } |
| 100 | } ); |
| 101 | } |
| 102 | } ); |
| 103 | |
| 104 | return showHide; |
| 105 | } ); |