1 /* global setUserSetting, ajaxurl, commonL10n, alert, confirm, pagenow */
2 var showNotice, adminMenu, columns, validateForm, screenMeta;
3 ( function( $, window, undefined ) {
5 // (perhaps) needed for back-compat
9 restoreMenuState : function() {},
10 toggle : function() {},
11 favorites : function() {}
14 // show/hide/save table columns
18 $('.hide-column-tog', '#adv-settings').click( function() {
19 var $t = $(this), column = $t.val();
20 if ( $t.prop('checked') )
23 that.unchecked(column);
25 columns.saveManageColumnsState();
29 saveManageColumnsState : function() {
30 var hidden = this.hidden();
32 action: 'hidden-columns',
34 screenoptionnonce: $('#screenoptionnonce').val(),
39 checked : function(column) {
40 $('.column-' + column).removeClass( 'hidden' );
41 this.colSpanChange(+1);
44 unchecked : function(column) {
45 $('.column-' + column).addClass( 'hidden' );
46 this.colSpanChange(-1);
50 return $( '.manage-column[id]' ).filter( ':hidden' ).map(function() {
55 useCheckboxesForHidden : function() {
56 this.hidden = function(){
57 return $('.hide-column-tog').not(':checked').map(function() {
59 return id.substring( id, id.length - 5 );
64 colSpanChange : function(diff) {
65 var $t = $('table').find('.colspanchange'), n;
68 n = parseInt( $t.attr('colspan'), 10 ) + diff;
69 $t.attr('colspan', n.toString());
73 $(document).ready(function(){columns.init();});
75 validateForm = function( form ) {
77 .find( '.form-required' )
78 .filter( function() { return $( 'input:visible', this ).val() === ''; } )
79 .addClass( 'form-invalid' )
80 .find( 'input:visible' )
81 .change( function() { $( this ).closest( '.form-invalid' ).removeClass( 'form-invalid' ); } )
85 // stub for doing better warnings
88 var msg = commonL10n.warnDelete || '';
96 note : function(text) {
102 element: null, // #screen-meta
103 toggles: null, // .screen-meta-toggle
104 page: null, // #wpcontent
107 this.element = $('#screen-meta');
108 this.toggles = $( '#screen-meta-links' ).find( '.show-settings' );
109 this.page = $('#wpcontent');
111 this.toggles.click( this.toggleEvent );
114 toggleEvent: function() {
115 var panel = $( '#' + $( this ).attr( 'aria-controls' ) );
120 if ( panel.is(':visible') )
121 screenMeta.close( panel, $(this) );
123 screenMeta.open( panel, $(this) );
126 open: function( panel, button ) {
128 $( '#screen-meta-links' ).find( '.screen-meta-toggle' ).not( button.parent() ).css( 'visibility', 'hidden' );
130 panel.parent().show();
131 panel.slideDown( 'fast', function() {
133 button.addClass( 'screen-meta-active' ).attr( 'aria-expanded', true );
136 $( document ).trigger( 'screen:options:open' );
139 close: function( panel, button ) {
140 panel.slideUp( 'fast', function() {
141 button.removeClass( 'screen-meta-active' ).attr( 'aria-expanded', false );
142 $('.screen-meta-toggle').css('visibility', '');
143 panel.parent().hide();
146 $( document ).trigger( 'screen:options:close' );
153 $('.contextual-help-tabs').delegate('a', 'click', function(e) {
159 // Don't do anything if the click is for the tab already showing.
160 if ( link.is('.active a') )
164 $('.contextual-help-tabs .active').removeClass('active');
165 link.parent('li').addClass('active');
167 panel = $( link.attr('href') );
170 $('.help-tab-content').not( panel ).removeClass('active').hide();
171 panel.addClass('active').show();
174 $(document).ready( function() {
175 var checks, first, last, checked, sliced, mobileEvent, transitionTimeout, focusedRowActions, $firstHeading,
177 pageInput = $('input.current-page'),
178 currentPage = pageInput.val(),
179 isIOS = /iPhone|iPad|iPod/.test( navigator.userAgent ),
180 isAndroid = navigator.userAgent.indexOf( 'Android' ) !== -1,
181 isIE8 = $( document.documentElement ).hasClass( 'ie8' ),
182 $document = $( document ),
183 $window = $( window ),
184 $body = $( document.body ),
185 $adminMenuWrap = $( '#adminmenuwrap' ),
186 $wpwrap = $( '#wpwrap' ),
187 $adminmenu = $( '#adminmenu' ),
188 $overlay = $( '#wp-responsive-overlay' ),
189 $toolbar = $( '#wp-toolbar' ),
190 $toolbarPopups = $toolbar.find( 'a[aria-haspopup="true"]' ),
191 $sortables = $('.meta-box-sortables'),
192 wpResponsiveActive = false,
193 $adminbar = $( '#wpadminbar' ),
194 lastScrollPosition = 0,
195 pinnedMenuTop = false,
196 pinnedMenuBottom = false,
198 menuIsPinned = false,
200 window: $window.height(),
201 wpwrap: $wpwrap.height(),
202 adminbar: $adminbar.height(),
203 menu: $adminMenuWrap.height()
207 // when the menu is folded, make the fly-out submenu header clickable
208 $adminmenu.on('click.wp-submenu-head', '.wp-submenu-head', function(e){
209 $(e.target).parent().siblings('a').get(0).click();
212 $('#collapse-menu').on('click.collapse-menu', function() {
213 var body = $( document.body ), respWidth, state;
215 // reset any compensation for submenus near the bottom of the screen
216 $('#adminmenu div.wp-submenu').css('margin-top', '');
218 if ( window.innerWidth ) {
219 // window.innerWidth is affected by zooming on phones
220 respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
222 // IE < 9 doesn't support @media CSS rules
226 if ( respWidth && respWidth < 960 ) {
227 if ( body.hasClass('auto-fold') ) {
228 body.removeClass('auto-fold').removeClass('folded');
229 setUserSetting('unfold', 1);
230 setUserSetting('mfold', 'o');
233 body.addClass('auto-fold');
234 setUserSetting('unfold', 0);
238 if ( body.hasClass('folded') ) {
239 body.removeClass('folded');
240 setUserSetting('mfold', 'o');
243 body.addClass('folded');
244 setUserSetting('mfold', 'f');
249 $( document ).trigger( 'wp-collapse-menu', { state: state } );
253 * Ensure an admin submenu is within the visual viewport.
257 * @param {jQuery} $menuItem The parent menu item containing the submenu.
259 function adjustSubmenu( $menuItem ) {
260 var bottomOffset, pageHeight, adjustment, theFold, menutop, wintop, maxtop,
261 $submenu = $menuItem.find( '.wp-submenu' );
263 menutop = $menuItem.offset().top;
264 wintop = $window.scrollTop();
265 maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
267 bottomOffset = menutop + $submenu.height() + 1; // Bottom offset of the menu
268 pageHeight = $wpwrap.height(); // Height of the entire page
269 adjustment = 60 + bottomOffset - pageHeight;
270 theFold = $window.height() + wintop - 50; // The fold
272 if ( theFold < ( bottomOffset - adjustment ) ) {
273 adjustment = bottomOffset - theFold;
276 if ( adjustment > maxtop ) {
280 if ( adjustment > 1 ) {
281 $submenu.css( 'margin-top', '-' + adjustment + 'px' );
283 $submenu.css( 'margin-top', '' );
287 if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device
288 // iOS Safari works with touchstart, the rest work with click
289 mobileEvent = isIOS ? 'touchstart' : 'click';
291 // close any open submenus when touch/click is not on the menu
292 $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
293 if ( $adminmenu.data('wp-responsive') ) {
297 if ( ! $( e.target ).closest( '#adminmenu' ).length ) {
298 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
302 $adminmenu.find( 'a.wp-has-submenu' ).on( mobileEvent + '.wp-mobile-hover', function( event ) {
303 var $menuItem = $(this).parent();
305 if ( $adminmenu.data( 'wp-responsive' ) ) {
309 // Show the sub instead of following the link if:
310 // - the submenu is not open
311 // - the submenu is not shown inline or the menu is not folded
312 if ( ! $menuItem.hasClass( 'opensub' ) && ( ! $menuItem.hasClass( 'wp-menu-open' ) || $menuItem.width() < 40 ) ) {
313 event.preventDefault();
314 adjustSubmenu( $menuItem );
315 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
316 $menuItem.addClass('opensub');
321 if ( ! isIOS && ! isAndroid ) {
322 $adminmenu.find( 'li.wp-has-submenu' ).hoverIntent({
324 var $menuItem = $( this ),
325 $submenu = $menuItem.find( '.wp-submenu' ),
326 top = parseInt( $submenu.css( 'top' ), 10 );
328 if ( isNaN( top ) || top > -5 ) { // the submenu is visible
332 if ( $adminmenu.data( 'wp-responsive' ) ) {
333 // The menu is in responsive mode, bail
337 adjustSubmenu( $menuItem );
338 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
339 $menuItem.addClass( 'opensub' );
342 if ( $adminmenu.data( 'wp-responsive' ) ) {
343 // The menu is in responsive mode, bail
347 $( this ).removeClass( 'opensub' ).find( '.wp-submenu' ).css( 'margin-top', '' );
354 $adminmenu.on( 'focus.adminmenu', '.wp-submenu a', function( event ) {
355 if ( $adminmenu.data( 'wp-responsive' ) ) {
356 // The menu is in responsive mode, bail
360 $( event.target ).closest( 'li.menu-top' ).addClass( 'opensub' );
361 }).on( 'blur.adminmenu', '.wp-submenu a', function( event ) {
362 if ( $adminmenu.data( 'wp-responsive' ) ) {
366 $( event.target ).closest( 'li.menu-top' ).removeClass( 'opensub' );
367 }).find( 'li.wp-has-submenu.wp-not-current-submenu' ).on( 'focusin.adminmenu', function() {
368 adjustSubmenu( $( this ) );
372 // Move .notice, .updated and .error alert boxes. Don't move boxes designed to be inline.
373 $firstHeading = $( '.wrap > h1:first' );
375 // Back compatibility: if there is no H1, apply to first H2.
376 if ( ! $firstHeading.length ) {
377 $firstHeading = $( '.wrap h2:first' );
380 $firstHeading.nextAll( 'div.updated, div.error, div.notice' ).addClass( 'below-h2' );
381 $( 'div.updated, div.error, div.notice' ).not( '.below-h2, .inline' ).insertAfter( $firstHeading );
383 // Make notices dismissible
384 $( '.notice.is-dismissible' ).each( function() {
385 var $this = $( this ),
386 $button = $( '<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>' ),
387 btnText = commonL10n.dismiss || '';
390 $button.find( '.screen-reader-text' ).text( btnText );
392 $this.append( $button );
394 $button.on( 'click.wp-dismiss-notice', function( event ) {
395 event.preventDefault();
396 $this.fadeTo( 100 , 0, function() {
397 $(this).slideUp( 100, function() {
407 // check all checkboxes
408 $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
409 if ( 'undefined' == e.shiftKey ) { return true; }
411 if ( !lastClicked ) { return true; }
412 checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
413 first = checks.index( lastClicked );
414 last = checks.index( this );
415 checked = $(this).prop('checked');
416 if ( 0 < first && 0 < last && first != last ) {
417 sliced = ( last > first ) ? checks.slice( first, last ) : checks.slice( last, first );
418 sliced.prop( 'checked', function() {
419 if ( $(this).closest('tr').is(':visible') )
428 // toggle "check all" checkboxes
429 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible').not(':checked');
430 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
431 return ( 0 === unchecked.length );
437 $('thead, tfoot').find('.check-column :checkbox').on( 'click.wp-toggle-checkboxes', function( event ) {
439 $table = $this.closest( 'table' ),
440 controlChecked = $this.prop('checked'),
441 toggle = event.shiftKey || $this.data('wp-toggle');
443 $table.children( 'tbody' ).filter(':visible')
444 .children().children('.check-column').find(':checkbox')
445 .prop('checked', function() {
446 if ( $(this).is(':hidden') ) {
451 return ! $(this).prop( 'checked' );
452 } else if ( controlChecked ) {
459 $table.children('thead, tfoot').filter(':visible')
460 .children().children('.check-column').find(':checkbox')
461 .prop('checked', function() {
464 } else if ( controlChecked ) {
472 // Show row actions on keyboard focus of its parent container element or any other elements contained within
473 $( '#wpbody-content' ).on({
474 focusin: function() {
475 clearTimeout( transitionTimeout );
476 focusedRowActions = $( this ).find( '.row-actions' );
477 // transitionTimeout is necessary for Firefox, but Chrome won't remove the CSS class without a little help.
478 $( '.row-actions' ).not( this ).removeClass( 'visible' );
479 focusedRowActions.addClass( 'visible' );
481 focusout: function() {
482 // Tabbing between post title and .row-actions links needs a brief pause, otherwise
483 // the .row-actions div gets hidden in transit in some browsers (ahem, Firefox).
484 transitionTimeout = setTimeout( function() {
485 focusedRowActions.removeClass( 'visible' );
488 }, '.has-row-actions' );
490 // Toggle list table rows on small screens
491 $( 'tbody' ).on( 'click', '.toggle-row', function() {
492 $( this ).closest( 'tr' ).toggleClass( 'is-expanded' );
495 $('#default-password-nag-no').click( function() {
496 setUserSetting('default_password_nag', 'hide');
497 $('div.default-password-nag').hide();
502 $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
503 var el = e.target, selStart, selEnd, val, scroll, sel;
505 if ( e.keyCode == 27 ) { // escape key
506 // when pressing Escape: Opera 12 and 27 blur form fields, IE 8 clears them
508 $(el).data('tab-out', true);
512 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
515 if ( $(el).data('tab-out') ) {
516 $(el).data('tab-out', false);
520 selStart = el.selectionStart;
521 selEnd = el.selectionEnd;
524 if ( document.selection ) {
526 sel = document.selection.createRange();
528 } else if ( selStart >= 0 ) {
529 scroll = this.scrollTop;
530 el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
531 el.selectionStart = el.selectionEnd = selStart + 1;
532 this.scrollTop = scroll;
535 if ( e.stopPropagation )
537 if ( e.preventDefault )
541 if ( pageInput.length ) {
542 pageInput.closest('form').submit( function() {
544 // Reset paging var for new filters/searches but not for bulk actions. See #17685.
545 if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
550 $('.search-box input[type="search"], .search-box input[type="submit"]').mousedown(function () {
551 $('select[name^="action"]').val('-1');
554 // Scroll into view when focused
555 $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
556 if ( e.target.scrollIntoView )
557 e.target.scrollIntoView(false);
560 // Disable upload buttons until files are selected
562 var button, input, form = $('form.wp-upload-form');
565 button = form.find('input[type="submit"]');
566 input = form.find('input[type="file"]');
568 function toggleUploadButton() {
569 button.prop('disabled', '' === input.map( function() {
570 return $(this).val();
573 toggleUploadButton();
574 input.on('change', toggleUploadButton);
577 function pinMenu( event ) {
578 var windowPos = $window.scrollTop(),
579 resizing = ! event || event.type !== 'scroll';
581 if ( isIOS || isIE8 || $adminmenu.data( 'wp-responsive' ) ) {
585 if ( height.menu + height.adminbar < height.window ||
586 height.menu + height.adminbar + 20 > height.wpwrap ) {
593 if ( height.menu + height.adminbar > height.window ) {
594 // Check for overscrolling
595 if ( windowPos < 0 ) {
596 if ( ! pinnedMenuTop ) {
597 pinnedMenuTop = true;
598 pinnedMenuBottom = false;
608 } else if ( windowPos + height.window > $document.height() - 1 ) {
609 if ( ! pinnedMenuBottom ) {
610 pinnedMenuBottom = true;
611 pinnedMenuTop = false;
623 if ( windowPos > lastScrollPosition ) {
625 if ( pinnedMenuTop ) {
627 pinnedMenuTop = false;
628 menuTop = $adminMenuWrap.offset().top - height.adminbar - ( windowPos - lastScrollPosition );
630 if ( menuTop + height.menu + height.adminbar < windowPos + height.window ) {
631 menuTop = windowPos + height.window - height.menu - height.adminbar;
635 position: 'absolute',
639 } else if ( ! pinnedMenuBottom && $adminMenuWrap.offset().top + height.menu < windowPos + height.window ) {
641 pinnedMenuBottom = true;
649 } else if ( windowPos < lastScrollPosition ) {
651 if ( pinnedMenuBottom ) {
653 pinnedMenuBottom = false;
654 menuTop = $adminMenuWrap.offset().top - height.adminbar + ( lastScrollPosition - windowPos );
656 if ( menuTop + height.menu > windowPos + height.window ) {
661 position: 'absolute',
665 } else if ( ! pinnedMenuTop && $adminMenuWrap.offset().top >= windowPos + height.adminbar ) {
667 pinnedMenuTop = true;
675 } else if ( resizing ) {
677 pinnedMenuTop = pinnedMenuBottom = false;
678 menuTop = windowPos + height.window - height.menu - height.adminbar - 1;
682 position: 'absolute',
692 lastScrollPosition = windowPos;
695 function resetHeights() {
697 window: $window.height(),
698 wpwrap: $wpwrap.height(),
699 adminbar: $adminbar.height(),
700 menu: $adminMenuWrap.height()
704 function unpinMenu() {
705 if ( isIOS || ! menuIsPinned ) {
709 pinnedMenuTop = pinnedMenuBottom = menuIsPinned = false;
717 function setPinMenu() {
720 if ( $adminmenu.data('wp-responsive') ) {
721 $body.removeClass( 'sticky-menu' );
723 } else if ( height.menu + height.adminbar > height.window ) {
725 $body.removeClass( 'sticky-menu' );
727 $body.addClass( 'sticky-menu' );
733 $window.on( 'scroll.pin-menu', pinMenu );
734 $document.on( 'tinymce-editor-init.pin-menu', function( event, editor ) {
735 editor.on( 'wp-autoresize', resetHeights );
739 window.wpResponsive = {
743 // Modify functionality based on custom activate/deactivate event
744 $document.on( 'wp-responsive-activate.wp-responsive', function() {
746 }).on( 'wp-responsive-deactivate.wp-responsive', function() {
750 $( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
752 // Toggle sidebar when toggle is clicked
753 $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
754 event.preventDefault();
756 // close any open toolbar submenus
757 $adminbar.find( '.hover' ).removeClass( 'hover' );
759 $wpwrap.toggleClass( 'wp-responsive-open' );
760 if ( $wpwrap.hasClass( 'wp-responsive-open' ) ) {
761 $(this).find('a').attr( 'aria-expanded', 'true' );
762 $( '#adminmenu a:first' ).focus();
764 $(this).find('a').attr( 'aria-expanded', 'false' );
769 $adminmenu.on( 'click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
770 if ( ! $adminmenu.data('wp-responsive') ) {
774 $( this ).parent( 'li' ).toggleClass( 'selected' );
775 event.preventDefault();
779 $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
781 // This needs to run later as UI Sortable may be initialized later on $(document).ready()
782 $window.on( 'load.wp-responsive', function() {
783 var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
785 if ( width <= 782 ) {
786 self.disableSortables();
791 activate: function() {
794 if ( ! $body.hasClass( 'auto-fold' ) ) {
795 $body.addClass( 'auto-fold' );
798 $adminmenu.data( 'wp-responsive', 1 );
799 this.disableSortables();
802 deactivate: function() {
804 $adminmenu.removeData('wp-responsive');
805 this.enableSortables();
808 trigger: function() {
811 if ( window.innerWidth ) {
812 // window.innerWidth is affected by zooming on phones
813 width = Math.max( window.innerWidth, document.documentElement.clientWidth );
815 // Exclude IE < 9, it doesn't support @media CSS rules
819 if ( width <= 782 ) {
820 if ( ! wpResponsiveActive ) {
821 $document.trigger( 'wp-responsive-activate' );
822 wpResponsiveActive = true;
825 if ( wpResponsiveActive ) {
826 $document.trigger( 'wp-responsive-deactivate' );
827 wpResponsiveActive = false;
831 if ( width <= 480 ) {
832 this.enableOverlay();
834 this.disableOverlay();
838 enableOverlay: function() {
839 if ( $overlay.length === 0 ) {
840 $overlay = $( '<div id="wp-responsive-overlay"></div>' )
841 .insertAfter( '#wpcontent' )
843 .on( 'click.wp-responsive', function() {
844 $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
849 $toolbarPopups.on( 'click.wp-responsive', function() {
854 disableOverlay: function() {
855 $toolbarPopups.off( 'click.wp-responsive' );
859 disableSortables: function() {
860 if ( $sortables.length ) {
862 $sortables.sortable('disable');
867 enableSortables: function() {
868 if ( $sortables.length ) {
870 $sortables.sortable('enable');
876 window.wpResponsive.init();
879 $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 );
882 // Fire a custom jQuery event at the end of window resize
886 function triggerEvent() {
887 $(document).trigger( 'wp-window-resized' );
890 function fireOnce() {
891 window.clearTimeout( timeout );
892 timeout = window.setTimeout( triggerEvent, 200 );
895 $(window).on( 'resize.wp-fire-once', fireOnce );
898 // Make Windows 8 devices play along nicely.
900 if ( '-ms-user-select' in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/) ) {
901 var msViewportStyle = document.createElement( 'style' );
902 msViewportStyle.appendChild(
903 document.createTextNode( '@-ms-viewport{width:auto!important}' )
905 document.getElementsByTagName( 'head' )[0].appendChild( msViewportStyle );
909 }( jQuery, window ));