Ext.ns("Sonia.authormapping");
Sonia.authormapping.ConfigPanel=Ext.extend(Sonia.repository.PropertiesFormPanel,{mappingStore:null,formTitleText:"用户映射",colNameText:"用户名",colDisplayNameText:"显示名",colMailText:"电子邮件",authorMappingGridHelpText:"管理结果集作者的用户映射.<br />\n    <b>用户名</b> = 将要映射的作者用户.<br />\n    <b>显示名</b> = 将要显示的作者显示名.<br />\n    <b>电子邮件</b> = 新作者的邮件地址.",addText:"添加",removeTest:"删除",addIcon:"resources/images/add.gif",removeIcon:"resources/images/delete.gif",
helpIcon:"resources/images/help.gif",initComponent:function(){this.mappingStore=new Ext.data.ArrayStore({root:"mappings",fields:[{name:"name"},{name:"displayName"},{name:"mail"}],sortInfo:{field:"name"}});if(this.isNewVersion()){this.addIcon="resources/images/add.png";this.removeIcon="resources/images/delete.png";this.helpIcon="resources/images/help.png"}this.loadAuthorMappings(this.mappingStore,this.item);var mappingsColModel=new Ext.grid.ColumnModel({defaults:{sortable:true,editable:true},columns:[{id:"name",
dataIndex:"name",header:this.colNameText,editor:Ext.form.TextField},{id:"displayName",dataIndex:"displayName",header:this.colDisplayNameText,editor:Ext.form.TextField},{id:"mail",dataIndex:"mail",header:this.colMailText,editor:Ext.form.TextField}]});var selectionModel=new Ext.grid.RowSelectionModel({singleSelect:true});var config={title:this.formTitleText,items:[{id:"authorMappingGrid",xtype:"editorgrid",clicksToEdit:1,autoExpandColumn:"displayName",frame:true,width:"100%",autoHeight:true,autoScroll:false,
colModel:mappingsColModel,sm:selectionModel,store:this.mappingStore,viewConfig:{forceFit:true},tbar:[{text:this.addText,scope:this,icon:this.addIcon,handler:function(){var Mapping=this.mappingStore.recordType;var p=new Mapping;var grid=Ext.getCmp("authorMappingGrid");grid.stopEditing();this.mappingStore.insert(0,p);grid.startEditing(0,0)}},{text:this.removeText,scope:this,icon:this.removeIcon,handler:function(){var grid=Ext.getCmp("authorMappingGrid");var selected=grid.getSelectionModel().getSelected();
if(selected)this.mappingStore.remove(selected)}},"->",{id:"authorMappingGridHelp",xtype:"box",autoEl:{tag:"img",src:this.helpIcon}}]}]};Ext.apply(this,Ext.apply(this.initialConfig,config));Sonia.authormapping.ConfigPanel.superclass.initComponent.apply(this,arguments)},isNewVersion:function(){var result=false;try{var version=state.version;var parts=version.split(".");if(parts[0]!="1")result=true;else{parts=parts[1].split("-");result=parseInt(parts[0])>11}}catch(e){if(debug)console.debug(e)}return result},
afterRender:function(){Sonia.authormapping.ConfigPanel.superclass.afterRender.apply(this,arguments);Ext.QuickTips.register({target:Ext.getCmp("authorMappingGridHelp"),title:"",text:this.authorMappingGridHelpText,enabled:true})},loadAuthorMappings:function(store,repository){if(debug)console.debug("load author mapping properties");if(!repository.properties)repository.properties=[];Ext.each(repository.properties,function(prop){if(prop.key=="sonia.authormapping.manual-mapping"){var value=prop.value;this.parseMappings(store,
value)}},this)},parseMappings:function(store,mappingString){var parts=mappingString.split(";");Ext.each(parts,function(part){var pa=part.split(",");if(pa.length>0&&pa[0].length>0){var mail="";if(pa.length>1)mail=pa[2].trim();var Mapping=store.recordType;var am=new Mapping({name:pa[0].trim(),displayName:pa[1].trim(),mail:mail});if(debug){console.debug("add author mapping: ");console.debug(am)}store.add(am)}})},storeExtraProperties:function(repository){if(debug)console.debug("store author mapping properties");
Ext.each(repository.properties,function(prop,index){if(prop.key=="sonia.authormapping.manual-mapping")delete repository.properties[index]});var mappingsString="";this.mappingStore.data.each(function(r){var am=r.data;mappingsString+=am.name+","+am.displayName+","+am.mail+";"});if(debug)console.debug("add author mapping string: "+mappingsString);repository.properties.push({key:"sonia.authormapping.manual-mapping",value:mappingsString})}});Ext.reg("authormappingConfigPanel",Sonia.authormapping.ConfigPanel);
Sonia.repository.openListeners.push(function(repository,panels){if(Sonia.repository.isOwner(repository))panels.push({xtype:"authormappingConfigPanel",item:repository})});