JavaScript插件化开发教程 (四)


Posted in Javascript onJanuary 27, 2015

一,开篇分析

Hi,还记得上一篇文章吗。主要讲述了一个“Tab”插件是如何组织代码以及实现的”,以及过程化设计与面向对象思想设计相结合的方式是

如何设计一个插件的,两种方式各有利弊取长补短,本系列文章是以学习为导向的,具体场景大家自己定夺使用方式。在从这篇文章中,我们还是以那个“Tab”实例为主,

继续扩展相关功能。嘿嘿嘿,废话少说,进入正题。直接上实际效果图:

JavaScript插件化开发教程 (四)

大家看到了吧,增加了一个新的功能,如果我们在初始化时,我们的模块配置信息项目的条目数大于我们指定的,那么就会显示在“更多模块”

操作项的隐藏列表中,我们的初始化参数配置也从新做了调整比如多了一个“displayMax”指定初始化时的条目数,还有一个项目属性,“status”

在初始化时也去掉了不需要配置了,在程序中动态生成配置,增加了程序的灵活性,下面就具体分析一下吧。

(二),实例分析

(1),首先确定这个插件做什么事。下面看一下插件的调用方式,以及配置参数说明。如下代码:

{

    buttonText : "添加模块" ,

    result : [ 

        {

            text : "向导提示" ,

            url : "help.html" ,

            showClose : "0"

        } ,

        {

            text : "学生信息" ,

            url : "info.html" ,

            showClose : "1"

        } ,

        {

            text : "学生分类" ,

            url : "category.html" ,

            showClose : "1"

        } ,

        {

            text : "大熊君{{bb}}" ,

            url : "bb.html" ,

            showClose : "1"

        } ,

        {

            text : "Beta测试模块" ,

            url : "test.html" ,

            showClose : "1"

        } ,

        {

            text : "三胖子" ,

            url : "help.html" ,

            showClose : "1"

        } ,

        {

            text : "四秃子" ,

            url : "help.html" ,

            showClose : "1"

        }

    ] ,

    displayMax : 5 // 最多显示项目

}   
 

“bigbear.ui.createTab”里面包含两个参数,第一个是dom节点对象,第二个是插件参数选项,"buttonText "代表“Tab“插件中,操作按钮的文字描述。

”result“是一个数组,里面包含的是选项卡项目的属性,包括文字描述,点击选项卡项目时做请求使用的url,”showClose“代表选项卡的选项是否显示关闭按钮。

“status”在初始化时也去掉了不需要配置了,在程序中动态生成配置。可能会有关闭状态,分别表示为:1-默认显示,0-关闭状态,2-超过默认的条目数。

(2),功能分步骤介绍

1---,通过可选参数,初始化插件:

$(function(){

    bigbear.ui.createTab($("#tab"),{

        buttonText : "添加模块" ,

        result : [ 

            {

                text : "向导提示" ,

                url : "help.html" ,

                showClose : "0"

            } ,

            {

                text : "学生信息" ,

                url : "info.html" ,

                showClose : "1"

            } ,

            {

                text : "学生分类" ,

                url : "category.html" ,

                showClose : "1"

            } ,

            {

                text : "大熊君{{bb}}" ,

                url : "bb.html" ,

                showClose : "1"

            } ,

            {

                text : "Beta测试模块" ,

                url : "test.html" ,

                showClose : "1"

            } ,

            {

                text : "三胖子" ,

                url : "help.html" ,

                showClose : "1"

            } ,

            {

                text : "四秃子" ,

                url : "help.html" ,

                showClose : "1"

            }

        ] ,

        displayMax : 5 // 最多显示项目

    }) ;

}) ;           

2---,渲染并且完成时间绑定以及相关的业务逻辑,比如初始化时条目数量验证。

