1 /* global setUserSetting, ajaxurl, commonL10n, alert, confirm, pagenow */
2 var showNotice, adminMenu, columns, validateForm, screenMeta;
3 ( function( $, window, undefined ) {
4 var $document = $( document ),
6 $body = $( document.body );
9 // (perhaps) needed for back-compat
13 restoreMenuState : function() {},
14 toggle : function() {},
15 favorites : function() {}
18 // show/hide/save table columns
22 $('.hide-column-tog', '#adv-settings').click( function() {
23 var $t = $(this), column = $t.val();
24 if ( $t.prop('checked') )
27 that.unchecked(column);
29 columns.saveManageColumnsState();
33 saveManageColumnsState : function() {
34 var hidden = this.hidden();
36 action: 'hidden-columns',
38 screenoptionnonce: $('#screenoptionnonce').val(),
43 checked : function(column) {
44 $('.column-' + column).removeClass( 'hidden' );
45 this.colSpanChange(+1);
48 unchecked : function(column) {
49 $('.column-' + column).addClass( 'hidden' );
50 this.colSpanChange(-1);
54 return $( '.manage-column[id]' ).filter( ':hidden' ).map(function() {
59 useCheckboxesForHidden : function() {
60 this.hidden = function(){
61 return $('.hide-column-tog').not(':checked').map(function() {
63 return id.substring( id, id.length - 5 );
68 colSpanChange : function(diff) {
69 var $t = $('table').find('.colspanchange'), n;
72 n = parseInt( $t.attr('colspan'), 10 ) + diff;
73 $t.attr('colspan', n.toString());
77 $document.ready(function(){columns.init();});
79 validateForm = function( form ) {
81 .find( '.form-required' )
82 .filter( function() { return $( 'input:visible', this ).val() === ''; } )
83 .addClass( 'form-invalid' )
84 .find( 'input:visible' )
85 .change( function() { $( this ).closest( '.form-invalid' ).removeClass( 'form-invalid' ); } )
89 // stub for doing better warnings
92 var msg = commonL10n.warnDelete || '';
100 note : function(text) {
106 element: null, // #screen-meta
107 toggles: null, // .screen-meta-toggle
108 page: null, // #wpcontent
111 this.element = $('#screen-meta');
112 this.toggles = $( '#screen-meta-links' ).find( '.show-settings' );
113 this.page = $('#wpcontent');
115 this.toggles.click( this.toggleEvent );
118 toggleEvent: function() {
119 var panel = $( '#' + $( this ).attr( 'aria-controls' ) );
124 if ( panel.is(':visible') )
125 screenMeta.close( panel, $(this) );
127 screenMeta.open( panel, $(this) );
130 open: function( panel, button ) {
132 $( '#screen-meta-links' ).find( '.screen-meta-toggle' ).not( button.parent() ).css( 'visibility', 'hidden' );
134 panel.parent().show();
135 panel.slideDown( 'fast', function() {
137 button.addClass( 'screen-meta-active' ).attr( 'aria-expanded', true );
140 $document.trigger( 'screen:options:open' );
143 close: function( panel, button ) {
144 panel.slideUp( 'fast', function() {
145 button.removeClass( 'screen-meta-active' ).attr( 'aria-expanded', false );
146 $('.screen-meta-toggle').css('visibility', '');
147 panel.parent().hide();
150 $document.trigger( 'screen:options:close' );
157 $('.contextual-help-tabs').delegate('a', 'click', function(e) {
163 // Don't do anything if the click is for the tab already showing.
164 if ( link.is('.active a') )
168 $('.contextual-help-tabs .active').removeClass('active');
169 link.parent('li').addClass('active');
171 panel = $( link.attr('href') );
174 $('.help-tab-content').not( panel ).removeClass('active').hide();
175 panel.addClass('active').show();
178 $document.ready( function() {
179 var checks, first, last, checked, sliced, mobileEvent, transitionTimeout, focusedRowActions,
181 pageInput = $('input.current-page'),
182 currentPage = pageInput.val(),
183 isIOS = /iPhone|iPad|iPod/.test( navigator.userAgent ),
184 isAndroid = navigator.userAgent.indexOf( 'Android' ) !== -1,
185 isIE8 = $( document.documentElement ).hasClass( 'ie8' ),
186 $adminMenuWrap = $( '#adminmenuwrap' ),
187 $wpwrap = $( '#wpwrap' ),
188 $adminmenu = $( '#adminmenu' ),
189 $overlay = $( '#wp-responsive-overlay' ),
190 $toolbar = $( '#wp-toolbar' ),
191 $toolbarPopups = $toolbar.find( 'a[aria-haspopup="true"]' ),
192 $sortables = $('.meta-box-sortables'),
193 wpResponsiveActive = false,
194 $adminbar = $( '#wpadminbar' ),
195 lastScrollPosition = 0,
196 pinnedMenuTop = false,
197 pinnedMenuBottom = false,
199 menuIsPinned = false,
201 window: $window.height(),
202 wpwrap: $wpwrap.height(),
203 adminbar: $adminbar.height(),
204 menu: $adminMenuWrap.height()
208 // when the menu is folded, make the fly-out submenu header clickable
209 $adminmenu.on('click.wp-submenu-head', '.wp-submenu-head', function(e){
210 $(e.target).parent().siblings('a').get(0).click();
213 $('#collapse-menu').on('click.collapse-menu', function() {
214 var respWidth, state;
216 // reset any compensation for submenus near the bottom of the screen
217 $('#adminmenu div.wp-submenu').css('margin-top', '');
219 if ( window.innerWidth ) {
220 // window.innerWidth is affected by zooming on phones
221 respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
223 // IE < 9 doesn't support @media CSS rules
227 if ( respWidth && respWidth < 960 ) {
228 if ( $body.hasClass('auto-fold') ) {
229 $body.removeClass('auto-fold').removeClass('folded');
230 setUserSetting('unfold', 1);
231 setUserSetting('mfold', 'o');
234 $body.addClass('auto-fold');
235 setUserSetting('unfold', 0);
239 if ( $body.hasClass('folded') ) {
240 $body.removeClass('folded');
241 setUserSetting('mfold', 'o');
244 $body.addClass('folded');
245 setUserSetting('mfold', 'f');
250 currentMenuItemHasPopup();
251 $document.trigger( 'wp-collapse-menu', { state: state } );
254 // Handle the `aria-haspopup` attribute on the current menu item when it has a sub-menu.
255 function currentMenuItemHasPopup() {
257 $current = $( 'a.wp-has-current-submenu' );
259 if ( window.innerWidth ) {
260 respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
265 if ( $body.hasClass( 'folded' ) || ( $body.hasClass( 'auto-fold' ) && respWidth && respWidth <= 960 && respWidth > 782 ) ) {
266 // When folded or auto-folded and not responsive view, the current menu item does have a fly-out sub-menu.
267 $current.attr( 'aria-haspopup', 'true' );
269 // When expanded or in responsive view, reset aria-haspopup.
270 $current.attr( 'aria-haspopup', 'false' );
274 $document.on( 'wp-window-resized wp-responsive-activate wp-responsive-deactivate', currentMenuItemHasPopup );
277 * Ensure an admin submenu is within the visual viewport.
281 * @param {jQuery} $menuItem The parent menu item containing the submenu.
283 function adjustSubmenu( $menuItem ) {
284 var bottomOffset, pageHeight, adjustment, theFold, menutop, wintop, maxtop,
285 $submenu = $menuItem.find( '.wp-submenu' );
287 menutop = $menuItem.offset().top;
288 wintop = $window.scrollTop();
289 maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
291 bottomOffset = menutop + $submenu.height() + 1; // Bottom offset of the menu
292 pageHeight = $wpwrap.height(); // Height of the entire page
293 adjustment = 60 + bottomOffset - pageHeight;
294 theFold = $window.height() + wintop - 50; // The fold
296 if ( theFold < ( bottomOffset - adjustment ) ) {
297 adjustment = bottomOffset - theFold;
300 if ( adjustment > maxtop ) {
304 if ( adjustment > 1 ) {
305 $submenu.css( 'margin-top', '-' + adjustment + 'px' );
307 $submenu.css( 'margin-top', '' );
311 if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device
312 // iOS Safari works with touchstart, the rest work with click
313 mobileEvent = isIOS ? 'touchstart' : 'click';
315 // close any open submenus when touch/click is not on the menu
316 $body.on( mobileEvent+'.wp-mobile-hover', function(e) {
317 if ( $adminmenu.data('wp-responsive') ) {
321 if ( ! $( e.target ).closest( '#adminmenu' ).length ) {
322 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
326 $adminmenu.find( 'a.wp-has-submenu' ).on( mobileEvent + '.wp-mobile-hover', function( event ) {
327 var $menuItem = $(this).parent();
329 if ( $adminmenu.data( 'wp-responsive' ) ) {
333 // Show the sub instead of following the link if:
334 // - the submenu is not open
335 // - the submenu is not shown inline or the menu is not folded
336 if ( ! $menuItem.hasClass( 'opensub' ) && ( ! $menuItem.hasClass( 'wp-menu-open' ) || $menuItem.width() < 40 ) ) {
337 event.preventDefault();
338 adjustSubmenu( $menuItem );
339 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
340 $menuItem.addClass('opensub');
345 if ( ! isIOS && ! isAndroid ) {
346 $adminmenu.find( 'li.wp-has-submenu' ).hoverIntent({
348 var $menuItem = $( this ),
349 $submenu = $menuItem.find( '.wp-submenu' ),
350 top = parseInt( $submenu.css( 'top' ), 10 );
352 if ( isNaN( top ) || top > -5 ) { // the submenu is visible
356 if ( $adminmenu.data( 'wp-responsive' ) ) {
357 // The menu is in responsive mode, bail
361 adjustSubmenu( $menuItem );
362 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
363 $menuItem.addClass( 'opensub' );
366 if ( $adminmenu.data( 'wp-responsive' ) ) {
367 // The menu is in responsive mode, bail
371 $( this ).removeClass( 'opensub' ).find( '.wp-submenu' ).css( 'margin-top', '' );
378 $adminmenu.on( 'focus.adminmenu', '.wp-submenu a', function( event ) {
379 if ( $adminmenu.data( 'wp-responsive' ) ) {
380 // The menu is in responsive mode, bail
384 $( event.target ).closest( 'li.menu-top' ).addClass( 'opensub' );
385 }).on( 'blur.adminmenu', '.wp-submenu a', function( event ) {
386 if ( $adminmenu.data( 'wp-responsive' ) ) {
390 $( event.target ).closest( 'li.menu-top' ).removeClass( 'opensub' );
391 }).find( 'li.wp-has-submenu.wp-not-current-submenu' ).on( 'focusin.adminmenu', function() {
392 adjustSubmenu( $( this ) );
397 * The `.below-h2` class is here just for backwards compatibility with plugins
398 * that are (incorrectly) using it. Do not use. Use `.inline` instead. See #34570.
400 $( 'div.updated, div.error, div.notice' ).not( '.inline, .below-h2' ).insertAfter( $( '.wrap' ).children( ':header' ).first() );
402 // Make notices dismissible
403 function makeNoticesDismissible() {
404 $( '.notice.is-dismissible' ).each( function() {
406 $button = $( '<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>' ),
407 btnText = commonL10n.dismiss || '';
410 $button.find( '.screen-reader-text' ).text( btnText );
411 $button.on( 'click.wp-dismiss-notice', function( event ) {
412 event.preventDefault();
413 $el.fadeTo( 100, 0, function() {
414 $el.slideUp( 100, function() {
420 $el.append( $button );
424 $document.on( 'wp-plugin-update-error', function() {
425 makeNoticesDismissible();
431 // check all checkboxes
432 $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
433 if ( 'undefined' == e.shiftKey ) { return true; }
435 if ( !lastClicked ) { return true; }
436 checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' ).filter( ':visible:enabled' );
437 first = checks.index( lastClicked );
438 last = checks.index( this );
439 checked = $(this).prop('checked');
440 if ( 0 < first && 0 < last && first != last ) {
441 sliced = ( last > first ) ? checks.slice( first, last ) : checks.slice( last, first );
442 sliced.prop( 'checked', function() {
443 if ( $(this).closest('tr').is(':visible') )
452 // toggle "check all" checkboxes
453 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible:enabled').not(':checked');
454 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
455 return ( 0 === unchecked.length );
461 $('thead, tfoot').find('.check-column :checkbox').on( 'click.wp-toggle-checkboxes', function( event ) {
463 $table = $this.closest( 'table' ),
464 controlChecked = $this.prop('checked'),
465 toggle = event.shiftKey || $this.data('wp-toggle');
467 $table.children( 'tbody' ).filter(':visible')
468 .children().children('.check-column').find(':checkbox')
469 .prop('checked', function() {
470 if ( $(this).is(':hidden,:disabled') ) {
475 return ! $(this).prop( 'checked' );
476 } else if ( controlChecked ) {
483 $table.children('thead, tfoot').filter(':visible')
484 .children().children('.check-column').find(':checkbox')
485 .prop('checked', function() {
488 } else if ( controlChecked ) {
496 // Show row actions on keyboard focus of its parent container element or any other elements contained within
497 $( '#wpbody-content' ).on({
498 focusin: function() {
499 clearTimeout( transitionTimeout );
500 focusedRowActions = $( this ).find( '.row-actions' );
501 // transitionTimeout is necessary for Firefox, but Chrome won't remove the CSS class without a little help.
502 $( '.row-actions' ).not( this ).removeClass( 'visible' );
503 focusedRowActions.addClass( 'visible' );
505 focusout: function() {
506 // Tabbing between post title and .row-actions links needs a brief pause, otherwise
507 // the .row-actions div gets hidden in transit in some browsers (ahem, Firefox).
508 transitionTimeout = setTimeout( function() {
509 focusedRowActions.removeClass( 'visible' );
512 }, '.has-row-actions' );
514 // Toggle list table rows on small screens
515 $( 'tbody' ).on( 'click', '.toggle-row', function() {
516 $( this ).closest( 'tr' ).toggleClass( 'is-expanded' );
519 $('#default-password-nag-no').click( function() {
520 setUserSetting('default_password_nag', 'hide');
521 $('div.default-password-nag').hide();
526 $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
527 var el = e.target, selStart, selEnd, val, scroll, sel;
529 if ( e.keyCode == 27 ) { // escape key
530 // when pressing Escape: Opera 12 and 27 blur form fields, IE 8 clears them
532 $(el).data('tab-out', true);
536 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
539 if ( $(el).data('tab-out') ) {
540 $(el).data('tab-out', false);
544 selStart = el.selectionStart;
545 selEnd = el.selectionEnd;
548 if ( document.selection ) {
550 sel = document.selection.createRange();
552 } else if ( selStart >= 0 ) {
553 scroll = this.scrollTop;
554 el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
555 el.selectionStart = el.selectionEnd = selStart + 1;
556 this.scrollTop = scroll;
559 if ( e.stopPropagation )
561 if ( e.preventDefault )
565 if ( pageInput.length ) {
566 pageInput.closest('form').submit( function() {
568 // Reset paging var for new filters/searches but not for bulk actions. See #17685.
569 if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
574 $('.search-box input[type="search"], .search-box input[type="submit"]').mousedown(function () {
575 $('select[name^="action"]').val('-1');
578 // Scroll into view when focused
579 $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
580 if ( e.target.scrollIntoView )
581 e.target.scrollIntoView(false);
584 // Disable upload buttons until files are selected
586 var button, input, form = $('form.wp-upload-form');
589 button = form.find('input[type="submit"]');
590 input = form.find('input[type="file"]');
592 function toggleUploadButton() {
593 button.prop('disabled', '' === input.map( function() {
594 return $(this).val();
597 toggleUploadButton();
598 input.on('change', toggleUploadButton);
601 function pinMenu( event ) {
602 var windowPos = $window.scrollTop(),
603 resizing = ! event || event.type !== 'scroll';
605 if ( isIOS || isIE8 || $adminmenu.data( 'wp-responsive' ) ) {
609 if ( height.menu + height.adminbar < height.window ||
610 height.menu + height.adminbar + 20 > height.wpwrap ) {
617 if ( height.menu + height.adminbar > height.window ) {
618 // Check for overscrolling
619 if ( windowPos < 0 ) {
620 if ( ! pinnedMenuTop ) {
621 pinnedMenuTop = true;
622 pinnedMenuBottom = false;
632 } else if ( windowPos + height.window > $document.height() - 1 ) {
633 if ( ! pinnedMenuBottom ) {
634 pinnedMenuBottom = true;
635 pinnedMenuTop = false;
647 if ( windowPos > lastScrollPosition ) {
649 if ( pinnedMenuTop ) {
651 pinnedMenuTop = false;
652 menuTop = $adminMenuWrap.offset().top - height.adminbar - ( windowPos - lastScrollPosition );
654 if ( menuTop + height.menu + height.adminbar < windowPos + height.window ) {
655 menuTop = windowPos + height.window - height.menu - height.adminbar;
659 position: 'absolute',
663 } else if ( ! pinnedMenuBottom && $adminMenuWrap.offset().top + height.menu < windowPos + height.window ) {
665 pinnedMenuBottom = true;
673 } else if ( windowPos < lastScrollPosition ) {
675 if ( pinnedMenuBottom ) {
677 pinnedMenuBottom = false;
678 menuTop = $adminMenuWrap.offset().top - height.adminbar + ( lastScrollPosition - windowPos );
680 if ( menuTop + height.menu > windowPos + height.window ) {
685 position: 'absolute',
689 } else if ( ! pinnedMenuTop && $adminMenuWrap.offset().top >= windowPos + height.adminbar ) {
691 pinnedMenuTop = true;
699 } else if ( resizing ) {
701 pinnedMenuTop = pinnedMenuBottom = false;
702 menuTop = windowPos + height.window - height.menu - height.adminbar - 1;
706 position: 'absolute',
716 lastScrollPosition = windowPos;
719 function resetHeights() {
721 window: $window.height(),
722 wpwrap: $wpwrap.height(),
723 adminbar: $adminbar.height(),
724 menu: $adminMenuWrap.height()
728 function unpinMenu() {
729 if ( isIOS || ! menuIsPinned ) {
733 pinnedMenuTop = pinnedMenuBottom = menuIsPinned = false;
741 function setPinMenu() {
744 if ( $adminmenu.data('wp-responsive') ) {
745 $body.removeClass( 'sticky-menu' );
747 } else if ( height.menu + height.adminbar > height.window ) {
749 $body.removeClass( 'sticky-menu' );
751 $body.addClass( 'sticky-menu' );
757 $window.on( 'scroll.pin-menu', pinMenu );
758 $document.on( 'tinymce-editor-init.pin-menu', function( event, editor ) {
759 editor.on( 'wp-autoresize', resetHeights );
763 window.wpResponsive = {
767 // Modify functionality based on custom activate/deactivate event
768 $document.on( 'wp-responsive-activate.wp-responsive', function() {
770 }).on( 'wp-responsive-deactivate.wp-responsive', function() {
774 $( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
776 // Toggle sidebar when toggle is clicked
777 $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
778 event.preventDefault();
780 // close any open toolbar submenus
781 $adminbar.find( '.hover' ).removeClass( 'hover' );
783 $wpwrap.toggleClass( 'wp-responsive-open' );
784 if ( $wpwrap.hasClass( 'wp-responsive-open' ) ) {
785 $(this).find('a').attr( 'aria-expanded', 'true' );
786 $( '#adminmenu a:first' ).focus();
788 $(this).find('a').attr( 'aria-expanded', 'false' );
793 $adminmenu.on( 'click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
794 if ( ! $adminmenu.data('wp-responsive') ) {
798 $( this ).parent( 'li' ).toggleClass( 'selected' );
799 event.preventDefault();
803 $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
805 // This needs to run later as UI Sortable may be initialized later on $(document).ready()
806 $window.on( 'load.wp-responsive', function() {
807 var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
809 if ( width <= 782 ) {
810 self.disableSortables();
815 activate: function() {
818 if ( ! $body.hasClass( 'auto-fold' ) ) {
819 $body.addClass( 'auto-fold' );
822 $adminmenu.data( 'wp-responsive', 1 );
823 this.disableSortables();
826 deactivate: function() {
828 $adminmenu.removeData('wp-responsive');
829 this.enableSortables();
832 trigger: function() {
835 if ( window.innerWidth ) {
836 // window.innerWidth is affected by zooming on phones
837 width = Math.max( window.innerWidth, document.documentElement.clientWidth );
839 // Exclude IE < 9, it doesn't support @media CSS rules
843 if ( width <= 782 ) {
844 if ( ! wpResponsiveActive ) {
845 $document.trigger( 'wp-responsive-activate' );
846 wpResponsiveActive = true;
849 if ( wpResponsiveActive ) {
850 $document.trigger( 'wp-responsive-deactivate' );
851 wpResponsiveActive = false;
855 if ( width <= 480 ) {
856 this.enableOverlay();
858 this.disableOverlay();
862 enableOverlay: function() {
863 if ( $overlay.length === 0 ) {
864 $overlay = $( '<div id="wp-responsive-overlay"></div>' )
865 .insertAfter( '#wpcontent' )
867 .on( 'click.wp-responsive', function() {
868 $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
873 $toolbarPopups.on( 'click.wp-responsive', function() {
878 disableOverlay: function() {
879 $toolbarPopups.off( 'click.wp-responsive' );
883 disableSortables: function() {
884 if ( $sortables.length ) {
886 $sortables.sortable('disable');
891 enableSortables: function() {
892 if ( $sortables.length ) {
894 $sortables.sortable('enable');
900 window.wpResponsive.init();
902 currentMenuItemHasPopup();
903 makeNoticesDismissible();
905 $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 );
908 // Fire a custom jQuery event at the end of window resize
912 function triggerEvent() {
913 $document.trigger( 'wp-window-resized' );
916 function fireOnce() {
917 window.clearTimeout( timeout );
918 timeout = window.setTimeout( triggerEvent, 200 );
921 $window.on( 'resize.wp-fire-once', fireOnce );
924 // Make Windows 8 devices play along nicely.
926 if ( '-ms-user-select' in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/) ) {
927 var msViewportStyle = document.createElement( 'style' );
928 msViewportStyle.appendChild(
929 document.createTextNode( '@-ms-viewport{width:auto!important}' )
931 document.getElementsByTagName( 'head' )[0].appendChild( msViewportStyle );
935 }( jQuery, window ));