X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/53f4633144ed68c8b8fb5861f992b5489894a940..607b7e02d77e7326161e8ec15639052d2040f745:/wp-admin/js/common.js diff --git a/wp-admin/js/common.js b/wp-admin/js/common.js index 5b5741c1..200d3e47 100644 --- a/wp-admin/js/common.js +++ b/wp-admin/js/common.js @@ -1,6 +1,10 @@ /* global setUserSetting, ajaxurl, commonL10n, alert, confirm, pagenow */ var showNotice, adminMenu, columns, validateForm, screenMeta; ( function( $, window, undefined ) { + var $document = $( document ), + $window = $( window ), + $body = $( document.body ); + // Removed in 3.3. // (perhaps) needed for back-compat adminMenu = { @@ -70,7 +74,7 @@ columns = { } }; -$(document).ready(function(){columns.init();}); +$document.ready(function(){columns.init();}); validateForm = function( form ) { return !$( form ) @@ -79,7 +83,7 @@ validateForm = function( form ) { .addClass( 'form-invalid' ) .find( 'input:visible' ) .change( function() { $( this ).closest( '.form-invalid' ).removeClass( 'form-invalid' ); } ) - .size(); + .length; }; // stub for doing better warnings @@ -133,7 +137,7 @@ screenMeta = { button.addClass( 'screen-meta-active' ).attr( 'aria-expanded', true ); }); - $( document ).trigger( 'screen:options:open' ); + $document.trigger( 'screen:options:open' ); }, close: function( panel, button ) { @@ -143,7 +147,7 @@ screenMeta = { panel.parent().hide(); }); - $( document ).trigger( 'screen:options:close' ); + $document.trigger( 'screen:options:close' ); } }; @@ -171,17 +175,14 @@ $('.contextual-help-tabs').delegate('a', 'click', function(e) { panel.addClass('active').show(); }); -$(document).ready( function() { - var checks, first, last, checked, sliced, mobileEvent, transitionTimeout, focusedRowActions, $firstHeading, +$document.ready( function() { + var checks, first, last, checked, sliced, mobileEvent, transitionTimeout, focusedRowActions, lastClicked = false, pageInput = $('input.current-page'), currentPage = pageInput.val(), isIOS = /iPhone|iPad|iPod/.test( navigator.userAgent ), isAndroid = navigator.userAgent.indexOf( 'Android' ) !== -1, isIE8 = $( document.documentElement ).hasClass( 'ie8' ), - $document = $( document ), - $window = $( window ), - $body = $( document.body ), $adminMenuWrap = $( '#adminmenuwrap' ), $wpwrap = $( '#wpwrap' ), $adminmenu = $( '#adminmenu' ), @@ -210,7 +211,7 @@ $(document).ready( function() { }); $('#collapse-menu').on('click.collapse-menu', function() { - var body = $( document.body ), respWidth, state; + var respWidth, state; // reset any compensation for submenus near the bottom of the screen $('#adminmenu div.wp-submenu').css('margin-top', ''); @@ -224,31 +225,54 @@ $(document).ready( function() { } if ( respWidth && respWidth < 960 ) { - if ( body.hasClass('auto-fold') ) { - body.removeClass('auto-fold').removeClass('folded'); + if ( $body.hasClass('auto-fold') ) { + $body.removeClass('auto-fold').removeClass('folded'); setUserSetting('unfold', 1); setUserSetting('mfold', 'o'); state = 'open'; } else { - body.addClass('auto-fold'); + $body.addClass('auto-fold'); setUserSetting('unfold', 0); state = 'folded'; } } else { - if ( body.hasClass('folded') ) { - body.removeClass('folded'); + if ( $body.hasClass('folded') ) { + $body.removeClass('folded'); setUserSetting('mfold', 'o'); state = 'open'; } else { - body.addClass('folded'); + $body.addClass('folded'); setUserSetting('mfold', 'f'); state = 'folded'; } } - $( document ).trigger( 'wp-collapse-menu', { state: state } ); + currentMenuItemHasPopup(); + $document.trigger( 'wp-collapse-menu', { state: state } ); }); + // 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. * @@ -289,7 +313,7 @@ $(document).ready( function() { mobileEvent = isIOS ? 'touchstart' : 'click'; // close any open submenus when touch/click is not on the menu - $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) { + $body.on( mobileEvent+'.wp-mobile-hover', function(e) { if ( $adminmenu.data('wp-responsive') ) { return; } @@ -369,37 +393,35 @@ $(document).ready( function() { }); } - // Move .notice, .updated and .error alert boxes. Don't move boxes designed to be inline. - $firstHeading = $( '.wrap > h1:first' ); - - // Back compatibility: if there is no H1, apply to first H2. - if ( ! $firstHeading.length ) { - $firstHeading = $( '.wrap h2:first' ); - } - - $firstHeading.nextAll( 'div.updated, div.error, div.notice' ).addClass( 'below-h2' ); - $( 'div.updated, div.error, div.notice' ).not( '.below-h2, .inline' ).insertAfter( $firstHeading ); + /* + * The `.below-h2` class is here just for backward compatibility with plugins + * that are (incorrectly) using it. Do not use. Use `.inline` instead. See #34570. + */ + $( 'div.updated, div.error, div.notice' ).not( '.inline, .below-h2' ).insertAfter( $( '.wrap h1, .wrap h2' ).first() ); // Make notices dismissible - $( '.notice.is-dismissible' ).each( function() { - var $this = $( this ), - $button = $( '' ), - btnText = commonL10n.dismiss || ''; - - // Ensure plain text - $button.find( '.screen-reader-text' ).text( btnText ); - - $this.append( $button ); - - $button.on( 'click.wp-dismiss-notice', function( event ) { - event.preventDefault(); - $this.fadeTo( 100 , 0, function() { - $(this).slideUp( 100, function() { - $(this).remove(); + function makeNoticesDismissible() { + $( '.notice.is-dismissible' ).each( function() { + var $el = $( this ), + $button = $( '' ), + btnText = commonL10n.dismiss || ''; + + // Ensure plain text + $button.find( '.screen-reader-text' ).text( btnText ); + $button.on( 'click.wp-dismiss-notice', function( event ) { + event.preventDefault(); + $el.fadeTo( 100, 0, function() { + $el.slideUp( 100, function() { + $el.remove(); + }); }); }); + + $el.append( $button ); }); - }); + } + + $document.on( 'wp-updates-notice-added wp-plugin-install-error wp-plugin-update-error wp-plugin-delete-error wp-theme-install-error wp-theme-delete-error', makeNoticesDismissible ); // Init screen meta screenMeta.init(); @@ -409,7 +431,7 @@ $(document).ready( function() { if ( 'undefined' == e.shiftKey ) { return true; } if ( e.shiftKey ) { if ( !lastClicked ) { return true; } - checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' ); + checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' ).filter( ':visible:enabled' ); first = checks.index( lastClicked ); last = checks.index( this ); checked = $(this).prop('checked'); @@ -426,7 +448,7 @@ $(document).ready( function() { lastClicked = this; // toggle "check all" checkboxes - var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible').not(':checked'); + var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible:enabled').not(':checked'); $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() { return ( 0 === unchecked.length ); }); @@ -443,7 +465,7 @@ $(document).ready( function() { $table.children( 'tbody' ).filter(':visible') .children().children('.check-column').find(':checkbox') .prop('checked', function() { - if ( $(this).is(':hidden') ) { + if ( $(this).is(':hidden,:disabled') ) { return false; } @@ -873,10 +895,25 @@ $(document).ready( function() { } }; + // Add an ARIA role `button` to elements that behave like UI controls when JavaScript is on. + function aria_button_if_js() { + $( '.aria-button-if-js' ).attr( 'role', 'button' ); + } + + $( document ).ajaxComplete( function() { + aria_button_if_js(); + }); + window.wpResponsive.init(); setPinMenu(); + currentMenuItemHasPopup(); + makeNoticesDismissible(); + aria_button_if_js(); $document.on( 'wp-pin-menu wp-window-resized.pin-menu postboxes-columnchange.pin-menu postbox-toggled.pin-menu wp-collapse-menu.pin-menu wp-scroll-start.pin-menu', setPinMenu ); + + // Set initial focus on a specific element. + $( '.wp-initial-focus' ).focus(); }); // Fire a custom jQuery event at the end of window resize @@ -884,7 +921,7 @@ $(document).ready( function() { var timeout; function triggerEvent() { - $(document).trigger( 'wp-window-resized' ); + $document.trigger( 'wp-window-resized' ); } function fireOnce() { @@ -892,7 +929,7 @@ $(document).ready( function() { timeout = window.setTimeout( triggerEvent, 200 ); } - $(window).on( 'resize.wp-fire-once', fireOnce ); + $window.on( 'resize.wp-fire-once', fireOnce ); }()); // Make Windows 8 devices play along nicely.