tabProto.init = function(){

    if(this._isEmptyResult()){

        this._setContent("暂无任何模块!") ;

    }

    var that = this ;

    this.getElem().find(".title .adder")

    .text("+" + this.getOpts()["buttonText"])

    .on("click",function(){

        that.getElem().find(".console-panel").slideToggle(function(){

            that._renderConsolePanel("0") ;

        }) ;

    }) ;

    $.each(this.getOpts()["result"],function(i,item){

        if(that._isDisplayMax(i + 1)){

            that._saveOrUpdateStatus(item,"1") ;

        }

        else{

            that._saveOrUpdateStatus(item,"2") ;

        }

        that._render(item) ;

    }) ;

    if(!that._isDisplayMax(this.getOpts()["result"].length)){

        this.getElem().find(".title .more-mod").fadeIn(function(){

            $(this).find(".tag").on("click",function(){

                var root = $(this).next() ;

                root.empty() ;

                $.each(that._getItemListByStatus("2"),function(i,data){

                    $("<div></div>").text(data["text"])

                    .on("click",function(){

                        if(that._getItemListByStatus("1").length < that.getOpts()["displayMax"]){

                            that.getElem().find(".title .items div").eq(data["index"]).fadeIn(function(){

                                that._saveOrUpdateStatus(data,"1") ;

                            }) ;

                        }

                        else{

                            alert("不能添加任何模块,目前已经是最大数量!") ;

                        }

                    })

                    .appendTo(root) ;

                }) ;

                root.toggle() ;

            }) ;

            

        });

    }

    this.getElem().find(".title .items div")

    .eq(0)

    .trigger("click") ; // 假定是必须有一项,否则插件意义就不大了!

} ;

3---,选项卡切换以及数据内容渲染操作。

 tabProto._setCurrent = function(index){

     var items = this.getElem().find(".title .items div").removeClass("active") ;

     items.eq(index).addClass("active") ;

     var contents = this.getElem().find(".content .c").hide() ;

     contents.eq(index).show() ;

 } ;   
 item.on("click",function(){

     that._setCurrent($(this).index()) ;

     that._getContent(data["url"]).done(function(result){

         that._setContent(result) ;

     })

     .fail(function(){

         throw new Error("Net Error !") ;

     });

 })

 
 tabProto._setContent = function(html){

     this.getElem().find(".content").html(html) ;

 } ;

 tabProto._getContent = function(url){

     return $.ajax({

         url : url

     }) ;

 } ;

4---,核心的辅助数据操作方法,不涉及dom。

 /* update time 2015 1/26 15:36 */ 

 tabProto._isDisplayMax = function(size){

     var displayMax = this.getOpts()["displayMax"] || 5 ;

     return (size <= displayMax) ? true : false ;

 } ;

 tabProto._isEmptyResult = function(){

     if(!this.getOpts()["result"].length){

         return false ;

     }

     return true ;

 } ;

 tabProto._saveOrUpdateStatus = function(item,status){

     item["status"] = status ;

 } ;

 tabProto._getItemListByStatus = function(status){

     var list = [] ;

     var result = this.getOpts()["result"] ;

     $.each(result,function(i,item){

         if(status == item["status"]){

             list.push(item) ;

         }

     }) ;

     return list ;

 } ;

 tabProto._getStatusByIndex = function(index){

     var status = null ;

     var result = this.getOpts()["result"] ;

     $.each(result,function(i,item){

         if(index == item["index"]){

             status = item["status"] ;

         }

     }) ;

     return status ;

 } ;

