]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/common.js
WordPress 4.6.1
[autoinstalls/wordpress.git] / wp-admin / js / common.js
1 /* global setUserSetting, ajaxurl, commonL10n, alert, confirm, pagenow */
2 var showNotice, adminMenu, columns, validateForm, screenMeta;
3 ( function( $, window, undefined ) {
4         var $document = $( document ),
5                 $window = $( window ),
6                 $body = $( document.body );
7
8 // Removed in 3.3.
9 // (perhaps) needed for back-compat
10 adminMenu = {
11         init : function() {},
12         fold : function() {},
13         restoreMenuState : function() {},
14         toggle : function() {},
15         favorites : function() {}
16 };
17
18 // show/hide/save table columns
19 columns = {
20         init : function() {
21                 var that = this;
22                 $('.hide-column-tog', '#adv-settings').click( function() {
23                         var $t = $(this), column = $t.val();
24                         if ( $t.prop('checked') )
25                                 that.checked(column);
26                         else
27                                 that.unchecked(column);
28
29                         columns.saveManageColumnsState();
30                 });
31         },
32
33         saveManageColumnsState : function() {
34                 var hidden = this.hidden();
35                 $.post(ajaxurl, {
36                         action: 'hidden-columns',
37                         hidden: hidden,
38                         screenoptionnonce: $('#screenoptionnonce').val(),
39                         page: pagenow
40                 });
41         },
42
43         checked : function(column) {
44                 $('.column-' + column).removeClass( 'hidden' );
45                 this.colSpanChange(+1);
46         },
47
48         unchecked : function(column) {
49                 $('.column-' + column).addClass( 'hidden' );
50                 this.colSpanChange(-1);
51         },
52
53         hidden : function() {
54                 return $( '.manage-column[id]' ).filter( ':hidden' ).map(function() {
55                         return this.id;
56                 }).get().join( ',' );
57         },
58
59         useCheckboxesForHidden : function() {
60                 this.hidden = function(){
61                         return $('.hide-column-tog').not(':checked').map(function() {
62                                 var id = this.id;
63                                 return id.substring( id, id.length - 5 );
64                         }).get().join(',');
65                 };
66         },
67
68         colSpanChange : function(diff) {
69                 var $t = $('table').find('.colspanchange'), n;
70                 if ( !$t.length )
71                         return;
72                 n = parseInt( $t.attr('colspan'), 10 ) + diff;
73                 $t.attr('colspan', n.toString());
74         }
75 };
76
77 $document.ready(function(){columns.init();});
78
79 validateForm = function( form ) {
80         return !$( 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' ); } )
86                 .length;
87 };
88
89 // stub for doing better warnings
90 showNotice = {
91         warn : function() {
92                 var msg = commonL10n.warnDelete || '';
93                 if ( confirm(msg) ) {
94                         return true;
95                 }
96
97                 return false;
98         },
99
100         note : function(text) {
101                 alert(text);
102         }
103 };
104
105 screenMeta = {
106         element: null, // #screen-meta
107         toggles: null, // .screen-meta-toggle
108         page:    null, // #wpcontent
109
110         init: function() {
111                 this.element = $('#screen-meta');
112                 this.toggles = $( '#screen-meta-links' ).find( '.show-settings' );
113                 this.page    = $('#wpcontent');
114
115                 this.toggles.click( this.toggleEvent );
116         },
117
118         toggleEvent: function() {
119                 var panel = $( '#' + $( this ).attr( 'aria-controls' ) );
120
121                 if ( !panel.length )
122                         return;
123
124                 if ( panel.is(':visible') )
125                         screenMeta.close( panel, $(this) );
126                 else
127                         screenMeta.open( panel, $(this) );
128         },
129
130         open: function( panel, button ) {
131
132                 $( '#screen-meta-links' ).find( '.screen-meta-toggle' ).not( button.parent() ).css( 'visibility', 'hidden' );
133
134                 panel.parent().show();
135                 panel.slideDown( 'fast', function() {
136                         panel.focus();
137                         button.addClass( 'screen-meta-active' ).attr( 'aria-expanded', true );
138                 });
139
140                 $document.trigger( 'screen:options:open' );
141         },
142
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();
148                 });
149
150                 $document.trigger( 'screen:options:close' );
151         }
152 };
153
154 /**
155  * Help tabs.
156  */
157 $('.contextual-help-tabs').delegate('a', 'click', function(e) {
158         var link = $(this),
159                 panel;
160
161         e.preventDefault();
162
163         // Don't do anything if the click is for the tab already showing.
164         if ( link.is('.active a') )
165                 return false;
166
167         // Links
168         $('.contextual-help-tabs .active').removeClass('active');
169         link.parent('li').addClass('active');
170
171         panel = $( link.attr('href') );
172
173         // Panels
174         $('.help-tab-content').not( panel ).removeClass('active').hide();
175         panel.addClass('active').show();
176 });
177
178 $document.ready( function() {
179         var checks, first, last, checked, sliced, mobileEvent, transitionTimeout, focusedRowActions,
180                 lastClicked = false,
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,
198                 menuTop = 0,
199                 menuIsPinned = false,
200                 height = {
201                         window: $window.height(),
202                         wpwrap: $wpwrap.height(),
203                         adminbar: $adminbar.height(),
204                         menu: $adminMenuWrap.height()
205                 };
206
207
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();
211         });
212
213         $('#collapse-menu').on('click.collapse-menu', function() {
214                 var respWidth, state;
215
216                 // reset any compensation for submenus near the bottom of the screen
217                 $('#adminmenu div.wp-submenu').css('margin-top', '');
218
219                 if ( window.innerWidth ) {
220                         // window.innerWidth is affected by zooming on phones
221                         respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
222                 } else {
223                         // IE < 9 doesn't support @media CSS rules
224                         respWidth = 961;
225                 }
226
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');
232                                 state = 'open';
233                         } else {
234                                 $body.addClass('auto-fold');
235                                 setUserSetting('unfold', 0);
236                                 state = 'folded';
237                         }
238                 } else {
239                         if ( $body.hasClass('folded') ) {
240                                 $body.removeClass('folded');
241                                 setUserSetting('mfold', 'o');
242                                 state = 'open';
243                         } else {
244                                 $body.addClass('folded');
245                                 setUserSetting('mfold', 'f');
246                                 state = 'folded';
247                         }
248                 }
249
250                 currentMenuItemHasPopup();
251                 $document.trigger( 'wp-collapse-menu', { state: state } );
252         });
253
254         // Handle the `aria-haspopup` attribute on the current menu item when it has a sub-menu.
255         function currentMenuItemHasPopup() {
256                 var respWidth,
257                         $current = $( 'a.wp-has-current-submenu' );
258
259                 if ( window.innerWidth ) {
260                         respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
261                 } else {
262                         respWidth = 961;
263                 }
264
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' );
268                 } else {
269                         // When expanded or in responsive view, reset aria-haspopup.
270                         $current.attr( 'aria-haspopup', 'false' );
271                 }
272         }
273
274         $document.on( 'wp-window-resized wp-responsive-activate wp-responsive-deactivate', currentMenuItemHasPopup );
275
276         /**
277          * Ensure an admin submenu is within the visual viewport.
278          *
279          * @since 4.1.0
280          *
281          * @param {jQuery} $menuItem The parent menu item containing the submenu.
282          */
283         function adjustSubmenu( $menuItem ) {
284                 var bottomOffset, pageHeight, adjustment, theFold, menutop, wintop, maxtop,
285                         $submenu = $menuItem.find( '.wp-submenu' );
286
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
290
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
295
296                 if ( theFold < ( bottomOffset - adjustment ) ) {
297                         adjustment = bottomOffset - theFold;
298                 }
299
300                 if ( adjustment > maxtop ) {
301                         adjustment = maxtop;
302                 }
303
304                 if ( adjustment > 1 ) {
305                         $submenu.css( 'margin-top', '-' + adjustment + 'px' );
306                 } else {
307                         $submenu.css( 'margin-top', '' );
308                 }
309         }
310
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';
314
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') ) {
318                                 return;
319                         }
320
321                         if ( ! $( e.target ).closest( '#adminmenu' ).length ) {
322                                 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
323                         }
324                 });
325
326                 $adminmenu.find( 'a.wp-has-submenu' ).on( mobileEvent + '.wp-mobile-hover', function( event ) {
327                         var $menuItem = $(this).parent();
328
329                         if ( $adminmenu.data( 'wp-responsive' ) ) {
330                                 return;
331                         }
332
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');
341                         }
342                 });
343         }
344
345         if ( ! isIOS && ! isAndroid ) {
346                 $adminmenu.find( 'li.wp-has-submenu' ).hoverIntent({
347                         over: function() {
348                                 var $menuItem = $( this ),
349                                         $submenu = $menuItem.find( '.wp-submenu' ),
350                                         top = parseInt( $submenu.css( 'top' ), 10 );
351
352                                 if ( isNaN( top ) || top > -5 ) { // the submenu is visible
353                                         return;
354                                 }
355
356                                 if ( $adminmenu.data( 'wp-responsive' ) ) {
357                                         // The menu is in responsive mode, bail
358                                         return;
359                                 }
360
361                                 adjustSubmenu( $menuItem );
362                                 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
363                                 $menuItem.addClass( 'opensub' );
364                         },
365                         out: function(){
366                                 if ( $adminmenu.data( 'wp-responsive' ) ) {
367                                         // The menu is in responsive mode, bail
368                                         return;
369                                 }
370
371                                 $( this ).removeClass( 'opensub' ).find( '.wp-submenu' ).css( 'margin-top', '' );
372                         },
373                         timeout: 200,
374                         sensitivity: 7,
375                         interval: 90
376                 });
377
378                 $adminmenu.on( 'focus.adminmenu', '.wp-submenu a', function( event ) {
379                         if ( $adminmenu.data( 'wp-responsive' ) ) {
380                                 // The menu is in responsive mode, bail
381                                 return;
382                         }
383
384                         $( event.target ).closest( 'li.menu-top' ).addClass( 'opensub' );
385                 }).on( 'blur.adminmenu', '.wp-submenu a', function( event ) {
386                         if ( $adminmenu.data( 'wp-responsive' ) ) {
387                                 return;
388                         }
389
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 ) );
393                 });
394         }
395
396         /*
397          * The `.below-h2` class is here just for backward compatibility with plugins
398          * that are (incorrectly) using it. Do not use. Use `.inline` instead. See #34570.
399          */
400         $( 'div.updated, div.error, div.notice' ).not( '.inline, .below-h2' ).insertAfter( $( '.wrap h1, .wrap h2' ).first() );
401
402         // Make notices dismissible
403         function makeNoticesDismissible() {
404                 $( '.notice.is-dismissible' ).each( function() {
405                         var $el = $( this ),
406                                 $button = $( '<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>' ),
407                                 btnText = commonL10n.dismiss || '';
408
409                         // Ensure plain text
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() {
415                                                 $el.remove();
416                                         });
417                                 });
418                         });
419
420                         $el.append( $button );
421                 });
422         }
423
424         $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 );
425
426         // Init screen meta
427         screenMeta.init();
428
429         // check all checkboxes
430         $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
431                 if ( 'undefined' == e.shiftKey ) { return true; }
432                 if ( e.shiftKey ) {
433                         if ( !lastClicked ) { return true; }
434                         checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' ).filter( ':visible:enabled' );
435                         first = checks.index( lastClicked );
436                         last = checks.index( this );
437                         checked = $(this).prop('checked');
438                         if ( 0 < first && 0 < last && first != last ) {
439                                 sliced = ( last > first ) ? checks.slice( first, last ) : checks.slice( last, first );
440                                 sliced.prop( 'checked', function() {
441                                         if ( $(this).closest('tr').is(':visible') )
442                                                 return checked;
443
444                                         return false;
445                                 });
446                         }
447                 }
448                 lastClicked = this;
449
450                 // toggle "check all" checkboxes
451                 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible:enabled').not(':checked');
452                 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
453                         return ( 0 === unchecked.length );
454                 });
455
456                 return true;
457         });
458
459         $('thead, tfoot').find('.check-column :checkbox').on( 'click.wp-toggle-checkboxes', function( event ) {
460                 var $this = $(this),
461                         $table = $this.closest( 'table' ),
462                         controlChecked = $this.prop('checked'),
463                         toggle = event.shiftKey || $this.data('wp-toggle');
464
465                 $table.children( 'tbody' ).filter(':visible')
466                         .children().children('.check-column').find(':checkbox')
467                         .prop('checked', function() {
468                                 if ( $(this).is(':hidden,:disabled') ) {
469                                         return false;
470                                 }
471
472                                 if ( toggle ) {
473                                         return ! $(this).prop( 'checked' );
474                                 } else if ( controlChecked ) {
475                                         return true;
476                                 }
477
478                                 return false;
479                         });
480
481                 $table.children('thead,  tfoot').filter(':visible')
482                         .children().children('.check-column').find(':checkbox')
483                         .prop('checked', function() {
484                                 if ( toggle ) {
485                                         return false;
486                                 } else if ( controlChecked ) {
487                                         return true;
488                                 }
489
490                                 return false;
491                         });
492         });
493
494         // Show row actions on keyboard focus of its parent container element or any other elements contained within
495         $( '#wpbody-content' ).on({
496                 focusin: function() {
497                         clearTimeout( transitionTimeout );
498                         focusedRowActions = $( this ).find( '.row-actions' );
499                         // transitionTimeout is necessary for Firefox, but Chrome won't remove the CSS class without a little help.
500                         $( '.row-actions' ).not( this ).removeClass( 'visible' );
501                         focusedRowActions.addClass( 'visible' );
502                 },
503                 focusout: function() {
504                         // Tabbing between post title and .row-actions links needs a brief pause, otherwise
505                         // the .row-actions div gets hidden in transit in some browsers (ahem, Firefox).
506                         transitionTimeout = setTimeout( function() {
507                                 focusedRowActions.removeClass( 'visible' );
508                         }, 30 );
509                 }
510         }, '.has-row-actions' );
511
512         // Toggle list table rows on small screens
513         $( 'tbody' ).on( 'click', '.toggle-row', function() {
514                 $( this ).closest( 'tr' ).toggleClass( 'is-expanded' );
515         });
516
517         $('#default-password-nag-no').click( function() {
518                 setUserSetting('default_password_nag', 'hide');
519                 $('div.default-password-nag').hide();
520                 return false;
521         });
522
523         // tab in textareas
524         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
525                 var el = e.target, selStart, selEnd, val, scroll, sel;
526
527                 if ( e.keyCode == 27 ) { // escape key
528                         // when pressing Escape: Opera 12 and 27 blur form fields, IE 8 clears them
529                         e.preventDefault();
530                         $(el).data('tab-out', true);
531                         return;
532                 }
533
534                 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
535                         return;
536
537                 if ( $(el).data('tab-out') ) {
538                         $(el).data('tab-out', false);
539                         return;
540                 }
541
542                 selStart = el.selectionStart;
543                 selEnd = el.selectionEnd;
544                 val = el.value;
545
546                 if ( document.selection ) {
547                         el.focus();
548                         sel = document.selection.createRange();
549                         sel.text = '\t';
550                 } else if ( selStart >= 0 ) {
551                         scroll = this.scrollTop;
552                         el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
553                         el.selectionStart = el.selectionEnd = selStart + 1;
554                         this.scrollTop = scroll;
555                 }
556
557                 if ( e.stopPropagation )
558                         e.stopPropagation();
559                 if ( e.preventDefault )
560                         e.preventDefault();
561         });
562
563         if ( pageInput.length ) {
564                 pageInput.closest('form').submit( function() {
565
566                         // Reset paging var for new filters/searches but not for bulk actions. See #17685.
567                         if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
568                                 pageInput.val('1');
569                 });
570         }
571
572         $('.search-box input[type="search"], .search-box input[type="submit"]').mousedown(function () {
573                 $('select[name^="action"]').val('-1');
574         });
575
576         // Scroll into view when focused
577         $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
578                 if ( e.target.scrollIntoView )
579                         e.target.scrollIntoView(false);
580         });
581
582         // Disable upload buttons until files are selected
583         (function(){
584                 var button, input, form = $('form.wp-upload-form');
585                 if ( ! form.length )
586                         return;
587                 button = form.find('input[type="submit"]');
588                 input = form.find('input[type="file"]');
589
590                 function toggleUploadButton() {
591                         button.prop('disabled', '' === input.map( function() {
592                                 return $(this).val();
593                         }).get().join(''));
594                 }
595                 toggleUploadButton();
596                 input.on('change', toggleUploadButton);
597         })();
598
599         function pinMenu( event ) {
600                 var windowPos = $window.scrollTop(),
601                         resizing = ! event || event.type !== 'scroll';
602
603                 if ( isIOS || isIE8 || $adminmenu.data( 'wp-responsive' ) ) {
604                         return;
605                 }
606
607                 if ( height.menu + height.adminbar < height.window ||
608                         height.menu + height.adminbar + 20 > height.wpwrap ) {
609                         unpinMenu();
610                         return;
611                 }
612
613                 menuIsPinned = true;
614
615                 if ( height.menu + height.adminbar > height.window ) {
616                         // Check for overscrolling
617                         if ( windowPos < 0 ) {
618                                 if ( ! pinnedMenuTop ) {
619                                         pinnedMenuTop = true;
620                                         pinnedMenuBottom = false;
621
622                                         $adminMenuWrap.css({
623                                                 position: 'fixed',
624                                                 top: '',
625                                                 bottom: ''
626                                         });
627                                 }
628
629                                 return;
630                         } else if ( windowPos + height.window > $document.height() - 1 ) {
631                                 if ( ! pinnedMenuBottom ) {
632                                         pinnedMenuBottom = true;
633                                         pinnedMenuTop = false;
634
635                                         $adminMenuWrap.css({
636                                                 position: 'fixed',
637                                                 top: '',
638                                                 bottom: 0
639                                         });
640                                 }
641
642                                 return;
643                         }
644
645                         if ( windowPos > lastScrollPosition ) {
646                                 // Scrolling down
647                                 if ( pinnedMenuTop ) {
648                                         // let it scroll
649                                         pinnedMenuTop = false;
650                                         menuTop = $adminMenuWrap.offset().top - height.adminbar - ( windowPos - lastScrollPosition );
651
652                                         if ( menuTop + height.menu + height.adminbar < windowPos + height.window ) {
653                                                 menuTop = windowPos + height.window - height.menu - height.adminbar;
654                                         }
655
656                                         $adminMenuWrap.css({
657                                                 position: 'absolute',
658                                                 top: menuTop,
659                                                 bottom: ''
660                                         });
661                                 } else if ( ! pinnedMenuBottom && $adminMenuWrap.offset().top + height.menu < windowPos + height.window ) {
662                                         // pin the bottom
663                                         pinnedMenuBottom = true;
664
665                                         $adminMenuWrap.css({
666                                                 position: 'fixed',
667                                                 top: '',
668                                                 bottom: 0
669                                         });
670                                 }
671                         } else if ( windowPos < lastScrollPosition ) {
672                                 // Scrolling up
673                                 if ( pinnedMenuBottom ) {
674                                         // let it scroll
675                                         pinnedMenuBottom = false;
676                                         menuTop = $adminMenuWrap.offset().top - height.adminbar + ( lastScrollPosition - windowPos );
677
678                                         if ( menuTop + height.menu > windowPos + height.window ) {
679                                                 menuTop = windowPos;
680                                         }
681
682                                         $adminMenuWrap.css({
683                                                 position: 'absolute',
684                                                 top: menuTop,
685                                                 bottom: ''
686                                         });
687                                 } else if ( ! pinnedMenuTop && $adminMenuWrap.offset().top >= windowPos + height.adminbar ) {
688                                         // pin the top
689                                         pinnedMenuTop = true;
690
691                                         $adminMenuWrap.css({
692                                                 position: 'fixed',
693                                                 top: '',
694                                                 bottom: ''
695                                         });
696                                 }
697                         } else if ( resizing ) {
698                                 // Resizing
699                                 pinnedMenuTop = pinnedMenuBottom = false;
700                                 menuTop = windowPos + height.window - height.menu - height.adminbar - 1;
701
702                                 if ( menuTop > 0 ) {
703                                         $adminMenuWrap.css({
704                                                 position: 'absolute',
705                                                 top: menuTop,
706                                                 bottom: ''
707                                         });
708                                 } else {
709                                         unpinMenu();
710                                 }
711                         }
712                 }
713
714                 lastScrollPosition = windowPos;
715         }
716
717         function resetHeights() {
718                 height = {
719                         window: $window.height(),
720                         wpwrap: $wpwrap.height(),
721                         adminbar: $adminbar.height(),
722                         menu: $adminMenuWrap.height()
723                 };
724         }
725
726         function unpinMenu() {
727                 if ( isIOS || ! menuIsPinned ) {
728                         return;
729                 }
730
731                 pinnedMenuTop = pinnedMenuBottom = menuIsPinned = false;
732                 $adminMenuWrap.css({
733                         position: '',
734                         top: '',
735                         bottom: ''
736                 });
737         }
738
739         function setPinMenu() {
740                 resetHeights();
741
742                 if ( $adminmenu.data('wp-responsive') ) {
743                         $body.removeClass( 'sticky-menu' );
744                         unpinMenu();
745                 } else if ( height.menu + height.adminbar > height.window ) {
746                         pinMenu();
747                         $body.removeClass( 'sticky-menu' );
748                 } else {
749                         $body.addClass( 'sticky-menu' );
750                         unpinMenu();
751                 }
752         }
753
754         if ( ! isIOS ) {
755                 $window.on( 'scroll.pin-menu', pinMenu );
756                 $document.on( 'tinymce-editor-init.pin-menu', function( event, editor ) {
757                         editor.on( 'wp-autoresize', resetHeights );
758                 });
759         }
760
761         window.wpResponsive = {
762                 init: function() {
763                         var self = this;
764
765                         // Modify functionality based on custom activate/deactivate event
766                         $document.on( 'wp-responsive-activate.wp-responsive', function() {
767                                 self.activate();
768                         }).on( 'wp-responsive-deactivate.wp-responsive', function() {
769                                 self.deactivate();
770                         });
771
772                         $( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
773
774                         // Toggle sidebar when toggle is clicked
775                         $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
776                                 event.preventDefault();
777
778                                 // close any open toolbar submenus
779                                 $adminbar.find( '.hover' ).removeClass( 'hover' );
780
781                                 $wpwrap.toggleClass( 'wp-responsive-open' );
782                                 if ( $wpwrap.hasClass( 'wp-responsive-open' ) ) {
783                                         $(this).find('a').attr( 'aria-expanded', 'true' );
784                                         $( '#adminmenu a:first' ).focus();
785                                 } else {
786                                         $(this).find('a').attr( 'aria-expanded', 'false' );
787                                 }
788                         } );
789
790                         // Add menu events
791                         $adminmenu.on( 'click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
792                                 if ( ! $adminmenu.data('wp-responsive') ) {
793                                         return;
794                                 }
795
796                                 $( this ).parent( 'li' ).toggleClass( 'selected' );
797                                 event.preventDefault();
798                         });
799
800                         self.trigger();
801                         $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
802
803                         // This needs to run later as UI Sortable may be initialized later on $(document).ready()
804                         $window.on( 'load.wp-responsive', function() {
805                                 var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
806
807                                 if ( width <= 782 ) {
808                                         self.disableSortables();
809                                 }
810                         });
811                 },
812
813                 activate: function() {
814                         setPinMenu();
815
816                         if ( ! $body.hasClass( 'auto-fold' ) ) {
817                                 $body.addClass( 'auto-fold' );
818                         }
819
820                         $adminmenu.data( 'wp-responsive', 1 );
821                         this.disableSortables();
822                 },
823
824                 deactivate: function() {
825                         setPinMenu();
826                         $adminmenu.removeData('wp-responsive');
827                         this.enableSortables();
828                 },
829
830                 trigger: function() {
831                         var width;
832
833                         if ( window.innerWidth ) {
834                                 // window.innerWidth is affected by zooming on phones
835                                 width = Math.max( window.innerWidth, document.documentElement.clientWidth );
836                         } else {
837                                 // Exclude IE < 9, it doesn't support @media CSS rules
838                                 return;
839                         }
840
841                         if ( width <= 782 ) {
842                                 if ( ! wpResponsiveActive ) {
843                                         $document.trigger( 'wp-responsive-activate' );
844                                         wpResponsiveActive = true;
845                                 }
846                         } else {
847                                 if ( wpResponsiveActive ) {
848                                         $document.trigger( 'wp-responsive-deactivate' );
849                                         wpResponsiveActive = false;
850                                 }
851                         }
852
853                         if ( width <= 480 ) {
854                                 this.enableOverlay();
855                         } else {
856                                 this.disableOverlay();
857                         }
858                 },
859
860                 enableOverlay: function() {
861                         if ( $overlay.length === 0 ) {
862                                 $overlay = $( '<div id="wp-responsive-overlay"></div>' )
863                                         .insertAfter( '#wpcontent' )
864                                         .hide()
865                                         .on( 'click.wp-responsive', function() {
866                                                 $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
867                                                 $( this ).hide();
868                                         });
869                         }
870
871                         $toolbarPopups.on( 'click.wp-responsive', function() {
872                                 $overlay.show();
873                         });
874                 },
875
876                 disableOverlay: function() {
877                         $toolbarPopups.off( 'click.wp-responsive' );
878                         $overlay.hide();
879                 },
880
881                 disableSortables: function() {
882                         if ( $sortables.length ) {
883                                 try {
884                                         $sortables.sortable('disable');
885                                 } catch(e) {}
886                         }
887                 },
888
889                 enableSortables: function() {
890                         if ( $sortables.length ) {
891                                 try {
892                                         $sortables.sortable('enable');
893                                 } catch(e) {}
894                         }
895                 }
896         };
897
898         // Add an ARIA role `button` to elements that behave like UI controls when JavaScript is on.
899         function aria_button_if_js() {
900                 $( '.aria-button-if-js' ).attr( 'role', 'button' );
901         }
902
903         $( document ).ajaxComplete( function() {
904                 aria_button_if_js();
905         });
906
907         window.wpResponsive.init();
908         setPinMenu();
909         currentMenuItemHasPopup();
910         makeNoticesDismissible();
911         aria_button_if_js();
912
913         $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 );
914
915         // Set initial focus on a specific element.
916         $( '.wp-initial-focus' ).focus();
917 });
918
919 // Fire a custom jQuery event at the end of window resize
920 ( function() {
921         var timeout;
922
923         function triggerEvent() {
924                 $document.trigger( 'wp-window-resized' );
925         }
926
927         function fireOnce() {
928                 window.clearTimeout( timeout );
929                 timeout = window.setTimeout( triggerEvent, 200 );
930         }
931
932         $window.on( 'resize.wp-fire-once', fireOnce );
933 }());
934
935 // Make Windows 8 devices play along nicely.
936 (function(){
937         if ( '-ms-user-select' in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/) ) {
938                 var msViewportStyle = document.createElement( 'style' );
939                 msViewportStyle.appendChild(
940                         document.createTextNode( '@-ms-viewport{width:auto!important}' )
941                 );
942                 document.getElementsByTagName( 'head' )[0].appendChild( msViewportStyle );
943         }
944 })();
945
946 }( jQuery, window ));