欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript插件化開發(fā)教程 (四)

 更新時間:2015年01月27日 15:27:19   投稿:hebedich  
這篇文章主要介紹了JavaScript插件化開發(fā)教程第四篇,需要的朋友可以參考下

一,開篇分析

Hi,還記得上一篇文章嗎。主要講述了一個“Tab”插件是如何組織代碼以及實現(xiàn)的”,以及過程化設(shè)計與面向?qū)ο笏枷朐O(shè)計相結(jié)合的方式是

如何設(shè)計一個插件的,兩種方式各有利弊取長補短,本系列文章是以學(xué)習(xí)為導(dǎo)向的,具體場景大家自己定奪使用方式。在從這篇文章中,我們還是以那個“Tab”實例為主,

繼續(xù)擴(kuò)展相關(guān)功能。嘿嘿嘿,廢話少說,進(jìn)入正題。直接上實際效果圖:

大家看到了吧,增加了一個新的功能,如果我們在初始化時,我們的模塊配置信息項目的條目數(shù)大于我們指定的,那么就會顯示在“更多模塊”

操作項的隱藏列表中,我們的初始化參數(shù)配置也從新做了調(diào)整比如多了一個“displayMax”指定初始化時的條目數(shù),還有一個項目屬性,“status”

在初始化時也去掉了不需要配置了,在程序中動態(tài)生成配置,增加了程序的靈活性,下面就具體分析一下吧。

(二),實例分析

(1),首先確定這個插件做什么事。下面看一下插件的調(diào)用方式,以及配置參數(shù)說明。如下代碼:

復(fù)制代碼 代碼如下:

{
    buttonText : "添加模塊" ,
    result : [
        {
            text : "向?qū)崾? ,
            url : "help.html" ,
            showClose : "0"
        } ,
        {
            text : "學(xué)生信息" ,
            url : "info.html" ,
            showClose : "1"
        } ,
        {
            text : "學(xué)生分類" ,
            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”里面包含兩個參數(shù),第一個是dom節(jié)點對象,第二個是插件參數(shù)選項,"buttonText "代表“Tab“插件中,操作按鈕的文字描述。

”result“是一個數(shù)組,里面包含的是選項卡項目的屬性,包括文字描述,點擊選項卡項目時做請求使用的url,”showClose“代表選項卡的選項是否顯示關(guān)閉按鈕。

“status”在初始化時也去掉了不需要配置了,在程序中動態(tài)生成配置??赡軙嘘P(guān)閉狀態(tài),分別表示為:1-默認(rèn)顯示,0-關(guān)閉狀態(tài),2-超過默認(rèn)的條目數(shù)。

(2),功能分步驟介紹

1---,通過可選參數(shù),初始化插件:

復(fù)制代碼 代碼如下:

$(function(){
    bigbear.ui.createTab($("#tab"),{
        buttonText : "添加模塊" ,
        result : [
            {
                text : "向?qū)崾? ,
                url : "help.html" ,
                showClose : "0"
            } ,
            {
                text : "學(xué)生信息" ,
                url : "info.html" ,
                showClose : "1"
            } ,
            {
                text : "學(xué)生分類" ,
                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---,渲染并且完成時間綁定以及相關(guān)的業(yè)務(wù)邏輯,比如初始化時條目數(shù)量驗證。

復(fù)制代碼 代碼如下:

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("不能添加任何模塊,目前已經(jīng)是最大數(shù)量!") ;
                        }
                    })
                    .appendTo(root) ;
                }) ;
                root.toggle() ;
            }) ;
           
        });
    }
    this.getElem().find(".title .items div")
    .eq(0)
    .trigger("click") ; // 假定是必須有一項,否則插件意義就不大了!
} ;

3---,選項卡切換以及數(shù)據(jù)內(nèi)容渲染操作。

復(fù)制代碼 代碼如下:

 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() ;
 } ;   

復(fù)制代碼 代碼如下:

 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 !") ;
     });
 })

 
復(fù)制代碼 代碼如下:

 tabProto._setContent = function(html){
     this.getElem().find(".content").html(html) ;
 } ;
 tabProto._getContent = function(url){
     return $.ajax({
         url : url
     }) ;
 } ;

4---,核心的輔助數(shù)據(jù)操作方法,不涉及dom。

