/*
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.onReady(function(){var k;function h(I){this.geom=I}function r(I,N,L,K,J,M){this.x=I;this.y=N;this.group=L;this.colour=K;this.weight=J;this.facet=new Object();this.layers=new Object()}r.prototype.set=function(I,K,J){if(J==null){if(K=="default"){delete this[I]}else{this[I]=K}}else{if(J=="facet"){if(K=="default"){delete this.facet[I]}else{this.facet[I]=K}}else{if(K=="default"){delete this.layers[J][I]}else{this.layers[J][I]=K}}}};r.prototype.addLayer=function(I,J){return this.layers[J]=new h(I)};r.prototype.removeLayer=function(I){delete this.layers[I]};r.prototype.toJSON=function(){return Ext.util.JSON.encode(this)};r.prototype.toString=function(){return Ext.util.JSON.encode(this)};function b(K){syntax="ggplot(myData, aes(";var I=true;for(z in K){if(typeof(K[z])=="string"&&z!="facet"){if(!I){syntax=syntax+", "}syntax=syntax+z+"="+K[z].substring(4);I=false}}syntax=syntax+"))";layers=K.layers;var M=true;for(everyLayer in layers){M=false;var L=layers[everyLayer];var J=new Array();var N=new Array();for(z in L){if(typeof(L[z])=="string"&&L[z].substring(0,3)=="set"){thisValue=L[z].substring(4);if(thisValue=="FALSE"||thisValue=="TRUE"){J.push(z+"="+L[z].substring(4))}else{if(z=="alpha"||z=="size"||z=="weight"||z=="shape"||z=="bins"||z=="angle"||z=="adjust"){J.push(z+"="+L[z].substring(4))}else{J.push(z+'="'+L[z].substring(4)+'"')}}}if(typeof(L[z])=="string"&&L[z].substring(0,3)=="map"){N.push(z+"="+L[z].substring(4))}}syntax=syntax+" + geom_"+L.geom+"("+J.join(",");if(N.length>0){if(J.length>0){syntax=syntax+", "}syntax=syntax+"aes("+N.join(",")+")"}syntax=syntax+")"}if(M&&K.y!=undefined){if(K.y.substring(4)=="..density.."||K.y.substring(4)=="..count.."){syntax=syntax+' + geom_blank(stat="bin")'}else{syntax=syntax+" + geom_blank()"}}if(K.facet.map){syntax=syntax+" + facet_wrap(~"+K.facet.map.substring(4);if(K.facet.nrow){syntax=syntax+", nrow = "+K.facet.nrow.substring(4)}if(K.facet.scales){syntax=syntax+', scales = "'+K.facet.scales.substring(4)+'"'}syntax=syntax+")"}return(syntax)}function B(){var I=document.getElementById("historyCmp");I.scrollTop=I.scrollHeight}function o(I){try{return Ext.util.JSON.decode(I)}catch(J){return false}}function l(I){myP=document.createElement("p");myP.appendChild(document.createTextNode(I));Ext.get("historyCmp").appendChild(myP);B()}function a(I){Ext.DomHelper.overwrite("syntaxdiv","<pre><code> > "+I+"</code></pre>")}function v(){var I=Ext.getCmp("workspace").plotConfig.dataFile;Ext.getCmp("workspace").plotConfig=new r();Ext.getCmp("workspace").plotConfig.dataFile=I;Ext.getCmp("layerList").getStore().removeAll();Ext.getCmp("workspace").load({url:"emptyfile.txt",method:"GET"});y(Ext.getCmp("workspace").plotConfig);s(null);q()}function G(){plotConfig=Ext.getCmp("workspace").plotConfig;if(plotConfig.x==null||plotConfig.y==null){alert("You need to set X and Y first!")}else{width=Ext.getCmp("workspace").getSize().width;height=Ext.getCmp("workspace").getSize().height;plotConfig.width=width;plotConfig.height=height-40;Ext.getCmp("drawButton").disable();l(b(plotConfig)+";");Ext.getCmp("workspace").load({timeout:60,url:"../brew/ggplot-png",method:"POST",params:{plotRequest:plotConfig.toJSON()},callback:function(J,K,I){Ext.getCmp("drawButton").enable();plotResult=o(I.responseText);if(plotResult&&!plotResult.success){l("Error: "+plotResult.error)}}})}}function c(){plotConfig=Ext.getCmp("workspace").plotConfig;if(plotConfig.x==null||plotConfig.y==null){alert("You need to set X and Y first!")}else{myWindow=window.open();myWindow.document.write("PDF link will appear here... please wait (don't close this window)\n\n");Ext.getCmp("pdfbutton").disable();Ext.Ajax.request({timeout:60000,url:"../brew/ggplot-pdf",method:"POST",params:{plotRequest:plotConfig.toJSON()},success:function(J){try{result=Ext.decode(J.responseText)}catch(I){alert("error in decoding data: "+J.responseText)}finally{Ext.getCmp("pdfbutton").enable();if(result.success){myWindow.document.write(('<a href="plots/'+result.pdfurl+'">click here to get pdf </a>'))}else{alert("R returned an error:\n\n"+result.error)}}},failure:function(){Ext.getCmp("pdfbutton").enable()}})}}function s(I){htmlString="<h1> Layer Config:</h1>";for(prop in I){htmlString=htmlString+"<p>"+prop+": "+I[prop]+"</p>"}document.getElementById("detailDiv").innerHTML=htmlString;Ext.get("detailDiv").hide().slideIn("t",{stopFx:true,duration:0.2});a(b(Ext.getCmp("workspace").plotConfig))}function y(I){htmlString="<h1> General Options:</h1>";if(I.x){htmlString=htmlString+"<p> x: "+I.x+"</p>"}else{htmlString=htmlString+"<p> <span class='invalid'> x: undefined </span></p>"}if(I.y){htmlString=htmlString+"<p> y: "+I.y+"</p>"}else{htmlString=htmlString+"<p><span class='invalid'> y: undefined </span></p>"}if(I.weight){htmlString=htmlString+"<p> weight: "+I.weight+"</p>"}if(I.group){htmlString=htmlString+"<p> group: "+I.group+"</p>"}if(I.colour){htmlString=htmlString+"<p> colour: "+I.colour+"</p>"}if(I.facet.map){htmlString=htmlString+"<p> facet: "+I.facet.map+"</p>"}numberOfLayers=Ext.getCmp("layerList").getStore().getCount();if(numberOfLayers>0){htmlString=htmlString+"<p> layers: "+Ext.getCmp("layerList").getStore().getCount()+"</p>"}else{htmlString=htmlString+"<p><span class='invalid'>  layers: 0 </span></p>"}document.getElementById("generalDetailDiv").innerHTML=htmlString;Ext.get("generalDetailDiv").hide().slideIn("l",{stopFx:true,duration:0.2});a(b(I))}function j(K,J,I){plotConfig=Ext.getCmp("workspace").plotConfig;s(plotConfig.layers[I.id])}function A(I,K,J){dataVariables=Ext.getCmp("workspace").dataVariables;thisLayerMenu=Ext.getCmp("plotMenu").add({id:J,text:"Layer: "+I,iconCls:"icon_"+I,handler:function(){return false},menu:{}});for(thisaes=0;thisaes<K.length;thisaes++){myAes=K[thisaes];myValues=k.Aesthetics[myAes].values;mapSetMenu=thisLayerMenu.menu.add({text:myAes,handler:function(){return false},iconCls:"icon_prop",key:myAes,menu:{}});if(k.Aesthetics[myAes].set){mapSetMenu.menu.add({text:"Set",iconCls:"icon_set",handler:function(){return false},menu:n(myAes,myValues,"default",true,J,"set_")})}if(k.Aesthetics[myAes].map){mapSetMenu.menu.add({iconCls:"icon_map",text:"Map",handler:function(){return false},menu:n(myAes,dataVariables,"default",false,J,"map_")})}}thisLayerMenu.menu.add("-");thisLayerMenu.menu.add({text:"Remove Layer",iconCls:"delete",handler:function(){plotConfig.removeLayer(J);Ext.getCmp("plotMenu").remove(J);t(J);s(null);y(Ext.getCmp("workspace").plotConfig)}})}function H(I,J){Ext.getCmp("layerList").getStore().loadData([[J,I]],true);activeRecord=Ext.getCmp("layerList").getStore().getById(J);Ext.getCmp("layerList").getSelectionModel().selectRecords([activeRecord])}function t(I){thisRecord=Ext.getCmp("layerList").getStore().getById(I);Ext.getCmp("layerList").getStore().remove(thisRecord);Ext.getCmp("layerList").getStore().refreshData()}function E(I,J){plotConfig=Ext.getCmp("workspace").plotConfig;var K="layer"+(Math.floor(Math.random()*90000)+10000);plotConfig.addLayer(I,K);A(I,J,K);H(I,K);y(Ext.getCmp("workspace").plotConfig);Ext.getCmp("layerList").getSelectionModel().on("rowselect",j)}var m=new Ext.menu.Menu({id:"plotMenu",items:[],handler:function(){return false}});function n(K,I,M,O,J,N){if(K=="y"){I=new Array("..count..","..density..","-").concat(I)}plotConfig=Ext.getCmp("workspace").plotConfig;var L=new Ext.menu.Menu({items:[]});if(O){L.add({text:"default",key:K,value:"default",layer:J,group:K,checked:M=="default",handler:function(){plotConfig.set(this.key,this.value,this.layer);if(this.layer!=null&&this.layer!="facet"){activeRecord=Ext.getCmp("layerList").getStore().getById(this.layer);Ext.getCmp("layerList").getSelectionModel().selectRecords([activeRecord])}else{y(plotConfig)}}})}for(thisVal=0;thisVal<I.length;thisVal++){if(I[thisVal]=="-"){L.add("-")}else{L.add({text:I[thisVal],key:K,value:N+I[thisVal],layer:J,group:K,checked:I[thisVal]==M,handler:function(){plotConfig.set(this.key,this.value,this.layer);if(this.key=="y"){if(this.value.substring(4)=="..count.."||this.value.substring(4)=="..density.."){Ext.getCmp("addLayerMenu1").enable();Ext.getCmp("addLayerMenu2").disable()}else{Ext.getCmp("addLayerMenu2").enable();Ext.getCmp("addLayerMenu1").disable()}}if(this.layer!=null&&this.layer!="facet"){activeRecord=Ext.getCmp("layerList").getStore().getById(this.layer);Ext.getCmp("layerList").getSelectionModel().selectRecords([activeRecord])}else{y(plotConfig)}}})}}return L}function q(){Ext.getCmp("plotMenu").removeAll();dataVariables=Ext.getCmp("workspace").dataVariables;Ext.Ajax.request({url:"menuData.json",success:function(M){try{k=Ext.decode(M.responseText);return(M.responseText)}catch(K){alert("error in decoding menuData.json")}finally{var L=k.Defaults;for(i=0;i<L.length;i++){if(L[i]=="-"){Ext.getCmp("plotMenu").add("-")}else{if(L[i].required){Ext.getCmp("plotMenu").add({id:"mapping"+L[i].name,text:"Map "+L[i].name+" (required)",iconCls:"icon_map",handler:function(){return false},menu:n(L[i].name,Ext.util.JSON.decode(Ext.util.JSON.encode(dataVariables)),"default",false,null,"map_")})}else{Ext.getCmp("plotMenu").add({id:"mapping"+L[i].name,text:"Map "+L[i].name,iconCls:"icon_map",handler:function(){return false},menu:n(L[i].name,Ext.util.JSON.decode(Ext.util.JSON.encode(dataVariables)),"default",true,null,"map_")})}}}facetMenu=Ext.getCmp("plotMenu").add({id:"facet",text:"Facet",iconCls:"icon_facet",handler:function(){return false},menu:{}});Ext.getCmp("plotMenu").add("-");facetProperties=k.Facet;for(thisaes=0;thisaes<facetProperties.length;thisaes++){thisProp=facetProperties[thisaes];if(thisProp.set){facetMenu.menu.add({text:thisProp.name,handler:function(){return false},iconCls:"icon_set",key:thisProp.name,menu:n(thisProp.name,thisProp.values,"default",true,"facet","set_")})}else{if(thisProp.map){facetMenu.menu.add({text:thisProp.name,handler:function(){return false},iconCls:"icon_map",key:thisProp.name,menu:n(thisProp.name,dataVariables,"default",true,"facet","map_")})}}}Ext.getCmp("plotMenu").add({id:"addLayerMenu1",text:"New Geom Layer (1D)",iconCls:"add",handler:function(){return false},disabled:true,menu:{}});var J=k.Geoms1;for(i=0;i<J.length;i++){Ext.getCmp("addLayerMenu1").menu.add({text:J[i].geom,iconCls:"icon_"+J[i].geom,geom:J[i].geom,aes:J[i].aes,values:J[i].values,id:"geom_"+J[i].geom,handler:function(){E(this.geom,this.aes)}})}Ext.getCmp("plotMenu").add({id:"addLayerMenu2",text:"New Geom Layer (2D)",iconCls:"add",handler:function(){return false},disabled:true,menu:{}});var I=k.Geoms2;for(i=0;i<I.length;i++){Ext.getCmp("addLayerMenu2").menu.add({text:I[i].geom,iconCls:"icon_"+I[i].geom,geom:I[i].geom,aes:I[i].aes,values:I[i].values,id:"geom_"+I[i].geom,handler:function(){E(this.geom,this.aes)}})}Ext.getCmp("plotMenu").add("-")}},failure:function(){alert("menuData.json could not be loaded")}})}imageContext=function(J,I,K){plotConfig=Ext.getCmp("workspace").plotConfig;m.showAt(J.getPoint());J.stopEvent()};var D=new Ext.FormPanel({id:"uploadPanel",border:true,margins:"3 3 0 3",fileUpload:true,bodyStyle:"padding: 5px 5px 5px 5px;",labelWidth:100,region:"north",height:45,items:{xtype:"fileuploadfield",id:"fileuploadfield",fieldLabel:'Upload new data eg: <a target="_blank" href="myMovies.csv">demodata</a>',buttonOnly:true,name:"datafile",listeners:{fileselected:function(J,I){if(D.getForm().isValid()){filename=Ext.getCmp("fileuploadfield").value;extension=filename.substring(filename.length-3,filename.length);if(extension=="csv"){var K='myData <- read.csv("'+filename+'", header=T);'}else{if(extension=="sav"){var K='myData <- read.spss("'+filename+'", to.data.frame=T);'}else{var K='myData <- read.table("'+filename+'", header=T);'}}l(K);D.getForm().submit({method:"post",url:"../brew/ggplot-upload",waitMsg:"Uploading data...",success:function(M,L){variables=L.result.variables[0].children.concat(L.result.variables[1].children);dataVariables=new Array();for(thisV=0;thisV<variables.length;thisV++){dataVariables[thisV]=variables[thisV].text}Ext.getCmp("workspace").dataVariables=dataVariables;q();Ext.getCmp("workspace").getEl().on("contextmenu",imageContext);rootNode=Ext.getCmp("tree-panel").getRootNode();while(rootNode.childNodes.length>0){rootNode.removeChild(rootNode.item(0))}Ext.getCmp("tree-panel").getRootNode().appendChild(L.result.variables);Ext.getCmp("workspace").plotConfig.dataFile=L.result.dataFile;Ext.getCmp("tree-panel").dataFile=L.result.dataFile;Ext.getCmp("tree-panel").varInfo=L.result.varInfo;Ext.getCmp("uploadPanel").hide();Ext.getCmp("dataPanel").doLayout();y(Ext.getCmp("workspace").plotConfig)},failure:function(M,L){alert("R catched an error: "+L.result.error)}})}}}}});var C=new Ext.tree.TreePanel({border:true,margins:"3 3 0 3",singleExpand:false,bodyStyle:"padding: 5px 5px 5px 5px;",id:"tree-panel",region:"center",iconCls:"downArrow",autoScroll:true,animate:true,rootVisible:false,lines:false,useArrows:true,root:new Ext.tree.AsyncTreeNode({id:"root1"})});var x={region:"south",border:true,margins:"3 3 3 3",id:"details-panel",height:165,bodyStyle:{background:"#ffffff",padding:"7px"},html:'<div id="generalDetailDiv"> <h1> General options </h1> </div>'};var f={id:"dataPanel",collapsible:true,region:"west",layout:"border",border:true,title:"Datapanel",margins:"5 5 5 5",width:200,items:[D,C,x]};var u={id:"workspace",layout:"fit",region:"center",html:'<div class="start-div"><div style="float:left; margin: 0 0 0 0; padding 0 0 0 0;" ><img src="images/layout-icon.gif" /></div><div style="display: block; margin-left:100px;"> <h2>ggplot2</h2><div><p>How to use:</p><p><b>1. Upload</b> a comma seperated (.csv), spss (.sav), or tab delimited (.txt) datafile. Make sure every column has a header.</p><p><b>2. Right mouseclick</b> somewhere in this plotarea to open the plot menu.</p> <p><b>3. Map X and Y</b> to a variable and <b>add Geom layers.</b></p><div id="demovideo"><p>Click <a href="http://www.youtube.com/watch?v=7XGN6OSCq6E&hd=1" target="_blank">Here</a> for a short how-to Demo Video!</p></div><p>Extra tricks:</p><p>- Click the tiny arrow in the bottom of the screen to open the <b>code panel</b>.</p><p>- Press <b>F11</b> to use this application in Full Screen.</p><p>- Press <b>F5</b> to start over with new data.</p></div></div></div>',border:true,margins:"5 0 5 0",buttons:[{text:"Draw plot",id:"drawButton",iconCls:"fit",handler:function(){G()}},{text:"Reset",iconCls:"reset",handler:function(){v()}}]};var F=new Ext.grid.GridPanel({id:"layerList",mode:"local",store:new Ext.data.ArrayStore({fields:["id","Geom"],data:[],idIndex:0,refreshData:function(){var I=this.getRange();this.removeAll();this.add(I)}}),columns:[new Ext.grid.RowNumberer(),{header:"Geom",width:120,dataIndex:"Geom",sortable:true}],sm:new Ext.grid.RowSelectionModel({singleSelect:true}),viewConfig:{forceFit:true},height:200,border:true,margins:"3 3 0 3",hideHeaders:true,autoScroll:true,region:"center"});var e={margins:"3 3 3 3",id:"layerDetails",layout:"fit",html:'<div id="detailDiv"> <h1> Layer options </h1> </div>',region:"south",height:200,bodyStyle:{background:"#ffffff",padding:"7px"},border:true,buttons:[{text:"Get PDF",id:"pdfbutton",iconCls:"pdf",handler:function(){c()}},{text:"Cite!",iconCls:"bib",handler:function(){w()}}]};var p={collapsible:true,region:"east",layout:"border",margins:"5 5 5 5",width:230,border:true,title:"Layers",items:[F,e]};var d={title:"Syntax",collapsible:true,collapsed:true,titleCollapse:false,floatable:false,header:false,collapseMode:"mini",layout:"anchor",id:"syntaxPanel",region:"south",border:false,items:[{border:false,applyTo:"historyCmp"},{border:false,height:25,applyTo:"syntaxdiv"}],height:100,listeners:{expand:B}};new Ext.Viewport({id:"viewport",bodyStyle:"background: none",layout:"border",cls:"viewport",title:"Ext Layout Browser",items:[{xtype:"box",region:"north",applyTo:"header",height:30},f,u,p,d],renderTo:Ext.getBody()});Ext.getCmp("workspace").plotConfig=new r();Ext.getCmp("syntaxPanel").getEl().on("dblclick",function(){Ext.getCmp("syntaxPanel").collapse()});var g;function w(){if(!g){g=new Ext.Window({closeAction:"hide",iconCls:"bib",title:"Citing lme4",closable:true,layout:"fit",height:400,width:700,autoScroll:false,items:{border:false,applyTo:"citeDiv"}})}g.show()}});