• 创建MSN界面式的Ext JS布局


    今天开始的几篇帖子都是关于Ext布局的。伴随这一系列开始,我打算以制作一个MSN式的界面展开内容,也就是一个通讯器,通讯器它有展现联系人、联系人状态的地方,还有展现回话的区域。我们的目标不是实现一个消息传递系统,只是介绍其用户界面的构建过程,并有一些教学为目的的代码完成全文。

    首先介绍通讯器的“联系人”区域部分,以Ext.Window作为容器。位于Window之中我会放置若干控件,控件应符合以下需求:

    • 显示用户名称、头像和当前的状态。
    • 可以让用户改变她当前的状态,可选:“在线、忙、离开或离线”。
    • 可以让用户分享一段简洁的信息(译注:类似于QQ的签名功能),并告知她在听着什么的音乐/歌曲。
    • 可以让用户进入她的个人信息、联系卡片或空间。
    • 显示用户的联系列表,以“是否最爱、是否在线、是否离线”划分。

    接下来的帖子我会继续增强这界面。到该贴介绍完毕之时,我们所弄的Window应该会是像这样子:

    Ext JS tutorial

    样子好像有点熟悉吧?:-)

    好的,马上开工!

    通讯器窗体

    前面已经说过了,我们采用Window作为容器。现在所做的只是设置窗体的标题和尺寸。

    1. var wnd = new Ext.Window({   
    2.      300,   
    3.     height: 450,   
    4.     layout:'fit',   
    5.     title: 'Instant Messenger'   
    6. });  

    截图如下:

    messenger-4

    显示用户名称、头像和当前的状态

    显示用户名称、头像和状态的话就使用窗体的工具条toolbar。第一步我是第一个工具条(如代码中的tbar ),并创建一个按钮组把工具条分为两列(columns),一列用于头像,一列用于显示用户的名称:

    1. var wnd = new Ext.Window({  
    2.      300,   
    3.     height: 450,   
    4.     layout:'fit',   
    5.     title: 'Instant Messenger',   
    6.     tbar: [{ xtype: 'buttongroup', columns: 2 }]   
    7. });  

    头像和用户名都做成为工具条中的按钮。这样做的目的是我想用户点击头像和用户名的时候,就会执行一些函数。Window现在就变为:

    1. <pre>  
    2. var wnd = new Ext.Window({   
    3.      300, height: 450,   
    4.     layout:'fit',  
    5.     title: 'Instant Messenger',  
    6.     tbar: [{   
    7.         xtype: 'buttongroup',   
    8.         columns: 2,   
    9.         items: [  
    10.             {   
    11.                 xtype: 'button', scale: 'large', iconCls: 'icon-user', rowspan: 2   
    12.             },  
    13.            
    14.             {   
    15.                 xtype: 'splitbutton', text: 'Jorge (available)'   
    16.             }  
    17.          ]   
    18.     }]   
    19. });</pre>  

    看起来还不错吧!?

    messenger-5

    用户改变其状态

    要改变某种状态,我们在用户名的按钮中插入一个菜单,如下代码:

    1. var wnd = new Ext.Window({  
    2.      300,  
    3.     height: 450,  
    4.     layout:'fit',  
    5.     title: '即時信息',  
    6.     tbar: [{  
    7.         xtype: 'buttongroup',  
    8.         columns: 2,  
    9.         items: [{ xtype: 'button', scale: 'large', iconCls: 'icon-user', rowspan: 2 },  
    10.             { xtype: 'splitbutton', text: 'Jorge (available)',  
    11.                 menu: [{ text: 'Available', iconCls: 'ico-sts-available' },  
    12.                         { text: 'Busy', iconCls: 'ico-sts-busy' },  
    13.                         { text: 'Away', iconCls: 'ico-sts-away' },  
    14.                         { text: 'Appear Offline', iconCls: 'ico-sts-offline'}]  
    15.         }]  
    16.     }]  
    17. });  
    结果如下:

    messenger-6

    让用户分享一段简洁的信息,或她在听着什么的音乐/歌。

    要实现该功能,我使用了split 按钮,就放在用户名按钮的下面:

    1. var wnd = new Ext.Window({  
    2.      300,  
    3.     height: 450,  
    4.     layout:'fit',  
    5.     title: 'Instant Messenger',  
    6.     tbar: [{  
    7.         xtype: 'buttongroup',  
    8.         columns: 2,  
    9.         items: [{ xtype: 'button', scale: 'large', iconCls: 'icon-user', rowspan: 2 },  
    10.             { xtype: 'splitbutton', text: 'Jorge (available)',  
    11.                 menu: [{ text: 'Available', iconCls: 'ico-sts-available' },  
    12.                         { text: 'Busy', iconCls: 'ico-sts-busy' },  
    13.                         { text: 'Away', iconCls: 'ico-sts-away' },  
    14.                         { text: 'Appear Offline', iconCls: 'ico-sts-offline'}]  
    15.             },{ xtype: 'splitbutton', text: 'Share a quick message',  
    16.                 menu: [{ text: 'Show what I am listening to'}]  
    17.          }]  
    18.     }]  
    19. });  
    messenger-7

    让用户进入她的个人信息、联系卡片或空间

    我将如上的功能加到头像的那个按钮中去:

    1. var wnd = new Ext.Window({  
    2.      300,  
    3.     height: 450,  
    4.     layout:'fit',  
    5.     title: 'Instant Messenger',  
    6.     tbar: [{  
    7.         xtype: 'buttongroup',  
    8.         columns: 2,  
    9.         items: [{ xtype: 'button', scale: 'large', iconCls: 'icon-user', rowspan: 2,  
    10.         menu: [{ text: 'Show profile' }, { text: 'View contact card' },  
    11.                { text: 'Go to your space'}]  
    12.             },{ xtype: 'splitbutton', text: 'Jorge (available)',  
    13.                 menu: [{ text: 'Available', iconCls: 'ico-sts-available' },  
    14.                         { text: 'Busy', iconCls: 'ico-sts-busy' },  
    15.                         { text: 'Away', iconCls: 'ico-sts-away' },  
    16.                         { text: 'Appear Offline', iconCls: 'ico-sts-offline'}]  
    17.             },{ xtype: 'splitbutton', text: 'Share a quick message',  
    18.                 menu: [{ text: 'Show what I am listening to'}]  
    19.          }]  
    20.     }]  
    21. });  

    菜单效果如下:

    messenger-8

    可以让用户改变她当前的状态,可选:“在线、忙、离开或离线”

    联系人的列表就是一个TreeView。我会创建一个不可见的根节点(root node),拥有三个分支节点,叫做“在线”、“忙”、“离开”或“离线”。那么用户的联系列表将会加入到这些分支节点中。

    首先在窗体中置入一个tree:

    1. items: [{ xtype: 'treepanel',  
    2.     id: 'contacts-tree',  
    3.     border: false,  
    4.     useArrows: true,  
    5.     autoScroll: true,  
    6.     animate: true,  
    7.     containerScroll: true,  
    8.     bodyCssClass: 'tree-body',  
    9.     dataUrl: 'messenger.aspx',  
    10.     requestMethod: 'get',  
    11.     rootVisible: false,  
    12.     root: {  
    13.         nodeType: 'async',  
    14.         text: 'My Reporting Project',  
    15.         draggable: false,  
    16.         id: 'root-node'  
    17. }}]  

    上面就是定义了一个Window窗体,里面还有根节点。这里就是加入“在线”、“忙”、“离开”或“离线”分支节点了,当然还有一些演示用途的联系人。afterrender 事件将会用来获取根节点的引用,在它身上加入子的节点。

    1. var tree = Ext.getCmp('contacts-tree');  
    2. tree.on('afterrender'function(loader, node) {  
    3.     var root = tree.getRootNode();  
    4.     var node = root.appendChild({ id: 'favorites', text: 'Favorites', expanded: true, iconCls: 'ico-fav' });  
    5.     node.appendChild({ text: 'Susie', leaf: true, iconCls: 'ico-sts-available' });  
    6.     node.appendChild({ text: 'Lara', leaf: true, iconCls: 'ico-sts-away' });  
    7.     node = root.appendChild({ text: 'Available', expanded: true, iconCls: 'ico-grp-available' });  
    8.     node.appendChild({ text: 'Jonh', leaf: true, iconCls: 'ico-sts-busy' });  
    9.     node.appendChild({ text: 'Lara', leaf: true, iconCls: 'ico-sts-away' });  
    10.     node.appendChild({ text: 'Susie', leaf: true, iconCls: 'ico-sts-available' });  
    11.     node = root.appendChild({ text: 'Offline', expanded: true, iconCls: 'ico-grp-offline' });  
    12. })  

    出来的窗体的确如我们所料:

    Ext JS window

    差不多这样子了,接下来我们继续对联系人窗体加入更多的功能。

    上述内容中,MSN式的通讯器的布局可显示用户的状态,显示用户的联系人列表,并提供修改状态的菜单、分享签名消息和进入用户个人信息或联系人的卡片。

    需要重申的是,本文的目标不涵盖后台方面的实现,也就是不讨论整个IM系统的设计,只是设计关于Ext UI部分的内容。

    本文的目标是在我构建的窗体上加入几项视觉任务:

    • 当右击联系人TreeView的“熟人”节点时,出现“编辑熟人列表"和“改变布局”。
    • 当任何联系人节点被右击时,出现“发送即时讯息”、“发送邮件讯息”和“加入熟人列表几项”。

    完成的时候应该会是这样的:

     ExtJS layout    Ext JS window

    用Ext.Action类代替菜单的处理函数

    “编辑熟人列表Edit favorites” 和“改变布局Change favorites layout ”这两项功能,对应的功能就是在它们的Ext.Action实例中。我定义其text以及handler两个配置项在对应设置菜单项的text文本和handler函数。

    1. var editFavAction = new Ext.Action({  text: 'Edit favorites',  
    2.     handler: function() {  
    3.         Ext.Msg.alert('Action executed''Edit favorites...');  
    4.     }  
    5. });  
    6. var changeFavAction = new Ext.Action({  
    7.     text: 'Change favorites layout',  
    8.     handler: function() {  
    9.         Ext.Msg.alert('Action executed''Change favorites layout...');  
    10.     }  
    11. });  
    12. var favMenu = new Ext.menu.Menu({  
    13.     items: [editFavAction, changeFavAction]  
    14. });  

    favMenu 菜单就会像这样:

    ext js tutorial favorites menu

    同样的套路,Action 的实例也的实例也可执行“发送即时讯息Send instant message ”、“发送邮件讯息Send instant message ”和“加入熟人列表Send instant message ”的功能:

    1. var sendImAction = new Ext.Action({  text: 'Send instant message',  
    2.     handler: function() {  
    3.         Ext.Msg.alert('Action executed''Send instant message...');  
    4.     }  
    5. });  
    6. var sendMailAction = new Ext.Action({  
    7.     text: 'Send email message',  
    8.     handler: function() {  
    9.         Ext.Msg.alert('Action executed''Send email message...');  
    10.     }  
    11. });  
    12. var addToFavesAction = new Ext.Action({  
    13.     text: 'Add to favorites',  
    14.     handler: function() {  
    15.         Ext.Msg.alert('Action executed''Add to favorites...');  
    16.     }  
    17. });  
    18. var contactMenu = new Ext.menu.Menu({  
    19.     items: [sendImAction, sendMailAction, '-', addToFavesAction]  
    20. });  

    contactMenu 菜单就会这样:

    extjs tutorial contact menu

    Action之目的在于允许不同的组件之间均使用同一个Action,共享这个Action的各项功能。尽管在这个例子中不是太明显,但在以后的例子就不一定用不上。假设一个例子,全局菜单有某一菜单项,右键菜单中又会有同样的菜单项,那么就可以把这菜单项的功能独立出来,甚至在其他的地方不断复用。

    1. var globalMenu = new Ext.menu.Menu({  
    2.     items: [editFavAction, changeFavAction,'-',  
    3.     sendImAction, sendMailAction,'-', addToFavesAction]  
    4. });  

    创建Ex创建Ext.tree.TreePanel实例的右键菜单

    树面板为我们提供了contextmenu事件,触发该事件换言之是出现“编辑熟人列表Edit favorites "和“改变布局Change favorites layout ”的菜单。要决定哪一种菜单会被显示,我们只需要检查一下所选节点的id:

    1. listeners: {    
    2.     contextmenu: function(node, e) {  
    3.             node.select();  
    4.             var contextMenu;  
    5.             if (node.id == 'favorites') {  
    6.                 contextMenu = favMenu;  
    7.             } else if (node.id != 'available' && node.id != 'offline') {  
    8.                 contextMenu = contactMenu;  
    9.             }  
    10.   
    11.         if (contextMenu) {  
    12.                 contextMenu.contextNode = node;  
    13.                 contextMenu.showAt(e.getXY());  
    14.         }  
    15.     }  
    16. }  

    请注意AvailableOffline 分支被选取的时候,哪个菜单的不活动的。

    最后,下面给出网友制作的聊天Session“对话框”,作为UI的补充。

    完整的代码下载:

    http://miamicoder.com/file.axd?file=ExtJs_Messenger_Layout_1.zip

    http://miamicoder.com/file.axd?file=ExtJs_Messenger_Layout_2.zip

  • 相关阅读:
    Ubuntu 18.04.3 更改系统语言为简体中文
    Centos7.3、nginx环境下部署hugo博客
    Centos7.3 卸载 Nginx(彻底卸载) 并重新安装 Nginx(RPM源yum安装)
    Centos7.3、nginx环境下部署hexo博客(非git推送方式)
    使用阿里云对象存储OSS+PicGo搭建图床
    Hexo博客添加LiveRe评论系统
    使用 jsDelivr CDN加速Github 仓库的图片
    解决win10一开机占用内存就飙到70%的问题
    [Andriod官方训练教程]管理Activity的生命活动之停止和重启一个Activity
    [Andriod官方训练教程]支持不同的设备之支持不同的语言
  • 原文地址:https://www.cnblogs.com/hannover/p/1890325.html
Copyright © 2020-2023  润新知