﻿
Ext.namespace("NPSL","NPSL.Cms");(function(){var Cms=NPSL.Cms;function compareObjects(o1,o2){if(o1===null){if(o2===null){return true;}
return false;}
for(var x in o1){if(String(o1[x])!=String(o2[x])){return false;}}
for(var x in o2){if(String(o1[x])!=String(o2[x])){return false;}}
return true;}
Cms.Record=function(data,id){this.id=(id||id===0)?id:++Ext.data.Record.AUTO_ID;this.data=data;this.stores=[];};Cms.Record.create=function(o){var f=Ext.extend(Cms.Record,{});var p=f.prototype;p.fields=new Ext.util.MixedCollection(false,function(field){return field.name;});for(var i=0,len=o.length;i<len;i++){p.fields.add(new Ext.data.Field(o[i]));}
f.getField=function(name){return p.fields.get(name);};return f;}
Cms.Record.prototype={dirty:false,editing:false,error:null,modified:null,refs:0,join:function(store){this.stores.push(store);},unjoin:function(store){for(var i=0;i<this.stores.length;++i){if(store===this.stores[i]){this.stores.splice(i,1);return;}}
throw"trying to unjoin a record from a store that it wasn't joined to";},setId:function(newId){var oldId=this.id;Cms.Megastore.changeId(this,newId);this.id=newId;Ext.each(this.stores,function(store){store.changeId(this,oldId);},this);},set:function(name,value){var current=this.data[name];if(typeof(current)==="object"&&typeof(value)==="object"){if(compareObjects(current,value)){return;}}else if(String(this.data[name])==String(value)){return;}
this.dirty=true;if(!this.modified){this.modified={};}
if(typeof this.modified[name]=='undefined'){this.modified[name]=this.data[name];}
this.data[name]=value;if(!this.editing){Ext.each(this.stores,function(store){store.afterEdit(this);},this);}},get:function(name){return this.data[name];},beginEdit:function(){this.editing=true;this.modified={};},cancelEdit:function(){this.editing=false;delete this.modified;},endEdit:function(){this.editing=false;if(this.dirty){Ext.each(this.stores,function(store){store.afterEdit(this);},this);}},reject:function(silent){var m=this.modified;for(var n in m){if(typeof m[n]!="function"){this.data[n]=m[n];}}
this.dirty=false;delete this.modified;this.editing=false;if(silent!==true){Ext.each(this.stores,function(store){store.afterReject(this);},this);}},commit:function(silent){this.dirty=false;delete this.modified;this.editing=false;if(silent!==true){Ext.each(this.stores,function(store){store.afterCommit(this);},this);}},getChanges:function(){var m=this.modified,cs={};for(var n in m){if(m.hasOwnProperty(n)){cs[n]=this.data[n];}}
return cs;},hasError:function(){return this.error!=null;},clearError:function(){this.error=null;},copy:function(newId){return new this.constructor(Ext.apply({},this.data),newId||this.id);},isModified:function(fieldName){return!!(this.modified&&this.modified.hasOwnProperty(fieldName));}};Cms.DataReader=function(table){this.table=table;};Ext.extend(Cms.DataReader,Ext.data.DataReader,{read:function(o){return this.readRecords(o);},readRecords:function(o){var flds=[];if(o.meta.fields){for(var i=0;i<o.meta.fields.length;++i){var m=o.meta.fields[i];var fld={};Ext.apply(fld,m);if(m.type==="DATE"){fld.convert=Date.parseISO;}else{fld.convert=function(v){return v;};}
if(fld.columnSid){var schema=Cms.CmsSchema.get();if(schema){var c=schema.columnsBySid[fld.columnSid];if(c){fld.column=c;fld.description=fld.description||c.description;}}}
if(this.table){var c=this.table.columnsByName[fld.name];if(c){fld.column=c;fld.description=fld.description||c.description;}}
flds.push(fld);}}else if(this.table){for(var i=0;i<this.table.columns.length;++i){var c=this.table.columns[i];var fld={name:c.name,description:c.description||c.name,column:c};if(c.type==="DATE"){fld.convert=Date.parseISO;}else{fld.convert=function(v){return v;};}
flds.push(fld);}}
if(o.meta){this.meta=o.meta;}
this.recordType=Cms.Record.create(flds);this.onMetaChange(this.meta,this.recordType,o);var data=o.data;var recordType=this.recordType,fields=recordType.prototype.fields;var records=[];var root=o;for(var i=0;i<data.length;i++){var n=data[i];var values={};for(var j=0,jlen=fields.length;j<jlen;j++){var f=fields.items[j];var k=f.mapping!==undefined&&f.mapping!==null?f.mapping:j;var v=n[k]!==undefined?n[k]:f.defaultValue;v=f.convert(v,n);values[f.name]=v;}
var id=Cms.Megastore.makeGlobalId(this.table,values);var record=new recordType(values,id);records[records.length]=record;}
var totalRecords=data.length;if(typeof(o.total)!="undefined"){totalRecords=parseInt(o.total);}
return{records:records,totalRecords:totalRecords};},onMetaChange:function(meta,recordType,o){}});Cms.Megastore=function(){var m_data={};var m_stores=[];return{add:function(record,store){var existingRecord=m_data[record.id];if(existingRecord){existingRecord.refs++;return existingRecord;}
m_data[record.id]=record;record.refs++;if(store.table){for(var i=0;i<m_stores.length;++i){if(m_stores[i]===store||m_stores[i].table!==store.table){continue;}
var j=0,keys=m_stores[i].keys;for(;j<keys.length;++j){var key=keys[j];if(record.get(key.name)!==key.value){break;}};if(j!=keys.length&&!m_stores[i].getById(record.id)){m_stores[i].add(record);}}}
return record;},remove:function(record){if(--record.refs==0){delete m_data[record.id];}},get:function(id){return m_data.hasOwnProperty(id)?m_data[id]:null;},changeId:function(record,id){delete m_data[record.id];m_data[id]=record;},addStore:function(store){m_stores.push(store);},removeStore:function(store){for(var i=0;i<m_stores.length;++i){if(m_stores[i]===store){m_stores.splice(i,1);break;}}},makeGlobalId:function(table,values){values=values||{};var id=null;if(table){id=table.sid;var pkc=table.pk.columns;for(var i=0;i<pkc.length;++i){var name=pkc[i].name;var val=values.hasOwnProperty(name)?values[name]:null;id+='&'+encodeURIComponent(new String(val));}}
return id;}};}();var uniqueId=0;Cms.DataStore=function(config){if(config.keys&&config.table){Cms.Megastore.addStore(this);}
Cms.DataStore.superclass.constructor.call(this,Ext.applyIf(config,{proxy:new Ext.data.JayrockProxy(config),reader:new Cms.DataReader(config.table)}));this.uniqueId=++uniqueId;}
Ext.extend(Cms.DataStore,Ext.data.Store,{destroy:function(){this.removeAll();Cms.Megastore.removeStore(this);Cms.DataStore.superclass.destroy.call(this);},removeFromAdded:function(record){for(var i=0;this.addedRecords&&i<this.addedRecords.length;++i){if(this.addedRecords[i]===record){this.addedRecords.splice(i,1);break;}}},removeAll:function(){this.data.each(function(record,index,length){this.removeFromAdded(record);Cms.Megastore.remove(record);record.unjoin(this);},this);Cms.DataStore.superclass.removeAll.call(this);},changeId:function(record,oldId){if(!this.data){return;}
var index=this.data.indexOfKey(oldId);if(index==-1)
throw"Tried to change the id for a record not present in the store";this.data.removeAt(index);this.data.insert(index,record.id,record);},add:function(records){records=[].concat(records);if(records.length<1){return;}
for(var i=0,len=records.length;i<len;i++){records[i]=Cms.Megastore.add(records[i],this);records[i].join(this);}
if(!this.addedRecords){this.addedRecords=[];}
this.addedRecords=this.addedRecords.concat(records);var index=this.data.length;this.data.addAll(records);if(this.snapshot){this.snapshot.addAll(records);}
this.fireEvent("add",this,records,index);},remove:function(record){this.removeFromAdded(record);Cms.Megastore.remove(record);record.unjoin(this);Cms.DataStore.superclass.remove.call(this,record);},insert:function(index,records){records=[].concat(records);for(var i=0,len=records.length;i<len;i++){records[i]=Cms.Megastore.add(records[i],this);records[i].join(this);this.data.insert(index,records[i]);}
if(!this.addedRecords){this.addedRecords=[];}
this.addedRecords=this.addedRecords.concat(records);this.fireEvent("add",this,records,index);},loadRecords:function(o,options,success){if(!o||success===false){if(success!==false){this.fireEvent("load",this,[],options);}
if(options.callback){options.callback.call(options.scope||this,[],options,false);}
return;}
var r=o.records,t=o.totalRecords||r.length;if(!options||options.add!==true){if(this.pruneModifiedRecords){this.modified=[];}
for(var i=0,len=r.length;i<len;i++){r[i]=Cms.Megastore.add(r[i],this);r[i].join(this);}
if(this.snapshot){this.data=this.snapshot;delete this.snapshot;}
this.data.each(function(record,index,length){Cms.Megastore.remove(record);record.unjoin(this);},this);this.data.clear();this.data.addAll(r);this.totalLength=t;this.applySort();this.fireEvent("datachanged",this);}else{this.totalLength=Math.max(t,this.data.length+r.length);this.add(r);}
this.fireEvent("load",this,r,options);if(options.callback){options.callback.call(options.scope||this,r,options,true);}},rejectChanges:function(){Cms.DataStore.superclass.rejectChanges.call(this);if(this.addedRecords){while(this.addedRecords.length>0){this.remove(this.addedRecords[0]);}
delete this.addedRecords;}},commitChanges:function(){delete this.addedRecords;Cms.DataStore.superclass.commitChanges.call(this);},rejectChange:function(record){this.commitOrRejectChange(record,'reject');},commitChange:function(record){this.commitOrRejectChange(record,'commit');},commitOrRejectChange:function(record,action){for(var i=0,len=this.modified.length;i<len;i++){if(this.modified[i].id===record.id){this.modified[i][action]();break;}}
if(this.addedRecords&&this.addedRecords.length){for(var i=0,len=this.addedRecords.length;i<len;i++){if(this.addedRecords[i].id===record.id){if(action==='reject'){this.remove(this.addedRecords[i]);}else{this.addedRecords.splice(i,1);}
break;}}}}});}());