







































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

mod.Util=imprt('ca.carleton.gcrc.dhtml.util');






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










publ.__init__=function(anElement)
{
if(null==anElement)
{
throw'BuilderCommon: An element must be specified at construction';
}

this.myDocument=anElement.ownerDocument;
}






publ.CurrentElement=function()
{
throw('Programming Error: BuilderCommon.CurrentElement() was called.');
}





publ.Document=function()
{
return this.myDocument;
}







publ.Style=function()
{
return mod.StyleBasedOn(this.CurrentElement());
}









publ.SetAttribute=function(attributeType,value)
{
this.CurrentElement().setAttribute(attributeType,value);
return this.CurrentElement();
}







publ.AssignClass=function(className_)
{
if(null==this.CurrentElement().className)
{
this.CurrentElement().className=className_;
}
else
{
this.CurrentElement().className+=' '+className_;
}
}







publ.Title=function(title_)
{
this.CurrentElement().title=title_;
}







publ.Alternate=function(title_)
{
this.CurrentElement().alt=title_;
}







publ.ID=function(id_)
{
this.CurrentElement().id=id_;
}
});










mod.StyleBasedOn=function(anElement)
{
var styleObj=new mod.Style(anElement);
return styleObj;
}






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






publ.__init__=function(anElement)
{





this.myElement=anElement;
}









publ.Cursor=function(cursorStyle)
{
if(null!=cursorStyle)
{
this.myElement.style.cursor=cursorStyle;
}
return this.myElement.style.cursor;
}









publ.Position=function(positionStyle)
{
if(null!=positionStyle)
{
this.myElement.style.position=positionStyle;
}
return this.myElement.style.position;
}









publ.Height=function(heightStyle)
{
if(null!=heightStyle)
{
this.myElement.style.height=heightStyle;
}
return this.myElement.style.height;
}









publ.Width=function(widthStyle)
{
if(null!=widthStyle)
{
this.myElement.style.width=widthStyle;
}
return this.myElement.style.width;
}









publ.Colour=function(colourStyle)
{
if(null!=colourStyle)
{
this.myElement.style.color=colourStyle;
}
return this.myElement.style.color;
}









publ.BgColour=function(backgroundColour)
{
if(null!=backgroundColour)
{
this.myElement.style.backgroundColor=backgroundColour;
}
return this.myElement.style.backgroundColor;
}









publ.BorderStyle=function(borderStyle)
{
if(null!=borderStyle)
{
this.myElement.style.borderStyle=borderStyle;
}
return this.myElement.style.borderStyle;
}









publ.BorderWidth=function(borderWidth)
{
if(null!=borderWidth)
{
this.myElement.style.borderWidth=borderWidth;
}
return this.myElement.style.borderWidth;
}









publ.BorderColour=function(borderColour)
{
if(null!=borderColour)
{
this.myElement.style.borderColor=borderColour;
}
return this.myElement.style.borderColor;
}









publ.Align=function(alignStyle)
{
if(null!=alignStyle)
{
this.myElement.style.textAlign=alignStyle;
}
return this.myElement.style.textAlign;
}









publ.VerticalAlign=function(alignStyle)
{
if(null!=alignStyle)
{
this.myElement.style.verticalAlign=alignStyle;
}
return this.myElement.style.verticalAlign;
}
});






mod.MoveHandlerBasedOn=function(movingElement)
{
if(movingElement==undefined||movingElement==null)
{
alert('Error while creating an instance of MoveHandler.');
return;
}

var moveHandler=movingElement.comCotiDHTML1MoveHandler;

if(moveHandler==undefined)
{
moveHandler=new mod.MoveHandler(movingElement);
}

return moveHandler;
}







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




publ.__init__=function(movingElement)
{

this.myDocument=movingElement.ownerDocument;
this.myMovingElement=movingElement;


this.movingFlag=false;

this.MovingElement().style.position='absolute';
this.MovingElement().onselectstart=function(){return false;}
}


publ.MovingElement=function()
{
return this.myMovingElement;
}


publ.Document=function()
{
return this.myDocument;
}


publ.AddControlElement=function(aControlElement)
{
var receiver=this;


aControlElement.style.cursor='move';
aControlElement.addEventListener('mousedown',this,false);
}



