796e1400f44df76e1c448d5805764a9ec811ec1b
[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                 .size();
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 backwards 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' ).children( ':header' ).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-plugin-update-error', function() {
425                 makeNoticesDismissible();
426         });
427
428         // Init screen meta
429         screenMeta.init();
430
431         // check all checkboxes
432         $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
433                 if ( 'undefined' == e.shiftKey ) { return true; }
434                 if ( e.shiftKey ) {
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') )
444                                                 return checked;
445
446                                         return false;
447                                 });
448                         }
449                 }
450                 lastClicked = this;
451
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 );
456                 });
457
458                 return true;
459         });
460
461         $('thead, tfoot').find('.check-column :checkbox').on( 'click.wp-toggle-checkboxes', function( event ) {
462                 var $this = $(this),
463                         $table = $this.closest( 'table' ),
464                         controlChecked = $this.prop('checked'),
465                         toggle = event.shiftKey || $this.data('wp-toggle');
466
467                 $table.children( 'tbody' ).filter(':visible')
468                         .children().children('.check-column').find(':checkbox')
469                         .prop('checked', function() {
470                                 if ( $(this).is(':hidden,:disabled') ) {
471                                         return false;
472                                 }
473
474                                 if ( toggle ) {
475                                         return ! $(this).prop( 'checked' );
476                                 } else if ( controlChecked ) {
477                                         return true;
478                                 }
479
480                                 return false;
481                         });
482
483                 $table.children('thead,  tfoot').filter(':visible')
484                         .children().children('.check-column').find(':checkbox')
485                         .prop('checked', function() {
486                                 if ( toggle ) {
487                                         return false;
488                                 } else if ( controlChecked ) {
489                                         return true;
490                                 }
491
492                                 return false;
493                         });
494         });
495
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' );
504                 },
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' );
510                         }, 30 );
511                 }
512         }, '.has-row-actions' );
513
514         // Toggle list table rows on small screens
515         $( 'tbody' ).on( 'click', '.toggle-row', function() {
516                 $( this ).closest( 'tr' ).toggleClass( 'is-expanded' );
517         });
518
519         $('#default-password-nag-no').click( function() {
520                 setUserSetting('default_password_nag', 'hide');
521                 $('div.default-password-nag').hide();
522                 return false;
523         });
524
525         // tab in textareas
526         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
527                 var el = e.target, selStart, selEnd, val, scroll, sel;
528
529                 if ( e.keyCode == 27 ) { // escape key
530                         // when pressing Escape: Opera 12 and 27 blur form fields, IE 8 clears them
531                         e.preventDefault();
532                         $(el).data('tab-out', true);
533                         return;
534                 }
535
536                 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
537                         return;
538
539                 if ( $(el).data('tab-out') ) {
540                         $(el).data('tab-out', false);
541                         return;
542                 }
543
544                 selStart = el.selectionStart;
545                 selEnd = el.selectionEnd;
546                 val = el.value;
547
548                 if ( document.selection ) {
549                         el.focus();
550                         sel = document.selection.createRange();
551                         sel.text = '\t';
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;
557                 }
558
559                 if ( e.stopPropagation )
560                         e.stopPropagation();
561                 if ( e.preventDefault )
562                         e.preventDefault();
563         });
564
565         if ( pageInput.length ) {
566                 pageInput.closest('form').submit( function() {
567
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 )
570                                 pageInput.val('1');
571                 });
572         }
573
574         $('.search-box input[type="search"], .search-box input[type="submit"]').mousedown(function () {
575                 $('select[name^="action"]').val('-1');
576         });
577
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);
582         });
583
584         // Disable upload buttons until files are selected
585         (function(){
586                 var button, input, form = $('form.wp-upload-form');
587                 if ( ! form.length )
588                         return;
589                 button = form.find('input[type="submit"]');
590                 input = form.find('input[type="file"]');
591
592                 function toggleUploadButton() {
593                         button.prop('disabled', '' === input.map( function() {
594                                 return $(this).val();
595                         }).get().join(''));
596                 }
597                 toggleUploadButton();
598                 input.on('change', toggleUploadButton);
599         })();
600
601         function pinMenu( event ) {
602                 var windowPos = $window.scrollTop(),
603                         resizing = ! event || event.type !== 'scroll';
604
605                 if ( isIOS || isIE8 || $adminmenu.data( 'wp-responsive' ) ) {
606                         return;
607                 }
608
609                 if ( height.menu + height.adminbar < height.window ||
610                         height.menu + height.adminbar + 20 > height.wpwrap ) {
611                         unpinMenu();
612                         return;
613                 }
614
615                 menuIsPinned = true;
616
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;
623
624                                         $adminMenuWrap.css({
625                                                 position: 'fixed',
626                                                 top: '',
627                                                 bottom: ''
628                                         });
629                                 }
630
631                                 return;
632                         } else if ( windowPos + height.window > $document.height() - 1 ) {
633                                 if ( ! pinnedMenuBottom ) {
634                                         pinnedMenuBottom = true;
635                                         pinnedMenuTop = false;
636
637                                         $adminMenuWrap.css({
638                                                 position: 'fixed',
639                                                 top: '',
640                                                 bottom: 0
641                                         });
642                                 }
643
644                                 return;
645                         }
646
647                         if ( windowPos > lastScrollPosition ) {
648                                 // Scrolling down
649                                 if ( pinnedMenuTop ) {
650                                         // let it scroll
651                                         pinnedMenuTop = false;
652                                         menuTop = $adminMenuWrap.offset().top - height.adminbar - ( windowPos - lastScrollPosition );
653
654                                         if ( menuTop + height.menu + height.adminbar < windowPos + height.window ) {
655                                                 menuTop = windowPos + height.window - height.menu - height.adminbar;
656                                         }
657
658                                         $adminMenuWrap.css({
659                                                 position: 'absolute',
660                                                 top: menuTop,
661                                                 bottom: ''
662                                         });
663                                 } else if ( ! pinnedMenuBottom && $adminMenuWrap.offset().top + height.menu < windowPos + height.window ) {
664                                         // pin the bottom
665                                         pinnedMenuBottom = true;
666
667                                         $adminMenuWrap.css({
668                                                 position: 'fixed',
669                                                 top: '',
670                                                 bottom: 0
671                                         });
672                                 }
673                         } else if ( windowPos < lastScrollPosition ) {
674                                 // Scrolling up
675                                 if ( pinnedMenuBottom ) {
676                                         // let it scroll
677                                         pinnedMenuBottom = false;
678                                         menuTop = $adminMenuWrap.offset().top - height.adminbar + ( lastScrollPosition - windowPos );
679
680                                         if ( menuTop + height.menu > windowPos + height.window ) {
681                                                 menuTop = windowPos;
682                                         }
683
684                                         $adminMenuWrap.css({
685                                                 position: 'absolute',
686                                                 top: menuTop,
687                                                 bottom: ''
688                                         });
689                                 } else if ( ! pinnedMenuTop && $adminMenuWrap.offset().top >= windowPos + height.adminbar ) {
690                                         // pin the top
691                                         pinnedMenuTop = true;
692
693                                         $adminMenuWrap.css({
694                                                 position: 'fixed',
695                                                 top: '',
696                                                 bottom: ''
697                                         });
698                                 }
699                         } else if ( resizing ) {
700                                 // Resizing
701                                 pinnedMenuTop = pinnedMenuBottom = false;
702                                 menuTop = windowPos + height.window - height.menu - height.adminbar - 1;
703
704                                 if ( menuTop > 0 ) {
705                                         $adminMenuWrap.css({
706                                                 position: 'absolute',
707                                                 top: menuTop,
708                                                 bottom: ''
709                                         });
710                                 } else {
711                                         unpinMenu();
712                                 }
713                         }
714                 }
715
716                 lastScrollPosition = windowPos;
717         }
718
719         function resetHeights() {
720                 height = {
721                         window: $window.height(),
722                         wpwrap: $wpwrap.height(),
723                         adminbar: $adminbar.height(),
724                         menu: $adminMenuWrap.height()
725                 };
726         }
727
728         function unpinMenu() {
729                 if ( isIOS || ! menuIsPinned ) {
730                         return;
731                 }
732
733                 pinnedMenuTop = pinnedMenuBottom = menuIsPinned = false;
734                 $adminMenuWrap.css({
735                         position: '',
736                         top: '',
737                         bottom: ''
738                 });
739         }
740
741         function setPinMenu() {
742                 resetHeights();
743
744                 if ( $adminmenu.data('wp-responsive') ) {
745                         $body.removeClass( 'sticky-menu' );
746                         unpinMenu();
747                 } else if ( height.menu + height.adminbar > height.window ) {
748                         pinMenu();
749                         $body.removeClass( 'sticky-menu' );
750                 } else {
751                         $body.addClass( 'sticky-menu' );
752                         unpinMenu();
753                 }
754         }
755
756         if ( ! isIOS ) {
757                 $window.on( 'scroll.pin-menu', pinMenu );
758                 $document.on( 'tinymce-editor-init.pin-menu', function( event, editor ) {
759                         editor.on( 'wp-autoresize', resetHeights );
760                 });
761         }
762
763         window.wpResponsive = {
764                 init: function() {
765                         var self = this;
766
767                         // Modify functionality based on custom activate/deactivate event
768                         $document.on( 'wp-responsive-activate.wp-responsive', function() {
769                                 self.activate();
770                         }).on( 'wp-responsive-deactivate.wp-responsive', function() {
771                                 self.deactivate();
772                         });
773
774                         $( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
775
776                         // Toggle sidebar when toggle is clicked
777                         $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
778                                 event.preventDefault();
779
780                                 // close any open toolbar submenus
781                                 $adminbar.find( '.hover' ).removeClass( 'hover' );
782
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();
787                                 } else {
788                                         $(this).find('a').attr( 'aria-expanded', 'false' );
789                                 }
790                         } );
791
792                         // Add menu events
793                         $adminmenu.on( 'click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
794                                 if ( ! $adminmenu.data('wp-responsive') ) {
795                                         return;
796                                 }
797
798                                 $( this ).parent( 'li' ).toggleClass( 'selected' );
799                                 event.preventDefault();
800                         });
801
802                         self.trigger();
803                         $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
804
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;
808
809                                 if ( width <= 782 ) {
810                                         self.disableSortables();
811                                 }
812                         });
813                 },
814
815                 activate: function() {
816                         setPinMenu();
817
818                         if ( ! $body.hasClass( 'auto-fold' ) ) {
819                                 $body.addClass( 'auto-fold' );
820                         }
821
822                         $adminmenu.data( 'wp-responsive', 1 );
823                         this.disableSortables();
824                 },
825
826                 deactivate: function() {
827                         setPinMenu();
828                         $adminmenu.removeData('wp-responsive');
829                         this.enableSortables();
830                 },
831
832                 trigger: function() {
833                         var width;
834
835                         if ( window.innerWidth ) {
836                                 // window.innerWidth is affected by zooming on phones
837                                 width = Math.max( window.innerWidth, document.documentElement.clientWidth );
838                         } else {
839                                 // Exclude IE < 9, it doesn't support @media CSS rules
840                                 return;
841                         }
842
843                         if ( width <= 782 ) {
844                                 if ( ! wpResponsiveActive ) {
845                                         $document.trigger( 'wp-responsive-activate' );
846                                         wpResponsiveActive = true;
847                                 }
848                         } else {
849                                 if ( wpResponsiveActive ) {
850                                         $document.trigger( 'wp-responsive-deactivate' );
851                                         wpResponsiveActive = false;
852                                 }
853                         }
854
855                         if ( width <= 480 ) {
856                                 this.enableOverlay();
857                         } else {
858                                 this.disableOverlay();
859                         }
860                 },
861
862                 enableOverlay: function() {
863                         if ( $overlay.length === 0 ) {
864                                 $overlay = $( '<div id="wp-responsive-overlay"></div>' )
865                                         .insertAfter( '#wpcontent' )
866                                         .hide()
867                                         .on( 'click.wp-responsive', function() {
868                                                 $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
869                                                 $( this ).hide();
870                                         });
871                         }
872
873                         $toolbarPopups.on( 'click.wp-responsive', function() {
874                                 $overlay.show();
875                         });
876                 },
877
878                 disableOverlay: function() {
879                         $toolbarPopups.off( 'click.wp-responsive' );
880                         $overlay.hide();
881                 },
882
883                 disableSortables: function() {
884                         if ( $sortables.length ) {
885                                 try {
886                                         $sortables.sortable('disable');
887                                 } catch(e) {}
888                         }
889                 },
890
891                 enableSortables: function() {
892                         if ( $sortables.length ) {
893                                 try {
894                                         $sortables.sortable('enable');
895                                 } catch(e) {}
896                         }
897                 }
898         };
899
900         window.wpResponsive.init();
901         setPinMenu();
902         currentMenuItemHasPopup();
903         makeNoticesDismissible();
904
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 );
906 });
907
908 // Fire a custom jQuery event at the end of window resize
909 ( function() {
910         var timeout;
911
912         function triggerEvent() {
913                 $document.trigger( 'wp-window-resized' );
914         }
915
916         function fireOnce() {
917                 window.clearTimeout( timeout );
918                 timeout = window.setTimeout( triggerEvent, 200 );
919         }
920
921         $window.on( 'resize.wp-fire-once', fireOnce );
922 }());
923
924 // Make Windows 8 devices play along nicely.
925 (function(){
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}' )
930                 );
931                 document.getElementsByTagName( 'head' )[0].appendChild( msViewportStyle );
932         }
933 })();
934
935 }( jQuery, window ));