AdminLTE/build/js/BoxWidget.js

177 lines
4.7 KiB
JavaScript
Raw Normal View History

2017-02-25 19:37:38 +00:00
/* BoxWidget()
* ======
* Adds box widget functions to boxes.
*
* @Usage: $('.my-box').boxWidget(options)
* This plugin auto activates on any element using the `.box` class
2017-02-25 19:37:38 +00:00
* Pass any option as data-option="value"
*/
+function ($) {
2017-10-26 20:53:11 +00:00
'use strict';
2017-02-25 19:37:38 +00:00
2017-10-26 20:53:11 +00:00
var DataKey = 'lte.boxwidget';
2017-02-25 19:37:38 +00:00
var Default = {
animationSpeed : 500,
collapseTrigger: '[data-widget="collapse"]',
removeTrigger : '[data-widget="remove"]',
collapseIcon : 'fa-minus',
expandIcon : 'fa-plus',
removeIcon : 'fa-times'
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:37:38 +00:00
var Selector = {
data : '.box',
collapsed: '.collapsed-box',
header : '.box-header',
2017-02-25 19:37:38 +00:00
body : '.box-body',
footer : '.box-footer',
tools : '.box-tools'
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:37:38 +00:00
var ClassName = {
collapsed: 'collapsed-box'
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:37:38 +00:00
var Event = {
collapsing: 'collapsing.boxwidget',
collapsed: 'collapsed.boxwidget',
expanding: 'expanding.boxwidget',
expanded: 'expanded.boxwidget',
removing: 'removing.boxwidget',
removed: 'removed.boxwidget'
};
2017-02-25 19:37:38 +00:00
// BoxWidget Class Definition
// =====================
var BoxWidget = function (element, options) {
2017-10-26 20:53:11 +00:00
this.element = element;
this.options = options;
2017-02-25 19:37:38 +00:00
2017-10-26 20:53:11 +00:00
this._setUpListeners();
};
2017-02-25 19:37:38 +00:00
BoxWidget.prototype.toggle = function () {
2017-10-26 20:53:11 +00:00
var isOpen = !$(this.element).is(Selector.collapsed);
2017-02-25 19:37:38 +00:00
if (isOpen) {
2017-10-26 20:53:11 +00:00
this.collapse();
2017-02-25 19:37:38 +00:00
} else {
2017-10-26 20:53:11 +00:00
this.expand();
2017-02-25 19:37:38 +00:00
}
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:37:38 +00:00
BoxWidget.prototype.expand = function () {
2017-10-26 20:53:11 +00:00
var expandedEvent = $.Event(Event.expanded);
var expandingEvent = $.Event(Event.expanding);
2017-10-26 20:53:11 +00:00
var collapseIcon = this.options.collapseIcon;
var expandIcon = this.options.expandIcon;
2017-02-25 19:37:38 +00:00
2017-10-26 20:53:11 +00:00
$(this.element).removeClass(ClassName.collapsed);
2017-02-25 19:37:38 +00:00
$(this.element)
.children(Selector.header + ', ' + Selector.body + ', ' + Selector.footer)
.children(Selector.tools)
2017-02-25 19:37:38 +00:00
.find('.' + expandIcon)
.removeClass(expandIcon)
2017-10-26 20:53:11 +00:00
.addClass(collapseIcon);
2017-02-25 19:37:38 +00:00
$(this.element).children(Selector.body + ', ' + Selector.footer)
2017-02-25 19:37:38 +00:00
.slideDown(this.options.animationSpeed, function () {
2017-10-26 20:53:11 +00:00
$(this.element).trigger(expandedEvent);
}.bind(this))
.trigger(expandingEvent);
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:37:38 +00:00
BoxWidget.prototype.collapse = function () {
2017-10-26 20:53:11 +00:00
var collapsedEvent = $.Event(Event.collapsed);
var collapsingEvent = $.Event(Event.collapsing);
2017-10-26 20:53:11 +00:00
var collapseIcon = this.options.collapseIcon;
var expandIcon = this.options.expandIcon;
2017-02-25 19:37:38 +00:00
$(this.element)
.children(Selector.header + ', ' + Selector.body + ', ' + Selector.footer)
.children(Selector.tools)
2017-02-25 19:37:38 +00:00
.find('.' + collapseIcon)
.removeClass(collapseIcon)
2017-10-26 20:53:11 +00:00
.addClass(expandIcon);
2017-02-25 19:37:38 +00:00
$(this.element).children(Selector.body + ', ' + Selector.footer)
2017-02-25 19:37:38 +00:00
.slideUp(this.options.animationSpeed, function () {
2017-10-26 20:53:11 +00:00
$(this.element).addClass(ClassName.collapsed);
$(this.element).trigger(collapsedEvent);
}.bind(this))
.trigger(collapsingEvent);
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:37:38 +00:00
BoxWidget.prototype.remove = function () {
2017-10-26 20:53:11 +00:00
var removedEvent = $.Event(Event.removed);
var removingEvent = $.Event(Event.removing);
2017-02-25 19:37:38 +00:00
$(this.element).slideUp(this.options.animationSpeed, function () {
2017-10-26 20:53:11 +00:00
$(this.element).trigger(removedEvent);
$(this.element).remove();
}.bind(this))
.trigger(removingEvent);
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:37:38 +00:00
// Private
BoxWidget.prototype._setUpListeners = function () {
2017-10-26 20:53:11 +00:00
var that = this;
2017-02-25 19:37:38 +00:00
$(this.element).on('click', this.options.collapseTrigger, function (event) {
2017-10-26 20:53:11 +00:00
if (event) event.preventDefault();
that.toggle($(this));
return false;
});
2017-02-25 19:37:38 +00:00
$(this.element).on('click', this.options.removeTrigger, function (event) {
2017-10-26 20:53:11 +00:00
if (event) event.preventDefault();
that.remove($(this));
return false;
});
};
2017-02-25 19:37:38 +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);
2017-02-25 19:37:38 +00:00
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 BoxWidget($this, options)));
2017-02-25 19:37:38 +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-02-25 19:37:38 +00:00
}
2017-10-26 20:53:11 +00:00
data[option]();
2017-02-25 19:37:38 +00:00
}
2017-10-26 20:53:11 +00:00
});
2017-02-25 19:37:38 +00:00
}
2017-10-26 20:53:11 +00:00
var old = $.fn.boxWidget;
2017-02-25 19:37:38 +00:00
2017-10-26 20:53:11 +00:00
$.fn.boxWidget = Plugin;
$.fn.boxWidget.Constructor = BoxWidget;
2017-02-25 19:37:38 +00:00
// No Conflict Mode
// ================
$.fn.boxWidget.noConflict = function () {
2017-10-26 20:53:11 +00:00
$.fn.boxWidget = old;
return this;
};
2017-02-25 19:37:38 +00:00
// BoxWidget Data API
// ==================
$(window).on('load', function () {
$(Selector.data).each(function () {
2017-10-26 20:53:11 +00:00
Plugin.call($(this));
});
});
}(jQuery);