
















































































Module('ca.carleton.gcrc.models.data','$Revision: 3838 $',function(mod){

var modModel=imprt('ca.carleton.gcrc.mvc.model');

var g_nextAtomId=1;






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



publ.__init__=function(
){

supr.__init__.call(this);








this.layers=new Array();





this._layersByName={};













this._processingInProgressCount=0;
this.busy=false;
this.busyWaiters=new Array();


this.featuresInFocus=new Array();


this.featuresSelected=new Array();
}





publ.toString=function()
{
return'Model';
}






publ.Layers=function()
{
return this.layers;
}







publ.LayerFromName=function(layerName_)
{
if(null==this._layersByName[layerName_])
{
var layer=new mod.Layer(this,layerName_);
this.layers.push(layer);
this._layersByName[layerName_]=layer;


layer.AddAllEventObserver(this,this.RedirectEvent,null);

this.CallObservers('add_layer',layer);
}

return this._layersByName[layerName_];
}








publ.Feature=function(layerId_,featureId_)
{
var layer=this.LayerFromName(layerId_);
return layer.FeatureFromId(featureId_);
}




publ.SelectionOnInit=function(layer_,feature_)
{
var reportSelectionTimings=false;
var startTimeMs;

if(true==reportSelectionTimings)
{
startTimeMs=(new Date()).getTime();
}

this.StartProcessing();


this.SelectionOffInit();


layer_.SelectionOn(feature_);


this.UpdateDisplayState();

this.EndProcessing();

if(true==reportSelectionTimings)
{
var endTimeMs=(new Date()).getTime();
alert('Selection time was '+(endTimeMs-startTimeMs)+'ms.');
}
}




publ.SelectionOffInit=function()
{
var featuresToGoOff=this.featuresSelected;
this.featuresSelected=new Array();



var loop;
for(loop=0;loop<featuresToGoOff.length;++loop){
var feature=featuresToGoOff[loop];
feature.SelectionOff(true);
}


for(loop=0;loop<featuresToGoOff.length;++loop){
var feature=featuresToGoOff[loop];
feature.UpdateDisplayState();
}
for(loop=0;loop<this.featuresSelected.length;++loop){
var feature=this.featuresSelected[loop];
feature.UpdateDisplayState();
}
}




publ.FocusOnInit=function(layer_,feature_,mouseEvent_)
{

this.FocusOffInit();


layer_.FocusOn(feature_,mouseEvent_);


this.UpdateDisplayState();
}




publ.FocusOffInit=function()
{
var featuresToGoOff=this.featuresInFocus;
this.featuresInFocus=new Array();



var loop;
for(loop=0;loop<featuresToGoOff.length;++loop){
var feature=featuresToGoOff[loop];
feature.FocusOff(true);
}


for(loop=0;loop<featuresToGoOff.length;++loop){
var feature=featuresToGoOff[loop];
feature.UpdateDisplayState();
}
for(loop=0;loop<this.featuresInFocus.length;++loop){
var feature=this.featuresInFocus[loop];
feature.UpdateDisplayState();
}
}




publ.UpdateDisplayState=function()
{
var loop;
for(loop=0;loop<this.featuresSelected.length;++loop){
var feature=this.featuresSelected[loop];
feature.UpdateDisplayState();
}
for(loop=0;loop<this.featuresInFocus.length;++loop){
var feature=this.featuresInFocus[loop];
feature.UpdateDisplayState();
}
}
















publ.StartProcessing=function()
{
if(0==this._processingInProgressCount)
{
this.busy=true;
}
this._processingInProgressCount=this._processingInProgressCount+1;
}
















publ.EndProcessing=function()
{
if(this._processingInProgressCount>0)
{
this._processingInProgressCount=this._processingInProgressCount-1;
}

if(0==this._processingInProgressCount)
{
this.busy=false;


if(this.busyWaiters.length>0){
while(this.busyWaiters.length>0){

var waiter=this.busyWaiters.shift();
waiter();
}
}
}
}






publ.RegisterForEndProcessing=function(callbackFunction){

if(false==this.busy){
callbackFunction();
}else{
this.busyWaiters.push(callbackFunction);
}
}
});