publ.StartMoving=function(event)
{

if(this.movingFlag==false)
{

var eventObj=new mod.Util.Event(event);
var pageCoord=eventObj.PageCoordinate();
this.startMouseOffsetLeft=pageCoord.x;
this.startMouseOffsetTop=pageCoord.y;
this.startElementOffsetLeft=parseInt(this.myMovingElement.style.left);
this.startElementOffsetTop=parseInt(this.myMovingElement.style.top);


this.myDocument.addEventListener('mousemove',this,false);
this.myDocument.addEventListener('mouseup',this,false);

this.movingFlag=true;
}
}



publ.handleEvent=function(event_)
{

var type=event_.type

if('mousedown'==type){
this.StartMoving(event_);
}else if('mousemove'==type){
this.Moving(event_);
}else if('mouseup'==type){
this.EndMoving(event_);
}
}



publ.Moving=function(event)
{
var eventObj=new mod.Util.Event(event);
var pageCoord=eventObj.PageCoordinate();



this.myMovingElement.style.left=
(this.startElementOffsetLeft
+pageCoord.x
-this.startMouseOffsetLeft)+'px';
this.myMovingElement.style.top=
(this.startElementOffsetTop
+pageCoord.y
-this.startMouseOffsetTop)+'px';
}



publ.EndMoving=function(event)
{
this.myDocument.removeEventListener('mousemove',this,false);
this.myDocument.removeEventListener('mouseup',this,false);

this.movingFlag=false;
}
});





mod.Builder=Class(mod.BuilderCommon,function(publ,priv,supr){



publ.__init__=function(topObject)
{

supr.__init__.call(this,topObject);


this.topObject=topObject;
this.definedElement=topObject;
this.stack=new Array;
}




publ.CurrentElement=function()
{
return this.definedElement;
}





publ.AddTable=function()
{
var tableBuilder=new mod.TableBuilder(this.CurrentElement());
return tableBuilder;
}


publ.AddImage=function(imgSrc)
{
var builder=new mod.ImageBuilder(this.CurrentElement(),imgSrc);
return builder;
}


publ.AddButton=function(text,func_)
{
var buttonBuilder=new mod.ButtonBuilder(this.CurrentElement(),text,func_);
return buttonBuilder;
}


publ.AddImageButton=function(imgSrc,func_)
{
var imageButtonBuilder=new mod.ImageButtonBuilder(this.CurrentElement(),imgSrc,func_);
return imageButtonBuilder;
}


publ.Add=function(elementType)
{
var element=this.Document().createElement(elementType);

this.CurrentElement().appendChild(element);

return element;
}


publ.AddText=function(text)
{
var element=this.Document().createTextNode(text);

this.CurrentElement().appendChild(element);

return element;
}


publ.AddInput=function(type,name,value,checked)
{
var element;
if(this.Document().all)
{
var checkedString='';
if(checked)
{
checkedString='CHECKED';
}
element=this.Document().createElement('<input type="'+type
+'" name="'+name
+'" value="'+value+'" '+checkedString+'/>');
}
else
{
element=this.Document().createElement('input');
element.type=type;
element.name=name;
if(null!=value)
{
element.value=value;
}
if(checked)
{
element.checked=true;
}
}

this.CurrentElement().appendChild(element);

return element;
}


publ.AddInputText=function(size_,value_)
{
return new mod.InputTextBuilder(this.CurrentElement(),size_,value_);
}


publ.AddSwappableContent=function()
{
return new mod.SwappableContentBuilder(this.CurrentElement());
}


publ.AddVerticalFlowManager=function()
{
return new mod.FlowManagerVertical(this.CurrentElement());
}


publ.AddHorizontalFlowManager=function()
{
return new mod.FlowManagerHorizontal(this.CurrentElement());
}


publ.AddSelectionList=function()
{
return new mod.SelectionList(this.CurrentElement());
}


publ.Push=function(elementType)
{
var element=this.Add(elementType);



this.stack.push(this.CurrentElement());
this.definedElement=element;

return element;
}


publ.Pop=function()
{
if(this.CurrentElement()!=this.topObject)
{
this.definedElement=this.stack.pop();
}

return this.CurrentElement();
}
});





