+ // Handle the `aria-haspopup` attribute on the current menu item when it has a sub-menu.
+ function currentMenuItemHasPopup() {
+ var respWidth,
+ $current = $( 'a.wp-has-current-submenu' );
+
+ if ( window.innerWidth ) {
+ respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
+ } else {
+ respWidth = 961;
+ }
+
+ if ( $body.hasClass( 'folded' ) || ( $body.hasClass( 'auto-fold' ) && respWidth && respWidth <= 960 && respWidth > 782 ) ) {
+ // When folded or auto-folded and not responsive view, the current menu item does have a fly-out sub-menu.
+ $current.attr( 'aria-haspopup', 'true' );
+ } else {
+ // When expanded or in responsive view, reset aria-haspopup.
+ $current.attr( 'aria-haspopup', 'false' );
+ }
+ }
+
+ $document.on( 'wp-window-resized wp-responsive-activate wp-responsive-deactivate', currentMenuItemHasPopup );
+
+ /**
+ * Ensure an admin submenu is within the visual viewport.
+ *
+ * @since 4.1.0
+ *
+ * @param {jQuery} $menuItem The parent menu item containing the submenu.
+ */
+ function adjustSubmenu( $menuItem ) {
+ var bottomOffset, pageHeight, adjustment, theFold, menutop, wintop, maxtop,
+ $submenu = $menuItem.find( '.wp-submenu' );
+
+ menutop = $menuItem.offset().top;
+ wintop = $window.scrollTop();
+ maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
+
+ bottomOffset = menutop + $submenu.height() + 1; // Bottom offset of the menu
+ pageHeight = $wpwrap.height(); // Height of the entire page
+ adjustment = 60 + bottomOffset - pageHeight;
+ theFold = $window.height() + wintop - 50; // The fold
+
+ if ( theFold < ( bottomOffset - adjustment ) ) {
+ adjustment = bottomOffset - theFold;
+ }
+
+ if ( adjustment > maxtop ) {
+ adjustment = maxtop;
+ }
+
+ if ( adjustment > 1 ) {
+ $submenu.css( 'margin-top', '-' + adjustment + 'px' );
+ } else {
+ $submenu.css( 'margin-top', '' );
+ }
+ }
+