







































Module('ca.carleton.gcrc.atlas.widgets.externalHtml','$Revision: 2230 $',function(mod){

var modObservable=imprt('ca.carleton.gcrc.mvc.observable');
var modDisplayWidget=imprt('ca.carleton.gcrc.atlas.displayWidget');
var xmlUtil=imprt('ca.carleton.gcrc.xml.util');
var xmlRequest=imprt('ca.carleton.gcrc.xml.request');
var domUtil=imprt('ca.carleton.gcrc.dom.util');

var ELEMENT_NODE=1;
var ATTRIBUTE_NODE=2;
var TEXT_NODE=3;
var CDATA_SECTION_NODE=4;
var ENTITY_REFERENCE_NODE=5;
var ENTITY_NODE=6;
var PROCESSING_INSTRUCTION_NODE=7;
var COMMENT_NODE=8;
var DOCUMENT_NODE=9;
var DOCUMENT_TYPE_NODE=10;
var DOCUMENT_FRAGMENT_NODE=11;
var NOTATION_NODE=12;









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








publ.__init__=function(
externalHtmlLocation_,
widgetId_
){

supr.__init__.call(this);








this.document=null;






this.htmlNS='http://www.w3.org/1999/xhtml';




this.externalHtmlLocation=externalHtmlLocation_;




this.widgetId=widgetId_;




this.externalHtml=null;


var receiver=this;
this.LoadSource(function(doc){

});
}




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





publ.Width=function()
{
return 1;
}





publ.Height=function()
{
return 1;
}












publ.Create=function(
htmlParent_,
dataModel_,
x_,
y_,
width_,
height_
)
{
var widget=new mod.Widget(
this,
htmlParent_,
dataModel_,
x_,
y_,
width_,
height_
);

return widget;
}





publ.LoadSource=function(callbackFunction_)
{
if(null!=this.externalHtml){
callbackFunction_(this.externalHtml);
}else{
var receiver=this;
xmlRequest.SimpleGetXml(this.externalHtmlLocation,function(doc_){
receiver.externalHtml=doc_;
if(null==doc_){
alert('Unable to load external HTML file: '+this.externalHtmlLocation);
}else if(null!=callbackFunction_){
callbackFunction_(receiver.externalHtml);
}
});
}
}
});