mod.TableBuilder=Class(mod.BuilderCommon,function(publ,priv,supr){

publ.__init__=function(parentObject)
{

supr.__init__.call(this,parentObject);


this.parentObject=parentObject;


this.tableElement=this.Document().createElement('table');
parentObject.appendChild(this.tableElement);
this.tbodyElement=this.Document().createElement('tbody');
this.tableElement.appendChild(this.tbodyElement);


this.rowElements=new Array;
this.dataElements=new Array;
}




publ.CurrentElement=function()
{
return this.tableElement;
}


publ.GetRowElement=function(index)
{
var rowCount=this.rowElements.length;


if(index>=rowCount)
{

while(index>=rowCount)
{
var trElement=this.Document().createElement('tr');
this.tbodyElement.appendChild(trElement);
this.rowElements.push(trElement);
this.dataElements.push(new Array);
rowCount=this.rowElements.length;
}
}

return this.rowElements[index];
}


publ._InsertCellElement=function(rowIndex,cellIndex,tdElement,insertFunc)
{


var rowElement=this.GetRowElement(rowIndex);


var dataArray=this.dataElements[rowIndex];


if(cellIndex>=dataArray.length)
{

while(cellIndex>dataArray.length)
{
var td=this.Document().createElement('td');
td.comCotiNullTd=1;
rowElement.appendChild(td);
dataArray.push(td);
}


if('insert'==insertFunc)
{
rowElement.appendChild(tdElement);
}
dataArray.push(tdElement);
}
else
{
var currentElement=dataArray[cellIndex];



if(currentElement.comCotiNullTd)
{
if('insert'==insertFunc)
{
rowElement.replaceChild(tdElement,currentElement);
}
else
{
rowElement.removeChild(currentElement);
}
dataArray[cellIndex]=tdElement;
}
}


return dataArray[cellIndex];
}


publ._GetCellElement=function(rowIndex,cellIndex,rowSpan,colSpan,label)
{

if(!rowSpan)
{
rowSpan=1;
}
if(!colSpan)
{
colSpan=1;
}


var tdElement=this.Document().createElement(label);
if(1!=rowSpan)
{
tdElement.rowSpan=''+rowSpan;
}
if(1!=colSpan)
{
tdElement.colSpan=''+colSpan;
}


var rowLoop;
var colLoop;
var insertFunc;
for(rowLoop=0;rowLoop<rowSpan;++rowLoop)
{
for(colLoop=0;colLoop<colSpan;++colLoop)
{
if(0==rowLoop&&0==colLoop)
{
insertFunc='insert';
}
else
{
insertFunc='hide';
}
this._InsertCellElement(rowIndex+rowLoop,cellIndex+colLoop,tdElement,insertFunc);
}
}


return tdElement;
}


publ.GetDataElement=function(rowIndex,dataIndex,rowSpan,colSpan)
{
return this._GetCellElement(rowIndex,dataIndex,rowSpan,colSpan,'td');
}


publ.GetHeaderElement=function(rowIndex,dataIndex,rowSpan,colSpan)
{
return this._GetCellElement(rowIndex,dataIndex,rowSpan,colSpan,'th');
}


publ.GetData=function(rowIndex,dataIndex,rowSpan,colSpan)
{
var tdElement=this.GetDataElement(rowIndex,dataIndex,rowSpan,colSpan);
var builder=new mod.Builder(tdElement);
return builder;
}


publ.GetHeader=function(rowIndex,dataIndex,rowSpan,colSpan)
{
var tdElement=this.GetHeaderElement(rowIndex,dataIndex,rowSpan,colSpan);
var builder=new mod.Builder(tdElement);
return builder;
}


publ.Border=function(pixels)
{
this.CurrentElement().border=pixels;
}


publ.Width=function(width)
{
this.CurrentElement().width=width;
}


publ.CellSpacing=function(size)
{
this.CurrentElement().cellSpacing=size;
}


publ.CellPadding=function(size)
{
this.CurrentElement().cellPadding=size;
}


publ.Summary=function(text)
{
this.CurrentElement().setAttribute('summary',text);
}


publ.Rules=function(text)
{
this.CurrentElement().rules=text;
}
});





