Chinaunix首页 | 论坛 | 博客
  • 博客访问: 351980
  • 博文数量: 26
  • 博客积分: 495
  • 博客等级: 下士
  • 技术积分: 562
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-26 13:50
文章分类

全部博文(26)

文章存档

2015年(9)

2014年(6)

2013年(7)

2012年(2)

2011年(2)

分类: JavaScript

2013-08-21 10:26:01

通常管理系统都是采用extjs 的border布局,即左边菜单中心工作区,顶部是log还有个人信息,状态等,由于之前对Extjs不太熟悉,各个组件写的比较散乱,层次结构很糟乱,这样会导致程序维护麻烦,代码重复比较多,非常影响浏览速度,找了很多资料,《Extjs in Action》给了我很大的启发,目前已经将自己构建的系统框架雏形已经完成,基本实现我的需求,代码如下,方便来日参考,无奈是使用的3版本,4中已经有Ext.define 这样的句子了,顺带说一句尽量用4版本

点击(此处)折叠或打开

  1. Ext.BLANK_IMAGE_URL = 'images/s.gif';
  2. Ext.QuickTips.init();
  3. Ext.lib.Ajax.defaultPostHeader += ";charset=utf-8";

  4. /**
  5.  * @class Kingnet.Yunwei.App
  6.  * 运维管理系统前端APP
  7.  *

  8.  * @constructor
  9.  * @singleton
  10.  **/
  11. //名称空间
  12. Ext.ns("Kingnet", "Kingnet.Yunwei");

  13. //Kingnet.Yunwei.App = function(){
  14. Kingnet.Yunwei.App= function(){
  15.     var viewport,loginWindow,cookieUtil = Ext.util.Cookies;
  16.     //treepanel 默认属性
  17.     var deFaults = {
  18.         loader:new Ext.tree.TreeLoader({
  19.             dataUrl:'/test/getTree.php',
  20.             listeners:{
  21.                 'beforeload':function(loader,node,cb){
  22.                     loader.dataUrl = '/test/getTree.php?&id='+ node.id;
  23.                 }

  24.             }
  25.         }),
  26.         listeners:{
  27.             'click':function(node,event){
  28.                 if(node && node.isLeaf()){
  29.                     LazyLoad.js(Ext.util.Format.capitalize(node.attributes.id)+'.js',function(){
  30.                             var ct,tp,p,np;
  31.                             ct = node.getOwnerTree().ownerCt.ownerCt;
  32.                             tp = ct.getComponent('main');//取得主工作区
  33.                             p = tp.getComponent(node.id);
  34.                             if(p){
  35.                                 tp.setActiveTab(p)
  36.                             }
  37.                             else{
  38.                                 tp.add(new Test.Office.FinancialPanel({
  39.                                     title:node.attributes.text,
  40.                                     closable : true,
  41.                                     itemId:node.attributes.id
  42.                                 })).show();

  43.                             }
  44.                     })
  45.                 
  46.                 }
  47.             }
  48.         }
  49.     };

  50.     return {
  51.         init:function(){

  52.             if(!cookieUtil.get('loginCookie')){
  53.                 if (! loginWindow){
  54.                     loginWindow = this.buildLoginWindow();
  55.                     //console.log('not login!');
  56.                 }
  57.                 loginWindow.show();
  58.             }
  59.             else{
  60.                 //console.log(Ext.get(this.header.contentEl));
  61.                 console.log('logined!!!');
  62.                 this.buildViewport();
  63.                 username = cookieUtil.get('loginCookie');
  64.                 console.log(cookieUtil.get('loginCookie'));
  65.                 //this.test();
  66.             }
  67.             
  68.         },

  69.         buildLoginWindow:function(){
  70.             return new Kingnet.Yunwei.UserLoginWindow({
  71.                 title    :'登入运维管理系统',
  72.                 scope : this,
  73.                 handler : this.onLogin
  74.             })
  75.         },

  76.         //顶部
  77.         buildHeader : function(){
  78.             return {
  79.                 xtype:'panel',
  80.                 region : 'north',
  81.                 border:false,
  82.                 height:60,
  83.                 bbar:new Ext.Toolbar({
  84.                     defaults:{height:60},
  85.                     items:[
  86.                     '->',{
  87.                         xtype:'label',
  88.                         text:'当前用户',
  89.                     },
  90.                     '-',{
  91.                         xtype:'label',
  92.                         html:''+cookieUtil.get('loginCookie')+'',
  93.                     },
  94.                     '-',{
  95.                         xtype:'button',
  96.                         text:'修改密码',
  97.                         scope:this,
  98.                         handler:function(){}
  99.                     },
  100.                     '-',{
  101.                         xtype:'button',
  102.                         text:'退出系统',
  103.                         scope:this,
  104.                         handler:this.onLogOut
  105.                     }]
  106.                 })
  107.             }
  108.         },
  109.     

  110.         //底部
  111.         footer:new Ext.Panel({
  112.             region : 'south',
  113.             height:35,
  114.             html : '
    '
  115.                 + '
    '
  116.                 + 'Power By:    Anthony  
'
  •                 + ''
  •                 + '版权所有:
  • '
  •                 + '
  • ',

  •         }),

  •         //左侧菜单
  •         menupanel:new Ext.Panel({
  •             title: '系统菜单',
  •             width : 200,
  •             split : true,
  •             region : 'west',
  •             collapseMode :'mini',
  •             layout: 'accordion',
  •             defaults:deFaults,
  •             layoutConfig : {
  •                 animate : true,
  •             },
  •             items:[
  •             {
  •                 xtype:'treepanel',
  •                 containerScroll:true,
  •                 itemId:'sheetManage',
  •                 title:'报表管理',
  •                 animate:true,
  •                 containerScroll:true,
  •                 root:new Ext.tree.AsyncTreeNode({
  •                     expanded:true,
  •                     text:'报表管理',
  •                     id:'2',
  •                     draggable:false,
  •                 }),
  •             },{
  •                 xtype:'treepanel',
  •                 containerScroll:true,
  •                 animate:true,
  •                 title:'应用管理',
  •                 containerScroll:true,
  •                 root:new Ext.tree.AsyncTreeNode({
  •                     expanded:true,
  •                     text:'应用管理',
  •                     id:'1',
  •                     draggable:false
  •                 }),    
  •             },{
  •                 xtype:'treepanel',
  •                 containerScroll:true,
  •                 animate:true,
  •                 containerScroll:true,
  •                 title:'资产管理',
  •                 root:new Ext.tree.AsyncTreeNode({
  •                     expanded:true,
  •                     text:'资产管理',
  •                     id:'0',
  •                     draggable:false
  •                 }),
  •             },{
  •                 xtype:'treepanel',
  •                 containerScroll:true,
  •                 animate:true,
  •                 title:'权限管理',
  •                 containerScroll:true,
  •                 root:new Ext.tree.AsyncTreeNode({
  •                     expanded:true,
  •                     text:'权限管理',
  •                     id:'3',
  •                     draggable:false
  •                 }),
  •             }],

  •         }),
  •         
  •         //中心区域
  •         mainpanel:new Ext.TabPanel({
  •             region:'center',
  •             activeTab : 0,
  •             itemId:'main',
  •             //id:'main',
  •             enableTabScroll : true,
  •             items : [{
  •                 title : '首页',
  •                 html : '
    welcom
    '

  •             }]

  •         }),
  •         
  •         buildViewport:function(){
  •             viewport = new Ext.Viewport({
  •                 layout:'border',
  •                 renderTo:'viewport',
  •                 items:[this.buildHeader(),this.menupanel,this.mainpanel,this.footer],
  •             });
  •             Ext.getBody().unmask();

  •         },

  •         onLogin: function() {
  •             var form = loginWindow.get(0);
  •             if (form.getForm().isValid()) {
  •                 loginWindow.el.mask('Please wait...', 'x-mask-loading');
  •                 form.getForm().submit({
  •                     success : this.onLoginSuccess,
  •                     failure : this.onLoginFailure,
  •                     scope : this
  •                 });
  •             }
  •         },

  •         onLoginSuccess : function() {
  •             loginWindow.el.unmask();
  •             var cookie = cookieUtil.get('loginCookie');
  •             if (cookie) {
  •                 this.buildViewport();
  •                 loginWindow.destroy();
  •                 loginWindow = null;
  •             }
  •             else {
  •                 this.onLoginFailure();
  •             }
  •         },

  •         onLoginFailure : function() {
  •             loginWindow.el.unmask();
  •             Ext.MessageBox.alert('Warning', "登入失败,请检查密码是否正确!");
  •         },


  •         onLogOut : function() {
  •             Ext.MessageBox.confirm(
  •                 'Please confirm',
  •                 'Are you sure you want to log out?',
  •                 function(btn) {
  •                     if (btn === 'yes') {
  •                         Ext.getBody().mask('Logging out...', 'x-mask-loading');    
  •                         Ext.Ajax.request({
  •                             url : 'userlogout.php',
  •                             params : { user : cookieUtil.get('loginCookie') },
  •                             scope : this,
  •                             success:function(){
  •                                 Ext.getBody().unmask();
  •                                 window.location.href='' //your URL
  •                             },
  •                             failure:function(){
  •                                 alert('somthing was wrong!!!');
  •                             }
  •                         });

  •                     }
  •                 },
  •                 this
  •             );
  •         }
  •     }
  • }();

  • Ext.onReady(Kingnet.Yunwei.App.init,Kingnet.Yunwei.App);



  • 主要实现方式:
    1.采用异步树节点加载,可以根据登入用户角色加载配置
    2.动态加载js文件,按需加载,当用户点击treenode 会加载相应的js文件

    loginwindow:

    点击(此处)折叠或打开

    1. Ext.ns("Kingnet.Yunwei");

    2. Kingnet.Yunwei.UserLoginWindow = Ext.extend(Ext.Window, {
    3.     initComponent : function() {
    4.         // Force defaults
    5.         Ext.apply(this, {
    6.             width : 250,
    7.             height : 125,
    8.             border :false,
    9.             modal : true,
    10.             draggable : false,
    11.             renderTo : 'logindiv'
    12.             layout : 'fit',
    13.             closable :true,
    14.             center : true,
    15.             closable : false,
    16.             resizable : false,
    17.             border : false,
    18.             items : this.buildForm(),
    19.             buttons : [
    20.                 {
    21.                     text : 'Login',
    22.                     handler : this.handler || Ext.emptyFn,
    23.                     scope : this.scope || this
    24.                 }
    25.             ]
    26.         });

    27.         Kingnet.Yunwei.UserLoginWindow.superclass.initComponent.call(this,arguments);
    28.     },
    29.     //private builds the form.
    30.     buildForm : function() {

    31.         var formItemDefaults = {
    32.             allowBlank : false,
    33.             anchor : '-5',
    34.             listeners : {
    35.                 scope : this,
    36.                 specialkey : function(field, e) {
    37.                     if (e.getKey() === e.ENTER && this.handler) {
    38.                         this.handler.call(this.scope);
    39.                     }
    40.                 }
    41.             }
    42.         };

    43.         return {
    44.             xtype : 'form',
    45.             defaultType : 'textfield',
    46.             labelWidth : 70,
    47.             frame : true,
    48.             url : 'login.php',
    49.             labelAlign : 'right',
    50.             defaults : formItemDefaults,
    51.             items : [
    52.                 {
    53.                     fieldLabel : '用户名 ',
    54.                     name : 'user'
    55.                 },
    56.                 {
    57.                     inputType : 'password',
    58.                     fieldLabel : '密码 ',
    59.                     name : 'password'
    60.                 }
    61.             ]
    62.         };
    63.     }

    64. });

    app.html:

    点击(此处)折叠或打开

    1. <html>
    2.         <head>
    3.                 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    4.                 <link rel="stylesheet" type="text/css" href="extjs3/resources/css/ext-all.css" />
    5.         <link rel="stylesheet" type="text/css" href="extjs3/examples/ux/css/RowEditor.css" />
    6.                 <script type="text/javascript" src="extjs3/adapter/ext/ext-base.js"></script>
    7.                 <script type="text/javascript" src="extjs3/ext-all-debug.js"></script>
    8.                 <script type="text/javascript" src="./UserLoginWindow.js"></script>
    9.                 <script type="text/javascript" src="./lazyload.js"></script>
    10.                 <script type="text/javascript" src="./WorkSpace.js"></script>
    11.                 <title>资产管理系统</title>
    12.         </head>
    13.         <style>
    14.         #logindiv {
    15.                 /*background: blue;*/
    16.                 width:100%;
    17.                 height:100%;
    18.         }
    19.         </style>
    20.         <body>
    21.                 <div id='logindiv' ></div>
    22.                 <div id = 'viewport'></div>
    23.         </body>
    24. </html



    阅读(7761) | 评论(3) | 转发(0) |
    给主人留下些什么吧!~~

    howge2013-11-05 14:58:42

    dataUrl:'/test/getTree.php',
                listeners:{
                    'beforeload':function(loader,node,cb){
                        loader.dataUrl = '/test/getTree.php?&id='+ node.id;
                    }

         &n

    howge2013-09-11 10:18:26

    guojieflying:哥们有4.x的框架么

    没有,其实我觉得4会更加好弄,我对4不是很熟悉

    回复 | 举报

    guojieflying2013-09-10 15:55:36

    哥们有4.x的框架么