JavaScript外掛程式化開發教程(五),javascript外掛程式

來源:互聯網
上載者:User

JavaScript外掛程式化開發教程(五),javascript外掛程式

一,開篇分析

Hi,大家好!前兩篇文章我們主要講述了以“jQuery的方式如何開發外掛程式”,以及過程化設計與物件導向思想設計相結合的方式是如何設計一個外掛程式的,兩種方式各有利弊取長補短,嘿嘿嘿,廢話少說,進入正題。直接上實際:

  大家看到了吧,這是一個下拉式功能表外掛程式,在我們日常開發中,系統提供的可能有時讓我們覺得不是很美觀並且功能有限,造成使用者

  的體驗形式以及使用者的可互動性不是很好,所以今天類比一個嘿嘿嘿。下面就具體分析一下吧。

  (二),執行個體分析

  (1),首先確定這個外掛程式做什麼事。下面看一下外掛程式的調用方式,以及配置參數說明。如下代碼:

複製代碼 代碼如下:
 $(function(){
     var itemSelector = new ItemSelector($("#item-selector"),{
         currentText : "Please Choose Item" ,
         items : [
             {
                 text : "JavaScript" ,
                 value : "js" ,
                 disabled : "1"
             } ,
             {
                 text : "Css" ,
                 value : "css" ,
                 disabled : "0"
             } ,
             {
                 text : "Html" ,
                 value : "html" ,
                 disabled : "0"
             }
         ] ,
         mode : "0" , // 為"1"時支援checkbox多選模式
         change : function(value){
             // put your code here
         }
     }) ;
     itemSelector.init() ;
     setTimeout(function(){
         console.log(itemSelector.getCurrentValue()) ; // 測試 擷取當先選中項
     },2000) ;
 

“var itemSelector = new ItemSelector()”裡麵包含兩個參數,第一個是dom節點對象,第二個是外掛程式參數選項,"currentText"代表“ItemSelector“外掛程式中,選中文本顯示地區的文字描述。

”items“是一個數組,裡麵包含的是“ItemSelector”項目的屬性,包括文字描述,選項值,”disabled“代表列表條目的可顯度,0代表顯示,1代表不可顯示。

”change“代表選中時的操作回呼函數,選項資料會以參數的形式進行回傳。

  (2),所涉的功能有哪些

    可顯的如下:

    不可顯的如下:

  二者的區別是:不可現狀態資料不會回傳,懸浮上去不會有任何效果。

 三),完整代碼以供學習,本代碼已經過測試,包括目錄結構以及相關的檔案。

  (1),html

複製代碼 代碼如下:
 <body>
     <div class="dxj-ui-hd">
         大熊君{{bb}} - DXJ UI ------ ItemSelector
     </div>
     <div class="dxj-ui-bd">
         <div id="item-selector">
             <div class="title">
                 <div></div><span>↓</span>
             </div>
             <div class="content">
                 <div class="items">
                    
                 </div>
             </div>
         </div>
     </div>
 </body>

(2),css

複製代碼 代碼如下:
 /* item-selector */
 #item-selector {
     margin : 0 auto;
     width : 220px ;
     overflow:hidden;
     border:2px solid #ccc;
 }
 #item-selector .title {
     border-bottom:1px solid #ccc;
     overflow:hidden;
 }
 #item-selector .title div {
     width:190px;
     border:0px ;
     color:#999;
     font-family: arial ;
     font-size: 14px;
     height:28px;
     line-height:28px;
     float:left;
     cursor:pointer;
 }
 #item-selector .title span {
     display:block;
     height:30px;
     line-height:30px;
     width:29px;
     float:left;
     text-align:center;
     border-left:1px solid #ccc;
     cursor:pointer;
 }
 #item-selector .content {
     width : 220px ;
     overflow:hidden;
 }
 #item-selector .content .items {
     overflow:hidden;
 }
 #item-selector .content .items div {
     padding-left:20px;
     width : 200px ;
     height:32px;
     line-height:32px;
     font-family: "微軟雅黑" ;
     font-size: 14px;
     font-weight:bold;
     cursor:pointer;
 }
 .item-hover {
     background:#3385ff;
     color:#fff;
 }

 (3),"ItemSelector.js"

複製代碼 代碼如下:
function ItemSelector(elem,opts){
    this.elem = elem ;
    this.opts = opts ;
} ;
var ISProto = ItemSelector.prototype ;
ISProto.getElem = function(){
    return this.elem ;
} ;
ISProto.getOpts = function(){
    return this.opts ;
} ;
/* data manip*/
ISProto._setCurrent = function(current){
    this.getOpts()["current"] = current ;
} ;
ISProto.getCurrentValue = function(current){
    return this.getOpts()["current"] ;
} ;
/* data manip*/
ISProto.init = function(){
    var that = this ;
    this.getOpts()["current"] = null ; // 資料遊標
    this._setItemValue(this.getOpts()["currentText"]) ;
    var itemsElem = that.getElem().find(".content .items") ;
    this.getElem().find(".title div").on("click",function(){
        itemsElem.toggle() ;
    }) ;
    this.getElem().find(".title span").on("click",function(){
        itemsElem.toggle() ;
    }) ;
    $.each(this.getOpts()["items"],function(i,item){
        item["id"] = (new Date().getTime()).toString() ;
        that._render(item) ;
    }) ;
} ;
ISProto._setItemValue = function(value){
    this.getElem().find(".title div").text(value)
} ;
ISProto._render = function(item){
    var that = this ;
    var itemElem = $("<div></div>")
    .text(item["text"])
    .attr("id",item["id"]) ;
    if("0" == item["disabled"]){
        itemElem.on("click",function(){
            var onChange = that.getOpts()["change"] ;
            that.getElem().find(".content .items").hide() ;
            that._setItemValue(item["text"]) ;
            that._setCurrent(item) ;
            onChange && onChange(item) ;
        })
        .mouseover(function(){
            $(this).addClass("item-hover") ;
        })
        .mouseout(function(){
            $(this).removeClass("item-hover") ;
        }) ;
    }
    else{
        itemElem.css("color","#ccc").on("click",function(){
            that.getElem().find(".content .items").hide() ;
            that._setItemValue(item["text"]) ;
        }) ;
    }
    itemElem.appendTo(this.getElem().find(".content .items")) ;
} ;

(四),最後總結

  (1),物件導向的思考方式合理分析功能需求。

  (2),以類的方式來組織我們的外掛程式邏輯。

  (3),不斷重構上面的執行個體,如何進行合理的重構那?不要設計過度,要遊刃有餘,推薦的方式是過程化設計與物件導向思想設計相結合。

    (4),下篇文章中會擴充相關功能,比如“mode”這個屬性,為"1"時支援checkbox多選模式,現在只是預設下拉模式。

本文先到這裡了,後續我們再繼續討論,希望小夥伴們能夠喜歡本系列文章。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.