/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.data.StoreExplorer"]){
dojo._hasResource["dojox.data.StoreExplorer"]=true;
dojo.provide("dojox.data.StoreExplorer");
dojo.require("dojox.grid.DataGrid");
dojo.require("dojox.data.ItemExplorer");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.declare("dojox.data.StoreExplorer",dijit.layout.BorderContainer,{constructor:function(_1){
dojo.mixin(this,_1);
},store:null,stringQueries:false,postCreate:function(){
var _2=this;
this.inherited(arguments);
var _3=new dijit.layout.ContentPane({region:"top"}).placeAt(this);
function _4(_5,_6){
var _7=new dijit.form.Button({label:_5}).placeAt(_3);
_7.onClick=_6;
return _7;
};
var _8=_3.containerNode.appendChild(document.createElement("span"));
_8.innerHTML="Enter query: &nbsp;";
_8.id="queryText";
var _9=_3.containerNode.appendChild(document.createElement("input"));
_9.type="text";
_9.id="queryTextBox";
_4("Query",function(){
var _a=_9.value;
_2.setQuery(_2.stringQueries?_a:dojo.fromJson(_a));
});
_3.containerNode.appendChild(document.createElement("span")).innerHTML="&nbsp;&nbsp;&nbsp;";
var _b=_4("Create New",dojo.hitch(this,"createNew"));
var _c=_4("Delete",function(){
var _d=_e.selection.getSelected();
for(var i=0;i<_d.length;i++){
_2.store.deleteItem(_d[i]);
}
});
this.setItemName=function(_10){
_b.attr("label","<img style='width:12px; height:12px' src='"+dojo.moduleUrl("dijit.themes.tundra.images","dndCopy.png")+"' /> Create New "+_10);
_c.attr("label","Delete "+_10);
};
_4("Save",function(){
_2.store.save();
_2.tree.refreshItem();
});
_4("Revert",function(){
_2.store.revert();
});
_4("Add Column",function(){
var _11=prompt("Enter column name:","property");
if(_11){
_2.gridLayout.push({field:_11,name:_11,formatter:dojo.hitch(_2,"_formatCell"),editable:true});
_2.grid.attr("structure",_2.gridLayout);
}
});
var _e=this.grid=new dojox.grid.DataGrid({store:this.store,region:"center",splitter:true}).placeAt(this);
_e.canEdit=function(_12,_13){
var _14=this._copyAttr(_13,_12.field);
return !(_14&&typeof _14=="object")||_14 instanceof Date;
};
_e.startup();
var _15=new dijit.layout.ContentPane({region:"trailing",splitter:true,style:"width: 300px",}).placeAt(this);
var _16=this.tree=new dojox.data.ItemExplorer({store:this.store}).placeAt(_15);
dojo.connect(_e,"onCellClick",function(){
var _17=_e.selection.getSelected()[0];
_16.setItem(_17);
});
this.gridOnFetchComplete=_e._onFetchComplete;
this.setStore(this.store);
_16.startup();
},setQuery:function(_18){
this.grid.setQuery(_18);
},_formatCell:function(_19){
if(this.store.isItem(_19)){
return this.store.getLabel(_19)||this.store.getIdentity(_19);
}
return _19;
},setStore:function(_1a){
this.store=_1a;
var _1b=this;
var _1c=this.grid;
_1c._pending_requests[0]=false;
function _1d(_1e){
return _1b._formatCell(_1e);
};
var _1f=this.gridOnFetchComplete;
_1c._onFetchComplete=function(_20,req){
var _22=_1b.gridLayout=[];
var _23,key,_25,i,j,k,_29=_1a.getIdentityAttributes();
for(i=0;i<_29.length;i++){
key=_29[i];
_22.push({field:key,name:key,_score:100,formatter:_1d,editable:false});
}
for(i=0;_25=_20[i++];){
var _2a=_1a.getAttributes(_25);
for(k=0;key=_2a[k++];){
var _2b=false;
for(j=0;_23=_22[j++];){
if(_23.field==key){
_23._score++;
_2b=true;
break;
}
}
if(!_2b){
_22.push({field:key,name:key,_score:1,formatter:_1d,styles:"white-space:nowrap; ",editable:true});
}
}
}
_22=_22.sort(function(a,b){
return a._score>b._score?-1:1;
});
for(j=0;_23=_22[j];j++){
if(_23._score<_20.length/40*j){
_22.splice(j,_22.length-j);
break;
}
}
for(j=0;_23=_22[j++];){
_23.width=Math.round(100/_22.length)+"%";
}
_1c._onFetchComplete=_1f;
_1c.attr("structure",_22);
var _2e=_1f.apply(this,arguments);
};
_1c.setStore(_1a);
this.queryOptions={cache:true};
this.tree.setStore(_1a);
},createNew:function(){
var _2f=prompt("Enter any properties to put in the new item (in JSON literal form):","{ }");
if(_2f){
try{
this.store.newItem(dojo.fromJson(_2f));
}
catch(e){
alert(e);
}
}
}});
}
