js城市选择器组件

 

 

  
  
  
  
  1. KISSY.add(function(S,Base,Template,Switchable,Data){ 
  2.     var $ = S.all, 
  3.         Evt = S.Event, 
  4.         IO = S.io; 
  5.     //首字符排序 
  6.     var  wordsList; 
  7.     var NewCitySelector = function(id,cfg){ 
  8.         this.init(id,cfg); 
  9.     }; 
  10.  
  11.  
  12.     function findCityByCityId(cityId){ 
  13.         return __findByProp(0,cityId); 
  14.     } 
  15.  
  16.     function findCityByCityName(cityName){ 
  17.         return __findByProp(1,cityName); 
  18.     } 
  19.  
  20.     function __findByProp(index,val){ 
  21.         for(var i in Data){ 
  22.             for(var j in Data[i]){ 
  23.                 if(val == Data[i][j][index]){ 
  24.                     return { 
  25.                         cityId:Data[i][j][0], 
  26.                         cityName:Data[i][j][1], 
  27.                         category:i 
  28.                     }; 
  29.                 } 
  30.             } 
  31.         } 
  32.     } 
  33.  
  34.     /** 
  35.         判断元素在元素内部 
  36.       **/ 
  37.       function isIn(t,p){ 
  38.         var $t = $(t), 
  39.             $p = $(p); 
  40.  
  41.         if($t[0] && $p[0] && $t[0] === $p[0]){ 
  42.             return true; 
  43.         } 
  44.  
  45.         if($($t,$p)[0]){ 
  46.             return true; 
  47.         } 
  48.       } 
  49.  
  50.     var CLS_PREFIX = "ks-newcityselector-"
  51.     var CONTAINER_TPL = "<div class='"+CLS_PREFIX+"container'>" + "</div>"; 
  52.     var INNER_CONTAINER_TPL = "{{wordsList}}{{citiesList}}"
  53.     // var WORDS 
  54.     S.extend(NewCitySelector,Base,{ 
  55.         init:function(id,cfg){ 
  56.             var self = this
  57.             var defaultCfg = { 
  58.                 autoOpen:false, 
  59.                 cityId:330100, 
  60.                 css:{ 
  61.                     "position":"absolute", 
  62.                     "z-index":"9999" 
  63.                 }, 
  64.                 target:$(id), 
  65.                 tpl:INNER_CONTAINER_TPL, 
  66.                 slideWidth:672 
  67.             }; 
  68.             if(!id || !$(id)[0]){ 
  69.                 S.log("no container"); 
  70.                 return; 
  71.             } 
  72.  
  73.             self.$el = $(id);  
  74.  
  75.             self.cfg = S.mix(defaultCfg,cfg,undefined,undefined,true); 
  76.  
  77.             self.render(); 
  78.         }, 
  79.         render:function(){ 
  80.             var self = this
  81.             self.$pop = $(CONTAINER_TPL).insertAfter(self.$el); 
  82.             self.$pop.css(self.cfg.css); 
  83.             wordsList = []; 
  84.             var citiesListHTML  = "<div class="+CLS_PREFIX+"citycontainer"+"><div class='scroll'>"; 
  85.             for(var i in Data){ 
  86.                 wordsList.push(i); 
  87.                 var citiesHTML =  self.getListHTML(Data[i],'city'); 
  88.                 citiesListHTML += "<div class="+CLS_PREFIX+"tab-content"+" tab="+i+">" + citiesHTML +"</div>
  89.             } 
  90.             citiesListHTML += "</div></div>
  91.             var wordsListHTML = self.getListHTML(wordsList,'words'); 
  92.             var tplData = { 
  93.                 wordsList : wordsListHTML, 
  94.                 citiesList : citiesListHTML 
  95.             }; 
  96.             self.$pop = self.$pop.html(Template("<div class='"+CLS_PREFIX+"inner'>" + self.cfg.tpl + "</div>").render(tplData)); 
  97.  
  98.             self.$cityContainer = $("."+CLS_PREFIX+"citycontainer",self.$pop); 
  99.              
  100.             self.$wordListContainer = $("."+CLS_PREFIX+"words",self.$pop); 
  101.  
  102.             self.bindEvt(); 
  103.  
  104.             self.autoOpen && self.show(); 
  105.  
  106.             S.log(self) 
  107.         }, 
  108.         bindEvt:function(){ 
  109.             var self = this
  110.  
  111.             var timeout; 
  112.  
  113.             self.$el.on("click",function(){ 
  114.                 self.show(); 
  115.             }); 
  116.  
  117.             $("li",$("." + CLS_PREFIX + "words",self.$pop)).on("click",function(e){ 
  118.                 e.preventDefault(); 
  119.                 var $li = $(e.currentTarget); 
  120.                 var stype = $li.attr("stype"); 
  121.                 self.switchTo(stype); 
  122.             }); 
  123.             //自动隐藏覆层 
  124.             self.$pop.on("mouseleave",function(e){ 
  125.                 e.stopPropagation(); 
  126.                 self.isOnPop = false
  127.                 clearTimeout(timeout); 
  128.                 timeout = setTimeout(function(){self.hide()},1000); 
  129.             }); 
  130.  
  131.             self.$pop.on("mousemove",function(e){ 
  132.                 e.stopPropagation(); 
  133.                 self.isOnPop = 1
  134.             }); 
  135.  
  136.             self.$el.on("mousemove",function(e){ 
  137.                 e.stopPropagation(); 
  138.                 self.isOnPop = 1
  139.             }); 
  140.  
  141.             $("li",$("." + CLS_PREFIX + "city",self.$pop)).on("click",function(e){ 
  142.                 e.preventDefault(); 
  143.                 var $a = $("a",$(e.currentTarget)); 
  144.                 self.onSelect({cityName:$a.text(),cityId:$a.attr("cityId")}); 
  145.             }); 
  146.  
  147.             $("body").on("click",function(e){ 
  148.                 if(!isIn($(e.target),self.$pop) && !isIn($(e.target),self.$el)){ 
  149.                     self.isOnPop = false
  150.                     self.hide(); 
  151.                     self.fire("blur"); 
  152.                 } 
  153.             }); 
  154.         }, 
  155.         switchTo:function(stype,cb){ 
  156.             var self = this
  157.                 index, 
  158.                 slideWidth = self.cfg.slideWidth; 
  159.             index = S.isNumber(stype) ? stype - 0 : self.indexOfArray(stype,wordsList); 
  160.             $(".scroll",self.$pop).animate({marginLeft:-index * slideWidth},0.5,"easeOut",cb) 
  161.             $("li",$("."+CLS_PREFIX+"words")).item(index-0).addClass("cur").siblings().removeClass("cur"); 
  162.             self.set("category",stype); 
  163.         }, 
  164.         /** 
  165.             TODO 设置city  
  166.         **/ 
  167.         setCity:function(city){ 
  168.             var self = this
  169.                 cityObj; 
  170.             if(city){ 
  171.                 if(city > 0){ 
  172.                     cityObj = findCityByCityId(city); 
  173.                 }else{ 
  174.                     cityObj = findCityByCityName(city); 
  175.                 } 
  176.                 if(cityObj){ 
  177.                     self.switchTo(cityObj.category); 
  178.                     self.set("currentCity",cityObj); 
  179.                     self.set("cityId",cityObj.cityId); 
  180.                     self.set("cityName",cityObj.cityName); 
  181.                     self.set("category",cityObj.category); 
  182.                     // var index = self.indexOfArray(cityObj.category,wordsList); 
  183.                     // alert(index) 
  184.                     // $("li",self.$cityContainer).item(index) 
  185.                     $(self.cfg.target).html(cityObj.cityName); 
  186.                 } 
  187.                  
  188.             } 
  189.         }, 
  190.         indexOfArray:function(el,ary){ 
  191.             for(var i in ary){ 
  192.                 if(el == ary[i]) return i || 0; 
  193.             } 
  194.         }, 
  195.         show:function(){ 
  196.             var self = this
  197.  
  198.             self.$pop.fadeIn(0.3); 
  199.  
  200.         }, 
  201.         hide:function(){ 
  202.             var self = this
  203.  
  204.             !self.isOnPop && self.$pop.fadeOut(0.3); 
  205.         }, 
  206.         getListHTML:function(ary,cls){ 
  207.             if(!ary || !cls) return ""; 
  208.             var html = "<ul class=" + CLS_PREFIX + cls + ">"; 
  209.             for(var i in ary){ 
  210.                 if(S.isArray(ary[i])){ 
  211.                     html += '<li><a href="#" cityId='+ary[i][0]+' title='+ary[i][1]+' >' + ary[i][1] + '</a></li>'; 
  212.                 }else{ 
  213.                     if(ary[i] == "hot"){ 
  214.                         html += '<li stype="hot"><a href="#">热门</a></li>';   
  215.                     }else{ 
  216.                         html += '<li stype='+ ary[i] +'><a href="#">' + ary[i] + '</a></li>'; 
  217.                     } 
  218.                 } 
  219.             } 
  220.             return html + '</ul>'; 
  221.         }, 
  222.         onSelect:function(e){ 
  223.             var self = this
  224.             self.fire("select",e); 
  225.             self.isOnPop = false
  226.             self.hide(); 
  227.         } 
  228.     }); 
  229.  
  230.  
  231.     return NewCitySelector; 
  232.  
  233. },{requires:['base','template','switchable','./data','./assets/index.css']}); 

 

你可能感兴趣的:(js,城市选择器)