mod.Layer=Class(modModel.Model,function(publ,priv,supr){






publ.__init__=function(
aMapModel_,
layerName_
){

supr.__init__.call(this);




this.mapModel=aMapModel_;




this.name=layerName_;





this._features=new Array();




this._featuresById={};








this.processingEvent=false;

this.listeningDatasources=new Array();
}





publ.toString=function()
{
return'Layer('+this.name+')';
}





publ.Features=function()
{
return this._features;
}







publ.FeaturesSorted=function(attributeName_)
{
var sortedFeatures=this._features.sort(
function(a,b)
{
var valueA=a.GetVariableValue(attributeName_,'');
var valueB=b.GetVariableValue(attributeName_,'');
if(valueA<valueB)return-1;
if(valueA>valueB)return 1;
return 0;
}
);

return sortedFeatures;
}






publ.FeatureFromId=function(id_)
{
if(null==this._featuresById[id_])
{
var feature=new mod.Feature(this,id_);

this._features.push(feature);
this._featuresById[id_]=feature;


feature.AddAllEventObserver(this,this.RedirectEvent,null);

this.CallObservers('add_feature',feature);
}

return this._featuresById[id_];
}





publ.SelectionOnInit=function(feature_)
{
this.mapModel.SelectionOnInit(this,feature_);
}





publ.SelectionOn=function(feature_)
{
feature_.SelectionOn(true);
}






publ.FocusOnInit=function(feature_,mouseEvent_)
{
this.mapModel.FocusOnInit(this,feature_,mouseEvent_);
}





publ.FocusOn=function(feature_,mouseEvent_)
{
feature_.FocusOn(true,mouseEvent_);
}

publ.AddListeningDatasource=function(ds_,uid_)
{
this.listeningDatasources.push({ds:ds_,uid:uid_});
}

publ.GetDatasources=function()
{
var dataSources=[];
for(var d=0;d<this.listeningDatasources.length;++d)
{
dataSources.push(this.listeningDatasources[d].ds);
}
return dataSources;
}

publ.GetDataSourcesInfo=function()
{
return this.listeningDatasources;
}




publ.IsEditable=function()
{
var editable=false;
for(var d=0;d<this.listeningDatasources.length;d++)
{
if(this.listeningDatasources[d].GetEditState().IsEditable())
{
editable=true;
break;
}
}
return editable;
}
});