mod.Widget=Class(modObservable.Observer,function(publ,priv,supr){










publ.__init__=function(
factory_,
htmlParent_,
dataModel_,
x_,
y_,
width_,
height_
){

supr.__init__.call(this);


this.dataModel=dataModel_;


this.document=htmlParent_.ownerDocument;
this.htmlNS=factory_.htmlNS;
this.extraNS='http://gcrc.carleton.ca/atlas/htmlWidget';


this.parentElement=htmlParent_;


this.factory=factory_;

this.x=x_;
this.y=y_;
this.width=width_;
this.height=height_;

this.peerElements=new Array();

var receiver=this;
factory_.LoadSource(function(doc_){
receiver.Display(doc_);
});
}




publ.Remove=function()
{

this.Terminate();


var loop;
for(loop=0;loop<this.peerElements.length;++loop){
var peerElement=this.peerElements[loop];
peerElement.removeEventListener('click',this,false);
peerElement.removeEventListener('mouseover',this,false);
peerElement.removeEventListener('mouseout',this,false);
}


if(null!=this.topElement&&null!=this.parentElement)
{
this.parentElement.removeChild(this.topElement);
this.topElement=null;
this.parentElement=null;
}
}




publ.handleEvent=function(evt)
{

var peerElement=evt.target;


var layerName=peerElement.getAttributeNS(this.extraNS,'layer');
var featureName=peerElement.getAttributeNS(this.extraNS,'feature');
var feature=this.dataModel.LayerFromName(layerName).FeatureFromId(featureName);


var type=evt.type;
if('click'==type)
{
feature.SelectionOnInit();
}
else if('mouseover'==type)
{
feature.FocusOnInit(evt);
}
else if('mouseout'==type)
{
feature.FocusOffInit();
}

}




publ.DisplayStateChanged=function(mvcEvent)
{
var feature=mvcEvent.originator;
var htmlElement=mvcEvent.observerData;

this.UpdateDisplayState(feature,htmlElement);
}




publ.UpdateDisplayState=function(feature_,htmlElement_)
{
var display=false;
if(feature_.IsSelected()&&feature_.IsInFocus())
{
if(1==htmlElement_.getAttributeNS(this.extraNS,'onFocusAndSelected'))
{
display=true;
}
}
else if(feature_.IsSelected())
{
if(1==htmlElement_.getAttributeNS(this.extraNS,'onSelected'))
{
display=true;
}
}
else if(feature_.IsInFocus())
{
if(1==htmlElement_.getAttributeNS(this.extraNS,'onFocus'))
{
display=true;
}
}
else
{
if(1==htmlElement_.getAttributeNS(this.extraNS,'onNormal'))
{
display=true;
}
}

if(display)
{
htmlElement_.setAttributeNS(null,'display','inline');
}
else
{
htmlElement_.setAttributeNS(null,'display','none');
}
}




publ.AnimateChanged=function(mvcEvent)
{
var feature=mvcEvent.originator;
var observerData=mvcEvent.observerData;
var htmlElement=observerData[0];
var attribute=observerData[1];
var name=observerData[2];

this.UpdateAnimate(feature,attribute,htmlElement,name);
}





publ.UpdateAnimate=function(feature_,attribute_,htmlElement_,name_)
{
var value=feature_.GetVariableValue(attribute_);
htmlElement_.setAttributeNS(null,name_,value);
}




publ.TextChanged=function(mvcEvent)
{
var feature=mvcEvent.originator;
var observerData=mvcEvent.observerData;
var htmlElement=observerData[0];
var attribute=observerData[1];

this.UpdateText(feature,attribute,htmlElement);
}





publ.UpdateText=function(feature_,attribute_,htmlElement_)
{
var value=feature_.GetVariableValue(attribute_,'');


domUtil.RemoveChildren(htmlElement_);

var text=htmlElement_.ownerDocument.createTextNode(value);
htmlElement_.appendChild(text);
}




publ.Display=function(externalDocument_)
{



this.topElement=this.document.createElementNS(this.htmlNS,'div');
this.topElement.setAttributeNS(
null,
'style',
'overflow:auto;display:inline;position:absolute;top:'+this.y+'px;left:'+this.x+'px;width:'+this.width+'px;height:'+this.height+'px'
);
this.topElement.setAttributeNS(
null,
'id',
'externalHtml_'+this.factory.widgetId
);
this.parentElement.appendChild(this.topElement);


this.ImportHtmlNode(externalDocument_.documentElement,this.topElement);
}




publ.ImportHtmlNode=function(externalNode_,parentNode_)
{



var htmlChild;
for(htmlChild=externalNode_.firstChild;null!=htmlChild;htmlChild=htmlChild.nextSibling){
if(htmlChild.localName=='body'){
var bodyChild;
for(bodyChild=htmlChild.firstChild;null!=bodyChild;bodyChild=bodyChild.nextSibling){
this.ImportNode(bodyChild,parentNode_);
}
}
}
}




publ.ImportNode=function(externalNode_,parentNode_)
{
if(PROCESSING_INSTRUCTION_NODE==externalNode_.nodeType
&&'atlas-controller'==externalNode_.nodeName
){

var value=externalNode_.nodeValue;

var layer=null;
var layerRegExp=/layer="([^"]*)"/;
		     	if( layerRegExp.test(value) ) {
		     		layer = layerRegExp.exec(value)[1];
		     	}
		
		     	var feature = null;
		     	var featureRegExp = /feature="([^"]*)"/;
if(featureRegExp.test(value)){
feature=featureRegExp.exec(value)[1];
}

parentNode_.setAttributeNS(this.extraNS,'layer',layer);
parentNode_.setAttributeNS(this.extraNS,'feature',feature);
parentNode_.addEventListener('click',this,false);
parentNode_.addEventListener('mouseover',this,false);
parentNode_.addEventListener('mouseout',this,false);
this.peerElements.push(parentNode_);
}
else if(PROCESSING_INSTRUCTION_NODE==externalNode_.nodeType
&&'atlas-display'==externalNode_.nodeName
){

var value=externalNode_.nodeValue;

var layerName=null;
var layerRegExp=/layer="([^"]*)"/;
		     	if( layerRegExp.test(value) ) {
		     		layerName = layerRegExp.exec(value)[1];
		     	}
		
		     	var featureName = null;
		     	var featureRegExp = /feature="([^"]*)"/;
if(featureRegExp.test(value)){
featureName=featureRegExp.exec(value)[1];
}

parentNode_.setAttributeNS(this.extraNS,'layer',layerName);
parentNode_.setAttributeNS(this.extraNS,'feature',featureName);


