/*
* developed by Matteo Bicocchi on JQuery
* 2002-2008 Open Lab srl, Matteo Bicocchi
* www.open-lab.com - info@open-lab.com
* version 1.0
* tested on: Explorer and FireFox for PC
* FireFox and Safari for Mac Os X
* FireFox for Linux
* GPL (GPL-LICENSE.txt) licenses.
*/
jQuery.fn.init_mbTree = function(options) {
return this.each (function () {
var tree=this;
this.options = {
treeWidth:600,
template:"/commons/layout/tree/partTreeDrawer.jsp",
className:"mbTree",
fade:true,
imgPath:"",
additionalData: "" ,
setSelOnClick:false,
multiSelection:false,
additionalOnClickScript:function(){},
selectedNodes:"",
ancestors:"",
indent:25,
callBack:function(){}
};
$.extend (this.options, options);
$(tree).css("width",this.options.treeWidth);
tree.selectedNodes=$(this.options.selectedNodes.split(",")).unique();
tree.ancestors=$(this.options.ancestors.split(",")).unique();
$(tree).addClass(this.options.className);
$(tree).children("tbody").children("tbody tr[id!='noResults']").each(function() {
var node = $(this);
this.tree=tree;
node.init_mbNode();
});
if (this.options.ancestors!="") {
$(tree).switch_mbNode();
// $(tree).get_mbSelectedNode();
// $(tree).expandCollapsAll_mbNodes();
}
this.options.callBack();
})
};
// initNodes
jQuery.fn.init_mbNode=function(parentNode) {
var node=$(this);
this.tree=node[0].tree;
node.attr("idx",node.attr("id"));
node.attr("id",$(this.tree).attr("id")+node.attr("id"));
node.css("cursor", "default");
node.addClass(parentNode?"":"root") ;
if (this.tree.selectedNodes=="null" || this.tree.selectedNodes==''){
this.tree.selectedNodes.push($(this.tree).find("tbody tr:first").attr("idx"));
//this.tree.options.additionalOnClickScript(node);
}
var isSelected=($.inArray (node.attr("idx").toString(), this.tree.selectedNodes)>-1);
var isAncestor=($.inArray (node.attr("idx").toString(), this.tree.ancestors)>-1);
node.addClass(isSelected ?"selected":"");
node.click(function(){
if(this.tree.options.setSelOnClick)
$(this).set_mbLastChoosenNodes();
this.tree.options.additionalOnClickScript(node);
});
var cell = node.find("td:first");
//cell.css({paddingRight:"50px",whiteSpace:"nowrap"});
var padding=0 ;
if (parentNode)
padding=parseInt(parentNode.find("td:first").css("padding-left")) + this.tree.options.indent;
cell.css("padding-left", padding + "px");
cell.prepend(' ');
var ico = $(cell).find(".ico");
ico.css("opacity",.7);
if($(node).hasClass("isParent")){
var expander = $(cell).find(".expander");
expander.bind("click",function() {node.switch_mbNode();});
ico.bind("click",function() { node.switch_mbNode();});
if(this.tree.options.fade) {
expander.bind("mouseover",function() { ico.css("opacity",1)}).bind("mouseout",function() { ico.css("opacity",.7)});
ico.bind("mouseover",function() { ico.css("opacity",1)}).bind("mouseout",function() { ico.css("opacity",.7)});
}
}
node.find("[selectNode='false']").bind("click",function(){return false;});
node.find("[selectNode='select']").bind("click",function(){if (node.is(".selected")) return false;});
if (isAncestor) {
$(node).switch_mbNode();
// $(node).expandCollapsAll_mbNodes();
//node.switch_mbNode();
}
if(isSelected) {
//this.tree.options.additionalOnClickScript(node);
}
node.bind("contextmenu", function(){
showContextualMenu(eval($(node.children("td")[0]).attr('contextualMenu')), node.attr("idx"), $(this.tree).attr("idx"), '' , '' );
return false;
});
};
// Hide all children of a node.
jQuery.fn.collapse_mbNode=function() {
var node=$(this);
this.tree=node[0].tree;
node.get_mbNodeChildren().each(function() {
var child = $(this);
child.collapse_mbNode();
if(this.tree.options.fade)
child.fadeOut(300);
else
child.hide();
});
};
// Show all children of a node.
jQuery.fn.expand_mbNode=function() {
var node=$(this);
this.tree=node[0].tree;
node.get_mbNodeChildren().each(function() {
var child = $(this);
if(child.is(".expanded.isParent"))
child.expand_mbNode();
if(this.tree.options.fade)
child.fadeIn(300);
else
child.show();
});
};
// Toggle a node
jQuery.fn.switch_mbNode= function() {
var node=$(this);
if(node.is(".collapsed")) {
node.removeClass("collapsed");
node.addClass("expanded");
node.expand_mbNode();
} else if(node.is(".expanded")){
node.removeClass("expanded");
node.addClass("collapsed");
node.collapse_mbNode();
} else if(node.is(".isParent") && ( node.not(".expanded") && node.not(".collapsed"))) {
node.getAjax_mbNodeChildren();
node.addClass("collapsed");
}
};
jQuery.fn.expandCollapsAll_mbNodes= function(){
var node=$(this);
this.tree=node[0].tree;
if (node.is(".isParent") && (node.is(".expanded") || node.is(".collapsed"))) {
if (node.is(".collapsed")) node.removeClass("collapsed").addClass("expanded");
node.expand_mbNode();
node.get_mbNodeChildren().each(function(act) {
$(this).expandCollapsAll_mbNodes(act);
});
} else if (node.is(".isParent") && (!node.is(".collapsed") && !node.is(".expanded"))){
this.tree=node[0].tree;
$.ajax({
url: this.tree.options.template,
cache: false,
async: true,
data:"node="+node.attr("idx")+"&JSPACT=DRAW_CHILDREN_BLOCK&unId="+$(this.tree).attr("id")+"&ts=1743580377135"+this.tree.options.additionalData,
success: function(html){
node.addClass("expanded");
node.after(html);
node.get_mbNodeChildren().each(function() {
var childNode = $(this);
this.tree = node[0].tree;
childNode.init_mbNode(node);
childNode.hide();
if(this.tree.options.fade)
childNode.fadeIn(300);
else
childNode.show();
if (childNode.is(".isParent"))
childNode.expandCollapsAll_mbNodes();
});
}
});
}
};
//get chidren via Ajax
jQuery.fn.getAjax_mbNodeChildren= function(){
var node=$(this);
this.tree=node[0].tree;
node.find("td:first").prepend("");
node.find(".loader").css("opacity",.5);
$.ajax({
url: this.tree.options.template,
cache: false,
async: true,
data: "node="+node.attr("idx")+"&JSPACT=DRAW_CHILDREN_BLOCK&unId="+$(this.tree).attr("id")+"&ts=1743580377135"+this.tree.options.additionalData,
success: function(html){
node.after(html);
node.get_mbNodeChildren().each(function() {
this.tree = node[0].tree;
var childNode = $(this);
childNode.init_mbNode(node);
childNode.hide();
});
node.switch_mbNode();
node.find(".loader").fadeOut(300,function(){$(this).remove()});
}
});
};
jQuery.fn.get_mbSelectedNode= function(arrayIndex){
var tree=$(this);
if (tree[0].ancestors=="")
return;
arrayIndex= !arrayIndex ? 0 : arrayIndex;
var actualNode=tree[0].ancestors[arrayIndex];
var template=tree[0].options.template;
var node=tree.find("[idx='"+actualNode+"']");
arrayIndex++;
node.find("td:first").prepend("");
node.find(".loader").css("opacity",.5);
$.ajax({
url: template,
cache: false,
async: false,
data: "node="+node.attr("idx")+"&JSPACT=DRAW_CHILDREN_BLOCK&unId="+$(this).attr("id")+"&ts=1743580377135"+tree[0].options.additionalData,
success: function(html){
node.after(html);
tree.find("tr[parent='empty']").attr("parent",node.attr("id")) ;
node.get_mbNodeChildren().each(function() {
this.tree = node[0].tree;
$(this).init_mbNode(node);
});
if (tree[0].ancestors[arrayIndex]){
tree.get_mbSelectedNode(arrayIndex);
}
node.addClass("expanded");
node.find(".loader").fadeOut(1000,function(){$(this).remove()});
}
});
} ;
//Utilities
jQuery.fn.select_mbNode= function() {
$(this).click();
} ;
jQuery.fn.unique=function() {
var r = new Array();
o:for(var i = 0, n = $(this).length; i < n; i++) {
for(var x = 0, y = r.length; x < y; x++)
if(r[x]==$(this)[i]) continue o;
r[r.length] = $(this)[i];
}
return r;
} ;
jQuery.fn.isChildOf = function(b){
return (this.parents(b).length > 0);
};
jQuery.fn.get_mbNodeChildren= function() {
return $("tr[parent='" + $(this).attr('idx')+"']");
} ;
jQuery.fn.get_mbNodeFirstChild = function() {
return $("tr[parent='" + $(this).attr('idx')+"']:first");
};
jQuery.fn.get_mbNodeLastChild = function() {
return $("tr[parent='" +$(this).attr('idx')+"']:last");
} ;
jQuery.fn.set_mbLastChoosenNodes = function() {
var node=$(this);
this.tree=node[0].tree;
//todo add control on key pressed
if(!this.tree.options.multiSelection) {
$(this.tree).find(".selected").removeClass("selected");
node.addClass("selected");
} else{
if (node.is(".selected")) node.removeClass("selected");
else node.addClass("selected");
}
// this.tree.options.additionalOnClickScript(node);
//console.log("stored node: "+node.attr("idx"));
$.ajax({
url: '/commons/layout/tree/ajaxSetLastNode.jsp',
cache: false,
async: true,
type: "POST",
data: 'setNid=yes&multi='+this.tree.options.multiSelection+'&nid='+node.attr("idx")+'&tui='+$(this.tree).attr("id"),
success: function(html){
//tree.lastChoosenNode=html;
}
});
} ;
function trace(arguments){
if ($.browser.msie)
alert(aguments) ;
else
console.log(arguments) ;
}