Ext.ns("Sonia.branchwp");
Sonia.branchwp.ConfigPanel=Ext.extend(Sonia.repository.PropertiesFormPanel,{formTitleText:"分支写保护",enabledText:"已启用",colBranchText:"分支",colNameText:"用户名",colGroupText:"是用户组",addText:"添加",removeTest:"删除",addIcon:"resources/images/add.gif",removeIcon:"resources/images/delete.gif",helpIcon:"resources/images/help.gif",enableHelpText:"启用分支写保护。\n    仅管理权，仓库拥有者以及置顶的人可以进行写入操作。",branchwpGridHelpText:"分支写保护白名单。 <b>提示:</b> 你可以使用通配符作为分支名称.",
branchwpStore:null,initComponent:function(){this.branchwpStore=new Ext.data.ArrayStore({root:"permissions",fields:[{name:"branch"},{name:"name"},{name:"group",type:"boolean"}],sortInfo:{field:"branch"}});this.loadBranchwpPermissions(this.branchwpStore,this.item);var selectionModel=new Ext.grid.RowSelectionModel({singleSelect:true});var branchwpColModel=new Ext.grid.ColumnModel({defaults:{sortable:true,editable:true},columns:[{id:"branch",dataIndex:"branch",header:this.colBranchText,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:"branchwpEnabled",inputValue:"true",property:"branchwp.enabled",helpText:this.enableHelpText},{id:"branchwpGrid",xtype:"editorgrid",clicksToEdit:1,autoExpandColumn:"branch",frame:true,width:"100%",autoHeight:true,autoScroll:false,
colModel:branchwpColModel,sm:selectionModel,store:this.branchwpStore,viewConfig:{forceFit:true},tbar:[{text:this.addText,scope:this,icon:this.addIcon,handler:function(){var Permission=this.branchwpStore.recordType;var p=new Permission;var grid=Ext.getCmp("branchwpGrid");grid.stopEditing();this.branchwpStore.insert(0,p);grid.startEditing(0,0)}},{text:this.removeText,scope:this,icon:this.removeIcon,handler:function(){var grid=Ext.getCmp("branchwpGrid");var selected=grid.getSelectionModel().getSelected();
if(selected)this.branchwpStore.remove(selected)}},"->",{id:"branchwpGridHelp",xtype:"box",autoEl:{tag:"img",src:this.helpIcon}}]}]};Ext.apply(this,Ext.apply(this.initialConfig,config));Sonia.branchwp.ConfigPanel.superclass.initComponent.apply(this,arguments)},afterRender:function(){Sonia.branchwp.ConfigPanel.superclass.afterRender.apply(this,arguments);Ext.QuickTips.register({target:Ext.getCmp("branchwpGridHelp"),title:"",text:this.branchwpGridHelpText,enabled:true})},loadBranchwpPermissions:function(store,
repository){if(debug)console.debug("load branchwp properties");if(!repository.properties)repository.properties=[];Ext.each(repository.properties,function(prop){if(prop.key=="branchwp.permissions"){var value=prop.value;this.parsePermissions(store,value)}},this)},parsePermissions:function(store,permString){var parts=permString.match(/[^,]+,[^,]+;/g);if(debug){console.debug("branch permissions:");console.debug(parts)}Ext.each(parts,function(part){if(part.endsWith(";"))part=part.substring(0,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({branch: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 branchpw properties");Ext.each(repository.properties,function(prop,index){if(prop.key=="branchwp.permissions")delete repository.properties[index]});var permissionString=
"";this.branchwpStore.data.each(function(r){var p=r.data;permissionString+=p.branch+",";if(p.group)permissionString+="@";permissionString+=p.name+";"});if(debug)console.debug("add branchwp permission string: "+permissionString);repository.properties.push({key:"branchwp.permissions",value:permissionString})}});Ext.reg("branchwpConfigPanel",Sonia.branchwp.ConfigPanel);
Sonia.repository.openListeners.push(function(repository,panels){if(Sonia.repository.isOwner(repository)){var type=Sonia.repository.getTypeByName(repository.type);if(type&&type.supportedCommands&&type.supportedCommands.indexOf("BRANCHES")>=0)panels.push({xtype:"branchwpConfigPanel",item:repository})}});