jquery-simpletree
Version:
jQuery plugin for TreeView support.
10 lines (7 loc) • 2.46 kB
JavaScript
/*
Simple Tree jQuery plugin
Based on JS for simple unobtrusive javascript treeview plugin
developed by Krijn Hoetmer
http://krijnhoetmer.nl/stuff/javascript/list-treeview-menu/
Adapted as a jQuery plugin by Maurizio Manetti
*/(function(e,t,n,r){"use strict";function a(t,n){this.element=e(t),this.settings=e.extend({},s,n),this._defaults=s,this._name=i,this.init()}var i="simpleTree",s={classChanged:"st-treed",classOpen:"st-open",classCollapsed:"st-collapsed",classLeaf:"st-file",classLast:"st-last",startCollapsed:!0},o=function(t){var n=t.css("backgroundColor"),i="rgba(0, 0, 0, 0)";return n===i&&(n=t.parents().filter(function(){return e(this).css("backgroundColor")!=i}).first().css("backgroundColor")),n===r&&(n="rgba(255, 255, 255, 255)"),n},u=function(e){if(!e)var e=t.event;e.cancelBubble=!0,e.stopPropagation&&e.stopPropagation()};a.prototype.init=function(){var t=this.settings,n=this.element;n.addClass(t.classChanged),n.find("li").each(function(n){var r=e(this);r.children("ul").length>0?(t.startCollapsed?r.hasClass(t.classOpen)||r.addClass(t.classCollapsed):r.hasClass(t.classCollapsed)||r.addClass(t.classOpen),r.on("mousedown",function(e){r.toggleClass(t.classOpen+" "+t.classCollapsed),u(e)})):r.addClass(t.classLeaf),r.next("li").length==0&&(r.addClass(t.classLast),r.css("backgroundColor",o(r)))}),n.find("a").on("mousedown",u)},a.prototype.expand=function(){var e=this.settings;this.element.find("li."+e.classCollapsed).removeClass(e.classCollapsed).addClass(e.classOpen)},a.prototype.collapse=function(){var e=this.settings;this.element.find("li."+e.classOpen).removeClass(e.classOpen).addClass(e.classCollapsed)},a.prototype._reset=function(){var t=this.settings,n=this.element;n.removeClass(t.classChanged),n.find("li").each(function(n){var r=e(this);r.hasClass(t.classLast)&&r.css("backgroundColor",""),r.removeClass(t.classLeaf).removeClass(t.classLast).off("mousedown")})},a.prototype.destroy=function(){this._reset(),this.element.removeData("plugin_"+this._name)},a.prototype.repaint=function(){this._reset(),this.init()},e.fn[i]=function(t){var n=typeof t=="string";return n?this.each(function(){var n=e.data(this,"plugin_"+i);n?e.isFunction(n[t])?n[t].call(n):console.error("No such method '"+t+"' for "+i):console.error("Cannot call methods on "+i+" prior to initialization; attempted to call method '"+t+"'")}):this.each(function(){e.data(this,"plugin_"+i)||e.data(this,"plugin_"+i,new a(this,t))})}})(jQuery,window,document);