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