2 * jQuery treetable Plugin 3.2.0
3 * http://ludo.cubicphuse.nl/jquery-treetable
5 * Copyright 2013, Ludo van den Boom
6 * Dual licensed under the MIT or GPL Version 2 licenses.
9 var Node, Tree, methods;
12 function Node(row, tree, settings) {
17 this.settings = settings;
19 // TODO Ensure id/parentId is always a string (not int)
20 this.id = this.row.data(this.settings.nodeIdAttr);
22 // TODO Move this to a setParentId function?
23 parentId = this.row.data(this.settings.parentIdAttr);
24 if (parentId != null && parentId !== "") {
25 this.parentId = parentId;
28 this.treeCell = $(this.row.children(this.settings.columnElType)[this.settings.column]);
29 this.expander = $(this.settings.expanderTemplate);
30 this.indenter = $(this.settings.indenterTemplate);
32 this.initialized = false;
33 this.treeCell.prepend(this.indenter);
36 Node.prototype.addChild = function(child) {
37 return this.children.push(child);
40 Node.prototype.ancestors = function() {
44 while (node = node.parentNode()) {
50 Node.prototype.collapse = function() {
51 if (this.collapsed()) {
55 this.row.removeClass("expanded").addClass("collapsed");
58 this.expander.attr("title", this.settings.stringExpand);
60 if (this.initialized && this.settings.onNodeCollapse != null) {
61 this.settings.onNodeCollapse.apply(this);
67 Node.prototype.collapsed = function() {
68 return this.row.hasClass("collapsed");
71 // TODO destroy: remove event handlers, expander, indenter, etc.
73 Node.prototype.expand = function() {
74 if (this.expanded()) {
78 this.row.removeClass("collapsed").addClass("expanded");
80 if (this.initialized && this.settings.onNodeExpand != null) {
81 this.settings.onNodeExpand.apply(this);
84 if ($(this.row).is(":visible")) {
88 this.expander.attr("title", this.settings.stringCollapse);
93 Node.prototype.expanded = function() {
94 return this.row.hasClass("expanded");
97 Node.prototype.hide = function() {
103 Node.prototype.isBranchNode = function() {
104 if(this.children.length > 0 || this.row.data(this.settings.branchAttr) === true) {
111 Node.prototype.updateBranchLeafClass = function(){
112 this.row.removeClass('branch');
113 this.row.removeClass('leaf');
114 this.row.addClass(this.isBranchNode() ? 'branch' : 'leaf');
117 Node.prototype.level = function() {
118 return this.ancestors().length;
121 Node.prototype.parentNode = function() {
122 if (this.parentId != null) {
123 return this.tree[this.parentId];
129 Node.prototype.removeChild = function(child) {
130 var i = $.inArray(child, this.children);
131 return this.children.splice(i, 1)
134 Node.prototype.render = function() {
136 settings = this.settings,
139 if (settings.expandable === true && this.isBranchNode()) {
140 handler = function(e) {
141 $(this).parents("table").treetable("node", $(this).parents("tr").data(settings.nodeIdAttr)).toggle();
142 return e.preventDefault();
145 this.indenter.html(this.expander);
146 target = settings.clickableNodeNames === true ? this.treeCell : this.expander;
148 target.off("click.treetable").on("click.treetable", handler);
149 target.off("keydown.treetable").on("keydown.treetable", function(e) {
150 if (e.keyCode == 13) {
151 handler.apply(this, [e]);
156 this.indenter[0].style.paddingLeft = "" + (this.level() * settings.indent) + "px";
161 Node.prototype.reveal = function() {
162 if (this.parentId != null) {
163 this.parentNode().reveal();
165 return this.expand();
168 Node.prototype.setParent = function(node) {
169 if (this.parentId != null) {
170 this.tree[this.parentId].removeChild(this);
172 this.parentId = node.id;
173 this.row.data(this.settings.parentIdAttr, node.id);
174 return node.addChild(this);
177 Node.prototype.show = function() {
178 if (!this.initialized) {
182 if (this.expanded()) {
183 this._showChildren();
188 Node.prototype.toggle = function() {
189 if (this.expanded()) {
197 Node.prototype._hideChildren = function() {
198 var child, _i, _len, _ref, _results;
199 _ref = this.children;
201 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
203 _results.push(child.hide());
208 Node.prototype._initialize = function() {
209 var settings = this.settings;
213 if (settings.expandable === true && settings.initialState === "collapsed") {
219 if (settings.onNodeInitialized != null) {
220 settings.onNodeInitialized.apply(this);
223 return this.initialized = true;
226 Node.prototype._showChildren = function() {
227 var child, _i, _len, _ref, _results;
228 _ref = this.children;
230 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
232 _results.push(child.show());
241 function Tree(table, settings) {
243 this.settings = settings;
246 // Cache the nodes and roots in simple arrays for quick access/iteration
251 Tree.prototype.collapseAll = function() {
252 var node, _i, _len, _ref, _results;
255 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
257 _results.push(node.collapse());
262 Tree.prototype.expandAll = function() {
263 var node, _i, _len, _ref, _results;
266 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
268 _results.push(node.expand());
273 Tree.prototype.findLastNode = function (node) {
274 if (node.children.length > 0) {
275 return this.findLastNode(node.children[node.children.length - 1]);
281 Tree.prototype.loadRows = function(rows) {
285 for (i = 0; i < rows.length; i++) {
288 if (row.data(this.settings.nodeIdAttr) != null) {
289 node = new Node(row, this.tree, this.settings);
290 this.nodes.push(node);
291 this.tree[node.id] = node;
293 if (node.parentId != null && this.tree[node.parentId]) {
294 this.tree[node.parentId].addChild(node);
296 this.roots.push(node);
302 for (i = 0; i < this.nodes.length; i++) {
303 node = this.nodes[i].updateBranchLeafClass();
309 Tree.prototype.move = function(node, destination) {
311 // 1: +node+ should not be inserted as a child of +node+ itself.
312 // 2: +destination+ should not be the same as +node+'s current parent (this
313 // prevents +node+ from being moved to the same location where it already
315 // 3: +node+ should not be inserted in a location in a branch if this would
316 // result in +node+ being an ancestor of itself.
317 var nodeParent = node.parentNode();
318 if (node !== destination && destination.id !== node.parentId && $.inArray(node, destination.ancestors()) === -1) {
319 node.setParent(destination);
320 this._moveRows(node, destination);
322 // Re-render parentNode if this is its first child node, and therefore
323 // doesn't have the expander yet.
324 if (node.parentNode().children.length === 1) {
325 node.parentNode().render();
330 nodeParent.updateBranchLeafClass();
332 if(node.parentNode()){
333 node.parentNode().updateBranchLeafClass();
335 node.updateBranchLeafClass();
339 Tree.prototype.removeNode = function(node) {
340 // Recursively remove all descendants of +node+
341 this.unloadBranch(node);
343 // Remove node from DOM (<tr>)
346 // Remove node from parent children list
347 if (node.parentId != null) {
348 node.parentNode().removeChild(node);
351 // Clean up Tree object (so Node objects are GC-ed)
352 delete this.tree[node.id];
353 this.nodes.splice($.inArray(node, this.nodes), 1);
358 Tree.prototype.render = function() {
359 var root, _i, _len, _ref;
361 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
364 // Naming is confusing (show/render). I do not call render on node from
371 Tree.prototype.sortBranch = function(node, sortFun) {
372 // First sort internal array of children
373 node.children.sort(sortFun);
375 // Next render rows in correct order on page
376 this._sortChildRows(node);
381 Tree.prototype.unloadBranch = function(node) {
382 // Use a copy of the children array to not have other functions interfere
383 // with this function if they manipulate the children array
385 var children = node.children.slice(0),
388 for (i = 0; i < children.length; i++) {
389 this.removeNode(children[i]);
392 // Reset node's collection of children
395 node.updateBranchLeafClass();
400 Tree.prototype._moveRows = function(node, destination) {
401 var children = node.children, i;
403 node.row.insertAfter(destination.row);
406 // Loop backwards through children to have them end up on UI in correct
408 for (i = children.length - 1; i >= 0; i--) {
409 this._moveRows(children[i], node);
413 // Special _moveRows case, move children to itself to force sorting
414 Tree.prototype._sortChildRows = function(parentNode) {
415 return this._moveRows(parentNode, parentNode);
423 init: function(options, force) {
426 settings = $.extend({
427 branchAttr: "ttBranch",
428 clickableNodeNames: false,
430 columnElType: "td", // i.e. 'td', 'th' or 'td,th'
432 expanderTemplate: "<a href='#'> </a>",
434 indenterTemplate: "<span class='indenter'></span>",
435 initialState: "collapsed",
436 nodeIdAttr: "ttId", // maps to data-tt-id
437 parentIdAttr: "ttParentId", // maps to data-tt-parent-id
438 stringExpand: "Expand",
439 stringCollapse: "Collapse",
443 onNodeCollapse: null,
445 onNodeInitialized: null
448 return this.each(function() {
449 var el = $(this), tree;
451 if (force || el.data("treetable") === undefined) {
452 tree = new Tree(this, settings);
453 tree.loadRows(this.rows).render();
455 el.addClass("treetable").data("treetable", tree);
457 if (settings.onInitialized != null) {
458 settings.onInitialized.apply(tree);
466 destroy: function() {
467 return this.each(function() {
468 return $(this).removeData("treetable").removeClass("treetable");
472 collapseAll: function() {
473 this.data("treetable").collapseAll();
477 collapseNode: function(id) {
478 var node = this.data("treetable").tree[id];
483 throw new Error("Unknown node '" + id + "'");
489 expandAll: function() {
490 this.data("treetable").expandAll();
494 expandNode: function(id) {
495 var node = this.data("treetable").tree[id];
498 if (!node.initialized) {
504 throw new Error("Unknown node '" + id + "'");
510 loadBranch: function(node, rows) {
511 var settings = this.data("treetable").settings,
512 tree = this.data("treetable").tree;
514 // TODO Switch to $.parseHTML
517 if (node == null) { // Inserting new root nodes
520 var lastNode = this.data("treetable").findLastNode(node);
521 rows.insertAfter(lastNode.row);
524 this.data("treetable").loadRows(rows);
526 // Make sure nodes are properly initialized
527 rows.filter("tr").each(function() {
528 tree[$(this).data(settings.nodeIdAttr)].show();
532 // Re-render parent to ensure expander icon is shown (#79)
533 node.render().expand();
539 move: function(nodeId, destinationId) {
540 var destination, node;
542 node = this.data("treetable").tree[nodeId];
543 destination = this.data("treetable").tree[destinationId];
544 this.data("treetable").move(node, destination);
550 return this.data("treetable").tree[id];
553 removeNode: function(id) {
554 var node = this.data("treetable").tree[id];
557 this.data("treetable").removeNode(node);
559 throw new Error("Unknown node '" + id + "'");
565 reveal: function(id) {
566 var node = this.data("treetable").tree[id];
571 throw new Error("Unknown node '" + id + "'");
577 sortBranch: function(node, columnOrFunction) {
578 var settings = this.data("treetable").settings,
582 columnOrFunction = columnOrFunction || settings.column;
583 sortFun = columnOrFunction;
585 if ($.isNumeric(columnOrFunction)) {
586 sortFun = function(a, b) {
587 var extractValue, valA, valB;
589 extractValue = function(node) {
590 var val = node.row.find("td:eq(" + columnOrFunction + ")").text();
591 // Ignore trailing/leading whitespace and use uppercase values for
592 // case insensitive ordering
593 return $.trim(val).toUpperCase();
596 valA = extractValue(a);
597 valB = extractValue(b);
599 if (valA < valB) return -1;
600 if (valA > valB) return 1;
605 this.data("treetable").sortBranch(node, sortFun);
609 unloadBranch: function(node) {
610 this.data("treetable").unloadBranch(node);
615 $.fn.treetable = function(method) {
616 if (methods[method]) {
617 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
618 } else if (typeof method === 'object' || !method) {
619 return methods.init.apply(this, arguments);
621 return $.error("Method " + method + " does not exist on jQuery.treetable");
625 // Expose classes to world
626 this.TreeTable || (this.TreeTable = {});
627 this.TreeTable.Node = Node;
628 this.TreeTable.Tree = Tree;