]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/common.js
WordPress 4.1
[autoinstalls/wordpress.git] / wp-admin / js / common.js
1 /* global setUserSetting, ajaxurl, commonL10n, alert, confirm, pagenow */
2 var showNotice, adminMenu, columns, validateForm, screenMeta;
3 ( function( $, window, undefined ) {
4 // 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,
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 .updated and .error alert boxes. Don't move boxes designed to be inline.
372         $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
373         $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
374
375         // Init screen meta
376         screenMeta.init();
377
378         // check all checkboxes
379         $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
380                 if ( 'undefined' == e.shiftKey ) { return true; }
381                 if ( e.shiftKey ) {
382                         if ( !lastClicked ) { return true; }
383                         checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
384                         first = checks.index( lastClicked );
385                         last = checks.index( this );
386                         checked = $(this).prop('checked');
387                         if ( 0 < first && 0 < last && first != last ) {
388                                 sliced = ( last > first ) ? checks.slice( first, last ) : checks.slice( last, first );
389                                 sliced.prop( 'checked', function() {
390                                         if ( $(this).closest('tr').is(':visible') )
391                                                 return checked;
392
393                                         return false;
394                                 });
395                         }
396                 }
397                 lastClicked = this;
398
399                 // toggle "check all" checkboxes
400                 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible').not(':checked');
401                 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
402                         return ( 0 === unchecked.length );
403                 });
404
405                 return true;
406         });
407
408         $('thead, tfoot').find('.check-column :checkbox').on( 'click.wp-toggle-checkboxes', function( event ) {
409                 var $this = $(this),
410                         $table = $this.closest( 'table' ),
411                         controlChecked = $this.prop('checked'),
412                         toggle = event.shiftKey || $this.data('wp-toggle');
413
414                 $table.children( 'tbody' ).filter(':visible')
415                         .children().children('.check-column').find(':checkbox')
416                         .prop('checked', function() {
417                                 if ( $(this).is(':hidden') ) {
418                                         return false;
419                                 }
420
421                                 if ( toggle ) {
422                                         return ! $(this).prop( 'checked' );
423                                 } else if ( controlChecked ) {
424                                         return true;
425                                 }
426
427                                 return false;
428                         });
429
430                 $table.children('thead,  tfoot').filter(':visible')
431                         .children().children('.check-column').find(':checkbox')
432                         .prop('checked', function() {
433                                 if ( toggle ) {
434                                         return false;
435                                 } else if ( controlChecked ) {
436                                         return true;
437                                 }
438
439                                 return false;
440                         });
441         });
442
443         // Show row actions on keyboard focus of its parent container element or any other elements contained within
444         $( 'td.post-title, td.title, td.comment, .bookmarks td.column-name, td.blogname, td.username, .dashboard-comment-wrap' ).focusin(function(){
445                 clearTimeout( transitionTimeout );
446                 focusedRowActions = $(this).find( '.row-actions' );
447                 focusedRowActions.addClass( 'visible' );
448         }).focusout(function(){
449                 // Tabbing between post title and .row-actions links needs a brief pause, otherwise
450                 // the .row-actions div gets hidden in transit in some browsers (ahem, Firefox).
451                 transitionTimeout = setTimeout(function(){
452                         focusedRowActions.removeClass( 'visible' );
453                 }, 30);
454         });
455
456         $('#default-password-nag-no').click( function() {
457                 setUserSetting('default_password_nag', 'hide');
458                 $('div.default-password-nag').hide();
459                 return false;
460         });
461
462         // tab in textareas
463         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
464                 var el = e.target, selStart, selEnd, val, scroll, sel;
465
466                 if ( e.keyCode == 27 ) { // escape key
467                         $(el).data('tab-out', true);
468                         return;
469                 }
470
471                 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
472                         return;
473
474                 if ( $(el).data('tab-out') ) {
475                         $(el).data('tab-out', false);
476                         return;
477                 }
478
479                 selStart = el.selectionStart;
480                 selEnd = el.selectionEnd;
481                 val = el.value;
482
483                 try {
484                         this.lastKey = 9; // not a standard DOM property, lastKey is to help stop Opera tab event. See blur handler below.
485                 } catch(err) {}
486
487                 if ( document.selection ) {
488                         el.focus();
489                         sel = document.selection.createRange();
490                         sel.text = '\t';
491                 } else if ( selStart >= 0 ) {
492                         scroll = this.scrollTop;
493                         el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
494                         el.selectionStart = el.selectionEnd = selStart + 1;
495                         this.scrollTop = scroll;
496                 }
497
498                 if ( e.stopPropagation )
499                         e.stopPropagation();
500                 if ( e.preventDefault )
501                         e.preventDefault();
502         });
503
504         $('#newcontent').bind('blur.wpevent_InsertTab', function() {
505                 if ( this.lastKey && 9 == this.lastKey )
506                         this.focus();
507         });
508
509         if ( pageInput.length ) {
510                 pageInput.closest('form').submit( function() {
511
512                         // Reset paging var for new filters/searches but not for bulk actions. See #17685.
513                         if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
514                                 pageInput.val('1');
515                 });
516         }
517
518         $('.search-box input[type="search"], .search-box input[type="submit"]').mousedown(function () {
519                 $('select[name^="action"]').val('-1');
520         });
521
522         // Scroll into view when focused
523         $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
524                 if ( e.target.scrollIntoView )
525                         e.target.scrollIntoView(false);
526         });
527
528         // Disable upload buttons until files are selected
529         (function(){
530                 var button, input, form = $('form.wp-upload-form');
531                 if ( ! form.length )
532                         return;
533                 button = form.find('input[type="submit"]');
534                 input = form.find('input[type="file"]');
535
536                 function toggleUploadButton() {
537                         button.prop('disabled', '' === input.map( function() {
538                                 return $(this).val();
539                         }).get().join(''));
540                 }
541                 toggleUploadButton();
542                 input.on('change', toggleUploadButton);
543         })();
544
545         function pinMenu( event ) {
546                 var windowPos = $window.scrollTop(),
547                         resizing = ! event || event.type !== 'scroll';
548
549                 if ( isIOS || isIE8 || $adminmenu.data( 'wp-responsive' ) ) {
550                         return;
551                 }
552
553                 if ( height.menu + height.adminbar < height.window ||
554                         height.menu + height.adminbar + 20 > height.wpwrap ) {
555                         unpinMenu();
556                         return;
557                 }
558
559                 menuIsPinned = true;
560
561                 if ( height.menu + height.adminbar > height.window ) {
562                         // Check for overscrolling
563                         if ( windowPos < 0 ) {
564                                 if ( ! pinnedMenuTop ) {
565                                         pinnedMenuTop = true;
566                                         pinnedMenuBottom = false;
567
568                                         $adminMenuWrap.css({
569                                                 position: 'fixed',
570                                                 top: '',
571                                                 bottom: ''
572                                         });
573                                 }
574
575                                 return;
576                         } else if ( windowPos + height.window > $document.height() - 1 ) {
577                                 if ( ! pinnedMenuBottom ) {
578                                         pinnedMenuBottom = true;
579                                         pinnedMenuTop = false;
580
581                                         $adminMenuWrap.css({
582                                                 position: 'fixed',
583                                                 top: '',
584                                                 bottom: 0
585                                         });
586                                 }
587
588                                 return;
589                         }
590
591                         if ( windowPos > lastScrollPosition ) {
592                                 // Scrolling down
593                                 if ( pinnedMenuTop ) {
594                                         // let it scroll
595                                         pinnedMenuTop = false;
596                                         menuTop = $adminMenuWrap.offset().top - height.adminbar - ( windowPos - lastScrollPosition );
597
598                                         if ( menuTop + height.menu + height.adminbar < windowPos + height.window ) {
599                                                 menuTop = windowPos + height.window - height.menu - height.adminbar;
600                                         }
601
602                                         $adminMenuWrap.css({
603                                                 position: 'absolute',
604                                                 top: menuTop,
605                                                 bottom: ''
606                                         });
607                                 } else if ( ! pinnedMenuBottom && $adminMenuWrap.offset().top + height.menu < windowPos + height.window ) {
608                                         // pin the bottom
609                                         pinnedMenuBottom = true;
610
611                                         $adminMenuWrap.css({
612                                                 position: 'fixed',
613                                                 top: '',
614                                                 bottom: 0
615                                         });
616                                 }
617                         } else if ( windowPos < lastScrollPosition ) {
618                                 // Scrolling up
619                                 if ( pinnedMenuBottom ) {
620                                         // let it scroll
621                                         pinnedMenuBottom = false;
622                                         menuTop = $adminMenuWrap.offset().top - height.adminbar + ( lastScrollPosition - windowPos );
623
624                                         if ( menuTop + height.menu > windowPos + height.window ) {
625                                                 menuTop = windowPos;
626                                         }
627
628                                         $adminMenuWrap.css({
629                                                 position: 'absolute',
630                                                 top: menuTop,
631                                                 bottom: ''
632                                         });
633                                 } else if ( ! pinnedMenuTop && $adminMenuWrap.offset().top >= windowPos + height.adminbar ) {
634                                         // pin the top
635                                         pinnedMenuTop = true;
636
637                                         $adminMenuWrap.css({
638                                                 position: 'fixed',
639                                                 top: '',
640                                                 bottom: ''
641                                         });
642                                 }
643                         } else if ( resizing ) {
644                                 // Resizing
645                                 pinnedMenuTop = pinnedMenuBottom = false;
646                                 menuTop = windowPos + height.window - height.menu - height.adminbar - 1;
647
648                                 if ( menuTop > 0 ) {
649                                         $adminMenuWrap.css({
650                                                 position: 'absolute',
651                                                 top: menuTop,
652                                                 bottom: ''
653                                         });
654                                 } else {
655                                         unpinMenu();
656                                 }
657                         }
658                 }
659
660                 lastScrollPosition = windowPos;
661         }
662
663         function resetHeights() {
664                 height = {
665                         window: $window.height(),
666                         wpwrap: $wpwrap.height(),
667                         adminbar: $adminbar.height(),
668                         menu: $adminMenuWrap.height()
669                 };
670         }
671
672         function unpinMenu() {
673                 if ( isIOS || ! menuIsPinned ) {
674                         return;
675                 }
676
677                 pinnedMenuTop = pinnedMenuBottom = menuIsPinned = false;
678                 $adminMenuWrap.css({
679                         position: '',
680                         top: '',
681                         bottom: ''
682                 });
683         }
684
685         function setPinMenu() {
686                 resetHeights();
687
688                 if ( $adminmenu.data('wp-responsive') ) {
689                         $body.removeClass( 'sticky-menu' );
690                         unpinMenu();
691                 } else if ( height.menu + height.adminbar > height.window ) {
692                         pinMenu();
693                         $body.removeClass( 'sticky-menu' );
694                 } else {
695                         $body.addClass( 'sticky-menu' );
696                         unpinMenu();
697                 }
698         }
699
700         if ( ! isIOS ) {
701                 $window.on( 'scroll.pin-menu', pinMenu );
702                 $document.on( 'tinymce-editor-init.pin-menu', function( event, editor ) {
703                         editor.on( 'wp-autoresize', resetHeights );
704                 });
705         }
706
707         window.wpResponsive = {
708                 init: function() {
709                         var self = this;
710
711                         // Modify functionality based on custom activate/deactivate event
712                         $document.on( 'wp-responsive-activate.wp-responsive', function() {
713                                 self.activate();
714                         }).on( 'wp-responsive-deactivate.wp-responsive', function() {
715                                 self.deactivate();
716                         });
717
718                         $( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
719
720                         // Toggle sidebar when toggle is clicked
721                         $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
722                                 event.preventDefault();
723                                 $wpwrap.toggleClass( 'wp-responsive-open' );
724                                 if ( $wpwrap.hasClass( 'wp-responsive-open' ) ) {
725                                         $(this).find('a').attr( 'aria-expanded', 'true' );
726                                         $( '#adminmenu a:first' ).focus();
727                                 } else {
728                                         $(this).find('a').attr( 'aria-expanded', 'false' );
729                                 }
730                         } );
731
732                         // Add menu events
733                         $adminmenu.on( 'click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
734                                 if ( ! $adminmenu.data('wp-responsive') ) {
735                                         return;
736                                 }
737
738                                 $( this ).parent( 'li' ).toggleClass( 'selected' );
739                                 event.preventDefault();
740                         });
741
742                         self.trigger();
743                         $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
744
745                         // This needs to run later as UI Sortable may be initialized later on $(document).ready()
746                         $window.on( 'load.wp-responsive', function() {
747                                 var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
748
749                                 if ( width <= 782 ) {
750                                         self.disableSortables();
751                                 }
752                         });
753                 },
754
755                 activate: function() {
756                         setPinMenu();
757
758                         if ( ! $body.hasClass( 'auto-fold' ) ) {
759                                 $body.addClass( 'auto-fold' );
760                         }
761
762                         $adminmenu.data( 'wp-responsive', 1 );
763                         this.disableSortables();
764                 },
765
766                 deactivate: function() {
767                         setPinMenu();
768                         $adminmenu.removeData('wp-responsive');
769                         this.enableSortables();
770                 },
771
772                 trigger: function() {
773                         var width;
774
775                         if ( window.innerWidth ) {
776                                 // window.innerWidth is affected by zooming on phones
777                                 width = Math.max( window.innerWidth, document.documentElement.clientWidth );
778                         } else {
779                                 // Exclude IE < 9, it doesn't support @media CSS rules
780                                 return;
781                         }
782
783                         if ( width <= 782 ) {
784                                 if ( ! wpResponsiveActive ) {
785                                         $document.trigger( 'wp-responsive-activate' );
786                                         wpResponsiveActive = true;
787                                 }
788                         } else {
789                                 if ( wpResponsiveActive ) {
790                                         $document.trigger( 'wp-responsive-deactivate' );
791                                         wpResponsiveActive = false;
792                                 }
793                         }
794
795                         if ( width <= 480 ) {
796                                 this.enableOverlay();
797                         } else {
798                                 this.disableOverlay();
799                         }
800                 },
801
802                 enableOverlay: function() {
803                         if ( $overlay.length === 0 ) {
804                                 $overlay = $( '<div id="wp-responsive-overlay"></div>' )
805                                         .insertAfter( '#wpcontent' )
806                                         .hide()
807                                         .on( 'click.wp-responsive', function() {
808                                                 $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
809                                                 $( this ).hide();
810                                         });
811                         }
812
813                         $toolbarPopups.on( 'click.wp-responsive', function() {
814                                 $overlay.show();
815                         });
816                 },
817
818                 disableOverlay: function() {
819                         $toolbarPopups.off( 'click.wp-responsive' );
820                         $overlay.hide();
821                 },
822
823                 disableSortables: function() {
824                         if ( $sortables.length ) {
825                                 try {
826                                         $sortables.sortable('disable');
827                                 } catch(e) {}
828                         }
829                 },
830
831                 enableSortables: function() {
832                         if ( $sortables.length ) {
833                                 try {
834                                         $sortables.sortable('enable');
835                                 } catch(e) {}
836                         }
837                 }
838         };
839
840         window.wpResponsive.init();
841         setPinMenu();
842
843         $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 );
844 });
845
846 // Fire a custom jQuery event at the end of window resize
847 ( function() {
848         var timeout;
849
850         function triggerEvent() {
851                 $(document).trigger( 'wp-window-resized' );
852         }
853
854         function fireOnce() {
855                 window.clearTimeout( timeout );
856                 timeout = window.setTimeout( triggerEvent, 200 );
857         }
858
859         $(window).on( 'resize.wp-fire-once', fireOnce );
860 }());
861
862 // Make Windows 8 devices play along nicely.
863 (function(){
864         if ( '-ms-user-select' in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/) ) {
865                 var msViewportStyle = document.createElement( 'style' );
866                 msViewportStyle.appendChild(
867                         document.createTextNode( '@-ms-viewport{width:auto!important}' )
868                 );
869                 document.getElementsByTagName( 'head' )[0].appendChild( msViewportStyle );
870         }
871 })();
872
873 }( jQuery, window ));