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