AdminLTE/build/js/Layout.js

179 lines
4.9 KiB
JavaScript
Raw Normal View History

/* Layout()
* ========
* Implements AdminLTE layout.
2017-02-19 15:43:19 +00:00
* Fixes the layout height in case min-height fails.
*
* @usage activated automatically upon window load.
* Configure any options by passing data-option="value"
* to the body tag.
2017-02-19 15:43:19 +00:00
*/
+function ($) {
2017-10-26 20:53:11 +00:00
'use strict';
2017-02-19 15:43:19 +00:00
2017-10-26 20:53:11 +00:00
var DataKey = 'lte.layout';
2017-02-19 15:43:19 +00:00
var Default = {
slimscroll : true,
resetHeight: true
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:43:19 +00:00
var Selector = {
wrapper : '.wrapper',
contentWrapper: '.content-wrapper',
layoutBoxed : '.layout-boxed',
mainFooter : '.main-footer',
mainHeader : '.main-header',
sidebar : '.sidebar',
controlSidebar: '.control-sidebar',
fixed : '.fixed',
2017-06-25 13:33:22 +00:00
sidebarMenu : '.sidebar-menu',
logo : '.main-header .logo'
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:43:19 +00:00
var ClassName = {
fixed : 'fixed',
holdTransition: 'hold-transition'
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:43:19 +00:00
var Layout = function (options) {
2017-10-26 20:53:11 +00:00
this.options = options;
this.bindedResize = false;
this.activate();
};
2017-02-19 15:43:19 +00:00
Layout.prototype.activate = function () {
2017-10-26 20:53:11 +00:00
this.fix();
this.fixSidebar();
2017-02-19 15:43:19 +00:00
2017-10-26 20:53:11 +00:00
$('body').removeClass(ClassName.holdTransition);
2017-02-19 15:43:19 +00:00
if (this.options.resetHeight) {
$('body, html, ' + Selector.wrapper).css({
'height' : 'auto',
'min-height': '100%'
2017-10-26 20:53:11 +00:00
});
}
if (!this.bindedResize) {
$(window).resize(function () {
2017-10-26 20:53:11 +00:00
this.fix();
this.fixSidebar();
2017-06-25 13:33:22 +00:00
$(Selector.logo + ', ' + Selector.sidebar).one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
2017-10-26 20:53:11 +00:00
this.fix();
this.fixSidebar();
}.bind(this));
}.bind(this));
2017-06-25 13:33:22 +00:00
2017-10-26 20:53:11 +00:00
this.bindedResize = true;
}
2017-02-19 15:43:19 +00:00
$(Selector.sidebarMenu).on('expanded.tree', function () {
2017-10-26 20:53:11 +00:00
this.fix();
this.fixSidebar();
}.bind(this));
2017-02-19 15:43:19 +00:00
$(Selector.sidebarMenu).on('collapsed.tree', function () {
2017-10-26 20:53:11 +00:00
this.fix();
this.fixSidebar();
}.bind(this));
};
2017-02-19 15:43:19 +00:00
Layout.prototype.fix = function () {
// Remove overflow from .wrapper if layout-boxed exists
2017-10-26 20:53:11 +00:00
$(Selector.layoutBoxed + ' > ' + Selector.wrapper).css('overflow', 'hidden');
2017-02-19 15:43:19 +00:00
// Get window height and the wrapper height
var footerHeight = $(Selector.mainFooter).outerHeight() || 0;
var headerHeight = $(Selector.mainHeader).outerHeight() || 0;
var neg = headerHeight + footerHeight;
2017-10-26 20:53:11 +00:00
var windowHeight = $(window).height();
var sidebarHeight = $(Selector.sidebar).height() || 0;
2017-02-19 15:43:19 +00:00
2017-06-25 13:33:22 +00:00
// Set the min-height of the content and sidebar based on
2017-02-19 15:43:19 +00:00
// the height of the document.
if ($('body').hasClass(ClassName.fixed)) {
2017-10-26 20:53:11 +00:00
$(Selector.contentWrapper).css('min-height', windowHeight - footerHeight);
2017-02-19 15:43:19 +00:00
} else {
2017-10-26 20:53:11 +00:00
var postSetHeight;
2017-02-19 15:43:19 +00:00
if (windowHeight >= sidebarHeight + headerHeight) {
2017-10-26 20:53:11 +00:00
$(Selector.contentWrapper).css('min-height', windowHeight - neg);
postSetHeight = windowHeight - neg;
2017-02-19 15:43:19 +00:00
} else {
2017-10-26 20:53:11 +00:00
$(Selector.contentWrapper).css('min-height', sidebarHeight);
postSetHeight = sidebarHeight;
2017-02-19 15:43:19 +00:00
}
// Fix for the control sidebar height
2017-10-26 20:53:11 +00:00
var $controlSidebar = $(Selector.controlSidebar);
2017-02-19 15:43:19 +00:00
if (typeof $controlSidebar !== 'undefined') {
2017-06-25 13:33:22 +00:00
if ($controlSidebar.height() > postSetHeight)
2017-10-26 20:53:11 +00:00
$(Selector.contentWrapper).css('min-height', $controlSidebar.height());
2017-02-19 15:43:19 +00:00
}
}
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:43:19 +00:00
Layout.prototype.fixSidebar = function () {
// Make sure the body tag has the .fixed class
if (!$('body').hasClass(ClassName.fixed)) {
if (typeof $.fn.slimScroll !== 'undefined') {
2017-10-26 20:53:11 +00:00
$(Selector.sidebar).slimScroll({ destroy: true }).height('auto');
2017-02-19 15:43:19 +00:00
}
2017-10-26 20:53:11 +00:00
return;
2017-02-19 15:43:19 +00:00
}
// Enable slimscroll for fixed layout
if (this.options.slimscroll) {
if (typeof $.fn.slimScroll !== 'undefined') {
2017-02-19 15:43:19 +00:00
// Destroy if it exists
// $(Selector.sidebar).slimScroll({ destroy: true }).height('auto')
2017-02-19 15:43:19 +00:00
// Add slimscroll
$(Selector.sidebar).slimScroll({
height: ($(window).height() - $(Selector.mainHeader).height()) + 'px'
2017-10-26 20:53:11 +00:00
});
2017-02-19 15:43:19 +00:00
}
}
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:43:19 +00:00
// Plugin Definition
// =================
function Plugin(option) {
return this.each(function () {
2017-10-26 20:53:11 +00:00
var $this = $(this);
var data = $this.data(DataKey);
if (!data) {
2017-10-26 20:53:11 +00:00
var options = $.extend({}, Default, $this.data(), typeof option === 'object' && option);
$this.data(DataKey, (data = new Layout(options)));
}
2017-02-19 15:43:19 +00:00
if (typeof option === 'string') {
if (typeof data[option] === 'undefined') {
2017-10-26 20:53:11 +00:00
throw new Error('No method named ' + option);
}
2017-10-26 20:53:11 +00:00
data[option]();
}
2017-10-26 20:53:11 +00:00
});
2017-02-19 15:43:19 +00:00
}
2017-10-26 20:53:11 +00:00
var old = $.fn.layout;
2017-02-19 15:43:19 +00:00
2017-10-26 20:53:11 +00:00
$.fn.layout = Plugin;
$.fn.layout.Constuctor = Layout;
2017-02-19 15:43:19 +00:00
// No conflict mode
// ================
$.fn.layout.noConflict = function () {
2017-10-26 20:53:11 +00:00
$.fn.layout = old;
return this;
};
2017-02-19 15:43:19 +00:00
// Layout DATA-API
// ===============
$(window).on('load', function () {
2017-10-26 20:53:11 +00:00
Plugin.call($('body'));
});
}(jQuery);