







































Module('ca.carleton.gcrc.dhtml.inspector','$Revision',function(mod){

mod.Window=imprt('ca.carleton.gcrc.dhtml.window');





mod.Inspector=Class(mod.Window.Window,function(publ,priv,supr){







publ.__init__=function(anObject,aDocument)
{

if(!aDocument){
if(anObject.ownerDocument){
aDocument=anObject.ownerDocument;
}else{
aDocument=document;
}
}


var windowParameters=new mod.Window.WindowParameters();
windowParameters.ownerDocument=aDocument;
windowParameters.border=windowParameters.BORDER_NONE;
windowParameters.windowBar=windowParameters.WINDOWBAR_SIMPLE;


supr.__init__.call(this,windowParameters);


this.inspectedObjects=new Array();
this.inspectedObjects.push(anObject);
this.currentlyInspectedIndex=0;

var DhtmlBuilder=imprt('ca.carleton.gcrc.dhtml.builder');
var builder=new DhtmlBuilder.Builder(this.ContentElement());
var tableBuilder=builder.AddTable();
tableBuilder.Border(0);
tableBuilder.CellSpacing(0);
tableBuilder.CellPadding(0);


var receiver=this;
var buttonBarElement=tableBuilder.GetDataElement(0,0);
buttonBarElement.style.backgroundColor='#dddddd';
tempBuilder=new DhtmlBuilder.Builder(buttonBarElement);
this.backButton=tempBuilder.AddButton('<-',function(){
receiver.Back();
});
this.forwardButton=tempBuilder.AddButton('->',function(){
receiver.Forward();
});


this.contentDivBuilder=tableBuilder.GetData(1,0);
this.contentDivBuilder.Push('div');
this.contentDivBuilder.CurrentElement().style.overflow='auto';
this.contentDivBuilder.CurrentElement().style.height='400px';
this.contentDivBuilder.CurrentElement().style.width='500px';
this.contentDivBuilder.Style().BgColour('gray');


this.DisplayObject();


this.MoveTo(0,0);
}



publ.InspectedObject=function()
{
return this.inspectedObjects[this.currentlyInspectedIndex];
}



publ.DisplayObject=function()
{

if(this.currentlyInspectedIndex==0)
{
this.backButton.CurrentElement().disabled=1;
}
else
{
this.backButton.CurrentElement().disabled=0;
}
if(this.currentlyInspectedIndex>=this.inspectedObjects.length-1)
{
this.forwardButton.CurrentElement().disabled=1;
}
else
{
this.forwardButton.CurrentElement().disabled=0;
}


var contentElement=this.contentDivBuilder.CurrentElement();
var children=contentElement.childNodes;
while(children.length>0)
{
var loop;
for(loop=0;loop<children.length;++loop)
{
contentElement.removeChild(children.item(loop));
}
children=contentElement.childNodes;
}


var tableBuilder=this.contentDivBuilder.AddTable();
tableBuilder.Border(1);
tableBuilder.CellSpacing(1);
tableBuilder.Width('100%');


this.currentDisplayRow=0;


var inspectedObject=this.InspectedObject();
var isObjectDHTML=false;
try
{
for(property in inspectedObject){}
isObjectDHTML=true;
}
catch(e)
{
isObjectDHTML=false;
}

if(true==isObjectDHTML)
{
this._DisplayDHTMLObject(inspectedObject,tableBuilder);
}
else
{

this._DisplayDOMObject(inspectedObject,tableBuilder);
}


if(this.currentDisplayRow==0)
{
this.contentDivBuilder.AddText('Empty');
}
}



publ._DisplayDHTMLObject=function(inspectedObject_,tableBuilder_)
{

var properties=new Array();
var property;
var propCount=0;
for(property in inspectedObject_)
{
if(++propCount>500)
{
break;
}

properties.push(property);
}


properties.sort(mod.PropertySort);


var loop;
for(loop=0;loop<properties.length;++loop)
{
var propertyName=properties[loop];

this._DisplayDHTMLProperty(inspectedObject_,propertyName,tableBuilder_);
}
}



publ._DisplayDHTMLProperty=function(inspectedObject_,aPropertyName_,tableBuilder_)
{
var receiver=this;

var row=this.currentDisplayRow;


this.AddString(tableBuilder_.GetData(row,0),aPropertyName_);


var aProperty;
var propertyType='';
var propertyAvailable=false;
try
{
aProperty=inspectedObject_[aPropertyName_];
propertyType=typeof aProperty;
propertyAvailable=true;
}
catch(e)
{
aProperty=e;
}


if(propertyAvailable)
{
tableBuilder_.GetData(row,1).AddText(propertyType);
}
else
{
tableBuilder_.GetData(row,1).AddText('ERROR');
tableBuilder_.GetData(row,1).Style().BgColour('red');
}


if(false==propertyAvailable)
{

var receiver=this;
var button=tableBuilder_.GetData(row,2).AddButton('Show Error',function(){
receiver.ShowError(aProperty);
});
}
else if(propertyType=='function')
{

var receiver=this;
var button=tableBuilder_.GetData(row,2).AddButton('Show Function',function(){
receiver.ShowFunction(aProperty);
});
}
else if(propertyType=='object')
{

var cellBuilder=tableBuilder_.GetData(row,2);
var button=cellBuilder.AddButton('Inspect',function(){
receiver.ChangeFocus(aProperty);
});
if(null!=aProperty)
{
cellBuilder.AddText(''+aProperty.toString());
}
else
{
cellBuilder.AddText('null');
}
}
else if(propertyType=='string')
{
this.AddString(tableBuilder_.GetData(row,2),aProperty);
}
else
{

tableBuilder_.GetData(row,2).AddText(''+aProperty);
}

++this.currentDisplayRow;
}



publ._DisplayDOMObject=function(inspectedObject_,tableBuilder_)
{

this.AddString(tableBuilder_.GetData(this.currentDisplayRow,1),'nodeType');
this.AddString(tableBuilder_.GetData(this.currentDisplayRow,2),this.StringFromNodeType(inspectedObject_.nodeType));
++this.currentDisplayRow;


this.AddString(tableBuilder_.GetData(this.currentDisplayRow,1),'nodeName');
this.AddString(tableBuilder_.GetData(this.currentDisplayRow,2),inspectedObject_.nodeName);
++this.currentDisplayRow;


this.AddString(tableBuilder_.GetData(this.currentDisplayRow,1),'localName');
this.AddString(tableBuilder_.GetData(this.currentDisplayRow,2),inspectedObject_.localName);
++this.currentDisplayRow;


this.AddString(tableBuilder_.GetData(this.currentDisplayRow,1),'namespaceURI');
this.AddString(tableBuilder_.GetData(this.currentDisplayRow,2),inspectedObject_.namespaceURI);
++this.currentDisplayRow;


this.AddString(tableBuilder_.GetData(this.currentDisplayRow,1),'prefix');
this.AddString(tableBuilder_.GetData(this.currentDisplayRow,2),inspectedObject_.prefix);
++this.currentDisplayRow;


var properties=new Array();
var attributeNamedMap=inspectedObject_.attributes;
var attrCount=attributeNamedMap.length;
var loop;
for(loop=0;loop<attrCount;++loop)
{
var attributeNode=attributeNamedMap.item(loop);
properties.push(attributeNode.nodeName);
}


properties.sort(mod.PropertySort);


var loop;
for(loop=0;loop<properties.length;++loop)
{
var propertyName=properties[loop];

var row=this.currentDisplayRow;


this.AddString(tableBuilder_.GetData(row,0),propertyName);


var attributeNamedMap=inspectedObject_.attributes;
var attributeNode=attributeNamedMap.getNamedItem(propertyName);


tableBuilder_.GetData(row,1).AddText('attribute');


this.AddString(tableBuilder_.GetData(row,2),attributeNode.nodeValue);

++this.currentDisplayRow;
}


var childNodeList=inspectedObject_.childNodes;
for(loop=0;loop<childNodeList.length;++loop)
{
var childNode=childNodeList.item(loop);
this._DisplayDOMChild(childNode);
}
}

publ._DisplayDOMChild=function(inspectedObject_,childNode,tableBuilder_)
{
var receiver=this;
var row=this.currentDisplayRow;


this.AddString(tableBuilder_.GetData(row,0),loop);


var nodeType=childNode.nodeType;
var nodeTypeString=this.StringFromNodeType(nodeType);
this.AddString(tableBuilder_.GetData(row,1),nodeTypeString);


if(1==nodeType)
{

var button=tableBuilder_.GetData(row,2).AddButton('Inspect '+childNode.nodeName,function(){
receiver.ChangeFocus(childNode);
});
}
else
{

var button=tableBuilder_.GetData(row,2).AddButton('Show Error',function(){
receiver.ShowError('Do not know how to handle node type: '+nodeTypeString);
});
}

++this.currentDisplayRow;
}


publ.AddString=function(aBuilder,aString)
{
var max=25;
var receiver=this;


if('number'==typeof aString)
{
aString=''+aString;
}

try
{
if(aString.length<max)
{
aBuilder.AddText(aString);
}
else
{
aBuilder.AddText(aString.substr(0,max-5));
var button=aBuilder.AddButton('...',function(){
receiver.ShowString(aString);
});
}
}
catch(e)
{
var button=aBuilder.AddButton('Show Error',function(){
receiver.ShowError(e+'\naString: '+aString);
});
}
}


publ.Back=function(data,element,event)
{
if(this.currentlyInspectedIndex>0)
{
--this.currentlyInspectedIndex;
this.DisplayObject();
}
}


publ.Forward=function(data,element,event)
{
if(this.currentlyInspectedIndex<this.inspectedObjects.length-1)
{
++this.currentlyInspectedIndex;
this.DisplayObject();
}
}


publ.ShowError=function(anError)
{
alert(''+anError);
}


publ.ShowFunction=function(aFunction)
{
alert(''+aFunction);
}


publ.ShowString=function(aString)
{
alert(''+aString);
}


publ.ChangeFocus=function(anObject)
{
++this.currentlyInspectedIndex;
if(this.inspectedObjects.length>this.currentlyInspectedIndex)
{
this.inspectedObjects.splice(
this.currentlyInspectedIndex
,this.inspectedObjects.length-this.currentlyInspectedIndex
);
}
this.inspectedObjects[this.currentlyInspectedIndex]=anObject;
this.DisplayObject();
}


publ.StringFromNodeType=function(domNodeType_)
{
switch(domNodeType_)
{
case 1:return'ELEMENT_NODE';
case 2:return'ATTRIBUTE_NODE';
case 3:return'TEXT_NODE';
case 4:return'CDATA_SECTION_NODE';
case 5:return'ENTITY_REFERENCE_NODE';
case 6:return'ENTITY_NODE';
case 7:return'PROCESSING_INSTRUCTION_NODE';
case 8:return'COMMENT_NODE';
case 9:return'DOCUMENT_NODE';
case 10:return'DOCUMENT_TYPE_NODE';
case 11:return'DOCUMENT_FRAGMENT_NODE';
case 12:return'NOTATION_NODE';
}

return'unknown('+nodeType+')';
}
});

mod.NumberRegex=/^[0-9]+$/;

mod.PropertySort=function(a,b)
{
var isNumA=a.match(mod.NumberRegex);
var isNumB=b.match(mod.NumberRegex);

if(isNumA&&isNumB)
{
var aVal=parseInt(a);
var bVal=parseInt(b);
return aVal-bVal;
}
else if(isNumA)
{
return-1;
}
else if(isNumB)
{
return 1;
}
else if(a>b)
{
return 1;
}
else if(a<b)
{
return-1;
}

return 0;
}
});