mod.Feature=Class(modModel.Model,function(publ,priv,supr){






publ.__init__=function(
layerModel_,
featureId_
){

supr.__init__.call(this);




this.layerModel=layerModel_;




this.id=featureId_;





this._atoms=new Array();





this._variables={};




this._isSelected=false;




this._isInFocus=false;




this._initiatedLastSelection=false;










this.SetVariableValue('display_state',0);
}





publ.toString=function()
{
return'Feature('+this.id+')';
}





publ.Atoms=function()
{
return this._atoms;
}





publ.ImportAtoms=function(atoms_)
{
var loop;
for(loop=0;loop<atoms_.length;++loop)
{
this.ImportAtom(atoms_[loop]);
}
}





publ.ImportAtom=function(anAtom_)
{
this._atoms.push(anAtom_);
anAtom_.features.push(this);

this.CallObservers('add_atom',anAtom_);


var propertyNames=anAtom_.PropertyNames();
var loop;
for(loop=0;loop<propertyNames.length;++loop)
{
this.CallObservers(propertyNames[loop],anAtom_.PropertyValueFromName(propertyNames[loop]));
}
}





publ.RemoveAtom=function(anAtom_)
{
for(var loop=0;loop<this._atoms.length;++loop)
{
var atom=this._atoms[loop];
if(atom==anAtom_)
{
this._atoms.splice(loop,1);

this.CallObservers('remove_atom',anAtom_);


var propertyNames=anAtom_.PropertyNames();
var loop;
for(loop=0;loop<propertyNames.length;++loop)
{
this.CallObservers(
propertyNames[loop]
,this.GetVariableValue(propertyNames[loop])
);
}

break;
}
}
}





publ.GetVariableValue=function(variableName_,defaultValue_)
{
if(null!=this._variables[variableName_])
{
return this._variables[variableName_][0];
}


var loop;
for(loop=0;loop<this._atoms.length;++loop)
{
if(this._atoms[loop].HasPropertyWithName(variableName_))
{
return this._atoms[loop].PropertyValueFromName(variableName_);
}
}

return defaultValue_;
}





publ.GetVariableArray=function(variableName_)
{
if(null!=this._variables[variableName_])
{
return this._variables[variableName_];
}


var loop;
var result=new Array();
for(loop=0;loop<this._atoms.length;++loop)
{
if(this._atoms[loop].HasPropertyWithName(variableName_))
{
result.push(this._atoms[loop].PropertyValueFromName(variableName_));
}
}

return result;
}






publ.SetVariableValue=function(variableName_,value_)
{


if(null!=this._variables[variableName_]
&&1==this._variables[variableName_].length
&&value_==this._variables[variableName_][0]){

return;
}


this.layerModel.mapModel.StartProcessing();

this._variables[variableName_]=[value_];
this.CallObservers(variableName_,value_);


this.layerModel.mapModel.EndProcessing();
}






publ.SetVariableArray=function(variableName_,valueArray_)
{

this.layerModel.mapModel.StartProcessing();

this._variables[variableName_]=valueArray_;
this.CallObservers(variableName_,valueArray_);


this.layerModel.mapModel.EndProcessing();
}




publ.IsSelected=function()
{
return this._isSelected;
}




publ.IsInFocus=function()
{
return this._isInFocus;
}






publ.SelectionOn=function(originatedFromLayer_)
{
var updateCurrent=false;


if(false==this.layerModel.processingEvent)
{
this.layerModel.processingEvent=true;

var previousState=this._isSelected;
this._isSelected=true;


this.layerModel.mapModel.featuresSelected.push(this);

this.CallObservers('selection_state',this._isSelected);


if((false==previousState)&&originatedFromLayer_)
{
this._initiatedLastSelection=true;
this.CallObservers('selected');
updateCurrent=true;
}

this.layerModel.processingEvent=false;
}

if(true==updateCurrent){
var controlFeature=this.layerModel.mapModel.LayerFromName('_global').FeatureFromId('model');
controlFeature.SetVariableValue('last_selected',this);
controlFeature.SetVariableValue('last_selected_layer',this.layerModel.name);
controlFeature.SetVariableValue('last_selected_feature',this.id);
controlFeature.SetVariableValue('current_selected',this);
controlFeature.SetVariableValue('current_selected_layer',this.layerModel.name);
controlFeature.SetVariableValue('current_selected_feature',this.id);
}
}




publ.SelectionOff=function(inhibitEvent_)
{
var updateCurrent=this._initiatedLastSelection;


if(false==this.layerModel.processingEvent)
{
this.layerModel.processingEvent=true;

this._isSelected=false;
this._initiatedLastSelection=false;

if(true!=inhibitEvent_)
{
this.CallObservers('selection_state',this._isSelected);
}

this.layerModel.processingEvent=false;
}

if(true==updateCurrent){
var controlFeature=this.layerModel.mapModel.LayerFromName('_global').FeatureFromId('model');
controlFeature.SetVariableArray('current_selected',[]);
controlFeature.SetVariableArray('current_selected_layer',[]);
controlFeature.SetVariableArray('current_selected_feature',[]);
}
}








publ.FocusOn=function(originatedFromLayer_,mouseEvent_)
{

if(false==this.layerModel.processingEvent)
{
this.layerModel.processingEvent=true;

if(null!=mouseEvent_){
this.mouseEvent={};
for(var name in mouseEvent_){
this.mouseEvent[name]=mouseEvent_[name];
}
}

var previousState=this._isInFocus;
this._isInFocus=true;


this.layerModel.mapModel.featuresInFocus.push(this);

this.CallObservers('focus_state',this._isInFocus);


if((false==previousState)&&originatedFromLayer_)
{
this.CallObservers('infocus');
}

this.layerModel.processingEvent=false;
}
}




publ.FocusOff=function(inhibitEvent_)
{

if(false==this.layerModel.processingEvent)
{
this.layerModel.processingEvent=true;

this.mouseEvent=null;

var previousState=this._isInFocus;
this._isInFocus=false;

if(true!=inhibitEvent_)
{
this.CallObservers('focus_state',this._isInFocus);
}

this.layerModel.processingEvent=false;
}
}





publ.SelectionOnInit=function()
{
if(true==this._initiatedLastSelection)
{
this.SelectionOffInit();
}
else
{

this.layerModel.SelectionOnInit(this);
}
}





publ.SelectionOffInit=function()
{

this.layerModel.mapModel.SelectionOffInit();
}






publ.FocusOnInit=function(mouseEvent_)
{

this.layerModel.FocusOnInit(this,mouseEvent_);
}





publ.FocusOffInit=function()
{

this.layerModel.mapModel.FocusOffInit();
}





publ.UpdateDisplayState=function()
{
var state=0;

if(this.IsInFocus())
{
state|=1;
}

if(this.IsSelected())
{
state|=2;
}

this.SetVariableValue('display_state',state);
}
});








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



publ.__init__=function(
dataSource_
){



this.id=g_nextAtomId;
++(g_nextAtomId);




this.dataSource=dataSource_;





this.features=new Array();





this._properties={};





this._propertyNames=new Array();

}




publ.Unload=function()
{
for(var loop=0;loop<this.features.length;++loop)
{
var feature=this.features[loop];
feature.RemoveAtom(this);
}
this.features=new Array();
}






publ.AddProperty=function(name_,value_)
{
this._properties[name_]=value_;
this._propertyNames.push(name_);
}





publ.PropertyNames=function()
{
return this._propertyNames;
}







publ.PropertyValueFromName=function(name_)
{
return this._properties[name_];
}






publ.HasPropertyWithName=function(name_)
{
return('undefined'!=typeof this._properties[name_]);
}

});
});