Make WordPress Core

Ticket #26088: 26088.diff

File 26088.diff, 3.1 KB (added by sanchothefat, 12 years ago)

Updated script using module revealing pattern

  • wp-admin/js/common.js

    diff --git wp-admin/js/common.js wp-admin/js/common.js
    index 3406ee0..58be5a5 100644
    $(document).ready( function() { 
    447447        })();
    448448});
    449449
    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         },
     450stickyMenu = ( function( window, undefined ) {
     451
     452        var t = this,
     453                active = false,
     454                $window,
     455                $body,
     456                $adminMenuWrap,
     457                $collapseMenu,
     458                bodyMinWidth;
     459
     460        function init() {
     461                $window = $( window );
     462                $body = $( document.body );
     463                $adminMenuWrap = $( '#adminmenuwrap' );
     464                $collapseMenu = $( '#collapse-menu' );
     465                bodyMinWidth = parseInt( $body.css( 'min-width' ), 10 );
     466                enable();
     467        }
    461468
    462         enable: function () {
    463                 if ( ! this.active ) {
    464                         this.$window.on( 'resize.stickyMenu scroll.stickyMenu', this.debounce(
    465                                 $.proxy( this.update, this ), 200
     469        function enable() {
     470                if ( ! active ) {
     471                        $window.on( 'resize.stickyMenu scroll.stickyMenu', debounce(
     472                                $.proxy( update, this ), 200
    466473                        ) );
    467                         this.$collapseMenu.on( 'click.stickyMenu', $.proxy( this.update, this ) );
    468                         this.update();
    469                         this.active = true;
     474                        $collapseMenu.on( 'click.stickyMenu', $.proxy( update, this ) );
     475                        update();
     476                        active = true;
    470477                }
    471         },
     478        }
    472479
    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;
     480        function disable() {
     481                if ( active ) {
     482                        $window.off( 'resize.stickyMenu scroll.stickyMenu' );
     483                        $collapseMenu.off( 'click.stickyMenu' );
     484                        $body.removeClass( 'sticky-menu' );
     485                        active = false;
    479486                }
    480         },
     487        }
    481488
    482         update: function () {
     489        function update() {
    483490                // Make the admin menu sticky if both of the following:
    484491                // 1. The viewport is taller than the admin menu
    485492                // 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' );
     493                if ( $window.height() > $adminMenuWrap.height() + 32 && $window.width() > bodyMinWidth) {
     494                        if ( ! $body.hasClass( 'sticky-menu' ) ) {
     495                                $body.addClass( 'sticky-menu' );
    489496                        }
    490497                } else {
    491                         if ( this.$body.hasClass( 'sticky-menu' ) ) {
    492                                 this.$body.removeClass( 'sticky-menu' );
     498                        if ( $body.hasClass( 'sticky-menu' ) ) {
     499                                $body.removeClass( 'sticky-menu' );
    493500                        }
    494501                }
    495         },
     502        }
    496503
    497504        // Borrowed from Underscore.js
    498         debounce: function( func, wait, immediate ) {
     505        function debounce( func, wait, immediate ) {
    499506                var timeout, args, context, timestamp, result;
    500507                return function() {
    501508                        var later, callNow;
    stickyMenu = { 
    526533                        return result;
    527534                };
    528535        }
    529 };
     536
     537        return {
     538                init: init,
     539                disable: disable,
     540                enable: enable
     541        };
     542} )( window );
    530543
    531544stickyMenu.init();
    532545