2011-11-12 13:13:24 +01:00
|
|
|
/**
|
|
|
|
* jQuery org-chart/tree plugin.
|
|
|
|
*
|
|
|
|
* Author: Wes Nolte
|
2011-11-18 22:55:28 +01:00
|
|
|
* http://twitter.com/wesnolte
|
2011-11-12 13:13:24 +01:00
|
|
|
*
|
|
|
|
* Based on the work of Mark Lee
|
|
|
|
* http://www.capricasoftware.co.uk
|
|
|
|
*
|
|
|
|
* This software is licensed under the Creative Commons Attribution-ShareAlike
|
|
|
|
* 3.0 License.
|
|
|
|
*
|
|
|
|
* See here for license terms:
|
|
|
|
* http://creativecommons.org/licenses/by-sa/3.0
|
|
|
|
*/
|
2011-11-11 18:51:44 +01:00
|
|
|
(function($) {
|
|
|
|
|
|
|
|
$.fn.jOrgChart = function(options) {
|
|
|
|
var opts = $.extend({}, $.fn.jOrgChart.defaults, options);
|
2012-01-19 16:48:19 +01:00
|
|
|
var $appendTo = $(opts.chartElement);
|
2011-11-11 18:51:44 +01:00
|
|
|
|
2011-11-18 22:55:28 +01:00
|
|
|
// build the tree
|
|
|
|
$this = $(this);
|
|
|
|
var $container = $("<div class='" + opts.chartClass + "'/>");
|
|
|
|
if($this.is("ul")) {
|
|
|
|
buildNode($this.find("li:first"), $container, 0, opts);
|
|
|
|
}
|
|
|
|
else if($this.is("li")) {
|
|
|
|
buildNode($this, $container, 0, opts);
|
|
|
|
}
|
|
|
|
$appendTo.append($container);
|
|
|
|
|
|
|
|
// add drag and drop if enabled
|
|
|
|
if(opts.dragAndDrop){
|
2011-11-19 15:19:41 +01:00
|
|
|
$('div.node').draggable({
|
2011-11-18 22:55:28 +01:00
|
|
|
cursor : 'move',
|
|
|
|
distance : 40,
|
2011-12-02 14:41:02 +01:00
|
|
|
helper : 'clone',
|
2011-11-18 22:55:28 +01:00
|
|
|
opacity : 0.8,
|
2011-12-02 14:41:02 +01:00
|
|
|
revert : true,
|
2011-11-18 22:55:28 +01:00
|
|
|
revertDuration : 100,
|
|
|
|
snap : 'div.node.expanded',
|
|
|
|
snapMode : 'inner',
|
2011-12-02 14:41:02 +01:00
|
|
|
stack : 'div.node'
|
2011-11-18 22:55:28 +01:00
|
|
|
});
|
|
|
|
|
2011-11-19 15:19:41 +01:00
|
|
|
$('div.node').droppable({
|
2011-12-02 14:41:02 +01:00
|
|
|
accept : '.node',
|
2011-11-18 22:55:28 +01:00
|
|
|
activeClass : 'drag-active',
|
|
|
|
hoverClass : 'drop-hover'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Drag start event handler for nodes
|
2011-12-02 14:41:02 +01:00
|
|
|
$('div.node').bind("dragstart", function handleDragStart( event, ui ){
|
2011-11-18 22:55:28 +01:00
|
|
|
|
|
|
|
var sourceNode = $(this);
|
|
|
|
sourceNode.parentsUntil('.node-container')
|
|
|
|
.find('*')
|
|
|
|
.filter('.node')
|
|
|
|
.droppable('disable');
|
2011-12-02 14:41:02 +01:00
|
|
|
});
|
2011-11-18 22:55:28 +01:00
|
|
|
|
|
|
|
// Drag stop event handler for nodes
|
2011-12-02 14:41:02 +01:00
|
|
|
$('div.node').bind("dragstop", function handleDragStop( event, ui ){
|
2011-11-18 22:55:28 +01:00
|
|
|
|
|
|
|
/* reload the plugin */
|
|
|
|
$(opts.chartElement).children().remove();
|
|
|
|
$this.jOrgChart(opts);
|
2011-12-02 14:41:02 +01:00
|
|
|
});
|
2011-11-18 22:55:28 +01:00
|
|
|
|
|
|
|
// Drop event handler for nodes
|
2011-12-02 14:41:02 +01:00
|
|
|
$('div.node').bind("drop", function handleDropEvent( event, ui ) {
|
2011-11-18 22:55:28 +01:00
|
|
|
var sourceNode = ui.draggable;
|
|
|
|
var targetNode = $(this);
|
|
|
|
|
2011-11-19 15:19:41 +01:00
|
|
|
// finding nodes based on plaintext and html
|
|
|
|
// content is hard!
|
|
|
|
var targetLi = $('li').filter(function(){
|
|
|
|
|
|
|
|
li = $(this).clone()
|
|
|
|
.children("ul,li")
|
|
|
|
.remove()
|
|
|
|
.end();
|
|
|
|
|
|
|
|
return li.html() == targetNode.html();
|
|
|
|
});
|
2011-11-18 22:55:28 +01:00
|
|
|
|
2011-11-19 15:19:41 +01:00
|
|
|
var sourceLi = $('li').filter(function(){
|
|
|
|
|
|
|
|
li = $(this).clone()
|
|
|
|
.children("ul,li")
|
|
|
|
.remove()
|
|
|
|
.end();
|
|
|
|
|
|
|
|
return li.html() == sourceNode.html();
|
|
|
|
});
|
|
|
|
|
|
|
|
var sourceliClone = sourceLi.clone();
|
2011-11-18 22:55:28 +01:00
|
|
|
var sourceUl = sourceLi.parent('ul');
|
|
|
|
|
|
|
|
if(sourceUl.children('li').size() > 1){
|
|
|
|
sourceLi.remove();
|
|
|
|
}else{
|
|
|
|
sourceUl.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(targetLi.children('ul').size() >0){
|
2011-11-19 15:19:41 +01:00
|
|
|
targetLi.children('ul').append('<li>'+sourceliClone.html()+'</li>');
|
2011-11-18 22:55:28 +01:00
|
|
|
}else{
|
2011-11-19 15:19:41 +01:00
|
|
|
targetLi.append('<ul><li>'+sourceliClone.html()+'</li></ul>');
|
2011-11-18 22:55:28 +01:00
|
|
|
}
|
|
|
|
|
2011-12-02 14:41:02 +01:00
|
|
|
}); // handleDropEvent
|
2011-11-18 22:55:28 +01:00
|
|
|
|
|
|
|
} // Drag and drop
|
2011-11-11 18:51:44 +01:00
|
|
|
};
|
|
|
|
|
2011-11-18 22:55:28 +01:00
|
|
|
// Option defaults
|
2011-11-11 18:51:44 +01:00
|
|
|
$.fn.jOrgChart.defaults = {
|
|
|
|
chartElement : 'body',
|
|
|
|
depth : -1,
|
2011-11-18 22:55:28 +01:00
|
|
|
chartClass : "jOrgChart",
|
|
|
|
dragAndDrop: false
|
2011-11-11 18:51:44 +01:00
|
|
|
};
|
|
|
|
|
2011-11-18 22:55:28 +01:00
|
|
|
// Method that recursively builds the tree
|
2011-11-11 18:51:44 +01:00
|
|
|
function buildNode($node, $appendTo, level, opts) {
|
|
|
|
|
|
|
|
var $table = $("<table cellpadding='0' cellspacing='0' border='0'/>");
|
|
|
|
var $tbody = $("<tbody/>");
|
|
|
|
|
|
|
|
// Construct the node container(s)
|
|
|
|
var $nodeRow = $("<tr/>").addClass("node-cells");
|
|
|
|
var $nodeCell = $("<td/>").addClass("node-cell").attr("colspan", 2);
|
|
|
|
var $childNodes = $node.children("ul:first").children("li");
|
2012-01-21 17:15:49 +01:00
|
|
|
var $nodeDiv;
|
2011-11-11 18:51:44 +01:00
|
|
|
|
|
|
|
if($childNodes.length > 1) {
|
|
|
|
$nodeCell.attr("colspan", $childNodes.length * 2);
|
|
|
|
}
|
|
|
|
// Draw the node
|
|
|
|
// Get the contents - any markup except li and ul allowed
|
2011-11-18 22:55:28 +01:00
|
|
|
var $nodeContent = $node.clone()
|
|
|
|
.children("ul,li")
|
|
|
|
.remove()
|
|
|
|
.end()
|
|
|
|
.html();
|
|
|
|
|
2011-11-11 18:51:44 +01:00
|
|
|
$nodeDiv = $("<div>").addClass("node").append($nodeContent);
|
|
|
|
|
|
|
|
// Expand and contract nodes
|
2012-01-23 13:35:45 +01:00
|
|
|
if ($childNodes.length > 0) {
|
|
|
|
$nodeDiv.click(function() {
|
|
|
|
var $this = $(this);
|
|
|
|
var $tr = $this.closest("tr");
|
|
|
|
$tr.nextAll("tr").fadeToggle("fast");
|
|
|
|
|
|
|
|
if($tr.hasClass('contracted')){
|
|
|
|
$this.css('cursor','n-resize');
|
|
|
|
$tr.removeClass('contracted');
|
|
|
|
$tr.addClass('expanded');
|
|
|
|
}else{
|
|
|
|
$this.css('cursor','s-resize');
|
|
|
|
$tr.removeClass('expanded');
|
|
|
|
$tr.addClass('contracted');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-11-11 18:51:44 +01:00
|
|
|
|
|
|
|
$nodeCell.append($nodeDiv);
|
|
|
|
$nodeRow.append($nodeCell);
|
|
|
|
$tbody.append($nodeRow);
|
|
|
|
|
|
|
|
if($childNodes.length > 0) {
|
|
|
|
// if it can be expanded then change the cursor
|
|
|
|
$nodeDiv.css('cursor','n-resize').addClass('expanded');
|
|
|
|
|
|
|
|
// recurse until leaves found (-1) or to the level specified
|
|
|
|
if(opts.depth == -1 || (level+1 < opts.depth)) {
|
|
|
|
var $downLineRow = $("<tr/>");
|
|
|
|
var $downLineCell = $("<td/>").attr("colspan", $childNodes.length*2);
|
|
|
|
$downLineRow.append($downLineCell);
|
|
|
|
|
|
|
|
// draw the connecting line from the parent node to the horizontal line
|
|
|
|
$downLine = $("<div></div>").addClass("line down");
|
|
|
|
$downLineCell.append($downLine);
|
|
|
|
$tbody.append($downLineRow);
|
|
|
|
|
|
|
|
// Draw the horizontal lines
|
|
|
|
var $linesRow = $("<tr/>");
|
|
|
|
$childNodes.each(function() {
|
2012-01-19 16:48:19 +01:00
|
|
|
var $left = $("<td> </td>").addClass("line left top");
|
|
|
|
var $right = $("<td> </td>").addClass("line right top");
|
2011-11-11 18:51:44 +01:00
|
|
|
$linesRow.append($left).append($right);
|
|
|
|
});
|
|
|
|
|
|
|
|
// horizontal line shouldn't extend beyond the first and last child branches
|
2011-11-18 22:55:28 +01:00
|
|
|
$linesRow.find("td:first")
|
|
|
|
.removeClass("top")
|
|
|
|
.end()
|
|
|
|
.find("td:last")
|
|
|
|
.removeClass("top");
|
|
|
|
|
2011-11-11 18:51:44 +01:00
|
|
|
$tbody.append($linesRow);
|
|
|
|
var $childNodesRow = $("<tr/>");
|
|
|
|
$childNodes.each(function() {
|
|
|
|
var $td = $("<td class='node-container'/>");
|
|
|
|
$td.attr("colspan", 2);
|
|
|
|
// recurse through children lists and items
|
|
|
|
buildNode($(this), $td, level+1, opts);
|
|
|
|
$childNodesRow.append($td);
|
|
|
|
});
|
2012-01-19 16:48:19 +01:00
|
|
|
|
2011-11-11 18:51:44 +01:00
|
|
|
}
|
|
|
|
$tbody.append($childNodesRow);
|
|
|
|
}
|
|
|
|
|
2012-01-21 17:15:49 +01:00
|
|
|
// any classes on the LI element get copied to the relevant node in the tree
|
|
|
|
// apart from the special 'collapsed' class, which collapses the sub-tree at this point
|
|
|
|
if ($node.attr('class') != undefined) {
|
|
|
|
var classList = $node.attr('class').split(/\s+/);
|
|
|
|
$.each(classList, function(index,item) {
|
|
|
|
if (item == 'collapsed') {
|
|
|
|
$nodeRow.nextAll('tr').css('display', 'none');
|
2012-01-23 13:35:45 +01:00
|
|
|
$nodeRow.removeClass('expanded');
|
|
|
|
$nodeRow.addClass('contracted');
|
|
|
|
$nodeDiv.css('cursor','s-resize');
|
2012-01-21 17:15:49 +01:00
|
|
|
} else {
|
|
|
|
$nodeDiv.addClass(item);
|
|
|
|
}
|
|
|
|
});
|
2012-01-19 16:48:19 +01:00
|
|
|
}
|
|
|
|
|
2011-11-11 18:51:44 +01:00
|
|
|
$table.append($tbody);
|
|
|
|
$appendTo.append($table);
|
|
|
|
};
|
|
|
|
|
|
|
|
})(jQuery);
|