+ function pinMenu( event ) {
+ var windowPos = $window.scrollTop(),
+ resizing = ! event || event.type !== 'scroll';
+
+ if ( isIOS || isIE8 || $adminmenu.data( 'wp-responsive' ) ) {
+ return;
+ }
+
+ if ( height.menu + height.adminbar < height.window ||
+ height.menu + height.adminbar + 20 > height.wpwrap ) {
+ unpinMenu();
+ return;
+ }
+
+ menuIsPinned = true;
+
+ if ( height.menu + height.adminbar > height.window ) {
+ // Check for overscrolling
+ if ( windowPos < 0 ) {
+ if ( ! pinnedMenuTop ) {
+ pinnedMenuTop = true;
+ pinnedMenuBottom = false;
+
+ $adminMenuWrap.css({
+ position: 'fixed',
+ top: '',
+ bottom: ''
+ });
+ }
+
+ return;
+ } else if ( windowPos + height.window > $document.height() - 1 ) {
+ if ( ! pinnedMenuBottom ) {
+ pinnedMenuBottom = true;
+ pinnedMenuTop = false;
+
+ $adminMenuWrap.css({
+ position: 'fixed',
+ top: '',
+ bottom: 0
+ });
+ }
+
+ return;
+ }
+
+ if ( windowPos > lastScrollPosition ) {
+ // Scrolling down
+ if ( pinnedMenuTop ) {
+ // let it scroll
+ pinnedMenuTop = false;
+ menuTop = $adminMenuWrap.offset().top - height.adminbar - ( windowPos - lastScrollPosition );
+
+ if ( menuTop + height.menu + height.adminbar < windowPos + height.window ) {
+ menuTop = windowPos + height.window - height.menu - height.adminbar;
+ }
+
+ $adminMenuWrap.css({
+ position: 'absolute',
+ top: menuTop,
+ bottom: ''
+ });
+ } else if ( ! pinnedMenuBottom && $adminMenuWrap.offset().top + height.menu < windowPos + height.window ) {
+ // pin the bottom
+ pinnedMenuBottom = true;
+
+ $adminMenuWrap.css({
+ position: 'fixed',
+ top: '',
+ bottom: 0
+ });
+ }
+ } else if ( windowPos < lastScrollPosition ) {
+ // Scrolling up
+ if ( pinnedMenuBottom ) {
+ // let it scroll
+ pinnedMenuBottom = false;
+ menuTop = $adminMenuWrap.offset().top - height.adminbar + ( lastScrollPosition - windowPos );
+
+ if ( menuTop + height.menu > windowPos + height.window ) {
+ menuTop = windowPos;
+ }
+
+ $adminMenuWrap.css({
+ position: 'absolute',
+ top: menuTop,
+ bottom: ''
+ });
+ } else if ( ! pinnedMenuTop && $adminMenuWrap.offset().top >= windowPos + height.adminbar ) {
+ // pin the top
+ pinnedMenuTop = true;
+
+ $adminMenuWrap.css({
+ position: 'fixed',
+ top: '',
+ bottom: ''
+ });
+ }
+ } else if ( resizing ) {
+ // Resizing
+ pinnedMenuTop = pinnedMenuBottom = false;
+ menuTop = windowPos + height.window - height.menu - height.adminbar - 1;
+
+ if ( menuTop > 0 ) {
+ $adminMenuWrap.css({
+ position: 'absolute',
+ top: menuTop,
+ bottom: ''
+ });
+ } else {
+ unpinMenu();
+ }
+ }
+ }
+
+ lastScrollPosition = windowPos;
+ }
+
+ function resetHeights() {
+ height = {
+ window: $window.height(),
+ wpwrap: $wpwrap.height(),
+ adminbar: $adminbar.height(),
+ menu: $adminMenuWrap.height()
+ };
+ }
+
+ function unpinMenu() {
+ if ( isIOS || ! menuIsPinned ) {
+ return;
+ }
+
+ pinnedMenuTop = pinnedMenuBottom = menuIsPinned = false;
+ $adminMenuWrap.css({
+ position: '',
+ top: '',
+ bottom: ''
+ });
+ }
+
+ function setPinMenu() {
+ resetHeights();
+
+ if ( $adminmenu.data('wp-responsive') ) {
+ $body.removeClass( 'sticky-menu' );
+ unpinMenu();
+ } else if ( height.menu + height.adminbar > height.window ) {
+ pinMenu();
+ $body.removeClass( 'sticky-menu' );
+ } else {
+ $body.addClass( 'sticky-menu' );
+ unpinMenu();
+ }
+ }
+
+ if ( ! isIOS ) {
+ $window.on( 'scroll.pin-menu', pinMenu );
+ $document.on( 'tinymce-editor-init.pin-menu', function( event, editor ) {
+ editor.on( 'wp-autoresize', resetHeights );
+ });
+ }
+
+ window.wpResponsive = {
+ init: function() {
+ var self = this;
+
+ // Modify functionality based on custom activate/deactivate event
+ $document.on( 'wp-responsive-activate.wp-responsive', function() {
+ self.activate();
+ }).on( 'wp-responsive-deactivate.wp-responsive', function() {
+ self.deactivate();
+ });
+
+ $( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
+
+ // Toggle sidebar when toggle is clicked
+ $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
+ event.preventDefault();
+
+ // close any open toolbar submenus
+ $adminbar.find( '.hover' ).removeClass( 'hover' );
+
+ $wpwrap.toggleClass( 'wp-responsive-open' );
+ if ( $wpwrap.hasClass( 'wp-responsive-open' ) ) {
+ $(this).find('a').attr( 'aria-expanded', 'true' );
+ $( '#adminmenu a:first' ).focus();
+ } else {
+ $(this).find('a').attr( 'aria-expanded', 'false' );
+ }
+ } );
+
+ // Add menu events
+ $adminmenu.on( 'click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
+ if ( ! $adminmenu.data('wp-responsive') ) {
+ return;
+ }
+
+ $( this ).parent( 'li' ).toggleClass( 'selected' );
+ event.preventDefault();
+ });
+
+ self.trigger();
+ $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
+
+ // This needs to run later as UI Sortable may be initialized later on $(document).ready()
+ $window.on( 'load.wp-responsive', function() {
+ var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
+
+ if ( width <= 782 ) {
+ self.disableSortables();
+ }
+ });
+ },
+
+ activate: function() {
+ setPinMenu();
+
+ if ( ! $body.hasClass( 'auto-fold' ) ) {
+ $body.addClass( 'auto-fold' );
+ }
+
+ $adminmenu.data( 'wp-responsive', 1 );
+ this.disableSortables();
+ },
+
+ deactivate: function() {
+ setPinMenu();
+ $adminmenu.removeData('wp-responsive');
+ this.enableSortables();
+ },