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).show();
41 this.colSpanChange(+1);
44 unchecked : function(column) {
45 $('.column-' + column).hide();
46 this.colSpanChange(-1);
50 return $('.manage-column').filter(':hidden').map(function() { return this.id; }).get().join(',');
53 useCheckboxesForHidden : function() {
54 this.hidden = function(){
55 return $('.hide-column-tog').not(':checked').map(function() {
57 return id.substring( id, id.length - 5 );
62 colSpanChange : function(diff) {
63 var $t = $('table').find('.colspanchange'), n;
66 n = parseInt( $t.attr('colspan'), 10 ) + diff;
67 $t.attr('colspan', n.toString());
71 $(document).ready(function(){columns.init();});
73 validateForm = function( form ) {
75 .find( '.form-required' )
76 .filter( function() { return $( 'input:visible', this ).val() === ''; } )
77 .addClass( 'form-invalid' )
78 .find( 'input:visible' )
79 .change( function() { $( this ).closest( '.form-invalid' ).removeClass( 'form-invalid' ); } )
83 // stub for doing better warnings
86 var msg = commonL10n.warnDelete || '';
94 note : function(text) {
100 element: null, // #screen-meta
101 toggles: null, // .screen-meta-toggle
102 page: null, // #wpcontent
105 this.element = $('#screen-meta');
106 this.toggles = $('.screen-meta-toggle a');
107 this.page = $('#wpcontent');
109 this.toggles.click( this.toggleEvent );
112 toggleEvent: function( e ) {
113 var panel = $( this.href.replace(/.+#/, '#') );
119 if ( panel.is(':visible') )
120 screenMeta.close( panel, $(this) );
122 screenMeta.open( panel, $(this) );
125 open: function( panel, link ) {
127 $('.screen-meta-toggle').not( link.parent() ).css('visibility', 'hidden');
129 panel.parent().show();
130 panel.slideDown( 'fast', function() {
132 link.addClass('screen-meta-active').attr('aria-expanded', true);
135 $( document ).trigger( 'screen:options:open' );
138 close: function( panel, link ) {
139 panel.slideUp( 'fast', function() {
140 link.removeClass('screen-meta-active').attr('aria-expanded', false);
141 $('.screen-meta-toggle').css('visibility', '');
142 panel.parent().hide();
145 $( document ).trigger( 'screen:options:close' );
152 $('.contextual-help-tabs').delegate('a', 'click', function(e) {
158 // Don't do anything if the click is for the tab already showing.
159 if ( link.is('.active a') )
163 $('.contextual-help-tabs .active').removeClass('active');
164 link.parent('li').addClass('active');
166 panel = $( link.attr('href') );
169 $('.help-tab-content').not( panel ).removeClass('active').hide();
170 panel.addClass('active').show();
173 $(document).ready( function() {
174 var checks, first, last, checked, sliced, mobileEvent, transitionTimeout, focusedRowActions,
176 pageInput = $('input.current-page'),
177 currentPage = pageInput.val(),
178 isIOS = /iPhone|iPad|iPod/.test( navigator.userAgent ),
179 isAndroid = navigator.userAgent.indexOf( 'Android' ) !== -1,
180 isIE8 = $( document.documentElement ).hasClass( 'ie8' ),
181 $document = $( document ),
182 $window = $( window ),
183 $body = $( document.body ),
184 $adminMenuWrap = $( '#adminmenuwrap' ),
185 $wpwrap = $( '#wpwrap' ),
186 $adminmenu = $( '#adminmenu' ),
187 $overlay = $( '#wp-responsive-overlay' ),
188 $toolbar = $( '#wp-toolbar' ),
189 $toolbarPopups = $toolbar.find( 'a[aria-haspopup="true"]' ),
190 $sortables = $('.meta-box-sortables'),
191 wpResponsiveActive = false,
192 $adminbar = $( '#wpadminbar' ),
193 lastScrollPosition = 0,
194 pinnedMenuTop = false,
195 pinnedMenuBottom = false,
197 menuIsPinned = false,
199 window: $window.height(),
200 wpwrap: $wpwrap.height(),
201 adminbar: $adminbar.height(),
202 menu: $adminMenuWrap.height()
206 // when the menu is folded, make the fly-out submenu header clickable
207 $adminmenu.on('click.wp-submenu-head', '.wp-submenu-head', function(e){
208 $(e.target).parent().siblings('a').get(0).click();
211 $('#collapse-menu').on('click.collapse-menu', function() {
212 var body = $( document.body ), respWidth, state;
214 // reset any compensation for submenus near the bottom of the screen
215 $('#adminmenu div.wp-submenu').css('margin-top', '');
217 if ( window.innerWidth ) {
218 // window.innerWidth is affected by zooming on phones
219 respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
221 // IE < 9 doesn't support @media CSS rules
225 if ( respWidth && respWidth < 960 ) {
226 if ( body.hasClass('auto-fold') ) {
227 body.removeClass('auto-fold').removeClass('folded');
228 setUserSetting('unfold', 1);
229 setUserSetting('mfold', 'o');
232 body.addClass('auto-fold');
233 setUserSetting('unfold', 0);
237 if ( body.hasClass('folded') ) {
238 body.removeClass('folded');
239 setUserSetting('mfold', 'o');
242 body.addClass('folded');
243 setUserSetting('mfold', 'f');
248 $( document ).trigger( 'wp-collapse-menu', { state: state } );
252 * Ensure an admin submenu is within the visual viewport.
256 * @param {jQuery} $menuItem The parent menu item containing the submenu.
258 function adjustSubmenu( $menuItem ) {
259 var bottomOffset, pageHeight, adjustment, theFold, menutop, wintop, maxtop,
260 $submenu = $menuItem.find( '.wp-submenu' );
262 menutop = $menuItem.offset().top;
263 wintop = $window.scrollTop();
264 maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
266 bottomOffset = menutop + $submenu.height() + 1; // Bottom offset of the menu
267 pageHeight = $wpwrap.height(); // Height of the entire page
268 adjustment = 60 + bottomOffset - pageHeight;
269 theFold = $window.height() + wintop - 50; // The fold
271 if ( theFold < ( bottomOffset - adjustment ) ) {
272 adjustment = bottomOffset - theFold;
275 if ( adjustment > maxtop ) {
279 if ( adjustment > 1 ) {
280 $submenu.css( 'margin-top', '-' + adjustment + 'px' );
282 $submenu.css( 'margin-top', '' );
286 if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device
287 // iOS Safari works with touchstart, the rest work with click
288 mobileEvent = isIOS ? 'touchstart' : 'click';
290 // close any open submenus when touch/click is not on the menu
291 $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
292 if ( $adminmenu.data('wp-responsive') ) {
296 if ( ! $( e.target ).closest( '#adminmenu' ).length ) {
297 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
301 $adminmenu.find( 'a.wp-has-submenu' ).on( mobileEvent + '.wp-mobile-hover', function( event ) {
302 var $menuItem = $(this).parent();
304 if ( $adminmenu.data( 'wp-responsive' ) ) {
308 // Show the sub instead of following the link if:
309 // - the submenu is not open
310 // - the submenu is not shown inline or the menu is not folded
311 if ( ! $menuItem.hasClass( 'opensub' ) && ( ! $menuItem.hasClass( 'wp-menu-open' ) || $menuItem.width() < 40 ) ) {
312 event.preventDefault();
313 adjustSubmenu( $menuItem );
314 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
315 $menuItem.addClass('opensub');
320 if ( ! isIOS && ! isAndroid ) {
321 $adminmenu.find( 'li.wp-has-submenu' ).hoverIntent({
323 var $menuItem = $( this ),
324 $submenu = $menuItem.find( '.wp-submenu' ),
325 top = parseInt( $submenu.css( 'top' ), 10 );
327 if ( isNaN( top ) || top > -5 ) { // the submenu is visible
331 if ( $adminmenu.data( 'wp-responsive' ) ) {
332 // The menu is in responsive mode, bail
336 adjustSubmenu( $menuItem );
337 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
338 $menuItem.addClass( 'opensub' );
341 if ( $adminmenu.data( 'wp-responsive' ) ) {
342 // The menu is in responsive mode, bail
346 $( this ).removeClass( 'opensub' ).find( '.wp-submenu' ).css( 'margin-top', '' );
353 $adminmenu.on( 'focus.adminmenu', '.wp-submenu a', function( event ) {
354 if ( $adminmenu.data( 'wp-responsive' ) ) {
355 // The menu is in responsive mode, bail
359 $( event.target ).closest( 'li.menu-top' ).addClass( 'opensub' );
360 }).on( 'blur.adminmenu', '.wp-submenu a', function( event ) {
361 if ( $adminmenu.data( 'wp-responsive' ) ) {
365 $( event.target ).closest( 'li.menu-top' ).removeClass( 'opensub' );
366 }).find( 'li.wp-has-submenu.wp-not-current-submenu' ).on( 'focusin.adminmenu', function() {
367 adjustSubmenu( $( this ) );
371 // Move .updated and .error alert boxes. Don't move boxes designed to be inline.
372 $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
373 $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
378 // check all checkboxes
379 $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
380 if ( 'undefined' == e.shiftKey ) { return true; }
382 if ( !lastClicked ) { return true; }
383 checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
384 first = checks.index( lastClicked );
385 last = checks.index( this );
386 checked = $(this).prop('checked');
387 if ( 0 < first && 0 < last && first != last ) {
388 sliced = ( last > first ) ? checks.slice( first, last ) : checks.slice( last, first );
389 sliced.prop( 'checked', function() {
390 if ( $(this).closest('tr').is(':visible') )
399 // toggle "check all" checkboxes
400 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible').not(':checked');
401 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
402 return ( 0 === unchecked.length );
408 $('thead, tfoot').find('.check-column :checkbox').on( 'click.wp-toggle-checkboxes', function( event ) {
410 $table = $this.closest( 'table' ),
411 controlChecked = $this.prop('checked'),
412 toggle = event.shiftKey || $this.data('wp-toggle');
414 $table.children( 'tbody' ).filter(':visible')
415 .children().children('.check-column').find(':checkbox')
416 .prop('checked', function() {
417 if ( $(this).is(':hidden') ) {
422 return ! $(this).prop( 'checked' );
423 } else if ( controlChecked ) {
430 $table.children('thead, tfoot').filter(':visible')
431 .children().children('.check-column').find(':checkbox')
432 .prop('checked', function() {
435 } else if ( controlChecked ) {
443 // Show row actions on keyboard focus of its parent container element or any other elements contained within
444 $( 'td.post-title, td.title, td.comment, .bookmarks td.column-name, td.blogname, td.username, .dashboard-comment-wrap' ).focusin(function(){
445 clearTimeout( transitionTimeout );
446 focusedRowActions = $(this).find( '.row-actions' );
447 focusedRowActions.addClass( 'visible' );
448 }).focusout(function(){
449 // Tabbing between post title and .row-actions links needs a brief pause, otherwise
450 // the .row-actions div gets hidden in transit in some browsers (ahem, Firefox).
451 transitionTimeout = setTimeout(function(){
452 focusedRowActions.removeClass( 'visible' );
456 $('#default-password-nag-no').click( function() {
457 setUserSetting('default_password_nag', 'hide');
458 $('div.default-password-nag').hide();
463 $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
464 var el = e.target, selStart, selEnd, val, scroll, sel;
466 if ( e.keyCode == 27 ) { // escape key
467 $(el).data('tab-out', true);
471 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
474 if ( $(el).data('tab-out') ) {
475 $(el).data('tab-out', false);
479 selStart = el.selectionStart;
480 selEnd = el.selectionEnd;
484 this.lastKey = 9; // not a standard DOM property, lastKey is to help stop Opera tab event. See blur handler below.
487 if ( document.selection ) {
489 sel = document.selection.createRange();
491 } else if ( selStart >= 0 ) {
492 scroll = this.scrollTop;
493 el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
494 el.selectionStart = el.selectionEnd = selStart + 1;
495 this.scrollTop = scroll;
498 if ( e.stopPropagation )
500 if ( e.preventDefault )
504 $('#newcontent').bind('blur.wpevent_InsertTab', function() {
505 if ( this.lastKey && 9 == this.lastKey )
509 if ( pageInput.length ) {
510 pageInput.closest('form').submit( function() {
512 // Reset paging var for new filters/searches but not for bulk actions. See #17685.
513 if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
518 $('.search-box input[type="search"], .search-box input[type="submit"]').mousedown(function () {
519 $('select[name^="action"]').val('-1');
522 // Scroll into view when focused
523 $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
524 if ( e.target.scrollIntoView )
525 e.target.scrollIntoView(false);
528 // Disable upload buttons until files are selected
530 var button, input, form = $('form.wp-upload-form');
533 button = form.find('input[type="submit"]');
534 input = form.find('input[type="file"]');
536 function toggleUploadButton() {
537 button.prop('disabled', '' === input.map( function() {
538 return $(this).val();
541 toggleUploadButton();
542 input.on('change', toggleUploadButton);
545 function pinMenu( event ) {
546 var windowPos = $window.scrollTop(),
547 resizing = ! event || event.type !== 'scroll';
549 if ( isIOS || isIE8 || $adminmenu.data( 'wp-responsive' ) ) {
553 if ( height.menu + height.adminbar < height.window ||
554 height.menu + height.adminbar + 20 > height.wpwrap ) {
561 if ( height.menu + height.adminbar > height.window ) {
562 // Check for overscrolling
563 if ( windowPos < 0 ) {
564 if ( ! pinnedMenuTop ) {
565 pinnedMenuTop = true;
566 pinnedMenuBottom = false;
576 } else if ( windowPos + height.window > $document.height() - 1 ) {
577 if ( ! pinnedMenuBottom ) {
578 pinnedMenuBottom = true;
579 pinnedMenuTop = false;
591 if ( windowPos > lastScrollPosition ) {
593 if ( pinnedMenuTop ) {
595 pinnedMenuTop = false;
596 menuTop = $adminMenuWrap.offset().top - height.adminbar - ( windowPos - lastScrollPosition );
598 if ( menuTop + height.menu + height.adminbar < windowPos + height.window ) {
599 menuTop = windowPos + height.window - height.menu - height.adminbar;
603 position: 'absolute',
607 } else if ( ! pinnedMenuBottom && $adminMenuWrap.offset().top + height.menu < windowPos + height.window ) {
609 pinnedMenuBottom = true;
617 } else if ( windowPos < lastScrollPosition ) {
619 if ( pinnedMenuBottom ) {
621 pinnedMenuBottom = false;
622 menuTop = $adminMenuWrap.offset().top - height.adminbar + ( lastScrollPosition - windowPos );
624 if ( menuTop + height.menu > windowPos + height.window ) {
629 position: 'absolute',
633 } else if ( ! pinnedMenuTop && $adminMenuWrap.offset().top >= windowPos + height.adminbar ) {
635 pinnedMenuTop = true;
643 } else if ( resizing ) {
645 pinnedMenuTop = pinnedMenuBottom = false;
646 menuTop = windowPos + height.window - height.menu - height.adminbar - 1;
650 position: 'absolute',
660 lastScrollPosition = windowPos;
663 function resetHeights() {
665 window: $window.height(),
666 wpwrap: $wpwrap.height(),
667 adminbar: $adminbar.height(),
668 menu: $adminMenuWrap.height()
672 function unpinMenu() {
673 if ( isIOS || ! menuIsPinned ) {
677 pinnedMenuTop = pinnedMenuBottom = menuIsPinned = false;
685 function setPinMenu() {
688 if ( $adminmenu.data('wp-responsive') ) {
689 $body.removeClass( 'sticky-menu' );
691 } else if ( height.menu + height.adminbar > height.window ) {
693 $body.removeClass( 'sticky-menu' );
695 $body.addClass( 'sticky-menu' );
701 $window.on( 'scroll.pin-menu', pinMenu );
702 $document.on( 'tinymce-editor-init.pin-menu', function( event, editor ) {
703 editor.on( 'wp-autoresize', resetHeights );
707 window.wpResponsive = {
711 // Modify functionality based on custom activate/deactivate event
712 $document.on( 'wp-responsive-activate.wp-responsive', function() {
714 }).on( 'wp-responsive-deactivate.wp-responsive', function() {
718 $( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
720 // Toggle sidebar when toggle is clicked
721 $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
722 event.preventDefault();
723 $wpwrap.toggleClass( 'wp-responsive-open' );
724 if ( $wpwrap.hasClass( 'wp-responsive-open' ) ) {
725 $(this).find('a').attr( 'aria-expanded', 'true' );
726 $( '#adminmenu a:first' ).focus();
728 $(this).find('a').attr( 'aria-expanded', 'false' );
733 $adminmenu.on( 'click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
734 if ( ! $adminmenu.data('wp-responsive') ) {
738 $( this ).parent( 'li' ).toggleClass( 'selected' );
739 event.preventDefault();
743 $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
745 // This needs to run later as UI Sortable may be initialized later on $(document).ready()
746 $window.on( 'load.wp-responsive', function() {
747 var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
749 if ( width <= 782 ) {
750 self.disableSortables();
755 activate: function() {
758 if ( ! $body.hasClass( 'auto-fold' ) ) {
759 $body.addClass( 'auto-fold' );
762 $adminmenu.data( 'wp-responsive', 1 );
763 this.disableSortables();
766 deactivate: function() {
768 $adminmenu.removeData('wp-responsive');
769 this.enableSortables();
772 trigger: function() {
775 if ( window.innerWidth ) {
776 // window.innerWidth is affected by zooming on phones
777 width = Math.max( window.innerWidth, document.documentElement.clientWidth );
779 // Exclude IE < 9, it doesn't support @media CSS rules
783 if ( width <= 782 ) {
784 if ( ! wpResponsiveActive ) {
785 $document.trigger( 'wp-responsive-activate' );
786 wpResponsiveActive = true;
789 if ( wpResponsiveActive ) {
790 $document.trigger( 'wp-responsive-deactivate' );
791 wpResponsiveActive = false;
795 if ( width <= 480 ) {
796 this.enableOverlay();
798 this.disableOverlay();
802 enableOverlay: function() {
803 if ( $overlay.length === 0 ) {
804 $overlay = $( '<div id="wp-responsive-overlay"></div>' )
805 .insertAfter( '#wpcontent' )
807 .on( 'click.wp-responsive', function() {
808 $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
813 $toolbarPopups.on( 'click.wp-responsive', function() {
818 disableOverlay: function() {
819 $toolbarPopups.off( 'click.wp-responsive' );
823 disableSortables: function() {
824 if ( $sortables.length ) {
826 $sortables.sortable('disable');
831 enableSortables: function() {
832 if ( $sortables.length ) {
834 $sortables.sortable('enable');
840 window.wpResponsive.init();
843 $document.on( 'wp-window-resized.pin-menu postboxes-columnchange.pin-menu postbox-toggled.pin-menu wp-collapse-menu.pin-menu wp-scroll-start.pin-menu', setPinMenu );
846 // Fire a custom jQuery event at the end of window resize
850 function triggerEvent() {
851 $(document).trigger( 'wp-window-resized' );
854 function fireOnce() {
855 window.clearTimeout( timeout );
856 timeout = window.setTimeout( triggerEvent, 200 );
859 $(window).on( 'resize.wp-fire-once', fireOnce );
862 // Make Windows 8 devices play along nicely.
864 if ( '-ms-user-select' in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/) ) {
865 var msViewportStyle = document.createElement( 'style' );
866 msViewportStyle.appendChild(
867 document.createTextNode( '@-ms-viewport{width:auto!important}' )
869 document.getElementsByTagName( 'head' )[0].appendChild( msViewportStyle );
873 }( jQuery, window ));