| 441 | var stickymenu = { |
| 442 | active: false, |
| 443 | |
| 444 | init: function () { |
| 445 | this.$window = $( window ); |
| 446 | this.$body = $( document.body ); |
| 447 | this.$adminMenuWrap = $( '#adminmenuwrap' ); |
| 448 | this.$collapseMenu = $( '#collapse-menu' ); |
| 449 | this.bodyMinWidth = parseInt( this.$body.css( 'min-width' ), 10 ); |
| 450 | this.enable(); |
| 451 | }, |
| 452 | |
| 453 | enable: function () { |
| 454 | if ( ! this.active ) { |
| 455 | this.$window.on( 'resize.stickymenu scroll.stickymenu', this.debounce( |
| 456 | $.proxy( this.update, this ), 200 |
| 457 | ) ); |
| 458 | this.$collapseMenu.on( 'click.stickymenu', $.proxy( this.update, this ) ); |
| 459 | this.update(); |
| 460 | this.active = true; |
| 461 | } |
| 462 | }, |
| 463 | |
| 464 | disable: function () { |
| 465 | if ( this.active ) { |
| 466 | this.$window.off( 'resize.stickymenu scroll.stickymenu' ); |
| 467 | this.$collapseMenu.off( 'click.stickymenu' ); |
| 468 | this.$body.removeClass( 'sticky-menu' ); |
| 469 | this.active = false; |
| 470 | } |
| 471 | }, |
| 472 | |
| 473 | update: function () { |
| 474 | // float the admin menu sticky if: |
| 475 | // 1) the viewport is taller than the admin menu |
| 476 | // 2) the viewport is wider than the min-width of the <body> |
| 477 | // to float it only if it's collapsed add: $(document.body).hasClass('folded') |
| 478 | if ( this.$window.height() > this.$adminMenuWrap.height() + 32 && this.$window.width() > this.bodyMinWidth) { |
| 479 | if ( ! this.$body.hasClass( 'sticky-menu' ) ) { |
| 480 | this.$body.addClass( 'sticky-menu' ); |
| 481 | } |
| 482 | } else { |
| 483 | if ( this.$body.hasClass( 'sticky-menu' ) ) { |
| 484 | this.$body.removeClass( 'sticky-menu' ); |
| 485 | } |
| 486 | } |
| 487 | }, |
| 488 | |
| 489 | // Borrowed from Underscore.js |
| 490 | debounce: function( func, wait, immediate ) { |
| 491 | var timeout, args, context, timestamp, result; |
| 492 | return function() { |
| 493 | context = this; |
| 494 | args = arguments; |
| 495 | timestamp = new Date().getTime(); |
| 496 | var later = function() { |
| 497 | var last = new Date().getTime() - timestamp; |
| 498 | if ( last < wait ) { |
| 499 | timeout = setTimeout( later, wait - last ); |
| 500 | } else { |
| 501 | timeout = null; |
| 502 | if ( ! immediate ) { |
| 503 | result = func.apply( context, args ); |
| 504 | context = args = null; |
| 505 | } |
| 506 | } |
| 507 | }; |
| 508 | var callNow = immediate && !timeout; |
| 509 | if ( ! timeout ) { |
| 510 | timeout = setTimeout( later, wait ); |
| 511 | } |
| 512 | if ( callNow ) { |
| 513 | result = func.apply( context, args ); |
| 514 | context = args = null; |
| 515 | } |
| 516 | |
| 517 | return result; |
| 518 | }; |
| 519 | } |
| 520 | }; |
| 521 | |
| 522 | stickymenu.init(); |
| 523 | |