/****组件****/

//文章网格
Ext.ArticleGrid=Ext.extend(Ext.grid.GridPanel ,{
xtype:'grid',
id:'show-btn',
title:'所有文章',
width:'100%',
height:500,
columns:[
{
header:'编号',
sortable:true,
resizable:true,
dataIndex:'id',
width:'10%'
},
{
header:'标题',
sortable:true,
resizable:true,
dataIndex:'title',
width:'70%'
},
{
header:'作者',
sortable:true,
resizable:true,
dataIndex:'updateTime',
width:'10%'
}
,
{
header:'更新时间',
sortable:true,
resizable:true,
dataIndex:'updateTime',
width:'10%'
}
],
 bbar: new Ext.PagingToolbar({
        store: articleStore,       
        displayInfo: true,
        pageSize: 5,
        prependButtons: true
    }),
    listeners:{
     "rowdblclick":queryArticleForUpdateEvent
    }, initComponent: function(){
Ext.ArticleGrid.superclass.initComponent.call(this);
}
})

Ext.AddArticleForm=Ext.extend(Ext.form.FormPanel ,{
xtype:"form",
title:'',
labelWidth:100,
labelAlign:"left",
layout:"form",
width:'100%',
heigth:'100%',
bodyStyle:'padding:5px,5px,5px,5px;',
initComponent: function(){
this.items=[
{
name:'article.title',
xtype:"textfield",
fieldLabel:"标题",
anchor:"90%"
},
{
name:'article.keyWords',
xtype:"textfield",
fieldLabel:"关键字",
anchor:"70%"
},
{
hiddenName:'article.classify.id',
xtype:"combo",
triggerAction:"all",
fieldLabel:"分类",
loadingText:"正在加载数据...",
anchor:"50%",
 store : articleClsStore,
 valueField : "id",
 displayField : "name",
 selectOnFocus:true
},
{
hiddenName:'article.special.id',
xtype:"combo",
triggerAction:"all",
fieldLabel:"专题",
loadingText:"正在加载数据...",
anchor:"50%",
 store : articleSpcStore,
 valueField : "id",
 displayField : "name",
 selectOnFocus:true
},{
hiddenName:'article.addTime',
xtype:"datefield",
fieldLabel:"有效时间",
anchor:"50%",
format: 'Y-m-d H:i:s'
},{
name:'article.mainContent',
xtype:"textarea",
anchor:"90%",
fieldLabel:"内容",
height:600,
width:450
},{
xtype:"button",
text:"保 存",
listeners:{
'click':addArticleEvent
}
}
],
Ext.AddArticleForm.superclass.initComponent.call(this);
}
})

Ext.UpdateArticleForm=Ext.extend(Ext.form.FormPanel ,{
xtype:"form",
title:'',
labelWidth:100,
labelAlign:"left",
layout:"form",
width:'100%',
heigth:'100%',
bodyStyle:'padding:5px,5px,5px,5px;',
initComponent: function(){
this.items=[
{
name:'article.id',
xtype:"hidden",
anchor:"90%"
},
{
name:'article.title',
xtype:"textfield",
fieldLabel:"标题",
anchor:"90%"
},
{
name:'article.keyWords',
xtype:"textfield",
fieldLabel:"关键字",
anchor:"70%"
},
{
hiddenName:'article.classify.id',
xtype:"combo",
triggerAction:"all",
fieldLabel:"分类",
loadingText:"正在加载数据...",
anchor:"50%",
 store : articleClsStore,
 valueField : "id",
 displayField : "name",
 selectOnFocus:true
},
{
hiddenName:'article.special.id',
xtype:"combo",
triggerAction:"all",
fieldLabel:"专题",
loadingText:"正在加载数据...",
anchor:"50%",
 store : articleSpcStore,
 valueField : "id",
 displayField : "name",
 selectOnFocus:true
},{
hiddenName:'article.addTime',
xtype:"datefield",
fieldLabel:"有效时间",
anchor:"50%",
format: 'Y-m-d H:i:s'
},{
name:'article.mainContent',
xtype:"textarea",
anchor:"90%",
fieldLabel:"内容",
height:600,
width:450
},{
xtype:"button",
text:"我的按钮",
listeners:{
'click':addArticleEvent
}
}
],
Ext.UpdateArticleForm.superclass.initComponent.call(this);
}
})

/****数据源****/
var articleStore = new Ext.data.Store({
reader:new Ext.data.JsonReader({
   totalRecords:'totalCount',//分页时需要的参数
   root:'root',//json格式的根节点
   fields:[      //在数据库中映射的字段名
    {name:'id',type:'int'},
    {name:'title',type:'String'},
    {name:'updateTime', type: 'date', dateFormat: 'n/j h:ia'}
   ]
  }),
                  //发送请求得到以上数据
  proxy:new Ext.data.HttpProxy({
  url:webRoot + '/manager/Article)list'
  }) }); var articleClsStore = new Ext.data.Store({
reader:new Ext.data.JsonReader({
   totalRecords:'totalCount',//分页时需要的参数
   root:'root',//json格式的根节点
   fields:[      //在数据库中映射的字段名
    {name:'id',type:'int'},
    {name:'name',type:'String'}
   ]
  }),
                  //发送请求得到以上数据
  proxy:new Ext.data.HttpProxy({
  url:webRoot + '/manager/ArticleClassify)list'
  }) }); var articleSpcStore = new Ext.data.Store({
reader:new Ext.data.JsonReader({
totalRecords:'totalCount',
root:'root',
fields:[    
              {name:'id',type:'int'},
              {name:'name',type:'String'}
              ]
}),
proxy:new Ext.data.HttpProxy({
url:webRoot + '/manager/ArticleSpecail)list'
})

});/****初始化****/

var articleGrid = new Ext.ArticleGrid({});
var articleWin ;
var articleForm ;
articleStore.load();
articleGrid.store = articleStore;
articleGrid.doLayout();

/****事件****/ function addArticleEvent(){
articleForm.getForm().submit({
          waitMsg: '正在提交数据...',
          waitTitle: '提示',
          method: 'post',
     url:webRoot + '/manager/Article)add',
          success: function(form, action) {
           var flag=action.result.success;
           Ext.Msg.alert('提示',flag);
           if(flag == '提交成功'){
           Ext.Msg.alert('提示',flag);
       addArticleForm.getForm().reset();
           }
         },
         failure: function(form, action) {
             Ext.Msg.alert('错误', action.result.error);
         }
     });
} function queryArticleForUpdateEvent(){
var btn = Ext.get('show-btn');
articleWin = new Ext.ModelWin({title:'文章管理 - 编辑',width:800,height:700});
articleForm = new Ext.UpdateArticleForm({});
articleWin.addListener({'close':refreshArticleList});
articleWin.add(articleForm);
articleWin.show(btn);
var fckobj = new FCKeditor('article.mainContent', "100%", "450px");
fckobj.ToolbarSet = 'Default';
fckobj.ReplaceTextarea();
var record =  articleGrid.getSelectionModel().getSelected();
var id = record.get('id');
articleForm.getForm().reader = new Ext.data.JsonReader({},
           [ 
             {name:'article.id',type:'int',mapping:'id'},
             {name:'article.title',type:'string',mapping:'title'},
             {name:'article.keyWords',type:'string',mapping:'keyWords'},
             {name:'article.mainContent',type:'string',mapping:'mainContent'}
             ])
articleForm.load({url:'/RHSite/manager/Article)queryForId?id=' + id });
}

function refreshArticleList(){
articleGrid.getView().refresh();