/*
 * yui-ext 0.33
 * Copyright(c) 2006, Jack Slocum.
 */


YAHOO.ext.grid.LoadableDataModel=function(dataType){YAHOO.ext.grid.LoadableDataModel.superclass.constructor.call(this,[]);this.onLoad=new YAHOO.util.CustomEvent('load');this.onLoadException=new YAHOO.util.CustomEvent('loadException');this.events['load']=this.onLoad;this.events['beforeload']=new YAHOO.util.CustomEvent('beforeload');this.events['loadexception']=this.onLoadException;this.dataType=dataType;this.preprocessors=[];this.postprocessors=[];this.loadedPage=1;this.remoteSort=false;this.pageSize=0;this.pageUrl=null;this.baseParams={};this.paramMap={'page':'page','pageSize':'pageSize','sortColumn':'sortColumn','sortDir':'sortDir'};};YAHOO.extendX(YAHOO.ext.grid.LoadableDataModel,YAHOO.ext.grid.DefaultDataModel,{setLoadedPage:function(pageNum,userCallback){this.loadedPage=pageNum;if(typeof userCallback=='function'){userCallback();}},isPaged:function(){return this.pageSize>0;},getTotalRowCount:function(){return this.totalCount||this.getRowCount();},getPageSize:function(){return this.pageSize;},getTotalPages:function(){if(this.getPageSize()==0||this.getTotalRowCount()==0){return 1;}
return Math.ceil(this.getTotalRowCount()/this.getPageSize());},initPaging:function(url,pageSize,baseParams){this.pageUrl=url;this.pageSize=pageSize;this.remoteSort=true;if(baseParams)this.baseParams=baseParams;},createParams:function(pageNum,sortColumn,sortDir){var params={},map=this.paramMap;for(var key in this.baseParams){if(typeof this.baseParams[key]!='function'){params[key]=this.baseParams[key];}}
params[map['page']]=pageNum;params[map['pageSize']]=this.getPageSize();params[map['sortColumn']]=(typeof sortColumn=='undefined'?'':sortColumn);params[map['sortDir']]=sortDir||'';return params;},loadPage:function(pageNum,callback,keepExisting){var sort=this.getSortState();var params=this.createParams(pageNum,sort.column,sort.direction);this.load(this.pageUrl,params,this.setLoadedPage.createDelegate(this,[pageNum,callback]),keepExisting?(pageNum-1)*this.pageSize:null);},applySort:function(suppressEvent){if(!this.remoteSort){YAHOO.ext.grid.LoadableDataModel.superclass.applySort.apply(this,arguments);}else if(!suppressEvent){var sort=this.getSortState();if(sort.column){this.fireRowsSorted(sort.column,sort.direction,true);}}},resetPaging:function(){this.loadedPage=1;},sort:function(sortInfo,columnIndex,direction,suppressEvent){if(!this.remoteSort){YAHOO.ext.grid.LoadableDataModel.superclass.sort.apply(this,arguments);}else{this.sortInfo=sortInfo;this.sortColumn=columnIndex;this.sortDir=direction;var params=this.createParams(this.loadedPage,columnIndex,direction);this.load(this.pageUrl,params,this.fireRowsSorted.createDelegate(this,[columnIndex,direction,true]));}},load:function(url,params,callback,insertIndex){this.fireEvent('beforeload',this);if(params&&typeof params!='string'){var buf=[];for(var key in params){if(typeof params[key]!='function'){buf.push(encodeURIComponent(key),'=',encodeURIComponent(params[key]),'&');}}
delete buf[buf.length-1];params=buf.join('');}
var cb={success:this.processResponse,failure:this.processException,scope:this,argument:{callback:callback,insertIndex:insertIndex}};var method=params?'POST':'GET';this.transId=YAHOO.util.Connect.asyncRequest(method,url,cb,params);},processResponse:function(response){var cb=response.argument.callback;var keepExisting=(typeof response.argument.insertIndex=='number');var insertIndex=response.argument.insertIndex;switch(this.dataType){case YAHOO.ext.grid.LoadableDataModel.XML:this.loadData(response.responseXML,cb,keepExisting,insertIndex);break;case YAHOO.ext.grid.LoadableDataModel.JSON:var rtext=response.responseText;try{while(rtext.substring(0,1)==" "){rtext=rtext.substring(1,rtext.length);}
if(rtext.indexOf("{")<0){throw"Invalid JSON response";}
if(rtext.indexOf("{}")===0){this.loadData({},response.argument.callback);return;}
var jsonObjRaw=eval("("+rtext+")");if(!jsonObjRaw){throw"Error evaling JSON response";}
this.loadData(jsonObjRaw,cb,keepExisting,insertIndex);}catch(e){this.fireLoadException(e,response);if(typeof cb=='function'){cb(this,false);}}
break;case YAHOO.ext.grid.LoadableDataModel.TEXT:this.loadData(response.responseText,cb,keepExisting,insertIndex);break;};},processException:function(response){this.fireLoadException(null,response);if(typeof response.argument.callback=='function'){response.argument.callback(this,false);}},fireLoadException:function(e,responseObj){this.onLoadException.fireDirect(this,e,responseObj);},fireLoadEvent:function(){this.fireEvent('load',this.loadedPage,this.getTotalPages());},addPreprocessor:function(columnIndex,fn){this.preprocessors[columnIndex]=fn;},getPreprocessor:function(columnIndex){return this.preprocessors[columnIndex];},removePreprocessor:function(columnIndex){this.preprocessors[columnIndex]=null;},addPostprocessor:function(columnIndex,fn){this.postprocessors[columnIndex]=fn;},getPostprocessor:function(columnIndex){return this.postprocessors[columnIndex];},removePostprocessor:function(columnIndex){this.postprocessors[columnIndex]=null;},loadData:function(data,callback,keepExisting,insertIndex){}});YAHOO.ext.grid.LoadableDataModel.XML='xml';YAHOO.ext.grid.LoadableDataModel.JSON='json';YAHOO.ext.grid.LoadableDataModel.TEXT='text';