/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.grid._Builder"]){
dojo._hasResource["dojox.grid._Builder"]=true;
dojo.provide("dojox.grid._Builder");
dojo.require("dojox.grid.util");
dojo.require("dojo.dnd.Moveable");
(function(){
var dg=dojox.grid;
var _2=function(td){
return td.cellIndex>=0?td.cellIndex:dojo.indexOf(td.parentNode.cells,td);
};
var _4=function(tr){
return tr.rowIndex>=0?tr.rowIndex:dojo.indexOf(tr.parentNode.childNodes,tr);
};
var _6=function(_7,_8){
return _7&&((_7.rows||0)[_8]||_7.childNodes[_8]);
};
var _9=function(_a){
for(var n=_a;n&&n.tagName!="TABLE";n=n.parentNode){
}
return n;
};
var _c=function(_d,_e){
for(var n=_d;n&&_e(n);n=n.parentNode){
}
return n;
};
var _10=function(_11){
var _12=_11.toUpperCase();
return function(_13){
return _13.tagName!=_12;
};
};
var _14=dojox.grid.util.rowIndexTag;
var _15=dojox.grid.util.gridViewTag;
dg._Builder=dojo.extend(function(_16){
if(_16){
this.view=_16;
this.grid=_16.grid;
}
},{view:null,_table:"<table class=\"dojoxGridRowTable\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" role=\"wairole:presentation\"",getTableArray:function(){
var _17=[this._table];
if(this.view.viewWidth){
_17.push([" style=\"width:",this.view.viewWidth,";\""].join(""));
}
_17.push(">");
return _17;
},generateCellMarkup:function(_18,_19,_1a,_1b){
var _1c=[],_1d;
if(_1b){
_1d=["<th tabIndex=\"-1\" role=\"wairole:columnheader\""];
}else{
_1d=["<td tabIndex=\"-1\" role=\"wairole:gridcell\""];
}
_18.colSpan&&_1d.push(" colspan=\"",_18.colSpan,"\"");
_18.rowSpan&&_1d.push(" rowspan=\"",_18.rowSpan,"\"");
_1d.push(" class=\"dojoxGridCell ");
_18.classes&&_1d.push(_18.classes," ");
_1a&&_1d.push(_1a," ");
_1c.push(_1d.join(""));
_1c.push("");
_1d=["\" idx=\"",_18.index,"\" style=\""];
if(_19&&_19[_19.length-1]!=";"){
_19+=";";
}
_1d.push(_18.styles,_19||"",_18.hidden?"display:none;":"");
_18.unitWidth&&_1d.push("width:",_18.unitWidth,";");
_1c.push(_1d.join(""));
_1c.push("");
_1d=["\""];
_18.attrs&&_1d.push(" ",_18.attrs);
_1d.push(">");
_1c.push(_1d.join(""));
_1c.push("");
_1c.push("</td>");
return _1c;
},isCellNode:function(_1e){
return Boolean(_1e&&_1e!=dojo.doc&&dojo.attr(_1e,"idx"));
},getCellNodeIndex:function(_1f){
return _1f?Number(dojo.attr(_1f,"idx")):-1;
},getCellNode:function(_20,_21){
for(var i=0,row;row=_6(_20.firstChild,i);i++){
for(var j=0,_25;_25=row.cells[j];j++){
if(this.getCellNodeIndex(_25)==_21){
return _25;
}
}
}
},findCellTarget:function(_26,_27){
var n=_26;
while(n&&(!this.isCellNode(n)||(n.offsetParent&&_15 in n.offsetParent.parentNode&&n.offsetParent.parentNode[_15]!=this.view.id))&&(n!=_27)){
n=n.parentNode;
}
return n!=_27?n:null;
},baseDecorateEvent:function(e){
e.dispatch="do"+e.type;
e.grid=this.grid;
e.sourceView=this.view;
e.cellNode=this.findCellTarget(e.target,e.rowNode);
e.cellIndex=this.getCellNodeIndex(e.cellNode);
e.cell=(e.cellIndex>=0?this.grid.getCell(e.cellIndex):null);
},findTarget:function(_2a,_2b){
var n=_2a;
while(n&&(n!=this.domNode)&&(!(_2b in n)||(_15 in n&&n[_15]!=this.view.id))){
n=n.parentNode;
}
return (n!=this.domNode)?n:null;
},findRowTarget:function(_2d){
return this.findTarget(_2d,_14);
},isIntraNodeEvent:function(e){
try{
return (e.cellNode&&e.relatedTarget&&dojo.isDescendant(e.relatedTarget,e.cellNode));
}
catch(x){
return false;
}
},isIntraRowEvent:function(e){
try{
var row=e.relatedTarget&&this.findRowTarget(e.relatedTarget);
return !row&&(e.rowIndex==-1)||row&&(e.rowIndex==row.gridRowIndex);
}
catch(x){
return false;
}
},dispatchEvent:function(e){
if(e.dispatch in this){
return this[e.dispatch](e);
}
},domouseover:function(e){
if(e.cellNode&&(e.cellNode!=this.lastOverCellNode)){
this.lastOverCellNode=e.cellNode;
this.grid.onMouseOver(e);
}
this.grid.onMouseOverRow(e);
},domouseout:function(e){
if(e.cellNode&&(e.cellNode==this.lastOverCellNode)&&!this.isIntraNodeEvent(e,this.lastOverCellNode)){
this.lastOverCellNode=null;
this.grid.onMouseOut(e);
if(!this.isIntraRowEvent(e)){
this.grid.onMouseOutRow(e);
}
}
},domousedown:function(e){
if(e.cellNode){
this.grid.onMouseDown(e);
}
this.grid.onMouseDownRow(e);
}});
dg._ContentBuilder=dojo.extend(function(_35){
dg._Builder.call(this,_35);
},dg._Builder.prototype,{update:function(){
this.prepareHtml();
},prepareHtml:function(){
var _36=this.grid.get,_37=this.view.structure.cells;
for(var j=0,row;(row=_37[j]);j++){
for(var i=0,_3b;(_3b=row[i]);i++){
_3b.get=_3b.get||(_3b.value==undefined)&&_36;
_3b.markup=this.generateCellMarkup(_3b,_3b.cellStyles,_3b.cellClasses,false);
}
}
},generateHtml:function(_3c,_3d){
var _3e=this.getTableArray(),v=this.view,_40=v.structure.cells,_41=this.grid.getItem(_3d);
dojox.grid.util.fire(this.view,"onBeforeRow",[_3d,_40]);
for(var j=0,row;(row=_40[j]);j++){
if(row.hidden||row.header){
continue;
}
_3e.push(!row.invisible?"<tr>":"<tr class=\"dojoxGridInvisible\">");
for(var i=0,_45,m,cc,cs;(_45=row[i]);i++){
m=_45.markup,cc=_45.customClasses=[],cs=_45.customStyles=[];
m[5]=_45.format(_3d,_41);
m[1]=cc.join(" ");
m[3]=cs.join(";");
_3e.push.apply(_3e,m);
}
_3e.push("</tr>");
}
_3e.push("</table>");
return _3e.join("");
},decorateEvent:function(e){
e.rowNode=this.findRowTarget(e.target);
if(!e.rowNode){
return false;
}
e.rowIndex=e.rowNode[_14];
this.baseDecorateEvent(e);
e.cell=this.grid.getCell(e.cellIndex);
return true;
}});
dg._HeaderBuilder=dojo.extend(function(_4a){
this.moveable=null;
dg._Builder.call(this,_4a);
},dg._Builder.prototype,{_skipBogusClicks:false,overResizeWidth:4,minColWidth:1,update:function(){
if(this.tableMap){
this.tableMap.mapRows(this.view.structure.cells);
}else{
this.tableMap=new dg._TableMap(this.view.structure.cells);
}
},generateHtml:function(_4b,_4c){
var _4d=this.getTableArray(),_4e=this.view.structure.cells;
dojox.grid.util.fire(this.view,"onBeforeRow",[-1,_4e]);
for(var j=0,row;(row=_4e[j]);j++){
if(row.hidden){
continue;
}
_4d.push(!row.invisible?"<tr>":"<tr class=\"dojoxGridInvisible\">");
for(var i=0,_52,_53;(_52=row[i]);i++){
_52.customClasses=[];
_52.customStyles=[];
if(this.view.simpleStructure){
if(_52.headerClasses){
if(_52.headerClasses.indexOf("dojoDndItem")==-1){
_52.headerClasses+=" dojoDndItem";
}
}else{
_52.headerClasses="dojoDndItem";
}
if(_52.attrs){
if(_52.attrs.indexOf("dndType='gridColumn'")==-1){
_52.attrs+=" dndType='gridColumn_"+this.grid.id+"'";
}
}else{
_52.attrs="dndType='gridColumn_"+this.grid.id+"'";
}
}
_53=this.generateCellMarkup(_52,_52.headerStyles,_52.headerClasses,true);
_53[5]=(_4c!=undefined?_4c:_4b(_52));
_53[3]=_52.customStyles.join(";");
_53[1]=_52.customClasses.join(" ");
_4d.push(_53.join(""));
}
_4d.push("</tr>");
}
_4d.push("</table>");
return _4d.join("");
},getCellX:function(e){
var x=e.layerX;
if(dojo.isMoz){
var n=_c(e.target,_10("th"));
x-=(n&&n.offsetLeft)||0;
var t=e.sourceView.getScrollbarWidth();
if(!dojo._isBodyLtr()&&e.sourceView.headerNode.scrollLeft<t){
x-=t;
}
}
var n=_c(e.target,function(){
if(!n||n==e.cellNode){
return false;
}
x+=(n.offsetLeft<0?0:n.offsetLeft);
return true;
});
return x;
},decorateEvent:function(e){
this.baseDecorateEvent(e);
e.rowIndex=-1;
e.cellX=this.getCellX(e);
return true;
},prepareResize:function(e,mod){
do{
var i=_2(e.cellNode);
e.cellNode=(i?e.cellNode.parentNode.cells[i+mod]:null);
e.cellIndex=(e.cellNode?this.getCellNodeIndex(e.cellNode):-1);
}while(e.cellNode&&e.cellNode.style.display=="none");
return Boolean(e.cellNode);
},canResize:function(e){
if(!e.cellNode||e.cellNode.colSpan>1){
return false;
}
var _5d=this.grid.getCell(e.cellIndex);
return !_5d.noresize&&!_5d.canResize();
},overLeftResizeArea:function(e){
if(dojo._isBodyLtr()){
return (e.cellIndex>0)&&(e.cellX<this.overResizeWidth)&&this.prepareResize(e,-1);
}
var t=e.cellNode&&(e.cellX<this.overResizeWidth);
return t;
},overRightResizeArea:function(e){
if(dojo._isBodyLtr()){
return e.cellNode&&(e.cellX>=e.cellNode.offsetWidth-this.overResizeWidth);
}
return (e.cellIndex>0)&&(e.cellX>=e.cellNode.offsetWidth-this.overResizeWidth)&&this.prepareResize(e,-1);
},domousemove:function(e){
if(!this.moveable){
var c=(this.overRightResizeArea(e)?"e-resize":(this.overLeftResizeArea(e)?"w-resize":""));
if(c&&!this.canResize(e)){
c="not-allowed";
}
if(dojo.isIE){
var t=e.sourceView.headerNode.scrollLeft;
e.sourceView.headerNode.style.cursor=c||"";
e.sourceView.headerNode.scrollLeft=t;
}else{
e.sourceView.headerNode.style.cursor=c||"";
}
if(c){
dojo.stopEvent(e);
}
}
},domousedown:function(e){
if(!this.moveable){
if((this.overRightResizeArea(e)||this.overLeftResizeArea(e))&&this.canResize(e)){
this.beginColumnResize(e);
}else{
this.grid.onMouseDown(e);
this.grid.onMouseOverRow(e);
}
}
},doclick:function(e){
if(this._skipBogusClicks){
dojo.stopEvent(e);
return true;
}
},beginColumnResize:function(e){
this.moverDiv=document.createElement("div");
dojo.style(this.moverDiv,{position:"absolute",left:0});
dojo.body().appendChild(this.moverDiv);
var m=this.moveable=new dojo.dnd.Moveable(this.moverDiv);
var _68=[],_69=this.tableMap.findOverlappingNodes(e.cellNode);
for(var i=0,_6b;(_6b=_69[i]);i++){
_68.push({node:_6b,index:this.getCellNodeIndex(_6b),width:_6b.offsetWidth});
}
var _6c=e.sourceView;
var adj=dojo._isBodyLtr()?1:-1;
var _6e=e.grid.views.views;
var _6f=[];
for(var i=_6c.idx+adj,_70;(_70=_6e[i]);i=i+adj){
_6f.push({node:_70.headerNode,left:window.parseInt(_70.headerNode.style.left)});
}
var _71=_6c.headerContentNode.firstChild;
var _72={scrollLeft:e.sourceView.headerNode.scrollLeft,view:_6c,node:e.cellNode,index:e.cellIndex,w:dojo.contentBox(e.cellNode).w,vw:dojo.contentBox(_6c.headerNode).w,table:_71,tw:dojo.contentBox(_71).w,spanners:_68,followers:_6f};
m.onMove=dojo.hitch(this,"doResizeColumn",_72);
dojo.connect(m,"onMoveStop",dojo.hitch(this,function(){
this.endResizeColumn(_72);
if(_72.node.releaseCapture){
_72.node.releaseCapture();
}
this.moveable.destroy();
delete this.moveable;
this.moveable=null;
}));
_6c.convertColPctToFixed();
if(e.cellNode.setCapture){
e.cellNode.setCapture();
}
m.onMouseDown(e);
},doResizeColumn:function(_73,_74,_75){
var _76=dojo._isBodyLtr();
var _77=_76?_75.l:-_75.l;
var w=_73.w+_77;
var vw=_73.vw+_77;
var tw=_73.tw+_77;
if(w>=this.minColWidth){
for(var i=0,s,sw;(s=_73.spanners[i]);i++){
sw=s.width+_77;
s.node.style.width=sw+"px";
_73.view.setColWidth(s.index,sw);
}
for(var i=0,f,fl;(f=_73.followers[i]);i++){
fl=f.left+_77;
f.node.style.left=fl+"px";
}
_73.node.style.width=w+"px";
_73.view.setColWidth(_73.index,w);
_73.view.headerNode.style.width=vw+"px";
_73.view.setColumnsWidth(tw);
if(!_76){
_73.view.headerNode.scrollLeft=_73.scrollLeft+_77;
}
}
if(_73.view.flexCells&&!_73.view.testFlexCells()){
var t=_9(_73.node);
t&&(t.style.width="");
}
},endResizeColumn:function(_81){
dojo.destroy(this.moverDiv);
delete this.moverDiv;
this._skipBogusClicks=true;
var _82=dojo.connect(_81.view,"update",this,function(){
dojo.disconnect(_82);
this._skipBogusClicks=false;
});
setTimeout(dojo.hitch(_81.view,"update"),50);
}});
dg._TableMap=dojo.extend(function(_83){
this.mapRows(_83);
},{map:null,mapRows:function(_84){
var _85=_84.length;
if(!_85){
return;
}
this.map=[];
for(var j=0,row;(row=_84[j]);j++){
this.map[j]=[];
}
for(var j=0,row;(row=_84[j]);j++){
for(var i=0,x=0,_8a,_8b,_8c;(_8a=row[i]);i++){
while(this.map[j][x]){
x++;
}
this.map[j][x]={c:i,r:j};
_8c=_8a.rowSpan||1;
_8b=_8a.colSpan||1;
for(var y=0;y<_8c;y++){
for(var s=0;s<_8b;s++){
this.map[j+y][x+s]=this.map[j][x];
}
}
x+=_8b;
}
}
},dumpMap:function(){
for(var j=0,row,h="";(row=this.map[j]);j++,h=""){
for(var i=0,_93;(_93=row[i]);i++){
h+=_93.r+","+_93.c+"   ";
}
}
},getMapCoords:function(_94,_95){
for(var j=0,row;(row=this.map[j]);j++){
for(var i=0,_99;(_99=row[i]);i++){
if(_99.c==_95&&_99.r==_94){
return {j:j,i:i};
}
}
}
return {j:-1,i:-1};
},getNode:function(_9a,_9b,_9c){
var row=_9a&&_9a.rows[_9b];
return row&&row.cells[_9c];
},_findOverlappingNodes:function(_9e,_9f,_a0){
var _a1=[];
var m=this.getMapCoords(_9f,_a0);
var row=this.map[m.j];
for(var j=0,row;(row=this.map[j]);j++){
if(j==m.j){
continue;
}
var rw=row[m.i];
var n=(rw?this.getNode(_9e,rw.r,rw.c):null);
if(n){
_a1.push(n);
}
}
return _a1;
},findOverlappingNodes:function(_a7){
return this._findOverlappingNodes(_9(_a7),_4(_a7.parentNode),_2(_a7));
}});
})();
}