(三),完整代码以供学习,本代码已经过测试,包括目录结构以及相关的文件。

 1,html

 <body>

     <div class="dxj-ui-hd">

         大熊君{{bb}} - DXJ UI ------ Tab

     </div>

     <div class="dxj-ui-bd">

         <div id="tab">

             <div class="title">

                 <div class="adder">

                     + 添加学生信息

                 </div>

                 <div class="items">

                     <!--<div><span class="del">X</span>欢迎页</div>

                     <div><span class="del">X</span>用户管理</div>

                     <div><span class="del">X</span>Bigbear</div>-->

                 </div>

                 <div class="more-mod">

                     <div class="tag">更多模块</div>

                     <div class="mods">

                         

                     </div>

                 </div>

             </div>

             <div class="console-panel">

             </div>

             <div class="content">

                 <!--<div class="c">

                 

                     <div class="input-content"><span>姓名:</span><input type="text" /></div>

                     <div class="input-content"><span>备注:</span><textarea></textarea></div>

                 

                 </div>    <div class="input-content"><input type="button" value="保存" /></div>

                 -->

             </div>

         </div>

     </div>

 </body>

2,css

 .dxj-ui-hd {

     padding:0px ;

     margin : 0 auto;

     margin-top:30px;

     width:780px;

     height:60px;

     line-height: 60px;

     background: #3385ff;

     color:#fff;

     font-family: "微软雅黑" ;

     font-size: 28px;

     text-align: center;

     font-weight:bold;

 }

 .dxj-ui-bd {

     padding:0px ;

     margin : 0 auto;

     width:778px;

     padding-top : 30px ;

     padding-bottom : 30px ;

     overflow: hidden;

     border:1px solid #3385ff;

 }

 .dxj-ui-bd #tab {

     padding:0px ;

     margin : 0 auto;

     width:720px;

     overflow: hidden;

     position:relative;

 }

 .dxj-ui-bd #tab .title {

     width:720px;

     overflow: hidden;

     border-bottom:2px solid #3385ff;

 }

 .dxj-ui-bd #tab .title .adder {

     width:160px;

     height:32px;

     line-height: 32px;

     background: #DC143C;

     color:#fff;

     font-family: "微软雅黑" ;

     font-size: 14px;

     text-align: center;

     font-weight:bold;

     float : left;

     cursor:pointer;

 }

 .dxj-ui-bd #tab .title .more-mod {

     overflow:hidden;

     border:1px solid #DC143C;

     width:70px;

     position:absolute;

     right:0;

     margin-right:6px;

     display:none;

 }

 .dxj-ui-bd #tab .title .more-mod .tag{

     height:32px;

     line-height:32px;

     width:70px;

     background: #DC143C;

     color:#fff;

     font-family: arial ;

     font-size: 12px;

     text-align: center;

     cursor:pointer;

 }

 .dxj-ui-bd #tab .title .more-mod .mods {

     overflow:hidden;

     width:70px;

     display:none;

 }

 .dxj-ui-bd #tab .title .more-mod .mods div {

     height:24px;

     line-height:24px;

     width:62px;

     font-family: arial ;

     font-size: 12px;

     cursor:pointer;

     padding-left:10px;

 }

 .dxj-ui-bd #tab .title .items {

     height:32px;

 

     width:480px;

     overflow: hidden;

     float : left;

 }

 .dxj-ui-bd #tab .title .items div {

     padding:0px;

     margin-left:10px;

     width:84px;

     height:32px;

     line-height: 32px;

     background: #3385ff;

     color:#fff;

     font-family: arial ;

     font-size: 12px;

     text-align: center;

     position:relative;

     float : left;

     cursor:pointer;

 }

 .dxj-ui-bd #tab .title .items div span.del {

     width:16px;

     height:16px;

     line-height: 16px;

     display:block;

     background: #DC143C;

     position:absolute;

     right:0 ;

     top:0;

     cursor:pointer;

 }

 .dxj-ui-bd #tab .content {

     width:716px;

     padding-top:30px;

     overflow: hidden;

     border:2px solid #3385ff;

     border-top:0px;

     min-height:130px;

     text-align:center;

 }

 .dxj-ui-bd #tab .content table {

     margin : 0 auto ;

 }

 .dxj-ui-bd #tab .content div.c {

     padding-top : 20px ;

     padding-left:20px;

     background:#eee;

     height:140px;

 }

 .dxj-ui-bd #tab .content div.c .input-content {

     margin-top : 10px ;

     font-family: arial ;

     font-size: 12px;

 }

 .dxj-ui-bd #tab .console-panel {

     width:716px;

     padding-top:20px;

     padding-bottom:20px;

     overflow: hidden;

     border:2px solid #3385ff;

     border-top:0px;

     border-bottom:2px solid #3385ff;

     background:#fff;

     display:none;

 }

 

 .active {

     font-weight:bold ;

 }