var onNormalRegExp=/onNormal="([^"]*)"/;
		     	if( onNormalRegExp.test(value) ) {
		     		var b = onNormalRegExp.exec(value)[1];
		     		if( 'true' == b ) {
		     			parentNode_.setAttributeNS(this.extraNS, 'onNormal', 1);
		     		}
		     	}
		
				// Deal with focus state     	
		     	var onFocusRegExp = /onFocus="([^"]*)"/;
if(onFocusRegExp.test(value)){
var b=onFocusRegExp.exec(value)[1];
if('true'==b){
parentNode_.setAttributeNS(this.extraNS,'onFocus',1);
}
}


var onSelectedRegExp=/onSelected="([^"]*)"/;
		     	if( onSelectedRegExp.test(value) ) {
		     		var b = onSelectedRegExp.exec(value)[1];
		     		if( 'true' == b ) {
		     			parentNode_.setAttributeNS(this.extraNS, 'onSelected', 1);
		     		}
		     	}
		
				// Deal with focus and selected state     	
		     	var onFocusAndSelectedRegExp = /onFocusAndSelected="([^"]*)"/;
if(onFocusAndSelectedRegExp.test(value)){
var b=onFocusAndSelectedRegExp.exec(value)[1];
if('true'==b){
parentNode_.setAttributeNS(this.extraNS,'onFocusAndSelected',1);
}
}

var feature=this.dataModel.LayerFromName(layerName).FeatureFromId(featureName);
this.Observe(feature,'display_state',this.DisplayStateChanged,parentNode_);

this.UpdateDisplayState(feature,parentNode_);
}
else if(PROCESSING_INSTRUCTION_NODE==externalNode_.nodeType
&&'atlas-animate'==externalNode_.nodeName
){

var value=externalNode_.nodeValue;

var layerName=null;
var layerRegExp=/layer="([^"]*)"/;
		     	if( layerRegExp.test(value) ) {
		     		layerName = layerRegExp.exec(value)[1];
		     	}
		
		     	var featureName = null;
		     	var featureRegExp = /feature="([^"]*)"/;
if(featureRegExp.test(value)){
featureName=featureRegExp.exec(value)[1];
}

var attributeName=null;
var attributeRegExp=/attribute="([^"]*)"/;
		     	if( attributeRegExp.test(value) ) {
		     		attributeName = attributeRegExp.exec(value)[1];
		     	}
		     	
		     	var nameName = null;
		     	var nameRegExp = /name="([^"]*)"/;
if(nameRegExp.test(value)){
nameName=nameRegExp.exec(value)[1];
}

if(
null!=layerName
&&
null!=featureName
&&
null!=attributeName
&&
null!=nameName
){
var feature=this.dataModel.LayerFromName(layerName).FeatureFromId(featureName);
this.Observe(feature,attributeName,this.AnimateChanged,[parentNode_,attributeName,nameName]);

this.UpdateAnimate(feature,attributeName,parentNode_,nameName);
}
}
else if(PROCESSING_INSTRUCTION_NODE==externalNode_.nodeType
&&'atlas-text'==externalNode_.nodeName
){

var value=externalNode_.nodeValue;

var layerName=null;
var layerRegExp=/layer="([^"]*)"/;
		     	if( layerRegExp.test(value) ) {
		     		layerName = layerRegExp.exec(value)[1];
		     	}
		
		     	var featureName = null;
		     	var featureRegExp = /feature="([^"]*)"/;
if(featureRegExp.test(value)){
featureName=featureRegExp.exec(value)[1];
}

var attributeName=null;
var attributeRegExp=/attribute="([^"]*)"/;
		     	if( attributeRegExp.test(value) ) {
		     		attributeName = attributeRegExp.exec(value)[1];
		     	}
		     	
		     	if(
		     		null != layerName
		     	&&
		     		null != featureName
		     	&&
		     		null != attributeName
		     	) {
			     	var feature = this.dataModel.LayerFromName(layerName).FeatureFromId(featureName);
			     	this.Observe(feature,attributeName,this.TextChanged,[parentNode_,attributeName]);
			     	
			     	this.UpdateText(feature,attributeName,parentNode_);
		     	}
		     	
		     } else {
		     	// Import this node
				//var newNode = this.document.importNode(externalNode_, false);
				var newNode = xmlUtil.importNodeSlow(externalNode_, false, this.document);
				parentNode_.appendChild(newNode);
				
				// Loop through all children, importing them one at a time, recursively
				var child;
				for(child=externalNode_.firstChild; null != child ;child=child.nextSibling) {
					this.ImportNode(child,newNode);
				}
			}
		}
	});

});