// SpryHTMLDataSet.js - version 0.18 - Spry Pre-Release 1.6
//
// Copyright (c) 2007. Adobe Systems Incorporated.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Adobe Systems Incorporated nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
Spry.Data.HTMLDataSet=function(dataSetURL,sourceElementID,dataSetOptions)
{this.sourceElementID=sourceElementID;this.sourceElement=null;this.sourceWasInitialized=false;this.usesExternalFile=(dataSetURL!=null)?true:false;this.firstRowAsHeaders=true;this.useColumnsAsRows=false;this.columnNames=null;this.hideDataSourceElement=true;this.rowSelector=null;this.dataSelector=null;Spry.Data.HTTPSourceDataSet.call(this,dataSetURL,dataSetOptions);};Spry.Data.HTMLDataSet.prototype=new Spry.Data.HTTPSourceDataSet();Spry.Data.HTMLDataSet.prototype.constructor=Spry.Data.HTMLDataSet;Spry.Data.HTMLDataSet.prototype.getDataRefStrings=function()
{var dep=[];if(this.url)
dep.push(this.url);if(typeof this.sourceElementID=="string")
dep.push(this.sourceElementID);return dep;};Spry.Data.HTMLDataSet.prototype.setDisplay=function(ele,display)
{if(ele)
ele.style.display=display;};Spry.Data.HTMLDataSet.prototype.initDataSource=function(callLoadData)
{if(!this.loadDependentDataSets())
return;if(!this.usesExternalFile)
{this.setSourceElement();if(this.hideDataSourceElement)
this.setDisplay(this.sourceElement,"none");}};Spry.Data.HTMLDataSet.prototype.setSourceElement=function(externalDataElement)
{this.sourceElement=null;if(!this.sourceElementID)
{if(externalDataElement)
this.sourceElement=externalDataElement;else
{this.hideDataSourceElement=false;this.sourceElement=document.body;}
return;}
var sourceElementID=Spry.Data.Region.processDataRefString(null,this.sourceElementID,this.dataSetsForDataRefStrings);if(!this.usesExternalFile)
this.sourceElement=Spry.$(sourceElementID);else
if(externalDataElement)
{var foundElement=false;var sources=Spry.Utils.getNodesByFunc(externalDataElement,function(node)
{if(foundElement)
return false;if(node.nodeType!=1)
return false;if(node.id&&node.id.toLowerCase()==sourceElementID.toLowerCase())
{foundElement=true;return true;}});this.sourceElement=sources[0];}
if(!this.sourceElement)
Spry.Debug.reportError("Spry.Data.HTMLDataSet: '"+sourceElementID+"' is not a valid element ID");};Spry.Data.HTMLDataSet.prototype.getSourceElement=function(){return this.sourceElement;};Spry.Data.HTMLDataSet.prototype.getSourceElementID=function(){return this.sourceElementID;};Spry.Data.HTMLDataSet.prototype.setSourceElementID=function(sourceElementID)
{if(this.sourceElementID!=sourceElementID)
{this.sourceElementID=sourceElementID;this.recalculateDataSetDependencies();this.dataWasLoaded=false;}};Spry.Data.HTMLDataSet.prototype.getDataSelector=function(){return this.dataSelector;};Spry.Data.HTMLDataSet.prototype.setDataSelector=function(dataSelector)
{if(this.dataSelector!=dataSelector)
{this.dataSelector=dataSelector;this.dataWasLoaded=false;}};Spry.Data.HTMLDataSet.prototype.getRowSelector=function(){return this.rowSelector;};Spry.Data.HTMLDataSet.prototype.setRowSelector=function(rowSelector)
{if(this.rowSelector!=rowSelector)
{this.rowSelector=rowSelector;this.dataWasLoaded=false;}};Spry.Data.HTMLDataSet.prototype.loadDataIntoDataSet=function(rawDataDoc)
{var responseText=rawDataDoc;responseText=Spry.Data.HTMLDataSet.cleanupSource(responseText);var div=document.createElement("div");div.id="htmlsource"+this.internalID;div.innerHTML=responseText;this.setSourceElement(div);if(this.sourceElement)
{var parsedStructure=this.getDataFromSourceElement();if(parsedStructure)
{this.dataHash=parsedStructure.dataHash;this.data=parsedStructure.data;}}
this.dataWasLoaded=true;div=null;};Spry.Data.HTMLDataSet.prototype.loadDependentDataSets=function()
{if(this.hasDataRefStrings)
{var allDataSetsReady=true;for(var i=0;i]+)>/gi,function(a,b,c){return'<'+b+c.replace(/\b(src|href)\s*=/gi,function(a,b){return'spry_'+b+'=';})+'>';});return source;};Spry.Data.HTMLDataSet.undoCleanupSource=function(source)
{source=source.replace(/<(img|script|link|frame|iframe|input)([^>]+)>/gi,function(a,b,c){return'<'+b+c.replace(/\bspry_(src|href)\s*=/gi,function(a,b){return b+'=';})+'>';});return source;};Spry.Data.HTMLDataSet.normalizeColumnName=function(colName)
{colName=colName.replace(/(?:^[\s\t]+|[\s\t]+$)/gi,"");colName=colName.replace(/<\/?([a-z]+)([^>]+)>/gi,"");colName=colName.replace(/[\s\t]+/gi,"_");return colName;};Spry.Data.HTMLDataSet.prototype.getDataFromSourceElement=function()
{if(!this.sourceElement)
return null;var extractedData;var usesTable=false;switch(this.sourceElement.nodeName.toLowerCase())
{case"table":usesTable=true;extractedData=this.getDataFromHTMLTable();break;default:extractedData=this.getDataFromNestedStructure();}
if(!extractedData)
return null;if(this.useColumnsAsRows)
{var flipedData=new Array;for(var rowIdx=0;rowIdx")
{if(node.parentNode!=root)
selected=false;else
currentSelector=currentSelector.substring(1).replace(/^\s+/,"");}
if(selected)
{tagName=currentSelector.toLowerCase();if(currentSelector.indexOf(".")!=-1)
{var parts=currentSelector.split(".");tagName=parts[0];className=parts[1];}
else if(currentSelector.indexOf("#")!=-1)
{var parts=currentSelector.split("#");tagName=parts[0];id=parts[1];}}
if(selected&&tagName!=''&&tagName!='*')
if(node.nodeName.toLowerCase()!=tagName)
selected=false;if(selected&&id&&node.id!=id)
selected=false;if(selected&&className&&node.className.search(new RegExp('\\b'+className+'\\b','i'))==-1)
selected=false;if(selected)
return true;}
return false;};