| | 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 | } |
| | 515 | } |
| | 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 | |