/* * Class that provides the top navbar functionality. * * @class MainNavbar */ (function() { PixelAdmin.MainNavbar = function() { this._scroller = false; this._wheight = null; this.scroll_pos = 0; return this; }; /* * Initialize plugin. */ PixelAdmin.MainNavbar.prototype.init = function() { var is_mobile; this.$navbar = $('#main-navbar'); this.$header = this.$navbar.find('.navbar-header'); this.$toggle = this.$navbar.find('.navbar-toggle:first'); this.$collapse = $('#main-navbar-collapse'); this.$collapse_div = this.$collapse.find('> div'); is_mobile = false; $(window).on('pa.screen.small pa.screen.tablet', (function(_this) { return function() { if (_this.$navbar.css('position') === 'fixed') { _this._setupScroller(); } return is_mobile = true; }; })(this)).on('pa.screen.desktop', (function(_this) { return function() { _this._removeScroller(); return is_mobile = false; }; })(this)); return this.$navbar.on('click', '.nav-icon-btn.dropdown > .dropdown-toggle', function(e) { if (is_mobile) { e.preventDefault(); e.stopPropagation(); document.location.href = $(this).attr('href'); return false; } }); }; /* * Attach scroller to navbar collapse. */ PixelAdmin.MainNavbar.prototype._setupScroller = function() { if (this._scroller) { return; } this._scroller = true; this.$collapse_div.pixelSlimScroll({}); this.$navbar.on('shown.bs.collapse.mn_collapse', $.proxy(((function(_this) { return function() { _this._updateCollapseHeight(); return _this._watchWindowHeight(); }; })(this)), this)).on('hidden.bs.collapse.mn_collapse', $.proxy(((function(_this) { return function() { _this._wheight = null; return _this.$collapse_div.pixelSlimScroll({ scrollTo: '0px' }); }; })(this)), this)).on('shown.bs.dropdown.mn_collapse', $.proxy(this._updateCollapseHeight, this)).on('hidden.bs.dropdown.mn_collapse', $.proxy(this._updateCollapseHeight, this)); return this._updateCollapseHeight(); }; /* * Detach scroller from navbar collapse. */ PixelAdmin.MainNavbar.prototype._removeScroller = function() { if (!this._scroller) { return; } this._wheight = null; this._scroller = false; this.$collapse_div.pixelSlimScroll({ destroy: 'destroy' }); this.$navbar.off('shown.bs.collapse.mn_collapse'); this.$navbar.off('hidden.bs.collapse.mn_collapse'); this.$navbar.off('shown.bs.dropdown.mn_collapse'); this.$navbar.off('hidden.bs.dropdown.mn_collapse'); return this.$collapse.attr('style', ''); }; /* * Update navbar collapse height. */ PixelAdmin.MainNavbar.prototype._updateCollapseHeight = function() { var h_height, scrollTop, w_height; if (!this._scroller) { return; } w_height = $(window).innerHeight(); h_height = this.$header.outerHeight(); scrollTop = this.$collapse_div.scrollTop(); if ((h_height + this.$collapse_div.css({ 'max-height': 'none' }).outerHeight()) > w_height) { this.$collapse_div.css({ 'max-height': w_height - h_height }); } else { this.$collapse_div.css({ 'max-height': 'none' }); } return this.$collapse_div.pixelSlimScroll({ scrollTo: scrollTop + 'px' }); }; /* * Detecting a change of the window height. */ PixelAdmin.MainNavbar.prototype._watchWindowHeight = function() { var checkWindowInnerHeight; this._wheight = $(window).innerHeight(); checkWindowInnerHeight = (function(_this) { return function() { if (_this._wheight === null) { return; } if (_this._wheight !== $(window).innerHeight()) { _this._updateCollapseHeight(); } _this._wheight = $(window).innerHeight(); return setTimeout(checkWindowInnerHeight, 100); }; })(this); return window.setTimeout(checkWindowInnerHeight, 100); }; PixelAdmin.MainNavbar.Constructor = PixelAdmin.MainNavbar; PixelAdmin.addInitializer(function() { return PixelAdmin.initPlugin('main_navbar', new PixelAdmin.MainNavbar); }); }).call(this);