






































Module('ca.carleton.gcrc.atlas.topic','$Revision: 3225 $',function(mod){

var modLayoutUtil=imprt('ca.carleton.gcrc.atlas.layout.util');
var modWidgetsSimple=imprt('ca.carleton.gcrc.atlas.widgets.simple');
var modTabWidget=imprt('ca.carleton.gcrc.atlas.widgets.tab');









mod.Factory=Class(function(publ,priv){



publ.__init__=function(){
this.lowQuadrantWidgets=new Array();
this.leftQuadrantWidgets=new Array();
this.rightQuadrantWidgets=new Array();
this.centerQuadrantWidgets=new Array();
this.invisibleWidgets=new Array();
}






publ.AddLowQuadrantWidget=function(widgetFactory_){
this.lowQuadrantWidgets.push(widgetFactory_);
}






publ.AddLeftQuadrantWidget=function(widgetFactory_){
this.leftQuadrantWidgets.push(widgetFactory_);
}






publ.AddRightQuadrantWidget=function(widgetFactory_){
this.rightQuadrantWidgets.push(widgetFactory_);
}






publ.AddCenterQuadrantWidget=function(widgetFactory_){
this.centerQuadrantWidgets.push(widgetFactory_);
}






publ.AddInvisibleWidget=function(widgetFactory_){
this.invisibleWidgets.push(widgetFactory_);
}




















publ.Create=function(
svgParent_,
svgX_,
svgY_,
svgWidth_,
svgHeight_,
htmlParent_,
htmlX_,
htmlY_,
htmlWidth_,
htmlHeight_,
dataModel_
)
{
return new mod.Topic(
this,
svgParent_,
svgX_,
svgY_,
svgWidth_,
svgHeight_,
htmlParent_,
htmlX_,
htmlY_,
htmlWidth_,
htmlHeight_,
dataModel_
);
}

});






mod.Topic=Class(function(publ,priv){
















publ.__init__=function(
factory_,
svgParent_,
svgX_,
svgY_,
svgWidth_,
svgHeight_,
htmlParent_,
htmlX_,
htmlY_,
htmlWidth_,
htmlHeight_,
dataModel_
){
this.widgets=new Array();

this.factory=factory_;

this.svgParent=svgParent_;
this.svgX=svgX_;
this.svgY=svgY_;
this.svgWidth=svgWidth_;
this.svgHeight=svgHeight_;

this.htmlParent=htmlParent_;
this.htmlX=htmlX_;
this.htmlY=htmlY_;
this.htmlWidth=htmlWidth_;
this.htmlHeight=htmlHeight_;

this.dataModel=dataModel_;

this.CreateLayout();
}

publ.Remove=function(){

while(this.widgets.length>0){
var widget=this.widgets.pop();
widget.Remove();
}
}

publ.CreateLayout=function(){

var receiver=this;
var simpleLayout=new modLayoutUtil.SimpleLayout(function(widgetFactory_,area_){
receiver.CreateWidgetInArea(widgetFactory_,area_);
});



var currentArea=new modLayoutUtil.Area(
this.svgX,
this.svgY,
this.svgWidth,
this.svgHeight,
this.htmlX,
this.htmlY,
this.htmlWidth,
this.htmlHeight
);














if(this.factory.lowQuadrantWidgets.length>0){

var maxNumberOfRows=2;
var rows=(this.factory.lowQuadrantWidgets.length<maxNumberOfRows)
?this.factory.lowQuadrantWidgets.length
:maxNumberOfRows;

var lowQuadrantArea=currentArea.RemoveBottomPortion(0.1*rows);


this.CreateQuadrantBorder(lowQuadrantArea);


simpleLayout.TileWidgetsMaxRows(this.factory.lowQuadrantWidgets,lowQuadrantArea,maxNumberOfRows);
}



if(this.factory.leftQuadrantWidgets.length>0){


var leftQuadrantArea;
if(0==this.factory.rightQuadrantWidgets.length&&0==this.factory.centerQuadrantWidgets.length){
leftQuadrantArea=currentArea;

}else{
leftQuadrantArea=currentArea.RemoveRightPortion(0.2);
}


this.CreateQuadrantBorder(leftQuadrantArea);

simpleLayout.StackWidgetsVertical(this.factory.leftQuadrantWidgets,leftQuadrantArea);
}



if(this.factory.rightQuadrantWidgets.length>0){


var rightQuadrantArea;
if(0==this.factory.centerQuadrantWidgets.length){
rightQuadrantArea=currentArea;
}else{
rightQuadrantArea=currentArea.RemoveRightPortion(0.41);
}


this.CreateQuadrantBorder(rightQuadrantArea);

simpleLayout.StackWidgetsVertical(this.factory.rightQuadrantWidgets,rightQuadrantArea);
}



if(this.factory.centerQuadrantWidgets.length>0){

this.CreateQuadrantBorder(currentArea);

simpleLayout.DistributeWidgetsSmart(this.factory.centerQuadrantWidgets,currentArea);
}



{
var widgetFactories=this.factory.invisibleWidgets;
var count=widgetFactories.length;
var loop;
for(loop=0;loop<count;++loop){
var widgetFactory=widgetFactories[loop];
this.CreateWidget(
widgetFactory,
null,
0,
0,
0,
0);
}
}
}

publ.CreateWidgetInArea=function(
widgetFactory_
,area_
){
var area=area_.AddBorders(8,8,8,8);

if(widgetFactory_.IsSVG()){
this.CreateWidget(
widgetFactory_,
this.svgParent,
area.svgX,
area.svgY,
area.svgWidth,
area.svgHeight
);
}else if(widgetFactory_.IsHTML()){
this.CreateWidget(
widgetFactory_,
this.htmlParent,
area.htmlX,
area.htmlY,
area.htmlWidth,
area.htmlHeight
);
}else{
throw'Widget factory does not support isSVG() nor isHTML()';
}
}

publ.CreateWidget=function(
widgetFactory_
,parentElement_
,x_
,y_
,width_
,height_
){
var widget=widgetFactory_.Create(
parentElement_,
this.dataModel,
x_,
y_,
width_,
height_,
this
);
this.widgets.push(widget);
}

publ.CreateQuadrantBorder=function(area_){

var area=area_.AddBorders(4,4,4,4);


var widget=new modWidgetsSimple.BorderWidget(
this.svgParent,
area.svgX,
area.svgY,
area.svgWidth,
area.svgHeight
);


this.widgets.push(widget);
}
});






mod.TopicDisplay=Class(function(publ,priv){



publ.__init__=function(
dataModel_
){
this.dataModel=dataModel_;

this.ready=false;

this.lastSelectedTopic=null;
this.topicStates={};

this.lastSelectedTemplate=null;
this.templateStates={};

this.factoriesByTopicId={};

this.controlFeature=this.dataModel.LayerFromName('_global').FeatureFromId('topic');

this.controlFeature.AddObserver('currentTopic',this,this.TopicSelected,null);
}





publ.TopicSelected=function(mvcEvent_){
var topicId=mvcEvent_.data;

this.currentTopicLabel=topicId;

this._TryTopic();
}







publ.GetTopicFactory=function(topicLabel_){
var factory=this.factoriesByTopicId[topicLabel_];

if(null==factory){
factory=new mod.Factory();
this.factoriesByTopicId[topicLabel_]=factory;
}

return factory;
}





publ._TryTopic=function(){

if(null==this.svgParent||null==this.htmlParent||null==this.currentTopicLabel||false==this.ready){

return;
}

if(null!=this.lastTopic){
this.lastTopic.Remove();
this.lastTopic=null;
}

var factory=this.GetTopicFactory(this.currentTopicLabel);
this.lastTopic=factory.Create(
this.svgParent,
this.svgX,
this.svgY,
this.svgWidth,
this.svgHeight,
this.htmlParent,
this.htmlX,
this.htmlY,
this.htmlWidth,
this.htmlHeight,
this.dataModel
);
}

publ.SetSvgParameters=function(
parent_,
x_,
y_,
width_,
height_
){
this.svgParent=parent_;
this.svgX=x_;
this.svgY=y_;
this.svgWidth=width_;
this.svgHeight=height_;

this._TryTopic();
}

publ.SetHtmlParameters=function(
parent_,
x_,
y_,
width_,
height_
){
this.htmlParent=parent_;
this.htmlX=x_;
this.htmlY=y_;
this.htmlWidth=width_;
this.htmlHeight=height_;

this._TryTopic();
}

publ.SetReady=function(
flag_
){
this.ready=flag_;

this._TryTopic();
}

});
});