mod.ImageBuilder=Class(mod.BuilderCommon,function(publ,priv,supr){


publ.__init__=function(parentObject,imgSrc)
{

supr.__init__.call(this,parentObject);


this.element=this.Document().createElement('img');


this.element.src=imgSrc;


parentObject.appendChild(this.element);
}




publ.CurrentElement=function()
{
return this.element;
}
});





mod.ButtonBuilder=Class(mod.BuilderCommon,function(publ,priv,supr){


publ.__init__=function(parentObject,text,func_)
{

supr.__init__.call(this,parentObject);


this.element=this.Document().createElement('input');


this.element.type='button';
this.element.value=text;
this.element.onclick=func_;


parentObject.appendChild(this.element);
}




publ.CurrentElement=function()
{
return this.element;
}


publ.Enabled=function(flag_)
{
this.element.disabled=(flag_==false);
return(this.element.disabled!=true);
}
});





mod.ImageButtonBuilder=Class(mod.BuilderCommon,function(publ,priv,supr){


publ.__init__=function(parentObject,imgSrc,func_)
{

supr.__init__.call(this,parentObject);


this.element=this.Document().createElement('img');


this.element.src=imgSrc;
this.element.border=0;
this.mouseOutImage=new Image();
this.mouseOutImage.src=imgSrc;


this.onClickFunction=func_;

var receiver=this;
this.element.onclick=function(){
receiver.OnClickHandler();
}
this.element.onmouseout=function(){
receiver.OnMouseOutHandler();
}


parentObject.appendChild(this.element);
}




publ.CurrentElement=function()
{
return this.element;
}


publ.OnMouseOverImage=function(imgSrc)
{

this.mouseOverImage=new Image();
this.mouseOverImage.src=imgSrc;

var receiver=this;
this.element.onmouseover=function(){
receiver.OnMouseOverHandler();
}
}


publ.OnMouseDownImage=function(imgSrc)
{

this.mouseDownImage=new Image();
this.mouseDownImage.src=imgSrc;

var receiver=this;
this.element.onmousedown=function(){
receiver.OnMouseDownHandler();
}
}


publ.OnClickHandler=function()
{

element.src=this.mouseOutImage.src;


this.onClickFunction();
}


publ.OnMouseOutHandler=function(data,element,event)
{

element.src=this.mouseOutImage.src;
}


publ.OnMouseOverHandler=function(data,element,event)
{

element.src=this.mouseOverImage.src;
}


publ.OnMouseDownHandler=function(data,element,event)
{

element.src=this.mouseDownImage.src;
}
});





mod.InputBuilder=Class(mod.BuilderCommon,function(publ,priv,supr){


publ.__init__=function(parentObject)
{

supr.__init__.call(this,parentObject);
}



publ.Name=function(name_)
{
var element=this.CurrentElement();
if(null!=name_)
{
element.name=name_;
}
return element.name;
}
});





mod.InputTextBuilder=Class(mod.BuilderCommon,function(publ,priv,supr){


publ.__init__=function(parentObject,size_,value_)
{

supr.__init__.call(this,parentObject);


this.element=this.Document().createElement('input');
this.element.type='text';
if(null!=size_)
{
this.element.size=size_;
}
if(null!=value_)
{
this.element.value=value_;
}


parentObject.appendChild(this.element);
}




publ.CurrentElement=function()
{
return this.element;
}



publ.Size=function(size_)
{
if(null!=size_)
{
this.element.size=size_;
}
return this.element.size;
}



publ.Value=function(text_)
{
if(null!=text_)
{
this.element.value=text_;
}
return this.element.value;
}



publ.OnKeyUp=function(onClickContextObject,onClickFunction,onClickData)
{
this.EventHandler().OnKeyUp(onClickContextObject,onClickFunction,onClickData);
}
});