復(fù)制代碼 代碼如下:

 /* 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 ;
 } ;

(三),完整代碼以供學(xué)習(xí),本代碼已經(jīng)過測試,包括目錄結(jié)構(gòu)以及相關(guān)的文件。

 1,html

復(fù)制代碼 代碼如下:

 <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">
                     + 添加學(xué)生信息
                 </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

復(fù)制代碼 代碼如下:

 .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

復(fù)制代碼 代碼如下:

(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節(jié)點存在
            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("不能添加任何模塊,目前已經(jīng)是最大數(shù)量!") ;
                            }
                        })
                        .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) ;
  

(四),最后總結(jié)

  (1),面向?qū)ο蟮乃伎挤绞胶侠矸治龉δ苄枨蟆?/p>

 ?。?),以類的方式來組織我們的插件邏輯。

 ?。?),不斷重構(gòu)上面的實例,如何進(jìn)行合理的重構(gòu)那?不要設(shè)計過度,要游刃有余,推薦的方式是過程化設(shè)計與面向?qū)ο笏枷朐O(shè)計相結(jié)合。

相關(guān)文章

  • JavaScript組件開發(fā)之輸入框加候選框

    JavaScript組件開發(fā)之輸入框加候選框

    本文給大家分享基于js組件開發(fā)的輸入框加候選框的實例代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的的朋友參考下
    2017-03-03
  • javascript中JSON對象與JSON字符串相互轉(zhuǎn)換實例

    javascript中JSON對象與JSON字符串相互轉(zhuǎn)換實例

    這篇文章主要介紹了javascript中JSON對象與JSON字符串相互轉(zhuǎn)換,實例分析了json對象與字符串常用的幾種轉(zhuǎn)換技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • JS數(shù)組在內(nèi)存中的效率問題淺析

    JS數(shù)組在內(nèi)存中的效率問題淺析

    用js有很久了,但都沒有深究過js的數(shù)組形式,下面這篇文章主要給大家介紹了關(guān)于JS數(shù)組在內(nèi)存中的效率問題,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • JavaScript引用賦值與傳值賦值總結(jié)

    JavaScript引用賦值與傳值賦值總結(jié)

    這篇文章主要介紹了JavaScript引用賦值與傳值賦值總結(jié),在JavaScript中基本數(shù)據(jù)類型都是傳值賦值,復(fù)合數(shù)據(jù)類型都是引用賦值(傳地址)也叫引用傳址,下文更多相關(guān)資料,需要的小伙伴可以參考一下
    2022-05-05
  • 淺談JS的原型和原型鏈

    淺談JS的原型和原型鏈

    在原型鏈中,Object是頂級公民,function是一級公民,其他的是二級公民,先記住這句話,下面我們來講解一下為什么這么說。
    2021-06-06
  • javascript動態(tài)獲取登錄時間和在線時長

    javascript動態(tài)獲取登錄時間和在線時長

    這篇文章主要為大家詳細(xì)介紹了javascript動態(tài)獲取登錄時間和在線時長的相關(guān)資料,獲得登錄時候的時間,用來和動態(tài)的時間做差來求時長,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Bootstrapvalidator校驗、校驗清除重置的實現(xiàn)代碼(推薦)

    Bootstrapvalidator校驗、校驗清除重置的實現(xiàn)代碼(推薦)

    這篇文章給大家介紹了bootstrapvalidator校驗、校驗清除重置的實現(xiàn)代碼,在代碼中需要我們引入css與js文件,大家可以參考下文的代碼
    2016-09-09
  • JavaScript 拖拽實例代碼

    JavaScript 拖拽實例代碼

    這篇文章主要介紹了JavaScript 拖拽實例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • webpack4.0+vue2.0利用批處理生成前端單頁或多頁應(yīng)用的方法

    webpack4.0+vue2.0利用批處理生成前端單頁或多頁應(yīng)用的方法

    這篇文章主要介紹了webpack4.0+vue2.0利用批處理生成前端單頁或多頁應(yīng)用的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 簡單封裝js的dom查詢實例代碼

    簡單封裝js的dom查詢實例代碼

    下面小編就為大家?guī)硪黄唵畏庋bjs的dom查詢實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07

最新評論