AdminLTE/build/js/Tree.js

146 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-02-19 15:42:30 +00:00
/* Tree()
* ======
* Converts a nested list into a multilevel
* tree view menu.
*
* @Usage: $('.my-menu').tree(options)
* or add [data-widget="tree"] to the ul element
* Pass any option as data-option="value"
*/
+function ($) {
2017-10-26 20:53:11 +00:00
'use strict';
2017-02-19 15:42:30 +00:00
2017-10-26 20:53:11 +00:00
var DataKey = 'lte.tree';
2017-02-19 15:42:30 +00:00
var Default = {
animationSpeed: 500,
accordion : true,
followLink : false,
trigger : '.treeview a'
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:42:30 +00:00
var Selector = {
tree : '.tree',
treeview : '.treeview',
treeviewMenu: '.treeview-menu',
open : '.menu-open, .active',
li : 'li',
data : '[data-widget="tree"]',
active : '.active'
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:42:30 +00:00
var ClassName = {
open: 'menu-open',
tree: 'tree'
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:42:30 +00:00
var Event = {
collapsed: 'collapsed.tree',
expanded : 'expanded.tree'
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:42:30 +00:00
// Tree Class Definition
// =====================
var Tree = function (element, options) {
2017-10-26 20:53:11 +00:00
this.element = element;
this.options = options;
2017-02-19 15:42:30 +00:00
2017-10-26 20:53:11 +00:00
$(this.element).addClass(ClassName.tree);
2017-02-19 15:42:30 +00:00
2017-10-26 20:53:11 +00:00
$(Selector.treeview + Selector.active, this.element).addClass(ClassName.open);
2017-02-19 15:42:30 +00:00
2017-10-26 20:53:11 +00:00
this._setUpListeners();
};
2017-02-19 15:42:30 +00:00
Tree.prototype.toggle = function (link, event) {
2017-10-26 20:53:11 +00:00
var treeviewMenu = link.next(Selector.treeviewMenu);
var parentLi = link.parent();
var isOpen = parentLi.hasClass(ClassName.open);
2017-02-19 15:42:30 +00:00
if (!parentLi.is(Selector.treeview)) {
2017-10-26 20:53:11 +00:00
return;
2017-02-19 15:42:30 +00:00
}
if (!this.options.followLink || link.attr('href') === '#') {
2017-10-26 20:53:11 +00:00
event.preventDefault();
2017-02-19 15:42:30 +00:00
}
if (isOpen) {
2017-10-26 20:53:11 +00:00
this.collapse(treeviewMenu, parentLi);
2017-02-19 15:42:30 +00:00
} else {
2017-10-26 20:53:11 +00:00
this.expand(treeviewMenu, parentLi);
2017-02-19 15:42:30 +00:00
}
2017-10-26 20:53:11 +00:00
};
2017-02-19 15:42:30 +00:00
Tree.prototype.expand = function (tree, parent) {
2017-10-26 20:53:11 +00:00
var expandedEvent = $.Event(Event.expanded);
2017-02-19 15:42:30 +00:00
if (this.options.accordion) {
2017-10-26 20:53:11 +00:00
var openMenuLi = parent.siblings(Selector.open);
var openTree = openMenuLi.children(Selector.treeviewMenu);
this.collapse(openTree, openMenuLi);
2017-02-19 15:42:30 +00:00
}
2017-10-26 20:53:11 +00:00
parent.addClass(ClassName.open);
2017-02-19 15:42:30 +00:00
tree.slideDown(this.options.animationSpeed, function () {
2017-10-26 20:53:11 +00:00
$(this.element).trigger(expandedEvent);
}.bind(this));
};
2017-02-19 15:42:30 +00:00
Tree.prototype.collapse = function (tree, parentLi) {
2017-10-26 20:53:11 +00:00
var collapsedEvent = $.Event(Event.collapsed);
2017-02-19 15:42:30 +00:00
//tree.find(Selector.open).removeClass(ClassName.open);
2017-10-26 20:53:11 +00:00
parentLi.removeClass(ClassName.open);
2017-02-19 15:42:30 +00:00
tree.slideUp(this.options.animationSpeed, function () {
//tree.find(Selector.open + ' > ' + Selector.treeview).slideUp();
2017-10-26 20:53:11 +00:00
$(this.element).trigger(collapsedEvent);
}.bind(this));
};
2017-02-19 15:42:30 +00:00
// Private
Tree.prototype._setUpListeners = function () {
2017-10-26 20:53:11 +00:00
var that = this;
2017-02-19 15:42:30 +00:00
2017-02-25 19:45:03 +00:00
$(this.element).on('click', this.options.trigger, function (event) {
2017-10-26 20:53:11 +00:00
that.toggle($(this), event);
});
};
2017-02-19 15:42:30 +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-19 15:42:30 +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, new Tree($this, options));
2017-02-19 15:42:30 +00:00
}
2017-10-26 20:53:11 +00:00
});
2017-02-19 15:42:30 +00:00
}
2017-10-26 20:53:11 +00:00
var old = $.fn.tree;
2017-02-19 15:42:30 +00:00
2017-10-26 20:53:11 +00:00
$.fn.tree = Plugin;
$.fn.tree.Constructor = Tree;
2017-02-19 15:42:30 +00:00
// No Conflict Mode
// ================
$.fn.tree.noConflict = function () {
2017-10-26 20:53:11 +00:00
$.fn.tree = old;
return this;
};
2017-02-19 15:42:30 +00:00
// Tree Data API
// =============
$(window).on('load', function () {
$(Selector.data).each(function () {
2017-10-26 20:53:11 +00:00
Plugin.call($(this));
});
});
2017-02-19 15:42:30 +00:00
2017-10-26 20:53:11 +00:00
}(jQuery);