(function($,undefined){$.widget("daredevel.tree",{_attachLi:function(li,parent,position){var ul=parent.find('ul:first');if(ul.length){if((undefined==position)||(ul.children('li').length');parent.append(ul.append(li));}},_attachNode:function(li,parentLi,position){if(undefined==parentLi){var parent=this.element;this._attachLi(li,parent,position);this._initializeNode(li);}else{var parent=parentLi;this._attachLi(li,parent,position);parent.removeClass('leaf collapsed').addClass('expanded');this._initializeNode(li);this._initializeNode(parent);}},_buildNode:function(attributes){attributes=$.extend(true,this.options.defaultNodeAttributes,attributes);var span=$('',attributes.span);var li=$('
  • ',attributes.li);if($.inArray('checkbox',this.options.components)>-1){var input=$('',attributes.input);li.append(input);} li.append(span);return li;},_create:function(){var t=this;this.options.core=this;this.element.addClass('ui-widget daredevel-tree');if(this.options.checkbox){this._createCheckbox();} if(this.options.collapsible){this._createCollapsible();} if(this.options.dnd){this._createDnd();} if(this.options.selectable){this._createSelectable();} this.element.find('li').each(function(){t._initializeNode($(this));});if(this.options.nodes!=null){$.each(this.options.nodes,function(key,value){t.options.core.addNode(value);});}},_destroy:function(){$.Widget.prototype.destroy.call(this);},_detachNode:function(li){var parentLi=this.options.core.parentNode(li);var ul=parentLi.find('ul:first');if(ul.children().length==1){ul.detach();parentLi.removeClass('collapsed expanded').addClass('leaf')}else{li.detach();} this.options.core._initializeNode(parentLi);},_initializeComponents:function(){for(var i in this.options.components){var initializeComponent='element.tree'+this.options.components[i]+'(options);';run=new Function("options","element",initializeComponent);run(this.options,this.element);}},_initializeNode:function(li){li.children('span:last').addClass('daredevel-tree-label');if(this.options.checkbox){this._initializeCheckboxNode(li);} if(this.options.collapsible){this._initializeCollapsibleNode(li);} if(this.options.dnd){this._initializeDndNode(li);} if(this.options.selectable){this._initializeSelectableNode(li);}},addNode:function(attributes,parentLi,position){var t=this;var li=this._buildNode(attributes);if((undefined==parentLi)||0==parentLi.length){this._attachNode($(li),undefined,position);}else{this._attachNode($(li),$(parentLi),position);} if(undefined!=attributes.children){$.each(attributes.children,function(key,value){t.addNode(value,li);});} t._trigger('add',true,li);},isRoot:function(li){li=$(li);var parents=li.parentsUntil('.daredevel-tree');return 1==parents.length;},moveNode:function(li,parentLi,position){this._detachNode($(li));if((undefined==parentLi)||0==parentLi.length){this._attachNode($(li),undefined,position);}else{this._attachNode($(li),$(parentLi),position);} this._trigger('move',true,$(li));},parentNode:function(li){return $(li).parents('li:first');},removeNode:function(li){this._detachNode($(li));this._trigger('remove',true,$(li));},_allDescendantChecked:function(li){return(li.find('li input:checkbox:not(:checked)').length==0);},_checkAncestors:function(li){li.parentsUntil('daredevel-tree').filter('li').find('input:checkbox:first:not(:checked)').prop('checked',true).change();},_checkDescendants:function(li){li.find('li input:checkbox:not(:checked)').prop('checked',true).change();},_checkOthers:function(li){var t=this;li.addClass('exclude');li.parents('li').addClass('exclude');li.find('li').addClass('exclude');$(this.element).find('li').each(function(){if(!$(this).hasClass('exclude')){$(this).find('input:checkbox:first:not(:checked)').prop('checked',true).change();}});$(this.element).find('li').removeClass('exclude');},_createCheckbox:function(){var t=this;this.element.on('click','input:checkbox:not(:checked)',function(){t.uncheck(t.options.core.parentNode($(this)));});this.element.on('click','input:checkbox:checked',function(){t.check(t.options.core.parentNode($(this)));});if(this.options.onUncheck.node=='collapse'){this.element.on("click",'input:checkbox:not(:checked)',function(){t.options.core.collapse(t.options.core.parentNode($(this)));});}else if(this.options.onUncheck.node=='expand'){this.element.on("click",'input:checkbox:not(:checked)',function(){t.options.core.expand(t.options.core.parentNode($(this)));});} if(this.options.onCheck.node=='collapse'){this.element.on("click",'input:checkbox:checked',function(){t.options.core.collapse(t.options.core.parentNode($(this)));});}else if(this.options.onCheck.node=='expand'){this.element.on("click",'input:checkbox:checked',function(){t.options.core.expand(t.options.core.parentNode($(this)));});}},_initializeCheckboxNode:function(li){},_uncheckAncestors:function(li){li.parentsUntil('daredevel-tree').filter('li').find('input:checkbox:first:checked').prop('checked',false).change();},_uncheckDescendants:function(li){li.find('li input:checkbox:checked').prop('checked',false).change();},_uncheckOthers:function(li){var t=this;li.addClass('exclude');li.parents('li').addClass('exclude');li.find('li').addClass('exclude');$(this.element).find('li').each(function(){if(!$(this).hasClass('exclude')){$(this).find('input:checkbox:first:checked').prop('checked',false).change();}});$(this.element).find('li').removeClass('exclude');},check:function(li){li=$(li);li.find('input:checkbox:first:not(:checked)').prop('checked',true).change();if(this.options.onCheck.others=='check'){this._checkOthers(li);}else if(this.options.onCheck.others=='uncheck'){this._uncheckOthers(li);} if(this.options.onCheck.descendants=='check'){this._checkDescendants(li);}else if(this.options.onCheck.descendants=='uncheck'){this._uncheckDescendants(li);} if(this.options.onCheck.ancestors=='check'){this._checkAncestors(li);}else if(this.options.onCheck.ancestors=='uncheck'){this._uncheckAncestors(li);}else if(this.options.onCheck.ancestors=='checkIfFull'){var isRoot=this.options.core.isRoot(li);var allDescendantChecked=this._allDescendantChecked(this.options.core.parentNode(li));if(!isRoot&&allDescendantChecked){this.check(this.options.core.parentNode(li));}}},checkAll:function(){$(this.element).find('input:checkbox:not(:checked)').prop('checked',true).change();},uncheck:function(li){li=$(li);li.find('input:checkbox:first:checked').prop('checked',false).change();if(this.options.onUncheck.others=='check'){this._checkOthers(li);}else if(this.options.onUncheck.others=='uncheck'){this._uncheckOthers(li);} if(this.options.onUncheck.descendants=='check'){this._checkDescendants(li);}else if(this.options.onUncheck.descendants=='uncheck'){this._uncheckDescendants(li);} if(this.options.onUncheck.ancestors=='check'){this._checkAncestors(li);}else if(this.options.onUncheck.ancestors=='uncheck'){this._uncheckAncestors(li);}},uncheckAll:function(){$(this.element).find('input:checkbox:checked').prop('checked',false).change();},_createCollapsible:function(){var t=this this.element.on("click",'li span.daredevel-tree-anchor',function(){var li=t.options.core.parentNode($(this));if(li.hasClass('collapsed')){t.expand(li);}else if(li.hasClass('expanded')){t.collapse(li);}});},_initializeCollapsibleNode:function(li){var t=this;var anchor=li.children('span.daredevel-tree-anchor');if(anchor.length<1){li.prepend($('',{'class':'daredevel-tree-anchor'}));} if(li.hasClass("leaf")){t._markAsLeaf(li);}else if(li.hasClass("collapsed")){t.collapse(li,false,true);}else if(li.hasClass("expanded")){t.expand(li,false,true);}else if(li.is("li:not(:has(ul))")){t._markAsLeaf(li);}else{t._markAsExpanded(li);}},_markAsCollapsed:function(li){var anchor=li.children('span.daredevel-tree-anchor');anchor.removeClass('ui-icon '+this.options.expandUiIcon+' '+this.options.leafUiIcon);if(this.options.collapseUiIcon.length>0){anchor.addClass('ui-icon '+this.options.collapseUiIcon)} li.removeClass("leaf").removeClass("expanded").addClass("collapsed");},_markAsExpanded:function(li){var anchor=li.children('span.daredevel-tree-anchor');anchor.removeClass('ui-icon '+this.options.collapseUiIcon+' '+this.options.leafUiIcon);if(this.options.expandUiIcon.length>0){anchor.addClass('ui-icon '+this.options.expandUiIcon)} li.removeClass("leaf").removeClass("collapsed").addClass("expanded");},_markAsLeaf:function(li){var anchor=li.children('span.daredevel-tree-anchor');anchor.removeClass('ui-icon '+this.options.collapseUiIcon+' '+this.options.expandUiIcon);if(this.options.leafUiIcon.length>0){anchor.addClass('ui-icon '+this.options.leafUiIcon)} li.removeClass("collapsed").removeClass("expanded").addClass("leaf");},_unmark:function(){li.removeClass("collapsed expanded leaf");},collapse:function(li,effect,force){li=$(li);if(force==undefined){force=false;} if(!force&&(li.hasClass('collapsed')||li.hasClass('leaf'))){return;} if(effect==undefined){effect=true;} var t=this;if(effect){li.children("ul").hide(this.options.collapseEffect,{},this.options.collapseDuration);setTimeout(function(){t._markAsCollapsed(li,t.options);},t.options.collapseDuration);}else{li.children("ul").hide();t._markAsCollapsed(li,t.options);} t.options.core._trigger('collapse',true,li);},collapseAll:function(){var t=this;$(this.element).find('li.expanded').each(function(){t.collapse($(this));});},expand:function(li,effect,force){var t=this;$(this.element).find('li.expanded').each(function(){t.collapse($(this));});li=$(li);if(force==undefined){force=false;} if(!force&&(li.hasClass('expanded')||li.hasClass('leaf'))){return;} if(effect==undefined){effect=true;} var t=this;if(effect){li.children("ul").show(t.options.expandEffect,{},t.options.expandDuration);setTimeout(function(){t._markAsExpanded(li,t.options);},t.options.expandDuration);}else{li.children("ul").show();t._markAsExpanded(li,t.options);} t.options.core._trigger('expand',true,li);},expandAll:function(){var t=this;$(this.element).find('li.collapsed').each(function(){t.expand($(this));});},_createDnd:function(){var t=this;},_initializeDndNode:function(li){var t=this;var span=$('',{'class':'prepended',html:'
    '}).droppable({hoverClass:'over',drop:function(event,ui){var li=$(this).closest('li');if(t.options.core.isRoot(li)){var parentLi=undefined;var droppable=t.options.core.element;}else{var parentLi=li.parent().closest('li');var droppable=parentLi;if($(ui.draggable.parent('li')).find(parentLi).length){return;}} var position=$($(this).parent('li')).index()+1;t.options.core.moveNode(ui.draggable.parent('li'),parentLi,position);t._trigger('drop',event,{draggable:ui.draggable,droppable:parentLi});}});$(li).find('.daredevel-tree-label:first').after(span);$(li).find('.daredevel-tree-label:first').draggable({start:function(event,ui){$(this).parent('li').find('ul, .prepended').css('visibility','hidden');$(this).parent('li').find('.droppable-label').css('display','none');},stop:function(event,ui){$(this).parent('li').find('ul').css('visibility','visible');$(this).parent('li').find('.prepended').css('visibility','');$(this).parent('li').find('.droppable-label').css('display','inherit');},revert:true,revertDuration:0});var span=$('',{'class':'droppable-label',html:'
    '}).droppable({drop:function(event,ui){var li=$(this).closest('li');if($(ui.draggable.parent('li')).find(li).length){return;} t.options.core.moveNode(ui.draggable.parent('li'),li,1);t._trigger('drop',event,{draggable:ui.draggable,droppable:li});},over:function(event,ui){$(this).parent('li').find('.daredevel-tree-label:first').addClass('ui-state-hover');},out:function(event,ui){$(this).parent('li').find('.daredevel-tree-label:first').removeClass('ui-state-hover');}});$(li).find('.daredevel-tree-label:first').after(span);},_createSelectable:function(){var t=this;this.element.on("click",'.daredevel-tree-label',function(){var li=$(this);if(li.hasClass(t.options.selectUiClass)){t.deselect($(this).parent(li));}else{t.select($(this).parent('li'));}});},_deselect:function(li){li.find('span.daredevel-tree-label:first').removeClass(this.options.selectUiClass);this._trigger('deselect',true,li);},_deselectAll:function(){var t=this;this.element.find('.daredevel-tree-label.'+this.options.selectUiClass).each(function(){t._deselect($(this).parent('li'));});},_destroySelectable:function(){},_initializeSelectableNode:function(li){},_select:function(li){li.find('span.daredevel-tree-label:first').addClass(this.options.selectUiClass);this._trigger('select',true,li);},deselect:function(li){li=$(li);this._deselect(li);},select:function(li){li=$(li);if(this.options.selectUnique){this._deselectAll();} this._select(li);},selected:function(){var selected=this.element.find('.daredevel-tree-label.'+this.options.selectUiClass);return $(selected).parent();},options:{defaultNodeAttributes:{span:{html:'new node'},li:{'class':'leaf'},input:{type:'checkbox'}},nodes:null,checkbox:true,onCheck:{ancestors:'check',descendants:'null',node:'',others:''},onUncheck:{ancestors:'',descendants:'uncheck',node:'',others:''},collapsible:true,collapseDuration:125,collapseEffect:'blind',collapseUiIcon:'ui-icon-plus',expandDuration:125,expandEffect:'blind',expandUiIcon:'ui-icon-minus',leafUiIcon:'',dnd:true,drop:function(event,element){},selectable:false,deselect:function(event,element){},selectUiClass:'ui-state-active',selectUnique:true,select:function(event,element){}}});$.ui.draggable.prototype._getRelativeOffset=function(){if(this.cssPosition=="relative"){var p=this.element.position();return{top:p.top-(parseInt(this.helper.css("top"),10)||0),left:p.left-(parseInt(this.helper.css("left"),10)||0)};}else{return{top:0,left:0};}};})(jQuery);