Make WordPress Core

Changeset 26402


Ignore:
Timestamp:
11/26/2013 08:56:11 AM (13 years ago)
Author:
azaozz
Message:

Clean up the sticky menu and responsive tweaks JS:

  • More descriptive names.
  • Share cached jQuery elements.
  • Fix re-enabling of touch events on the admin menu.
  • Fix disabling/enabling of UI Sortable.

Props tollmanz, props sanchothefat, see #26086, fixes #26088.

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/css/color-schemes/_admin.scss

    r26387 r26402  
    385385/* Responsive Component */
    386386
    387 div#moby6-toggle a:before {
    388         color: $menu-icon;
    389 }
    390 
    391 .moby6-open div#moby6-toggle a {
     387div#wp-responsive-toggle a:before {
     388        color: $menu-icon;
     389}
     390
     391.wp-responsive-open div#wp-responsive-toggle a {
    392392        // ToDo: make inset border
    393393        border-color: transparent;
  • trunk/src/wp-admin/css/wp-admin.css

    r26396 r26402  
    1060110601}
    1060210602
    10603 li#wp-admin-bar-toggle-button {
     10603li#wp-admin-bar-menu-toggle {
    1060410604        display: none;
    1060510605}
     
    1109911099
    1110011100        /*  Sidebar Toggle */
    11101         #moby6-toggle {
     11101        #wp-responsive-toggle {
    1110211102                position: fixed;
    1110311103                top: 5px;
     
    1111411114        }
    1111511115
    11116         .moby6-open #wpadminbar #wp-admin-bar-toggle-button a {
     11116        .wp-responsive-open #wpadminbar #wp-admin-bar-menu-toggle a {
    1111711117                background: #000;
    1111811118        }
    1111911119
    11120         .moby6-open #wpbody {
     11120        .wp-responsive-open #wpbody {
    1112111121                right: -190px;
    1112211122        }
    1112311123
    11124         .auto-fold .moby6-open #adminmenuback,
    11125         .auto-fold .moby6-open #adminmenuwrap {
     11124        .auto-fold .wp-responsive-open #adminmenuback,
     11125        .auto-fold .wp-responsive-open #adminmenuwrap {
    1112611126                left: 0;
    1112711127        }
     
    1213912139
    1214012140@media screen and ( max-width: 782px ) {
    12141         #wpadminbar #wp-admin-bar-toggle-button a {
     12141        #wpadminbar #wp-admin-bar-menu-toggle a {
    1214212142                display: block;
    1214312143                padding: 0;
     
    1215112151        }
    1215212152
    12153         li#wp-admin-bar-toggle-button {
     12153        li#wp-admin-bar-menu-toggle {
    1215412154                display: block;
    1215512155        }
    1215612156
    12157         #wpadminbar #wp-admin-bar-toggle-button a:hover {
     12157        #wpadminbar #wp-admin-bar-menu-toggle a:hover {
    1215812158                border: 1px solid transparent;
    1215912159        }
    1216012160
    12161         #wpadminbar #wp-admin-bar-toggle-button .ab-icon:before {
     12161        #wpadminbar #wp-admin-bar-menu-toggle .ab-icon:before {
    1216212162                content: '\f228';
    1216312163                display: inline-block;
     
    1219412194        }
    1219512195       
    12196         #moby6-overlay {
     12196        #wp-responsive-overlay {
    1219712197                position: fixed;
    1219812198                top: 0;
  • trunk/src/wp-admin/js/common.js

    r26208 r26402  
    11/* global setUserSetting, ajaxurl, commonL10n, alert, confirm, toggleWithKeyboard, pagenow */
    2 var showNotice, adminMenu, columns, validateForm, screenMeta, stickyMenu;
    3 (function($){
     2var showNotice, adminMenu, columns, validateForm, screenMeta;
     3( function( $, window, undefined ) {
    44// Removed in 3.3.
    55// (perhaps) needed for back-compat
     
    218218                // close any open submenus when touch/click is not on the menu
    219219                $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
    220                         if ( !$(e.target).closest('#adminmenu').length )
     220                        if ( menu.data('wp-responsive') ) {
     221                                return;
     222                        }
     223
     224                        if ( ! $(e.target).closest('#adminmenu').length ) {
    221225                                menu.find('li.wp-has-submenu.opensub').removeClass('opensub');
     226                        }
    222227                });
    223228
    224229                menu.find('a.wp-has-submenu').on( mobileEvent+'.wp-mobile-hover', function(e) {
    225230                        var el = $(this), parent = el.parent();
     231
     232                        if ( menu.data('wp-responsive') ) {
     233                                return;
     234                        }
    226235
    227236                        // Show the sub instead of following the link if:
     
    448457});
    449458
    450 stickyMenu = {
    451         active: false,
    452 
    453         init: function () {
    454                 this.$window = $( window );
    455                 this.$body = $( document.body );
    456                 this.$adminMenuWrap = $( '#adminmenuwrap' );
    457                 this.$collapseMenu = $( '#collapse-menu' );
    458                 this.bodyMinWidth = parseInt( this.$body.css( 'min-width' ), 10 );
    459                 this.enable();
    460         },
    461 
    462         enable: function () {
    463                 if ( ! this.active ) {
    464                         this.$window.on( 'resize.stickyMenu scroll.stickyMenu', this.debounce(
    465                                 $.proxy( this.update, this ), 200
    466                         ) );
    467                         this.$collapseMenu.on( 'click.stickyMenu', $.proxy( this.update, this ) );
    468                         this.update();
    469                         this.active = true;
    470                 }
    471         },
    472 
    473         disable: function () {
    474                 if ( this.active ) {
    475                         this.$window.off( 'resize.stickyMenu scroll.stickyMenu' );
    476                         this.$collapseMenu.off( 'click.stickyMenu' );
    477                         this.$body.removeClass( 'sticky-menu' );
    478                         this.active = false;
    479                 }
    480         },
    481 
    482         update: function () {
    483                 // Make the admin menu sticky if both of the following:
    484                 // 1. The viewport is taller than the admin menu
    485                 // 2. The viewport is wider than the min-width of the <body>
    486                 if ( this.$window.height() > this.$adminMenuWrap.height() + 32 && this.$window.width() > this.bodyMinWidth) {
    487                         if ( ! this.$body.hasClass( 'sticky-menu' ) ) {
    488                                 this.$body.addClass( 'sticky-menu' );
    489                         }
    490                 } else {
    491                         if ( this.$body.hasClass( 'sticky-menu' ) ) {
    492                                 this.$body.removeClass( 'sticky-menu' );
    493                         }
    494                 }
    495         },
    496 
    497         // Borrowed from Underscore.js
    498         debounce: function( func, wait, immediate ) {
    499                 var timeout, args, context, timestamp, result;
    500                 return function() {
    501                         var later, callNow;
    502                         context = this;
    503                         args = arguments;
    504                         timestamp = new Date().getTime();
    505                         later = function() {
    506                                 var last = new Date().getTime() - timestamp;
    507                                 if ( last < wait ) {
    508                                         timeout = setTimeout( later, wait - last );
    509                                 } else {
    510                                         timeout = null;
    511                                         if ( ! immediate ) {
    512                                                 result = func.apply( context, args );
    513                                                 context = args = null;
    514                                         }
     459// Fire a custom jQuery event at the end of window resize
     460( function() {
     461        var timeout;
     462
     463        function triggerEvent() {
     464                $(document).trigger( 'wp-window-resized' );
     465        }
     466
     467        function fireOnce() {
     468                window.clearTimeout( timeout );
     469                timeout = window.setTimeout( triggerEvent, 200 );
     470        }
     471
     472        $(window).on( 'resize.wp-fire-once', fireOnce );
     473}());
     474
     475$(document).ready( function() {
     476        var $document = $( document ),
     477                $window = $( window ),
     478                $body = $( document.body ),
     479                $adminMenuWrap = $( '#adminmenuwrap' ),
     480                $collapseMenu = $( '#collapse-menu' ),
     481                $wpwrap = $( '#wpwrap' ),
     482                $adminmenu = $( '#adminmenu' ),
     483                $overlay = $( '#wp-responsive-overlay' ),
     484                $toolbar = $( '#wp-toolbar' ),
     485                $toolbarPopups = $toolbar.find( 'a[aria-haspopup="true"]' ),
     486                $sortables = $('.meta-box-sortables'),
     487                stickyMenuActive = false,
     488                wpResponsiveActive = false;
     489
     490        window.stickyMenu = {
     491                enable: function() {
     492                        if ( ! stickyMenuActive ) {
     493                                $document.on( 'wp-window-resized.sticky-menu', $.proxy( this.update, this ) );
     494                                $collapseMenu.on( 'click.sticky-menu', $.proxy( this.update, this ) );
     495                                this.update();
     496                                stickyMenuActive = true;
     497                        }
     498                },
     499
     500                disable: function() {
     501                        if ( stickyMenuActive ) {
     502                                $window.off( 'resize.sticky-menu' );
     503                                $collapseMenu.off( 'click.sticky-menu' );
     504                                $body.removeClass( 'sticky-menu' );
     505                                stickyMenuActive = false;
     506                        }
     507                },
     508
     509                update: function() {
     510                        // Make the admin menu sticky if the viewport is taller than it
     511                        if ( $window.height() > $adminMenuWrap.height() + 32 ) {
     512                                if ( ! $body.hasClass( 'sticky-menu' ) ) {
     513                                        $body.addClass( 'sticky-menu' );
    515514                                }
    516                         };
    517                         callNow = immediate && !timeout;
    518                         if ( ! timeout ) {
    519                                 timeout = setTimeout( later, wait );
    520                         }
    521                         if ( callNow ) {
    522                                 result = func.apply( context, args );
    523                                 context = args = null;
    524                         }
    525 
    526                         return result;
    527                 };
    528         }
    529 };
    530 
    531 stickyMenu.init();
    532 
    533 var moby6 = {
    534 
    535         init: function() {
    536                 // cached selectors
    537                 this.$html = $( document.documentElement );
    538                 this.$body = $( document.body );
    539                 this.$wpwrap = $( '#wpwrap' );
    540                 this.$wpbody = $( '#wpbody' );
    541                 this.$adminmenu = $( '#adminmenu' );
    542                 this.$overlay = $( '#moby6-overlay' );
    543                 this.$toolbar = $( '#wp-toolbar' );
    544                 this.$toolbarPopups = this.$toolbar.find( 'a[aria-haspopup="true"]' );
    545 
    546                 // Modify functionality based on custom activate/deactivate event
    547                 this.$html
    548                         .on( 'activate.moby6', function() { moby6.activate(); } )
    549                         .on( 'deactivate.moby6', function() { moby6.deactivate(); } );
    550 
    551                 // Toggle sidebar when toggle is clicked
    552                 $( '#wp-admin-bar-toggle-button' ).on( 'click', function(evt) {
    553                         evt.preventDefault();
    554                         moby6.$wpwrap.toggleClass( 'moby6-open' );
    555                 } );
    556 
    557                 // Trigger custom events based on active media query.
    558                 this.matchMedia();
    559                 $( window ).on( 'resize', $.proxy( this.matchMedia, this ) );
    560         },
    561 
    562         activate: function() {
    563 
    564                 window.stickymenu && window.stickymenu.disable();
    565 
    566                 if ( ! moby6.$body.hasClass( 'auto-fold' ) )
    567                         moby6.$body.addClass( 'auto-fold' );
    568 
    569                 this.modifySidebarEvents();
    570                 this.disableDraggables();
    571                 this.movePostSearch();
    572 
    573         },
    574 
    575         deactivate: function() {
    576 
    577                 window.stickymenu && window.stickymenu.enable();
    578 
    579                 this.enableDraggables();
    580                 this.removeHamburgerButton();
    581                 this.restorePostSearch();
    582 
    583         },
    584 
    585         matchMedia: function() {
    586                 clearTimeout( this.resizeTimeout );
    587                 this.resizeTimeout = setTimeout( function() {
    588 
    589                         if ( ! window.matchMedia )
    590                                 return;
    591 
    592                         if ( window.matchMedia( '(max-width: 782px)' ).matches ) {
    593                                 if ( moby6.$html.hasClass( 'touch' ) )
     515                        } else {
     516                                if ( $body.hasClass( 'sticky-menu' ) ) {
     517                                        $body.removeClass( 'sticky-menu' );
     518                                }
     519                        }
     520                }
     521        };
     522
     523        window.wpResponsive = {
     524                init: function() {
     525                        var self = this,
     526                                scrollStart = 0;
     527
     528                        // Modify functionality based on custom activate/deactivate event
     529                        $document.on( 'wp-responsive-activate.wp-responsive', function() {
     530                                self.activate();
     531                        }).on( 'wp-responsive-deactivate.wp-responsive', function() {
     532                                self.deactivate();
     533                        });
     534
     535                        // Toggle sidebar when toggle is clicked
     536                        $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
     537                                event.preventDefault();
     538                                $wpwrap.toggleClass( 'wp-responsive-open' );
     539                        } );
     540
     541                        // Add menu events
     542                        $adminmenu.on( 'touchstart.wp-responsive', 'li.wp-has-submenu > a', function() {
     543                                scrollStart = $window.scrollTop();
     544                        }).on( 'touchend.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
     545                                if ( ! $adminmenu.data('wp-responsive') || $window.scrollTop() !== scrollStart ) {
    594546                                        return;
    595                                 moby6.$html.addClass( 'touch' ).trigger( 'activate.moby6' );
     547                                }
     548
     549                                $( this ).find( 'li.wp-has-submenu' ).removeClass( 'selected' );
     550                                $( this ).parent( 'li' ).addClass( 'selected' );
     551                                event.preventDefault();
     552                        });
     553
     554                        self.trigger();
     555                        $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
     556                },
     557
     558                activate: function() {
     559                        window.stickyMenu.disable();
     560
     561                        if ( ! $body.hasClass( 'auto-fold' ) ) {
     562                                $body.addClass( 'auto-fold' );
     563                        }
     564
     565                        $adminmenu.data( 'wp-responsive', 1 );
     566                        this.disableSortables();
     567                        this.movePostSearch();
     568                },
     569
     570                deactivate: function() {
     571                        window.stickyMenu.enable();
     572                        $adminmenu.removeData('wp-responsive');
     573                        this.enableSortables();
     574                        this.restorePostSearch();
     575                },
     576
     577                trigger: function() {
     578                        var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
     579
     580                        if ( width <= 782 ) {
     581                                if ( ! wpResponsiveActive ) {
     582                                        $document.trigger( 'wp-responsive-activate' );
     583                                        wpResponsiveActive = true;
     584                                }
    596585                        } else {
    597                                 if ( ! moby6.$html.hasClass( 'touch' ) )
    598                                         return;
    599                                 moby6.$html.removeClass( 'touch' ).trigger( 'deactivate.moby6' );
    600                         }
    601 
    602                         if ( window.matchMedia( '(max-width: 480px)' ).matches ) {
    603                                 moby6.enableOverlay();
     586                                if ( wpResponsiveActive ) {
     587                                        $document.trigger( 'wp-responsive-deactivate' );
     588                                        wpResponsiveActive = false;
     589                                }
     590                        }
     591
     592                        if ( width <= 480 ) {
     593                                this.enableOverlay();
    604594                        } else {
    605                                 moby6.disableOverlay();
    606                         }
    607 
    608                 }, 150 );
    609         },
    610 
    611         enableOverlay: function() {
    612                 if ( this.$overlay.length === 0 ) {
    613                         this.$overlay = $( '<div id="moby6-overlay"></div>' )
    614                                 .insertAfter( '#wpcontent' )
    615                                 .hide()
    616                                 .on( 'click.moby6', function() {
    617                                         moby6.$toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
    618                                         $( this ).hide();
    619                                 });
    620                 }
    621                 this.$toolbarPopups.on( 'click.moby6', function() {
    622                         moby6.$overlay.show();
    623                 });
    624         },
    625 
    626         disableOverlay: function() {
    627                 this.$toolbarPopups.off( 'click.moby6' );
    628                 this.$overlay.hide();
    629         },
    630 
    631         modifySidebarEvents: function() {
    632                 this.$body.off( '.wp-mobile-hover' );
    633                 this.$adminmenu.find( 'a.wp-has-submenu' ).off( '.wp-mobile-hover' );
    634 
    635                 var scrollStart = 0;
    636                 this.$adminmenu.on( 'touchstart.moby6', 'li.wp-has-submenu > a', function() {
    637                         scrollStart = $( window ).scrollTop();
    638                 });
    639 
    640                 this.$adminmenu.on( 'touchend.moby6', 'li.wp-has-submenu > a', function( e ) {
    641                         e.preventDefault();
    642 
    643                         if ( $( window ).scrollTop() !== scrollStart )
    644                                 return false;
    645 
    646                         $( this ).find( 'li.wp-has-submenu' ).removeClass( 'selected' );
    647                         $( this ).parent( 'li' ).addClass( 'selected' );
    648                 });
    649         },
    650 
    651         disableDraggables: function() {
    652                 this.$wpbody
    653                         .find( '.hndle' )
    654                         .removeClass( 'hndle' )
    655                         .addClass( 'hndle-disabled' );
    656         },
    657 
    658         enableDraggables: function() {
    659                 this.$wpbody
    660                         .find( '.hndle-disabled' )
    661                         .removeClass( 'hndle-disabled' )
    662                         .addClass( 'hndle' );
    663         },
    664 
    665         removeHamburgerButton: function() {
    666                 if ( this.hamburgerButtonView !== undefined )
    667                         this.hamburgerButtonView.destroy();
    668         },
    669 
    670         movePostSearch: function() {
    671                 this.searchBox = this.$wpbody.find( 'p.search-box' );
    672                 if ( this.searchBox.length ) {
    673                         this.searchBox.hide();
    674                         if ( this.searchBoxClone === undefined ) {
    675                                 this.searchBoxClone = this.searchBox.first().clone().insertAfter( 'div.tablenav.bottom' );
    676                         }
    677                         this.searchBoxClone.show();
    678                 }
    679         },
    680 
    681         restorePostSearch: function() {
    682                 if ( this.searchBox !== undefined ) {
    683                         this.searchBox.show();
    684                         if ( this.searchBoxClone !== undefined )
    685                                 this.searchBoxClone.hide();
    686                 }
    687         }
    688 };
    689 
    690 // Fire moby6.init when document is ready
    691 $( document ).ready( $.proxy( moby6.init, moby6 ) );
     595                                this.disableOverlay();
     596                        }
     597                },
     598
     599                enableOverlay: function() {
     600                        if ( $overlay.length === 0 ) {
     601                                $overlay = $( '<div id="wp-responsive-overlay"></div>' )
     602                                        .insertAfter( '#wpcontent' )
     603                                        .hide()
     604                                        .on( 'click.wp-responsive', function() {
     605                                                $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
     606                                                $( this ).hide();
     607                                        });
     608                        }
     609
     610                        $toolbarPopups.on( 'click.wp-responsive', function() {
     611                                $overlay.show();
     612                        });
     613                },
     614
     615                disableOverlay: function() {
     616                        $toolbarPopups.off( 'click.wp-responsive' );
     617                        $overlay.hide();
     618                },
     619
     620                disableSortables: function() {
     621                        if ( $sortables.length ) {
     622                                try {
     623                                        $sortables.sortable('disable');
     624                                } catch(e) {}
     625                        }
     626                },
     627
     628                enableSortables: function() {
     629                        if ( $sortables.length ) {
     630                                try {
     631                                        $sortables.sortable('enable');
     632                                } catch(e) {}
     633                        }
     634                },
     635
     636                movePostSearch: function() {
     637                        this.searchBox = $( 'p.search-box' );
     638
     639                        if ( this.searchBox.length ) {
     640                                this.searchBox.hide();
     641
     642                                if ( this.searchBoxClone === undefined ) {
     643                                        this.searchBoxClone = this.searchBox.first().clone().insertAfter( 'div.tablenav.bottom' );
     644                                }
     645
     646                                this.searchBoxClone.show();
     647                        }
     648                },
     649
     650                restorePostSearch: function() {
     651                        if ( this.searchBox !== undefined ) {
     652                                this.searchBox.show();
     653
     654                                if ( this.searchBoxClone !== undefined ) {
     655                                        this.searchBoxClone.hide();
     656                                }
     657                        }
     658                }
     659        };
     660
     661        window.stickyMenu.enable();
     662        window.wpResponsive.init();
     663});
    692664
    693665// make Windows 8 devices playing along nicely
     
    713685});
    714686
    715 })(jQuery);
     687}( jQuery, window ));
  • trunk/src/wp-includes/admin-bar.php

    r26134 r26402  
    166166        if ( is_admin() ) {
    167167                $wp_admin_bar->add_menu( array(
    168                         'id'    => 'toggle-button',
     168                        'id'    => 'menu-toggle',
    169169                        'title' => '<span class="ab-icon"></span>',
    170170                        'href'  => '#',
  • trunk/src/wp-includes/css/admin-bar.css

    r26382 r26402  
    946946        }
    947947       
    948         #moby6-overlay {
     948        #wp-responsive-overlay {
    949949                position: fixed;
    950950                top: 0;
Note: See TracChangeset for help on using the changeset viewer.