Ext.ns("Sonia.pathwp");
Sonia.pathwp.ConfigPanel=Ext.extend(Sonia.repository.PropertiesFormPanel,{formTitleText:"路径写保护",enabledText:"启用",colPathText:"路径",colNameText:"用户名",colGroupText:"是用户组",addText:"添加",removeTest:"移除",addIcon:"resources/images/add.gif",removeIcon:"resources/images/delete.gif",enableHelpText:"启用路径写保护. \n    只有管理员，拥有者和白名单中的用户可以进行写入操作。",pathwpGridHelpText:"Path write protection whitelist.",pathwpStore:null,
initComponent:function(){this.pathwpStore=new Ext.data.ArrayStore({root:"permissions",fields:[{name:"path"},{name:"name"},{name:"group",type:"boolean"}],sortInfo:{field:"path"}});this.loadPathwpPermissions(this.pathwpStore,this.item);var selectionModel=new Ext.grid.RowSelectionModel({singleSelect:true});var pathwpColModel=new Ext.grid.ColumnModel({defaults:{sortable:true,editable:true},columns:[{id:"path",dataIndex:"path",header:this.colPathText,editor:Ext.form.TextField},{id:"name",dataIndex:"name",
header:this.colNameText,editor:Ext.form.TextField},{id:"group",dataIndex:"group",xtype:"checkcolumn",header:this.colGroupText,width:40,editable:true}]});var config={title:this.formTitleText,items:[{xtype:"checkbox",fieldLabel:this.enabledText,name:"pathwpEnabled",inputValue:"true",property:"pathwp.enabled",helpText:this.enableHelpText},{id:"pathwpGrid",xtype:"editorgrid",clicksToEdit:1,autoExpandColumn:"path",frame:true,width:"100%",autoHeight:true,autoScroll:false,colModel:pathwpColModel,sm:selectionModel,
store:this.pathwpStore,viewConfig:{forceFit:true},tbar:[{text:this.addText,scope:this,icon:this.addIcon,handler:function(){var Permission=this.pathwpStore.recordType;var p=new Permission;var grid=Ext.getCmp("pathwpGrid");grid.stopEditing();this.pathwpStore.insert(0,p);grid.startEditing(0,0)}},{text:this.removeText,scope:this,icon:this.removeIcon,handler:function(){var grid=Ext.getCmp("pathwpGrid");var selected=grid.getSelectionModel().getSelected();if(selected)this.pathwpStore.remove(selected)}}]}]};
Ext.apply(this,Ext.apply(this.initialConfig,config));Sonia.pathwp.ConfigPanel.superclass.initComponent.apply(this,arguments)},loadPathwpPermissions:function(store,repository){if(debug)console.debug("load pathpw properties");if(!repository.properties)repository.properties=[];Ext.each(repository.properties,function(prop){if(prop.key=="pathwp.permissions"){var value=prop.value;this.parsePermissions(store,value)}},this)},parsePermissions:function(store,permString){var parts=/\[[^,]+,[^,]+\]/g.exec(permString);
parts=permString.match(/\[[^,]+,[^,]+\]/g);if(debug){console.debug("path permissions:");console.debug(parts)}Ext.each(parts,function(part){part=part.substring(1,part.length-1);var pa=part.split(",");var group=false;if(pa[1].indexOf("@")==0){group=true;pa[1]=pa[1].substring(1)}var Permission=store.recordType;var p=new Permission({path:pa[0],group:group,name:pa[1]});if(debug){console.debug("add permission: ");console.debug(p)}store.add(p)})},storeExtraProperties:function(repository){if(debug)console.debug("store pathpw properties");
Ext.each(repository.properties,function(prop,index){if(prop.key=="pathwp.permissions")delete repository.properties[index]});var permissionString="";this.pathwpStore.data.each(function(r){var p=r.data;permissionString+="["+p.path+",";if(p.group)permissionString+="@";permissionString+=p.name+"]"});if(debug)console.debug("add pathwp permission string: "+permissionString);repository.properties.push({key:"pathwp.permissions",value:permissionString})}});Ext.reg("pathwpConfigPanel",Sonia.pathwp.ConfigPanel);
Sonia.repository.openListeners.push(function(repository,panels){if(Sonia.repository.isOwner(repository))panels.push({xtype:"pathwpConfigPanel",item:repository})});