Skip to content

Instantly share code, notes, and snippets.

@dolzenko
Created December 4, 2012 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolzenko/4206501 to your computer and use it in GitHub Desktop.
Save dolzenko/4206501 to your computer and use it in GitHub Desktop.
/*jslint vars: true, unparam: true, white: true */
/*global jQuery */
jQuery.fn.mintree = (function($) {
"use strict";
function MinTree(root, options) {
this.options = $.extend({}, options);
var el = $(root).addClass("mintree"),
self = this;
$("li ul, li ol", root).closest("li").each(function(i, node) {
self.injectToggle(node);
});
el.on("click", "a.toggle", this.onToggle);
}
MinTree.prototype = {
onToggle: function(ev) { // TODO: option to exclude individual lists
$(this).closest("li").children("ul, ol").toggle(); // XXX: use of `children` disallows wrappers
var toggler = $(this).text() == "▹" ? "▿" : "▹";
$(this).text(toggler);
},
injectToggle: function(node) { // TODO: option to exclude individual lists
$('<a href="javascript:;" class="toggle" />').
text("▿").
prependTo(node);
}
};
return function(options) {
this.each(function(i, node) {
new MinTree(node, options);
});
return this;
}; // TODO: expose internal functions?
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment