includes/clientside/static/SpryJSONDataSet.js
changeset 699 c7d737202d59
parent 696 bd5069e1f19a
child 700 491314c44d23
equal deleted inserted replaced
696:bd5069e1f19a 699:c7d737202d59
     1 // SpryJSONDataSet.js - version 0.6 - Spry Pre-Release 1.6.1
       
     2 //
       
     3 // Copyright (c) 2007. Adobe Systems Incorporated.
       
     4 // All rights reserved.
       
     5 //
       
     6 // Redistribution and use in source and binary forms, with or without
       
     7 // modification, are permitted provided that the following conditions are met:
       
     8 //
       
     9 //   * Redistributions of source code must retain the above copyright notice,
       
    10 //     this list of conditions and the following disclaimer.
       
    11 //   * Redistributions in binary form must reproduce the above copyright notice,
       
    12 //     this list of conditions and the following disclaimer in the documentation
       
    13 //     and/or other materials provided with the distribution.
       
    14 //   * Neither the name of Adobe Systems Incorporated nor the names of its
       
    15 //     contributors may be used to endorse or promote products derived from this
       
    16 //     software without specific prior written permission.
       
    17 //
       
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
       
    22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    28 // POSSIBILITY OF SUCH DAMAGE.
       
    29 
       
    30 Spry.Data.JSONDataSet=function(dataSetURL,dataSetOptions)
       
    31 {this.path="";this.pathIsObjectOfArrays=false;this.doc=null;this.subPaths=[];this.useParser=false;this.preparseFunc=null;Spry.Data.HTTPSourceDataSet.call(this,dataSetURL,dataSetOptions);var jwType=typeof this.subPaths;if(jwType=="string"||(jwType=="object"&&this.subPaths.constructor!=Array))
       
    32 this.subPaths=[this.subPaths];};Spry.Data.JSONDataSet.prototype=new Spry.Data.HTTPSourceDataSet();Spry.Data.JSONDataSet.prototype.constructor=Spry.Data.JSONDataSet;Spry.Data.JSONDataSet.prototype.getDataRefStrings=function()
       
    33 {var strArr=[];if(this.url)strArr.push(this.url);if(this.path)strArr.push(this.path);if(this.requestInfo&&this.requestInfo.postData)strArr.push(this.requestInfo.postData);return strArr;};Spry.Data.JSONDataSet.prototype.getDocument=function(){return this.doc;};Spry.Data.JSONDataSet.prototype.getPath=function(){return this.path;};Spry.Data.JSONDataSet.prototype.setPath=function(path)
       
    34 {if(this.path!=path)
       
    35 {this.path=path;if(this.dataWasLoaded&&this.doc)
       
    36 {this.notifyObservers("onPreLoad");this.setDataFromDoc(this.doc);}}};Spry.Data.JSONDataSet.getMatchingObjects=function(path,jsonObj)
       
    37 {var results=[];if(path&&jsonObj)
       
    38 {var prop="";var leftOverPath="";var offset=path.search(/\./);if(offset!=-1)
       
    39 {prop=path.substring(0,offset);leftOverPath=path.substring(offset+1);}
       
    40 else
       
    41 prop=path;var matches=[];if(prop&&typeof jsonObj=="object")
       
    42 {var obj=jsonObj[prop];var objType=typeof obj;if(objType!=undefined&&objType!=null)
       
    43 {if(obj&&objType=="object"&&obj.constructor==Array)
       
    44 matches=matches.concat(obj);else
       
    45 matches.push(obj);}}
       
    46 var numMatches=matches.length;if(leftOverPath)
       
    47 {for(var i=0;i<numMatches;i++)
       
    48 results=results.concat(Spry.Data.JSONDataSet.getMatchingObjects(leftOverPath,matches[i]));}
       
    49 else
       
    50 results=matches;}
       
    51 return results;};Spry.Data.JSONDataSet.flattenObject=function(obj,basicColumnName)
       
    52 {var basicName=basicColumnName?basicColumnName:"column0";var row=new Object;var objType=typeof obj;if(objType=="object")
       
    53 Spry.Data.JSONDataSet.copyProps(row,obj);else
       
    54 row[basicName]=obj;row.ds_JSONObject=obj;return row;};Spry.Data.JSONDataSet.copyProps=function(dstObj,srcObj,suppressObjProps)
       
    55 {if(srcObj&&dstObj)
       
    56 {for(var prop in srcObj)
       
    57 {if(suppressObjProps&&typeof srcObj[prop]=="object")
       
    58 continue;dstObj[prop]=srcObj[prop];}}
       
    59 return dstObj;};Spry.Data.JSONDataSet.flattenDataIntoRecordSet=function(jsonObj,path,pathIsObjectOfArrays)
       
    60 {var rs=new Object;rs.data=[];rs.dataHash={};if(!path)
       
    61 path="";var obj=jsonObj;var objType=typeof obj;var basicColName="";if(objType!="object"||!obj)
       
    62 {if(obj!=null)
       
    63 {var row=new Object;row.column0=obj;row.ds_RowID=0;rs.data.push(row);rs.dataHash[row.ds_RowID]=row;}
       
    64 return rs;}
       
    65 var matches=[];if(obj.constructor==Array)
       
    66 {var arrLen=obj.length;if(arrLen<1)
       
    67 return rs;var eleType=typeof obj[0];if(eleType!="object")
       
    68 {for(var i=0;i<arrLen;i++)
       
    69 {var row=new Object;row.column0=obj[i];row.ds_RowID=i;rs.data.push(row);rs.dataHash[row.ds_RowID]=row;}
       
    70 return rs;}
       
    71 if(obj[0].constructor==Array)
       
    72 return rs;if(path)
       
    73 {for(var i=0;i<arrLen;i++)
       
    74 matches=matches.concat(Spry.Data.JSONDataSet.getMatchingObjects(path,obj[i]));}
       
    75 else
       
    76 {for(var i=0;i<arrLen;i++)
       
    77 matches.push(obj[i]);}}
       
    78 else
       
    79 {if(path)
       
    80 matches=Spry.Data.JSONDataSet.getMatchingObjects(path,obj);else
       
    81 matches.push(obj);}
       
    82 var numMatches=matches.length;if(path&&numMatches>=1&&typeof matches[0]!="object")
       
    83 basicColName=path.replace(/.*\./,"");if(!pathIsObjectOfArrays)
       
    84 {for(var i=0;i<numMatches;i++)
       
    85 {var row=Spry.Data.JSONDataSet.flattenObject(matches[i],basicColName,pathIsObjectOfArrays);row.ds_RowID=i;rs.dataHash[i]=row;rs.data.push(row);}}
       
    86 else
       
    87 {var rowID=0;for(var i=0;i<numMatches;i++)
       
    88 {var obj=matches[i];var colNames=[];var maxNumRows=0;for(var propName in obj)
       
    89 {var prop=obj[propName];var propyType=typeof prop;if(propyType=='object'&&prop.constructor==Array)
       
    90 {colNames.push(propName);maxNumRows=Math.max(maxNumRows,obj[propName].length);}}
       
    91 var numColNames=colNames.length;for(var j=0;j<maxNumRows;j++)
       
    92 {var row=new Object;for(var k=0;k<numColNames;k++)
       
    93 {var colName=colNames[k];row[colName]=obj[colName][j];}
       
    94 row.ds_RowID=rowID++;rs.dataHash[row.ds_RowID]=row;rs.data.push(row);}}}
       
    95 return rs;};Spry.Data.JSONDataSet.prototype.flattenSubPaths=function(rs,subPaths)
       
    96 {if(!rs||!subPaths)
       
    97 return;var numSubPaths=subPaths.length;if(numSubPaths<1)
       
    98 return;var data=rs.data;var dataHash={};var pathArray=[];var cleanedPathArray=[];var isObjectOfArraysArr=[];for(var i=0;i<numSubPaths;i++)
       
    99 {var subPath=subPaths[i];if(typeof subPath=="object")
       
   100 {isObjectOfArraysArr[i]=subPath.pathIsObjectOfArrays;subPath=subPath.path;}
       
   101 if(!subPath)
       
   102 subPath="";pathArray[i]=Spry.Data.Region.processDataRefString(null,subPath,this.dataSetsForDataRefStrings);cleanedPathArray[i]=pathArray[i].replace(/\[.*\]/g,"");}
       
   103 var row;var numRows=data.length;var newData=[];for(var i=0;i<numRows;i++)
       
   104 {row=data[i];var newRows=[row];for(var j=0;j<numSubPaths;j++)
       
   105 {var newRS=Spry.Data.JSONDataSet.flattenDataIntoRecordSet(row.ds_JSONObject,pathArray[j],isObjectOfArraysArr[j]);if(newRS&&newRS.data&&newRS.data.length)
       
   106 {if(typeof subPaths[j]=="object"&&subPaths[j].subPaths)
       
   107 {var sp=subPaths[j].subPaths;spType=typeof sp;if(spType=="string")
       
   108 sp=[sp];else if(spType=="object"&&spType.constructor==Object)
       
   109 sp=[sp];this.flattenSubPaths(newRS,sp);}
       
   110 var newRSData=newRS.data;var numRSRows=newRSData.length;var cleanedPath=cleanedPathArray[j]+".";var numNewRows=newRows.length;var joinedRows=[];for(var k=0;k<numNewRows;k++)
       
   111 {var newRow=newRows[k];for(var l=0;l<numRSRows;l++)
       
   112 {var newRowObj=new Object;var newRSRow=newRSData[l];for(var prop in newRSRow)
       
   113 {var newPropName=cleanedPath+prop;if(cleanedPath==prop||cleanedPath.search(new RegExp("\\."+prop+"\\.$"))!=-1)
       
   114 newPropName=cleanedPathArray[j];newRowObj[newPropName]=newRSRow[prop];}
       
   115 Spry.Data.JSONDataSet.copyProps(newRowObj,newRow);joinedRows.push(newRowObj);}}
       
   116 newRows=joinedRows;}}
       
   117 newData=newData.concat(newRows);}
       
   118 data=newData;numRows=data.length;for(i=0;i<numRows;i++)
       
   119 {row=data[i];row.ds_RowID=i;dataHash[row.ds_RowID]=row;}
       
   120 rs.data=data;rs.dataHash=dataHash;};Spry.Data.JSONDataSet.prototype.parseJSON=function(str,filter)
       
   121 {try
       
   122 {if(/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(str))
       
   123 {var j=eval('('+str+')');if(typeof filter==='function')
       
   124 {function walk(k,v)
       
   125 {if(v&&typeof v==='object')
       
   126 {for(var i in v)
       
   127 {if(v.hasOwnProperty(i))
       
   128 {v[i]=walk(i,v[i]);}}}
       
   129 return filter(k,v);}
       
   130 j=walk('',j);}
       
   131 return j;}}catch(e){}
       
   132 throw new Error("Failed to parse JSON string.");};Spry.Data.JSONDataSet.prototype.syncColumnTypesToData=function()
       
   133 {var row=this.getData()[0];for(var colName in row)
       
   134 {if(!this.columnTypes[colName])
       
   135 {var type=typeof row[colName];if(type=="number")
       
   136 this.setColumnType(colName,type);}}};Spry.Data.JSONDataSet.prototype.loadDataIntoDataSet=function(rawDataDoc)
       
   137 {if(this.preparseFunc)
       
   138 rawDataDoc=this.preparseFunc(this,rawDataDoc);var jsonObj;try{jsonObj=this.useParser?this.parseJSON(rawDataDoc):eval("("+rawDataDoc+")");}
       
   139 catch(e)
       
   140 {Spry.Debug.reportError("Caught exception in JSONDataSet.loadDataIntoDataSet: "+e);jsonObj={};}
       
   141 if(jsonObj==null)
       
   142 jsonObj="null";var rs=Spry.Data.JSONDataSet.flattenDataIntoRecordSet(jsonObj,Spry.Data.Region.processDataRefString(null,this.path,this.dataSetsForDataRefStrings),this.pathIsObjectOfArrays);this.flattenSubPaths(rs,this.subPaths);this.doc=rawDataDoc;this.docObj=jsonObj;this.data=rs.data;this.dataHash=rs.dataHash;this.dataWasLoaded=(this.doc!=null);this.syncColumnTypesToData();};