AdminLTE/build/js/ControlSidebar.js

142 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-02-20 00:20:47 +01:00
/* ControlSidebar()
* ===============
* Toggles the state of the control sidebar
*
* @Usage: $('#control-sidebar-trigger').controlSidebar(options)
* or add [data-toggle="control-sidebar"] to the trigger
* Pass any option as data-option="value"
*/
+function ($) {
2017-10-26 22:53:11 +02:00
'use strict';
2017-02-20 00:20:47 +01:00
2017-10-26 22:53:11 +02:00
var DataKey = 'lte.controlsidebar';
2017-02-20 00:20:47 +01:00
var Default = {
slide: true
2017-10-26 22:53:11 +02:00
};
2017-02-20 00:20:47 +01:00
var Selector = {
sidebar: '.control-sidebar',
data : '[data-toggle="control-sidebar"]',
open : '.control-sidebar-open',
bg : '.control-sidebar-bg',
wrapper: '.wrapper',
content: '.content-wrapper',
boxed : '.layout-boxed'
2017-10-26 22:53:11 +02:00
};
2017-02-20 00:20:47 +01:00
var ClassName = {
open : 'control-sidebar-open',
fixed: 'fixed'
2017-10-26 22:53:11 +02:00
};
2017-02-20 00:20:47 +01:00
var Event = {
collapsed: 'collapsed.controlsidebar',
expanded : 'expanded.controlsidebar'
2017-10-26 22:53:11 +02:00
};
2017-02-20 00:20:47 +01:00
// ControlSidebar Class Definition
// ===============================
var ControlSidebar = function (element, options) {
2017-10-26 22:53:11 +02:00
this.element = element;
this.options = options;
this.hasBindedResize = false;
2017-02-20 00:20:47 +01:00
2017-10-26 22:53:11 +02:00
this.init();
};
2017-02-20 00:20:47 +01:00
ControlSidebar.prototype.init = function () {
// Add click listener if the element hasn't been
// initialized using the data API
if (!$(this.element).is(Selector.data)) {
2017-10-26 22:53:11 +02:00
$(this).on('click', this.toggle);
2017-02-20 00:20:47 +01:00
}
2017-10-26 22:53:11 +02:00
this.fix();
2017-02-20 00:20:47 +01:00
$(window).resize(function () {
2017-10-26 22:53:11 +02:00
this.fix();
}.bind(this));
};
2017-02-20 00:20:47 +01:00
ControlSidebar.prototype.toggle = function (event) {
2017-10-26 22:53:11 +02:00
if (event) event.preventDefault();
2017-02-20 00:20:47 +01:00
2017-10-26 22:53:11 +02:00
this.fix();
2017-02-20 00:20:47 +01:00
if (!$(Selector.sidebar).is(Selector.open) && !$('body').is(Selector.open)) {
2017-10-26 22:53:11 +02:00
this.expand();
2017-02-20 00:20:47 +01:00
} else {
2017-10-26 22:53:11 +02:00
this.collapse();
2017-02-20 00:20:47 +01:00
}
2017-10-26 22:53:11 +02:00
};
2017-02-20 00:20:47 +01:00
ControlSidebar.prototype.expand = function () {
$(Selector.sidebar).show();
2017-02-20 00:20:47 +01:00
if (!this.options.slide) {
2017-10-26 22:53:11 +02:00
$('body').addClass(ClassName.open);
2017-02-20 00:20:47 +01:00
} else {
2017-10-26 22:53:11 +02:00
$(Selector.sidebar).addClass(ClassName.open);
2017-02-20 00:20:47 +01:00
}
2017-10-26 22:53:11 +02:00
$(this.element).trigger($.Event(Event.expanded));
};
2017-02-20 00:20:47 +01:00
ControlSidebar.prototype.collapse = function () {
2017-10-26 22:53:11 +02:00
$('body, ' + Selector.sidebar).removeClass(ClassName.open);
$(Selector.sidebar).fadeOut();
2017-10-26 22:53:11 +02:00
$(this.element).trigger($.Event(Event.collapsed));
};
2017-02-20 00:20:47 +01:00
ControlSidebar.prototype.fix = function () {
if ($('body').is(Selector.boxed)) {
2017-10-26 22:53:11 +02:00
this._fixForBoxed($(Selector.bg));
2017-02-20 00:20:47 +01:00
}
2017-10-26 22:53:11 +02:00
};
2017-02-20 00:20:47 +01:00
// Private
ControlSidebar.prototype._fixForBoxed = function (bg) {
bg.css({
position: 'absolute',
height : $(Selector.wrapper).height()
2017-10-26 22:53:11 +02:00
});
};
2017-02-20 00:20:47 +01:00
// Plugin Definition
// =================
function Plugin(option) {
return this.each(function () {
2017-10-26 22:53:11 +02:00
var $this = $(this);
var data = $this.data(DataKey);
2017-02-20 00:20:47 +01:00
if (!data) {
2017-10-26 22:53:11 +02:00
var options = $.extend({}, Default, $this.data(), typeof option == 'object' && option);
$this.data(DataKey, (data = new ControlSidebar($this, options)));
2017-02-20 00:20:47 +01:00
}
2017-10-26 22:53:11 +02:00
if (typeof option == 'string') data.toggle();
});
2017-02-20 00:20:47 +01:00
}
2017-10-26 22:53:11 +02:00
var old = $.fn.controlSidebar;
2017-02-20 00:20:47 +01:00
2017-10-26 22:53:11 +02:00
$.fn.controlSidebar = Plugin;
$.fn.controlSidebar.Constructor = ControlSidebar;
2017-02-20 00:20:47 +01:00
// No Conflict Mode
// ================
$.fn.controlSidebar.noConflict = function () {
2017-10-26 22:53:11 +02:00
$.fn.controlSidebar = old;
return this;
};
2017-02-20 00:20:47 +01:00
// ControlSidebar Data API
// =======================
$(document).on('click', Selector.data, function (event) {
2017-10-26 22:53:11 +02:00
if (event) event.preventDefault();
Plugin.call($(this), 'toggle');
});
2017-02-20 00:20:47 +01:00
2017-10-26 22:53:11 +02:00
}(jQuery);