]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/common.js
WordPress 4.7
[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                 menuState,
200                 menuIsPinned = false,
201                 height = {
202                         window: $window.height(),
203                         wpwrap: $wpwrap.height(),
204                         adminbar: $adminbar.height(),
205                         menu: $adminMenuWrap.height()
206                 },
207                 $headerEnd = $( '.wp-header-end' );
208
209
210         // when the menu is folded, make the fly-out submenu header clickable
211         $adminmenu.on('click.wp-submenu-head', '.wp-submenu-head', function(e){
212                 $(e.target).parent().siblings('a').get(0).click();
213         });
214
215         $( '#collapse-button' ).on( 'click.collapse-menu', function() {
216                 var viewportWidth = getViewportWidth() || 961;
217
218                 // reset any compensation for submenus near the bottom of the screen
219                 $('#adminmenu div.wp-submenu').css('margin-top', '');
220
221                 if ( viewportWidth < 960 ) {
222                         if ( $body.hasClass('auto-fold') ) {
223                                 $body.removeClass('auto-fold').removeClass('folded');
224                                 setUserSetting('unfold', 1);
225                                 setUserSetting('mfold', 'o');
226                                 menuState = 'open';
227                         } else {
228                                 $body.addClass('auto-fold');
229                                 setUserSetting('unfold', 0);
230                                 menuState = 'folded';
231                         }
232                 } else {
233                         if ( $body.hasClass('folded') ) {
234                                 $body.removeClass('folded');
235                                 setUserSetting('mfold', 'o');
236                                 menuState = 'open';
237                         } else {
238                                 $body.addClass('folded');
239                                 setUserSetting('mfold', 'f');
240                                 menuState = 'folded';
241                         }
242                 }
243
244                 $document.trigger( 'wp-collapse-menu', { state: menuState } );
245         });
246
247         // Handle the `aria-haspopup` attribute on the current menu item when it has a sub-menu.
248         function currentMenuItemHasPopup() {
249                 var $current = $( 'a.wp-has-current-submenu' );
250
251                 if ( 'folded' === menuState ) {
252                         // When folded or auto-folded and not responsive view, the current menu item does have a fly-out sub-menu.
253                         $current.attr( 'aria-haspopup', 'true' );
254                 } else {
255                         // When expanded or in responsive view, reset aria-haspopup.
256                         $current.attr( 'aria-haspopup', 'false' );
257                 }
258         }
259
260         $document.on( 'wp-menu-state-set wp-collapse-menu wp-responsive-activate wp-responsive-deactivate', currentMenuItemHasPopup );
261
262         /**
263          * Ensure an admin submenu is within the visual viewport.
264          *
265          * @since 4.1.0
266          *
267          * @param {jQuery} $menuItem The parent menu item containing the submenu.
268          */
269         function adjustSubmenu( $menuItem ) {
270                 var bottomOffset, pageHeight, adjustment, theFold, menutop, wintop, maxtop,
271                         $submenu = $menuItem.find( '.wp-submenu' );
272
273                 menutop = $menuItem.offset().top;
274                 wintop = $window.scrollTop();
275                 maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
276
277                 bottomOffset = menutop + $submenu.height() + 1; // Bottom offset of the menu
278                 pageHeight = $wpwrap.height(); // Height of the entire page
279                 adjustment = 60 + bottomOffset - pageHeight;
280                 theFold = $window.height() + wintop - 50; // The fold
281
282                 if ( theFold < ( bottomOffset - adjustment ) ) {
283                         adjustment = bottomOffset - theFold;
284                 }
285
286                 if ( adjustment > maxtop ) {
287                         adjustment = maxtop;
288                 }
289
290                 if ( adjustment > 1 ) {
291                         $submenu.css( 'margin-top', '-' + adjustment + 'px' );
292                 } else {
293                         $submenu.css( 'margin-top', '' );
294                 }
295         }
296
297         if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device
298                 // iOS Safari works with touchstart, the rest work with click
299                 mobileEvent = isIOS ? 'touchstart' : 'click';
300
301                 // close any open submenus when touch/click is not on the menu
302                 $body.on( mobileEvent+'.wp-mobile-hover', function(e) {
303                         if ( $adminmenu.data('wp-responsive') ) {
304                                 return;
305                         }
306
307                         if ( ! $( e.target ).closest( '#adminmenu' ).length ) {
308                                 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
309                         }
310                 });
311
312                 $adminmenu.find( 'a.wp-has-submenu' ).on( mobileEvent + '.wp-mobile-hover', function( event ) {
313                         var $menuItem = $(this).parent();
314
315                         if ( $adminmenu.data( 'wp-responsive' ) ) {
316                                 return;
317                         }
318
319                         // Show the sub instead of following the link if:
320                         //      - the submenu is not open
321                         //      - the submenu is not shown inline or the menu is not folded
322                         if ( ! $menuItem.hasClass( 'opensub' ) && ( ! $menuItem.hasClass( 'wp-menu-open' ) || $menuItem.width() < 40 ) ) {
323                                 event.preventDefault();
324                                 adjustSubmenu( $menuItem );
325                                 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
326                                 $menuItem.addClass('opensub');
327                         }
328                 });
329         }
330
331         if ( ! isIOS && ! isAndroid ) {
332                 $adminmenu.find( 'li.wp-has-submenu' ).hoverIntent({
333                         over: function() {
334                                 var $menuItem = $( this ),
335                                         $submenu = $menuItem.find( '.wp-submenu' ),
336                                         top = parseInt( $submenu.css( 'top' ), 10 );
337
338                                 if ( isNaN( top ) || top > -5 ) { // the submenu is visible
339                                         return;
340                                 }
341
342                                 if ( $adminmenu.data( 'wp-responsive' ) ) {
343                                         // The menu is in responsive mode, bail
344                                         return;
345                                 }
346
347                                 adjustSubmenu( $menuItem );
348                                 $adminmenu.find( 'li.opensub' ).removeClass( 'opensub' );
349                                 $menuItem.addClass( 'opensub' );
350                         },
351                         out: function(){
352                                 if ( $adminmenu.data( 'wp-responsive' ) ) {
353                                         // The menu is in responsive mode, bail
354                                         return;
355                                 }
356
357                                 $( this ).removeClass( 'opensub' ).find( '.wp-submenu' ).css( 'margin-top', '' );
358                         },
359                         timeout: 200,
360                         sensitivity: 7,
361                         interval: 90
362                 });
363
364                 $adminmenu.on( 'focus.adminmenu', '.wp-submenu a', function( event ) {
365                         if ( $adminmenu.data( 'wp-responsive' ) ) {
366                                 // The menu is in responsive mode, bail
367                                 return;
368                         }
369
370                         $( event.target ).closest( 'li.menu-top' ).addClass( 'opensub' );
371                 }).on( 'blur.adminmenu', '.wp-submenu a', function( event ) {
372                         if ( $adminmenu.data( 'wp-responsive' ) ) {
373                                 return;
374                         }
375
376                         $( event.target ).closest( 'li.menu-top' ).removeClass( 'opensub' );
377                 }).find( 'li.wp-has-submenu.wp-not-current-submenu' ).on( 'focusin.adminmenu', function() {
378                         adjustSubmenu( $( this ) );
379                 });
380         }
381
382         /*
383          * The `.below-h2` class is here just for backward compatibility with plugins
384          * that are (incorrectly) using it. Do not use. Use `.inline` instead. See #34570.
385          * If '.wp-header-end' is found, append the notices after it otherwise
386          * after the first h1 or h2 heading found within the main content.
387          */
388         if ( ! $headerEnd.length ) {
389                 $headerEnd = $( '.wrap h1, .wrap h2' ).first();
390         }
391         $( 'div.updated, div.error, div.notice' ).not( '.inline, .below-h2' ).insertAfter( $headerEnd );
392
393         // Make notices dismissible
394         function makeNoticesDismissible() {
395                 $( '.notice.is-dismissible' ).each( function() {
396                         var $el = $( this ),
397                                 $button = $( '<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>' ),
398                                 btnText = commonL10n.dismiss || '';
399
400                         // Ensure plain text
401                         $button.find( '.screen-reader-text' ).text( btnText );
402                         $button.on( 'click.wp-dismiss-notice', function( event ) {
403                                 event.preventDefault();
404                                 $el.fadeTo( 100, 0, function() {
405                                         $el.slideUp( 100, function() {
406                                                 $el.remove();
407                                         });
408                                 });
409                         });
410
411                         $el.append( $button );
412                 });
413         }
414
415         $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 );
416
417         // Init screen meta
418         screenMeta.init();
419
420         // This event needs to be delegated. Ticket #37973.
421         $body.on( 'click', 'tbody .check-column :checkbox', function( event ) {
422                 // Shift click to select a range of checkboxes.
423                 if ( 'undefined' == event.shiftKey ) { return true; }
424                 if ( event.shiftKey ) {
425                         if ( !lastClicked ) { return true; }
426                         checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' ).filter( ':visible:enabled' );
427                         first = checks.index( lastClicked );
428                         last = checks.index( this );
429                         checked = $(this).prop('checked');
430                         if ( 0 < first && 0 < last && first != last ) {
431                                 sliced = ( last > first ) ? checks.slice( first, last ) : checks.slice( last, first );
432                                 sliced.prop( 'checked', function() {
433                                         if ( $(this).closest('tr').is(':visible') )
434                                                 return checked;
435
436                                         return false;
437                                 });
438                         }
439                 }
440                 lastClicked = this;
441
442                 // Toggle the "Select all" checkboxes depending if the other ones are all checked or not.
443                 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible:enabled').not(':checked');
444                 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
445                         return ( 0 === unchecked.length );
446                 });
447
448                 return true;
449         });
450
451         // This event needs to be delegated. Ticket #37973.
452         $body.on( 'click.wp-toggle-checkboxes', 'thead .check-column :checkbox, tfoot .check-column :checkbox', function( event ) {
453                 var $this = $(this),
454                         $table = $this.closest( 'table' ),
455                         controlChecked = $this.prop('checked'),
456                         toggle = event.shiftKey || $this.data('wp-toggle');
457
458                 $table.children( 'tbody' ).filter(':visible')
459                         .children().children('.check-column').find(':checkbox')
460                         .prop('checked', function() {
461                                 if ( $(this).is(':hidden,:disabled') ) {
462                                         return false;
463                                 }
464
465                                 if ( toggle ) {
466                                         return ! $(this).prop( 'checked' );
467                                 } else if ( controlChecked ) {
468                                         return true;
469                                 }
470
471                                 return false;
472                         });
473
474                 $table.children('thead,  tfoot').filter(':visible')
475                         .children().children('.check-column').find(':checkbox')
476                         .prop('checked', function() {
477                                 if ( toggle ) {
478                                         return false;
479                                 } else if ( controlChecked ) {
480                                         return true;
481                                 }
482
483                                 return false;
484                         });
485         });
486
487         // Show row actions on keyboard focus of its parent container element or any other elements contained within
488         $( '#wpbody-content' ).on({
489                 focusin: function() {
490                         clearTimeout( transitionTimeout );
491                         focusedRowActions = $( this ).find( '.row-actions' );
492                         // transitionTimeout is necessary for Firefox, but Chrome won't remove the CSS class without a little help.
493                         $( '.row-actions' ).not( this ).removeClass( 'visible' );
494                         focusedRowActions.addClass( 'visible' );
495                 },
496                 focusout: function() {
497                         // Tabbing between post title and .row-actions links needs a brief pause, otherwise
498                         // the .row-actions div gets hidden in transit in some browsers (ahem, Firefox).
499                         transitionTimeout = setTimeout( function() {
500                                 focusedRowActions.removeClass( 'visible' );
501                         }, 30 );
502                 }
503         }, '.has-row-actions' );
504
505         // Toggle list table rows on small screens
506         $( 'tbody' ).on( 'click', '.toggle-row', function() {
507                 $( this ).closest( 'tr' ).toggleClass( 'is-expanded' );
508         });
509
510         $('#default-password-nag-no').click( function() {
511                 setUserSetting('default_password_nag', 'hide');
512                 $('div.default-password-nag').hide();
513                 return false;
514         });
515
516         // tab in textareas
517         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
518                 var el = e.target, selStart, selEnd, val, scroll, sel;
519
520                 if ( e.keyCode == 27 ) { // escape key
521                         // when pressing Escape: Opera 12 and 27 blur form fields, IE 8 clears them
522                         e.preventDefault();
523                         $(el).data('tab-out', true);
524                         return;
525                 }
526
527                 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
528                         return;
529
530                 if ( $(el).data('tab-out') ) {
531                         $(el).data('tab-out', false);
532                         return;
533                 }
534
535                 selStart = el.selectionStart;
536                 selEnd = el.selectionEnd;
537                 val = el.value;
538
539                 if ( document.selection ) {
540                         el.focus();
541                         sel = document.selection.createRange();
542                         sel.text = '\t';
543                 } else if ( selStart >= 0 ) {
544                         scroll = this.scrollTop;
545                         el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
546                         el.selectionStart = el.selectionEnd = selStart + 1;
547                         this.scrollTop = scroll;
548                 }
549
550                 if ( e.stopPropagation )
551                         e.stopPropagation();
552                 if ( e.preventDefault )
553                         e.preventDefault();
554         });
555
556         if ( pageInput.length ) {
557                 pageInput.closest('form').submit( function() {
558
559                         // Reset paging var for new filters/searches but not for bulk actions. See #17685.
560                         if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
561                                 pageInput.val('1');
562                 });
563         }
564
565         $('.search-box input[type="search"], .search-box input[type="submit"]').mousedown(function () {
566                 $('select[name^="action"]').val('-1');
567         });
568
569         // Scroll into view when focused
570         $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
571                 if ( e.target.scrollIntoView )
572                         e.target.scrollIntoView(false);
573         });
574
575         // Disable upload buttons until files are selected
576         (function(){
577                 var button, input, form = $('form.wp-upload-form');
578                 if ( ! form.length )
579                         return;
580                 button = form.find('input[type="submit"]');
581                 input = form.find('input[type="file"]');
582
583                 function toggleUploadButton() {
584                         button.prop('disabled', '' === input.map( function() {
585                                 return $(this).val();
586                         }).get().join(''));
587                 }
588                 toggleUploadButton();
589                 input.on('change', toggleUploadButton);
590         })();
591
592         function pinMenu( event ) {
593                 var windowPos = $window.scrollTop(),
594                         resizing = ! event || event.type !== 'scroll';
595
596                 if ( isIOS || isIE8 || $adminmenu.data( 'wp-responsive' ) ) {
597                         return;
598                 }
599
600                 if ( height.menu + height.adminbar < height.window ||
601                         height.menu + height.adminbar + 20 > height.wpwrap ) {
602                         unpinMenu();
603                         return;
604                 }
605
606                 menuIsPinned = true;
607
608                 if ( height.menu + height.adminbar > height.window ) {
609                         // Check for overscrolling
610                         if ( windowPos < 0 ) {
611                                 if ( ! pinnedMenuTop ) {
612                                         pinnedMenuTop = true;
613                                         pinnedMenuBottom = false;
614
615                                         $adminMenuWrap.css({
616                                                 position: 'fixed',
617                                                 top: '',
618                                                 bottom: ''
619                                         });
620                                 }
621
622                                 return;
623                         } else if ( windowPos + height.window > $document.height() - 1 ) {
624                                 if ( ! pinnedMenuBottom ) {
625                                         pinnedMenuBottom = true;
626                                         pinnedMenuTop = false;
627
628                                         $adminMenuWrap.css({
629                                                 position: 'fixed',
630                                                 top: '',
631                                                 bottom: 0
632                                         });
633                                 }
634
635                                 return;
636                         }
637
638                         if ( windowPos > lastScrollPosition ) {
639                                 // Scrolling down
640                                 if ( pinnedMenuTop ) {
641                                         // let it scroll
642                                         pinnedMenuTop = false;
643                                         menuTop = $adminMenuWrap.offset().top - height.adminbar - ( windowPos - lastScrollPosition );
644
645                                         if ( menuTop + height.menu + height.adminbar < windowPos + height.window ) {
646                                                 menuTop = windowPos + height.window - height.menu - height.adminbar;
647                                         }
648
649                                         $adminMenuWrap.css({
650                                                 position: 'absolute',
651                                                 top: menuTop,
652                                                 bottom: ''
653                                         });
654                                 } else if ( ! pinnedMenuBottom && $adminMenuWrap.offset().top + height.menu < windowPos + height.window ) {
655                                         // pin the bottom
656                                         pinnedMenuBottom = true;
657
658                                         $adminMenuWrap.css({
659                                                 position: 'fixed',
660                                                 top: '',
661                                                 bottom: 0
662                                         });
663                                 }
664                         } else if ( windowPos < lastScrollPosition ) {
665                                 // Scrolling up
666                                 if ( pinnedMenuBottom ) {
667                                         // let it scroll
668                                         pinnedMenuBottom = false;
669                                         menuTop = $adminMenuWrap.offset().top - height.adminbar + ( lastScrollPosition - windowPos );
670
671                                         if ( menuTop + height.menu > windowPos + height.window ) {
672                                                 menuTop = windowPos;
673                                         }
674
675                                         $adminMenuWrap.css({
676                                                 position: 'absolute',
677                                                 top: menuTop,
678                                                 bottom: ''
679                                         });
680                                 } else if ( ! pinnedMenuTop && $adminMenuWrap.offset().top >= windowPos + height.adminbar ) {
681                                         // pin the top
682                                         pinnedMenuTop = true;
683
684                                         $adminMenuWrap.css({
685                                                 position: 'fixed',
686                                                 top: '',
687                                                 bottom: ''
688                                         });
689                                 }
690                         } else if ( resizing ) {
691                                 // Resizing
692                                 pinnedMenuTop = pinnedMenuBottom = false;
693                                 menuTop = windowPos + height.window - height.menu - height.adminbar - 1;
694
695                                 if ( menuTop > 0 ) {
696                                         $adminMenuWrap.css({
697                                                 position: 'absolute',
698                                                 top: menuTop,
699                                                 bottom: ''
700                                         });
701                                 } else {
702                                         unpinMenu();
703                                 }
704                         }
705                 }
706
707                 lastScrollPosition = windowPos;
708         }
709
710         function resetHeights() {
711                 height = {
712                         window: $window.height(),
713                         wpwrap: $wpwrap.height(),
714                         adminbar: $adminbar.height(),
715                         menu: $adminMenuWrap.height()
716                 };
717         }
718
719         function unpinMenu() {
720                 if ( isIOS || ! menuIsPinned ) {
721                         return;
722                 }
723
724                 pinnedMenuTop = pinnedMenuBottom = menuIsPinned = false;
725                 $adminMenuWrap.css({
726                         position: '',
727                         top: '',
728                         bottom: ''
729                 });
730         }
731
732         function setPinMenu() {
733                 resetHeights();
734
735                 if ( $adminmenu.data('wp-responsive') ) {
736                         $body.removeClass( 'sticky-menu' );
737                         unpinMenu();
738                 } else if ( height.menu + height.adminbar > height.window ) {
739                         pinMenu();
740                         $body.removeClass( 'sticky-menu' );
741                 } else {
742                         $body.addClass( 'sticky-menu' );
743                         unpinMenu();
744                 }
745         }
746
747         if ( ! isIOS ) {
748                 $window.on( 'scroll.pin-menu', pinMenu );
749                 $document.on( 'tinymce-editor-init.pin-menu', function( event, editor ) {
750                         editor.on( 'wp-autoresize', resetHeights );
751                 });
752         }
753
754         window.wpResponsive = {
755                 init: function() {
756                         var self = this;
757
758                         // Modify functionality based on custom activate/deactivate event
759                         $document.on( 'wp-responsive-activate.wp-responsive', function() {
760                                 self.activate();
761                         }).on( 'wp-responsive-deactivate.wp-responsive', function() {
762                                 self.deactivate();
763                         });
764
765                         $( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
766
767                         // Toggle sidebar when toggle is clicked
768                         $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
769                                 event.preventDefault();
770
771                                 // close any open toolbar submenus
772                                 $adminbar.find( '.hover' ).removeClass( 'hover' );
773
774                                 $wpwrap.toggleClass( 'wp-responsive-open' );
775                                 if ( $wpwrap.hasClass( 'wp-responsive-open' ) ) {
776                                         $(this).find('a').attr( 'aria-expanded', 'true' );
777                                         $( '#adminmenu a:first' ).focus();
778                                 } else {
779                                         $(this).find('a').attr( 'aria-expanded', 'false' );
780                                 }
781                         } );
782
783                         // Add menu events
784                         $adminmenu.on( 'click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
785                                 if ( ! $adminmenu.data('wp-responsive') ) {
786                                         return;
787                                 }
788
789                                 $( this ).parent( 'li' ).toggleClass( 'selected' );
790                                 event.preventDefault();
791                         });
792
793                         self.trigger();
794                         $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
795
796                         // This needs to run later as UI Sortable may be initialized later on $(document).ready()
797                         $window.on( 'load.wp-responsive', function() {
798                                 var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
799
800                                 if ( width <= 782 ) {
801                                         self.disableSortables();
802                                 }
803                         });
804                 },
805
806                 activate: function() {
807                         setPinMenu();
808
809                         if ( ! $body.hasClass( 'auto-fold' ) ) {
810                                 $body.addClass( 'auto-fold' );
811                         }
812
813                         $adminmenu.data( 'wp-responsive', 1 );
814                         this.disableSortables();
815                 },
816
817                 deactivate: function() {
818                         setPinMenu();
819                         $adminmenu.removeData('wp-responsive');
820                         this.enableSortables();
821                 },
822
823                 trigger: function() {
824                         var viewportWidth = getViewportWidth();
825
826                         // Exclude IE < 9, it doesn't support @media CSS rules.
827                         if ( ! viewportWidth ) {
828                                 return;
829                         }
830
831                         if ( viewportWidth <= 782 ) {
832                                 if ( ! wpResponsiveActive ) {
833                                         $document.trigger( 'wp-responsive-activate' );
834                                         wpResponsiveActive = true;
835                                 }
836                         } else {
837                                 if ( wpResponsiveActive ) {
838                                         $document.trigger( 'wp-responsive-deactivate' );
839                                         wpResponsiveActive = false;
840                                 }
841                         }
842
843                         if ( viewportWidth <= 480 ) {
844                                 this.enableOverlay();
845                         } else {
846                                 this.disableOverlay();
847                         }
848                 },
849
850                 enableOverlay: function() {
851                         if ( $overlay.length === 0 ) {
852                                 $overlay = $( '<div id="wp-responsive-overlay"></div>' )
853                                         .insertAfter( '#wpcontent' )
854                                         .hide()
855                                         .on( 'click.wp-responsive', function() {
856                                                 $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
857                                                 $( this ).hide();
858                                         });
859                         }
860
861                         $toolbarPopups.on( 'click.wp-responsive', function() {
862                                 $overlay.show();
863                         });
864                 },
865
866                 disableOverlay: function() {
867                         $toolbarPopups.off( 'click.wp-responsive' );
868                         $overlay.hide();
869                 },
870
871                 disableSortables: function() {
872                         if ( $sortables.length ) {
873                                 try {
874                                         $sortables.sortable('disable');
875                                 } catch(e) {}
876                         }
877                 },
878
879                 enableSortables: function() {
880                         if ( $sortables.length ) {
881                                 try {
882                                         $sortables.sortable('enable');
883                                 } catch(e) {}
884                         }
885                 }
886         };
887
888         // Add an ARIA role `button` to elements that behave like UI controls when JavaScript is on.
889         function aria_button_if_js() {
890                 $( '.aria-button-if-js' ).attr( 'role', 'button' );
891         }
892
893         $( document ).ajaxComplete( function() {
894                 aria_button_if_js();
895         });
896
897         /**
898          * @summary Get the viewport width.
899          *
900          * @since 4.7.0
901          *
902          * @returns {number|boolean} The current viewport width or false if the
903          *                           browser doesn't support innerWidth (IE < 9).
904          */
905         function getViewportWidth() {
906                 var viewportWidth = false;
907
908                 if ( window.innerWidth ) {
909                         // On phones, window.innerWidth is affected by zooming.
910                         viewportWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
911                 }
912
913                 return viewportWidth;
914         }
915
916         /**
917          * @summary Set the admin menu collapsed/expanded state.
918          *
919          * Sets the global variable `menuState` and triggers a custom event passing
920          * the current menu state.
921          *
922          * @since 4.7.0
923          *
924          * @returns {void}
925          */
926         function setMenuState() {
927                 var viewportWidth = getViewportWidth() || 961;
928
929                 if ( viewportWidth <= 782  ) {
930                         menuState = 'responsive';
931                 } else if ( $body.hasClass( 'folded' ) || ( $body.hasClass( 'auto-fold' ) && viewportWidth <= 960 && viewportWidth > 782 ) ) {
932                         menuState = 'folded';
933                 } else {
934                         menuState = 'open';
935                 }
936
937                 $document.trigger( 'wp-menu-state-set', { state: menuState } );
938         }
939
940         // Set the menu state when the window gets resized.
941         $document.on( 'wp-window-resized.set-menu-state', setMenuState );
942
943         /**
944          * @summary Set ARIA attributes on the collapse/expand menu button.
945          *
946          * When the admin menu is open or folded, updates the `aria-expanded` and
947          * `aria-label` attributes of the button to give feedback to assistive
948          * technologies. In the responsive view, the button is always hidden.
949          *
950          * @since 4.7.0
951          *
952          * @returns {void}
953          */
954         $document.on( 'wp-menu-state-set wp-collapse-menu', function( event, eventData ) {
955                 var $collapseButton = $( '#collapse-button' ),
956                         ariaExpanded = 'true',
957                         ariaLabelText = commonL10n.collapseMenu;
958
959                 if ( 'folded' === eventData.state ) {
960                         ariaExpanded = 'false';
961                         ariaLabelText = commonL10n.expandMenu;
962                 }
963
964                 $collapseButton.attr({
965                         'aria-expanded': ariaExpanded,
966                         'aria-label': ariaLabelText
967                 });
968         });
969
970         window.wpResponsive.init();
971         setPinMenu();
972         setMenuState();
973         currentMenuItemHasPopup();
974         makeNoticesDismissible();
975         aria_button_if_js();
976
977         $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 );
978
979         // Set initial focus on a specific element.
980         $( '.wp-initial-focus' ).focus();
981 });
982
983 // Fire a custom jQuery event at the end of window resize
984 ( function() {
985         var timeout;
986
987         function triggerEvent() {
988                 $document.trigger( 'wp-window-resized' );
989         }
990
991         function fireOnce() {
992                 window.clearTimeout( timeout );
993                 timeout = window.setTimeout( triggerEvent, 200 );
994         }
995
996         $window.on( 'resize.wp-fire-once', fireOnce );
997 }());
998
999 // Make Windows 8 devices play along nicely.
1000 (function(){
1001         if ( '-ms-user-select' in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/) ) {
1002                 var msViewportStyle = document.createElement( 'style' );
1003                 msViewportStyle.appendChild(
1004                         document.createTextNode( '@-ms-viewport{width:auto!important}' )
1005                 );
1006                 document.getElementsByTagName( 'head' )[0].appendChild( msViewportStyle );
1007         }
1008 })();
1009
1010 }( jQuery, window ));