3,bigbear.js

JavaScript插件化开发教程 (四)

(function($){

    var win = window ;

    var bb = win.bigbear = win.bigbear || {

        ui : {}

    } ;

    var ui = bb.ui = {} ;

    var Tab = function(elem,opts){

        this.elem = elem ;

        this.opts = opts ;

    } ;

    var tabProto = Tab.prototype ;

    /* update time 2015 1/26 15:36 */

    tabProto._isDisplayMax = function(size){

        var displayMax = this.getOpts()["displayMax"] || 5 ;

        return (size <= displayMax) ? true : false ;

    } ;

    tabProto._isEmptyResult = function(){

        if(!this.getOpts()["result"].length){

            return false ;

        }

        return true ;

    } ;

    tabProto._saveOrUpdateStatus = function(item,status){

        item["status"] = status ;

    } ;

    tabProto._getItemListByStatus = function(status){

        var list = [] ;

        var result = this.getOpts()["result"] ;

        $.each(result,function(i,item){

            if(status == item["status"]){

                list.push(item) ;

            }

        }) ;

        return list ;

    } ;

    tabProto._getStatusByIndex = function(index){

        var status = null ;

        var result = this.getOpts()["result"] ;

        $.each(result,function(i,item){

            if(index == item["index"]){

                status = item["status"] ;

            }

        }) ;

        return status ;

    } ;

    tabProto._renderConsolePanel = function(status){

        var that = this ;

        var root = that.getElem().find(".console-panel") ;

        this._resetConsolePanel() ;

        $.each(that._getItemListByStatus(status),function(i,item){

            var elem = $("<div style='float:left';></div>").appendTo(root) ;

            $("<input type='radio' name='addmod' />")

            .data("item",item)

            .appendTo(elem) ;

            $("<span></span>").text(item["text"]).appendTo(elem) ;

        }) ;

        if(root.find("div").size()){

            $("<input type='button' value='添加模块' style='margin-left:20px'/>")

            .on("click",function(){

                var data = root.find("input[type=radio]:checked").data("item") ;

                if(that._getItemListByStatus("1").length < that.getOpts()["displayMax"]){

                    that.getElem().find(".title .items div").eq(data["index"]).fadeIn(function(){

                        that._saveOrUpdateStatus(data,"1") ;

                    })

                    .trigger("click") ;

                }

                else{

                    that._saveOrUpdateStatus(data,"2") ;

                }

                that.getElem().find(".title .adder").trigger("click") ;

            })

            .appendTo(root) ;

        }

        else{

            root.text("暂无任何可添加的项目!") ;

        }

    } ;

    /* update time 2015 1/26 15:36 */  

    tabProto._setCurrent = function(index){

        var items = this.getElem().find(".title .items div").removeClass("active") ;

        items.eq(index).addClass("active") ;

        var contents = this.getElem().find(".content .c").hide() ;

        contents.eq(index).show() ;

    } ;

    tabProto.getElem = function(){

        return this.elem ;

    } ;

    tabProto.getOpts = function(){

        return this.opts ;

    } ;

    tabProto._resetContent = function(){

        this.getElem().find(".content").html("") ;

    } ;

    tabProto._setContent = function(html){

        this.getElem().find(".content").html(html) ;

    } ;

    tabProto._getContent = function(url){

        return $.ajax({

            url : url

        }) ;

    } ;

    tabProto._deleteItem = function(elem){

        var that = this ;

        this.getElem().find(".title .items div")

        .eq(elem.index())

        .fadeOut(function(){

            that._resetContent() ;

            that._saveOrUpdateStatus(elem.data("item"),"0") ;

            that._triggerItem(elem.index() + 1) ;

        }) ;

    } ;

    tabProto._triggerItem = function(next){

        var nextStatus = this._getStatusByIndex(next) ;

        var items = this.getElem().find(".title .items div") ;

        next = items.eq(next) ;

        if(next.size() && "1" == nextStatus){ //后继dom节点存在

            next.trigger("click") ;

        }

        else{

            items.eq(0).trigger("click") ;

        }

    } ;

    tabProto._resetConsolePanel = function(){

        this.getElem().find(".console-panel").empty() ;

    } ;

    tabProto.init = function(){

        if(this._isEmptyResult()){

            this._setContent("暂无任何模块!") ;

        }

        var that = this ;

        this.getElem().find(".title .adder")

        .text("+" + this.getOpts()["buttonText"])

        .on("click",function(){

            that.getElem().find(".console-panel").slideToggle(function(){

                that._renderConsolePanel("0") ;

            }) ;

        }) ;

        $.each(this.getOpts()["result"],function(i,item){

            if(that._isDisplayMax(i + 1)){

                that._saveOrUpdateStatus(item,"1") ;

            }

            else{

                that._saveOrUpdateStatus(item,"2") ;

            }

            that._render(item) ;

        }) ;

        if(!that._isDisplayMax(this.getOpts()["result"].length)){

            this.getElem().find(".title .more-mod").fadeIn(function(){

                $(this).find(".tag").on("click",function(){

                    var root = $(this).next() ;

                    root.empty() ;

                    $.each(that._getItemListByStatus("2"),function(i,data){

                        $("<div></div>").text(data["text"])

                        .on("click",function(){

                            if(that._getItemListByStatus("1").length < that.getOpts()["displayMax"]){

                                that.getElem().find(".title .items div").eq(data["index"]).fadeIn(function(){

                                    that._saveOrUpdateStatus(data,"1") ;

                                }) ;

                            }

                            else{

                                alert("不能添加任何模块,目前已经是最大数量!") ;

                            }

                        })

                        .appendTo(root) ;

                    }) ;

                    root.toggle() ;

                }) ;

                 

            });

        }

        this.getElem().find(".title .items div")

        .eq(0)

        .trigger("click") ; // 假定是必须有一项,否则插件意义就不大了!

    } ;

    tabProto._render = function(data){

        var that = this ;

        var item = $("<div></div>").text(data["text"]).appendTo(this.getElem().find(".title .items")) ;

        data["index"] = item.index() ;

        item.on("click",function(){

            that._setCurrent($(this).index()) ;

            that._getContent(data["url"]).done(function(result){

                that._setContent(result) ;

            })

            .fail(function(){

                throw new Error("Net Error !") ;

            });

        })

        .data("item",data) ;

        if("2" == data["status"]){

            item.hide() ;

        }

        if("1" == data["showClose"]){

            $("<span class='del'>X</span>")

            .on("click",function(){

                if(win.confirm("是否删除此项?")){

                    that._deleteItem(item) ;

                    return false ; // 阻止冒泡

                }

            })

            .appendTo(item) ;

        }

    } ;

    ui.createTab = function(elem,opts){

        var tab = new Tab(elem,opts) ;

        tab.init() ;

        return tab ;

    } ;    

})(jQuery) ;

