Add more options and allow methods to get executed from outside the plugin

pull/1498/head
Abdullah Almsaeed 2017-02-25 14:43:23 -05:00
parent 17c9366f4b
commit 01b4740d93
1 changed files with 38 additions and 17 deletions

View File

@ -1,8 +1,11 @@
/* Layout /* Layout()
* ====== * ========
* Implements AdminLTE layout.
* Fixes the layout height in case min-height fails. * Fixes the layout height in case min-height fails.
* *
* @usage activated automatically upon window load * @usage activated automatically upon window load.
* Configure any options by passing data-option="value"
* to the body tag.
*/ */
+function ($) { +function ($) {
'use strict' 'use strict'
@ -10,7 +13,8 @@
var DataKey = 'lte.layout' var DataKey = 'lte.layout'
var Default = { var Default = {
slimscroll: true slimscroll : true,
resetHeight: true
} }
var Selector = { var Selector = {
@ -26,11 +30,13 @@
} }
var ClassName = { var ClassName = {
fixed: 'fixed' fixed : 'fixed',
holdTransition: 'hold-transition'
} }
var Layout = function (options) { var Layout = function (options) {
this.options = options this.options = options
this.bindedResize = false
this.activate() this.activate()
} }
@ -38,13 +44,19 @@
this.fix() this.fix()
this.fixSidebar() this.fixSidebar()
$('body').removeClass('hold-transition') $('body').removeClass(ClassName.holdTransition)
$('body, html, ' + Selector.wrapper).css('height', 'auto')
$(window).resize(function () { if (this.options.resetHeight) {
this.fix() $('body, html, ' + Selector.wrapper).css('height', 'auto')
this.fixSidebar() }
}.bind(this))
if (!this.bindedResize) {
$(window).resize(function () {
this.fix()
this.fixSidebar()
}.bind(this))
this.bindedResize = true;
}
$(Selector.sidebarMenu).on('expanded.tree', function () { $(Selector.sidebarMenu).on('expanded.tree', function () {
this.fix() this.fix()
@ -120,11 +132,20 @@
// ================= // =================
function Plugin(option) { function Plugin(option) {
return this.each(function () { return this.each(function () {
var $this = $(this) var $this = $(this)
var data = $this.data(DataKey) var data = $this.data(DataKey)
var options = $.extend({}, Default, $this.data(), typeof option == 'object' && option)
if (!data) $this.data(DataKey, (data = new Layout(options))) if (!data) {
var options = $.extend({}, Default, $this.data(), typeof option == 'object' && option)
$this.data(DataKey, (data = new Layout(options)))
}
if (typeof option == 'string') {
if (typeof data[option] == 'undefined') {
throw new Error('No method named ' + option)
}
data[option]()
}
}) })
} }
@ -143,6 +164,6 @@
// Layout DATA-API // Layout DATA-API
// =============== // ===============
$(window).on('load', function () { $(window).on('load', function () {
Plugin.call($('body'), $('body').data()) Plugin.call($('body'))
}) })
}(jQuery) }(jQuery)