mod.SwappableContentBuilder=Class(mod.BuilderCommon,function(publ,priv,supr){


publ.__init__=function(parentObject)
{

supr.__init__.call(this,parentObject);



this.divElement=this.Document().createElement('div');
parentObject.appendChild(this.divElement);



this.contents=new Array;
this.GetBuilderForView(0);
this.InstallView(0);
}




publ.CurrentElement=function()
{
return this.divElement;
}



publ.GetBuilderForView=function(index)
{

var divElement=this.contents[index];

if(undefined==divElement)
{

divElement=this.Document().createElement('div');


this.contents[index]=divElement;
}


var builder=new mod.Builder(divElement);

return builder;
}



publ.InstallView=function(index)
{

var newDivElement=this.contents[index];


if(undefined==newDivElement)
{
alert('Attempting to install a view with invalid index '+index);
}
else
{

var oldDivElement=this.currentDiv;
if(undefined!=oldDivElement)
{
this.divElement.removeChild(oldDivElement);
}


this.divElement.appendChild(newDivElement);
this.currentDiv=newDivElement;
}
}
});






mod.FlowManager=Class(mod.BuilderCommon,function(publ,priv,supr){


publ.__init__=function(parentObject)
{

supr.__init__.call(this,parentObject);
}

});








mod.FlowManagerVertical=Class(mod.FlowManager,function(publ,priv,supr){


publ.__init__=function(parentObject)
{

supr.__init__.call(this,parentObject);


this.tableBuilder=new mod.TableBuilder(parentObject);
this.tableBuilder.Border('0');
this.tableBuilder.CellSpacing('0');
this.tableBuilder.CellPadding('0')


this.rowIndex=0;
}




publ.CurrentElement=function()
{
return this.tableBuilder.GetDataElement(this.rowIndex,0);
}




publ.Next=function()
{
var builder=new mod.Builder(this.CurrentElement());
++this.rowIndex;
return builder;
}
});








mod.FlowManagerHorizontal=Class(mod.FlowManager,function(publ,priv,supr){


publ.__init__=function(parentObject)
{

supr.__init__.call(this,parentObject);


this.tableBuilder=new mod.TableBuilder(parentObject);
this.tableBuilder.Border('0');
this.tableBuilder.CellSpacing('0');
this.tableBuilder.CellPadding('0')


this.colIndex=0;
}




publ.CurrentElement=function()
{
return this.tableBuilder.GetDataElement(0,this.colIndex);
}




publ.Next=function()
{
var builder=new mod.Builder(this.CurrentElement());
++this.colIndex;
return builder;
}
});





mod.SelectionList=Class(mod.BuilderCommon,function(publ,priv,supr){


publ.__init__=function(parentObject)
{

supr.__init__.call(this,parentObject);


this.element=this.Document().createElement('select');


parentObject.appendChild(this.element);


var receiver=this;
this.element.onchange=function(event_){
receiver._Changed(event_);
};
this.element.onkeyup=function(event_){
receiver._Changed(event_);
};


this.options=new Array();

this.selectedData=null;


this.onChangeObservers=new Array();
}




publ.CurrentElement=function()
{
return this.element;
}




publ.SelectedData=function()
{
return this.selectedData;
}



publ.AddOption=function(data,text,selected)
{
var optionElement=this.Document().createElement('option');
if(true==selected)
{
optionElement.selected='selected';
this.selectedData=data;
}
var textNode=this.Document().createTextNode(text);
optionElement.appendChild(textNode);
this.element.appendChild(optionElement);

var option={};
option.data=data;
option.text=text;
option.element=optionElement;
this.options.push(option);
}



publ.SelectOption=function(data)
{
var loop;
for(loop=0;loop<this.options.length;++loop)
{
var option=this.options[loop];

if(option.data==data)
{
option.element.selected='selected';
}
else
{
option.element.selected='';
}
}
}




publ.SelectOptionFromIndex=function(index_)
{
var loop;
for(loop=0;loop<this.options.length;++loop)
{
var option=this.options[loop];

if(loop==index_)
{
option.element.selected='selected';
}
else
{
option.element.selected='';
}
}
}



publ.OnChange=function(func_)
{
this.onChangeObservers.push(func_);
}




publ._Changed=function(event_)
{

var index=this.element.selectedIndex;
this.selectedData=this.options[index].data;


var loop;
for(loop=0;loop<this.onChangeObservers.length;++loop)
{
var func=this.onChangeObservers[loop];
func(event_,this);
}
}
});

});