(四),最后总结

(1),面向对象的思考方式合理分析功能需求。

(2),以类的方式来组织我们的插件逻辑。

(3),不断重构上面的实例,如何进行合理的重构那?不要设计过度,要游刃有余,推荐的方式是过程化设计与面向对象思想设计相结合。

Javascript 相关文章推荐
(JS实现)MapBar中坐标的加密和解密的脚本
May 16 Javascript
Javascript YUI 读码日记之 YAHOO.util.Dom - Part.2 0
Mar 22 Javascript
《JavaScript DOM 编程艺术》读书笔记之JavaScript 语法
Jan 09 Javascript
JavaScript数组去重的3种方法和代码实例
Jul 01 Javascript
jQuery实现的仿select功能代码
Aug 19 Javascript
基于jquery实现页面滚动到底自动加载数据的功能
Dec 19 Javascript
jQuery UI仿淘宝搜索下拉列表功能
Jan 10 Javascript
Javascript同时声明一连串(多个)变量的方法
Jan 23 Javascript
JavaScript中Promise的使用详解
Feb 26 Javascript
vux uploader 图片上传组件的安装使用方法
May 15 Javascript
vue fetch中的.then()的正确使用方法
Apr 17 Javascript
JS 设计模式之:工厂模式定义与实现方法浅析
May 06 Javascript
JavaScript插件化开发教程 (三)
Jan 27 #Javascript
js实现简单随机抽奖的方法
Jan 27 #Javascript
JavaScript插件化开发教程 (二)
Jan 27 #Javascript
javascript将数字转换整数金额大写的方法
Jan 27 #Javascript
JS实现同时搜索百度和必应的方法
Jan 27 #Javascript
js获取域名的方法
Jan 27 #Javascript
JavaScript插件化开发教程 (一)
Jan 27 #Javascript
You might like
重料打造自己的“宝马”---第三代
2021/03/02 无线电
浅析php过滤html字符串,防止SQL注入的方法
2013/07/02 PHP
了解PHP的返回引用和局部静态变量
2015/06/04 PHP
PHP的PDO常用类库实例分析
2016/04/07 PHP
PHP简单实现上一页下一页功能示例
2016/09/14 PHP
php-msf源码详解
2017/12/25 PHP
[对联广告] JS脚本类
2006/08/27 Javascript
获取Javscript执行函数名称的方法
2006/12/22 Javascript
javascript 读取XML数据,在页面中展现、编辑、保存的实现
2009/10/27 Javascript
js关闭模态窗口刷新父页面或跳转页面
2012/12/13 Javascript
JS实现div内部的文字或图片自动循环滚动代码
2013/04/19 Javascript
window resize和scroll事件的基本优化思路
2014/04/29 Javascript
jQuery中:enabled选择器用法实例
2015/01/04 Javascript
jQuery选择器源码解读(一):Sizzle方法
2015/03/31 Javascript
javascript cookie基础应用之记录用户名的方法
2016/09/20 Javascript
原生js编写焦点图效果
2016/12/08 Javascript
Bootstrap媒体对象学习使用
2017/03/07 Javascript
swiper插件自定义切换箭头按钮
2017/12/28 Javascript
使用json-server简单完成CRUD模拟后台数据的方法
2018/07/12 Javascript
利用js将ajax获取到的后台数据动态加载至网页中的方法
2018/08/08 Javascript
微信小程序生成海报分享朋友圈的实现方法
2019/05/06 Javascript
Nodejs监控事件循环异常示例详解
2019/09/22 NodeJs
openlayers 3实现车辆轨迹回放
2020/09/24 Javascript
python通过socket查询whois的方法
2015/07/18 Python
Python批量创建迅雷任务及创建多个文件
2016/02/13 Python
简单谈谈Python中的json与pickle
2017/07/19 Python
Python实现在线暴力破解邮箱账号密码功能示例【测试可用】
2017/09/06 Python
python email smtplib模块发送邮件代码实例
2018/04/26 Python
详解Canvas 跨域脱坑实践
2018/11/07 HTML / CSS
阿玛瑞酒店中文官方网站:Amari.com
2018/02/13 全球购物
群胜软件Java笔试题
2012/09/29 面试题
车间班组长的职责
2013/12/13 职场文书
毕业设计论文评语
2014/12/31 职场文书
2015年环保局工作总结
2015/05/22 职场文书
六年级作文之关于梦
2019/10/22 职场文书
Android开发手册Chip监听及ChipGroup监听
2022/06/10 Java/Android