







































Module('ca.carleton.gcrc.atlas.dataSources.simpleDataSource','$Revision: 3838 $',function(mod){

var modDataModel=imprt('ca.carleton.gcrc.models.data');
var modDataSource=imprt('ca.carleton.gcrc.atlas.dataSources.dataSource');
var modDomUtil=imprt('ca.carleton.gcrc.dom.util');
var modGui=imprt('ca.carleton.gcrc.atlas.guiFactory');

var ELEMENT_NODE=1;
var TEXT_NODE=3;





mod.SimpleDataSource=Class(modDataSource.DataSource,function(publ,priv,supr){





publ.__init__=function(
identifier_
,model_
){

supr.__init__.call(
this
,identifier_
,model_
);

this.importedContent=new Array();
}




publ.Load=function(
){

var atoms=new Array();
for(var loop=0;loop<this.importedContent.length;++loop){
var dic=this.importedContent[loop];

var atom=this.CreateAtomFromContentObject(dic);

atoms.push(atom);
}


this.AddAtoms(atoms);
}






publ.CreateAtomFromContentObject=function(
contentObject_
){

var atom=new modDataModel.Atom(this);

for(var key in contentObject_){
var value=contentObject_[key];
atom.AddProperty(key,value);
}


atom.contentObject=contentObject_;

return atom;
}





publ.ImportContent=function(
content_
){

this.importedContent=content_;
}





publ.IsEditSupported=function(
){
return true;
}







publ.EditReadForm=function(
formElem_
){

var newContent={};


for(var formChild=formElem_.firstChild;formChild!=null;formChild=formChild.nextSibling){
if(ELEMENT_NODE==formChild.nodeType
&&('div'==formChild.tagName||'DIV'==formChild.tagName)){


for(var divChild=formChild.firstChild;divChild!=null;divChild=divChild.nextSibling){
if(ELEMENT_NODE==divChild.nodeType){
if('input'==divChild.tagName||'INPUT'==divChild.tagName){


if('text'==divChild.getAttribute('type')){

var key=divChild.getAttribute('name');
var value=divChild.value;
newContent[key]=value;
}else if('file'==divChild.getAttribute('type')){

var key=divChild.getAttribute('name');
var value=divChild.value;



if(value==null||value==''){
var sib;
while((sib=divChild.previousSibling)!=null){
if(TEXT_NODE==sib.nodeType){

value=sib.nodeValue;
break;
}
}
}
newContent[key]=value;
}

}else if('textarea'==divChild.tagName||'TEXTAREA'==divChild.tagName){


var key=divChild.getAttribute('name');
var value=divChild.value;
newContent[key]=value;
}
}
}
}
}

return newContent;
}







publ.EditAddContentFromForm=function(
formElem_
){

var newContent=this.EditReadForm(formElem_);


this.importedContent.push(newContent);


var atom=this.CreateAtomFromContentObject(newContent);
this.AddAtom(atom);
}









publ.EditModifyContentFromForm=function(
formElem_
,atom_
){

this.EditDeleteContent(atom_);
this.EditAddContentFromForm(formElem_);
}





publ.EditDeleteContent=function(
atom_
){

for(var loop=0;loop<this.importedContent.length;++loop){
if(this.importedContent[loop]==atom_.contentObject){
this.importedContent.splice(loop,1);
break;
}
}


this.RemoveAtom(atom_);
}






publ.EditConvertSvgGeometry=function(
type_
,svgString
){
return svgString;
}
















publ.GetModifyForm=function(
doc_
,atom_
,callBackFunc_
,errorCallbackFunc_
,doneCallback_
){

var receiver=this;
var doc=doc_;
var gui=new modGui.GUIFactory(doc_);


var values={};
if(null!=atom_){
var names=atom_.PropertyNames();
for(var loop=0;loop<names.length;++loop){
var value=atom_.PropertyValueFromName(names[loop]);

values[names[loop]]=value;
}
}


var modFormMerge=imprt('ca.carleton.gcrc.atlas.dataSources.formMerge');
var mergeTool=new modFormMerge.FormMerge(doc_);
var inputDictionary={};
var formElem=this._CreateForm(doc_,values,inputDictionary);
mergeTool.SetInputs(inputDictionary);
mergeTool.SetEditState(this.GetEditState());


formElem.appendChild(gui.CreatePushbutton('Update',
function(){
receiver.EditModifyContentFromForm(formElem,atom_);
doneCallback_();
},'U')
);

formElem.appendChild(gui.CreatePushbutton('Delete',
function(){
receiver.EditDeleteContent(atom_);
doneCallback_();
},'D')
);



callBackFunc_(mergeTool.GetForm());
}























publ.GetAddForm=function(
doc_
,callBackFunc_
,errorCallbackFunc_
,doneCallback_
,geometries_
,contributedValues_
){

var gui=new modGui.GUIFactory(doc_);
var receiver=this;


var columns={};
if(null!=this.importedContent){
for(var loop=0;loop<this.importedContent.length;++loop){
var dic=this.importedContent[loop];

for(var key in dic){
if(null==columns[key]){
columns[key]='';



if(null!=geometries_&&null!=geometries_[key]){
var effectiveValue=this.EditConvertSvgGeometry(geometries_[key].type,geometries_[key].geometry);
columns[key]=effectiveValue;
}


if(null!=contributedValues_&&null!=contributedValues_[key]){
columns[key]=contributedValues_[key];
}
}
}
}
}


var modFormMerge=imprt('ca.carleton.gcrc.atlas.dataSources.formMerge');
var mergeTool=new modFormMerge.FormMerge(doc_);
var inputDictionary={};
var formElem=this._CreateForm(doc_,columns,inputDictionary);
mergeTool.SetInputs(inputDictionary);
mergeTool.SetEditState(this.GetEditState());


formElem.appendChild(gui.CreatePushbutton('Create',
function(){
receiver.EditAddContentFromForm(formElem);
doneCallback_();
},null)
);



callBackFunc_(mergeTool.GetForm());
}










publ._CreateForm=function(
doc_
,values_
,inputDic_
){
inputDic_.attributes={};


var doc=doc_;
var formElem=doc.createElement('form');
inputDic_.form=formElem;
formElem.setAttribute('class','editDsForm');


for(var attrName in values_){
inputDic_.attributes[attrName]={};

var divElem=doc.createElement('div');
formElem.appendChild(divElem);

var spanElem=doc.createElement('span');
divElem.appendChild(spanElem);
spanElem.appendChild(doc.createTextNode(attrName));
spanElem.appendChild(doc.createTextNode(': '));

var inputElem=doc.createElement('input');
divElem.appendChild(inputElem);
inputElem.setAttribute('type','text');
inputElem.setAttribute('size','80');
inputElem.setAttribute('name',attrName);
inputElem.setAttribute('value',values_[attrName]);

inputDic_.attributes[attrName].input=inputElem;
}

return formElem;
}
});
});