/** * jQuery org-chart/tree plugin. * * Author: Wes Nolte * http://twitter.com/wesnolte * * Based on the work of Mark Lee * http://www.capricasoftware.co.uk * * Copyright (c) 2011 Wesley Nolte * Dual licensed under the MIT and GPL licenses. * */ (function($) { $.fn.jOrgChart = function(options) { var opts = $.extend({}, $.fn.jOrgChart.defaults, options); var $appendTo = $(opts.chartElement); // build the tree var $this = $(this); var $container = $("
"); 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){ $('div.node').draggable({ cursor : 'move', distance : 40, helper : 'clone', opacity : 0.8, revert : 'invalid', revertDuration : 100, snap : 'div.node.expanded', snapMode : 'inner', stack : 'div.node' }); $('div.node').droppable({ accept : '.node', activeClass : 'drag-active', hoverClass : 'drop-hover' }); // Drag start event handler for nodes $('div.node').bind("dragstart", function handleDragStart( event, ui ){ var sourceNode = $(this); sourceNode.parentsUntil('.node-container') .find('*') .filter('.node') .droppable('disable'); }); // Drag stop event handler for nodes $('div.node').bind("dragstop", function handleDragStop( event, ui ){ /* reload the plugin */ $(opts.chartElement).children().remove(); $this.jOrgChart(opts); }); // Drop event handler for nodes $('div.node').bind("drop", function handleDropEvent( event, ui ) { var targetID = $(this).data("tree-node"); var targetLi = $this.find("li").filter(function() { return $(this).data("tree-node") === targetID; } ); var targetUl = targetLi.children('ul'); var sourceID = ui.draggable.data("tree-node"); var sourceLi = $this.find("li").filter(function() { return $(this).data("tree-node") === sourceID; } ); var sourceUl = sourceLi.parent('ul'); if (targetUl.length > 0){ targetUl.append(sourceLi); } else { targetLi.append("