二次封装dojo slider

Posted 疯子加天才

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二次封装dojo slider相关的知识,希望对你有一定的参考价值。

上次的二次封装timeslider,挺有意思,又来封装一个dojo的,样式还是用arcgis的。
实现更多功能,包括HorizontalSlider和VerticalSlider, 刻度的显示隐藏,标签的显示和隐藏,上刻度和下刻度的显示隐藏,
无序数显示刻度,标签图标的自由选择,大小选择。。。。更多功能大家看完code也可以自己慢慢加进去哈~~~

code有点长,先上slider的class::MyCustomSlider.js

 

[javascript] view plain copy
 
  1. /** 
  2.  * yilei 
  3.  * custom dojo slider 
  4.  * Note:>>!! sliderType got 2 type there are "HorizontalSlider" and "VerticalSlider", they use same setting. 
  5.  * The Top setting will became Left setting and the Buttom setting will became Right setting 
  6.  * when the sliderType set to "VerticalSlider" 
  7.  --------------------------------------------------- 
  8.  * For example: 
  9.  * The topLabels array is setting to display the top Graduation for "HorizontalSlider", it is setting to dispay the 
  10.  * left Graduation for "VerticalSlider" also. 
  11.  ----------------------------------------------------- 
  12.  * This slider not suport image label for "VerticalSlider" now..... 
  13.  *custom obj example:: 
  14.  *10    5   4   53  50   
  15.  * 
  16.  *  { [id: 1, label:10], 
  17.  *      [id:2, label: 5], 
  18.  *      [id:3, label: 4],..... 
  19.  *  } 
  20.  * need map.js suport 
  21.  */  
  22.    
  23. dojo.declare("mapComponent.MyCustomSlider",null,{  
  24.     minimum:null,  
  25.     maximum:null,  
  26.     stepIncrement:1, //PANI  
  27.     customSliderArray: null, //[]  
  28.     divId:"",  
  29.     intermediateChanges:true,     
  30.     showButtons:true,//only for VerticalSlider or CustomSliderV  
  31.     sliderClass:"",  
  32.     replaceFlag:"#",  
  33.     sliderName:"temp",  
  34.     sliderId:"tempId",  
  35.     SliderCssClass: "ies-Slider",  
  36.     labelImageCssClass:"sliderLabelImage",  
  37.     sliderType:"HorizontalSlider",  
  38.     /********HorizontalSlider and VerticalSlider and CustomSliderV and CustomSliderH**************/  
  39.     showCustomTopLabel: true,  //It is show custom label at left side on VerticalSlider  
  40.     showTopLabel:false,  //It is show label at left side on VerticalSlider  
  41.     showBottomMark:false, //It is show Graduation at right side on VerticalSlider  
  42.     showTopMark:false,  //It is show Graduation at left side on VerticalSlider  
  43.     showBottomLabel:true,    //It is show label at right side on VerticalSlider  
  44.     noOfTopLabels:3,        //It is setting total labels number at left side on VerticalSlider  
  45.     topLabels:null,         //.....same to verticalslider  
  46.     noOfLabels:3, // Old noOfBottomlabels  
  47.     bottomlabels:null,    
  48.     labelFooterFormat: "#",  
  49.     labelTopFormat: "#",      
  50.     customTopLabelCssClass:"sliderCustomTopLabel",  
  51.     toplabelCssClass:"sliderTopLabel",  
  52.     topMarkCssClass:"sliderTopMark",  
  53.     bottomLabelCssClass:"sliderBottomLabel",  
  54.     bottomMarkCssClass:"sliderBottomMark",    
  55.     showTopImageLabel:false,  
  56.     showBottomImageLabel:true,  
  57.     showSingleTopImageLabel:true,  
  58.     showSingleBottomImageLabel:true,  
  59.     showPlayPause:true,  
  60.     showPre:true,  
  61.     showNext:true,  
  62.       
  63.     topImageLabels:["dropDownArrow_grey.png"],  
  64.     bottomImageLabels:["dropDownArrow_grey.png"],  
  65.     imageOnly:false,  
  66.     sliderLoop:false,  
  67.     thumbMovingRate:500,  
  68.       
  69.     /********HorizontalSlider and VerticalSlider**************/  
  70.       
  71.     onsliderChange:function(timeExtentObj){},  
  72.     defaultValue:0,  
  73.     increase:1,  
  74.     self:null,  
  75.     _totleStep:0,  
  76.     _customType:null,  
  77.     _sliderObj:null,  
  78.     _intervalObj:null,  
  79.     _playPauseButton:null,  
  80.     _nextButton:null,  
  81.     _preButton:null,  
  82.     _wrapString:"   <div class=\\"esriTimeSlider\\" id=\\"sliderId\\">\\r\\n   <table width=\\"100%\\" cellspacing=\\"0\\" cellpadding=\\"0\\" border=\\"0\\">\\r\\n   <tr>\\r\\n   <td align=\\"right\\" valign=\\"middle\\"><button id=\\"sliderId_playpauseButtonNode\\" type=\\"button\\">Play/Pause</button></td>\\r\\n   <td align=\\"center\\" valign=\\"middle\\" width=\\"80%\\" class=\\"tsTmp\\"></td>\\r\\n   <td align=\\"left\\" valign=\\"middle\\" width=\\"30\\"><button id=\\"sliderId_preButtonNode\\"  type=\\"button\\">Previous</button></td>\\r\\n   <td align=\\"left\\" valign=\\"middle\\"><button id=\\"sliderId_nextButtonNode\\"  type=\\"button\\">Next</button></td>\\r\\n   </tr>    \\r\\n   </table>\\r\\n   </div>",  
  83.     /**********constructor function for init*************/  
  84.     constructor:function(params){         
  85.                   
  86.         dojo.mixin(this, params);         
  87.           
  88.                   
  89.         if((this.sliderType=="CustomSliderH" || this.sliderType=="CustomSliderV" )&&(this.customSliderArray && this.customSliderArray.length>0) )  
  90.         {  
  91.             if (this.showTopLabel == true)  
  92.                 {this.topLabels=[];}  
  93.                   
  94.             if (this.showBottomLabel == true)  
  95.                 {this.bottomlabels=[];}  
  96.                   
  97.             if (this.customSliderArray) {  
  98.                 if (this.customSliderArray.length > 0) {  
  99.                     this.minimum = 0;  
  100.                     this.maximum = this.customSliderArray.length-1;  
  101.                 }  
  102.             }  
  103.           
  104.         // Setting Labels     
  105.         console.log("this.maximum - " + this.maximum);  
  106.         console.log("this.minimum - " + this.minimum);  
  107.           
  108.           
  109.           
  110.         var val = this.minimum;  
  111.             if (this.customSliderArray) {  
  112.                 var obj=this.customSliderArray[this.minimum];  
  113.                 val = obj.label;  
  114.             }  
  115.                   
  116.             if (this.showTopLabel == true)  
  117.                 {this.topLabels.push(val);}  
  118.                   
  119.             if (this.showBottomLabel == true)  
  120.                 {this.bottomlabels.push(val);}  
  121.           
  122.         //var increment = parseInt(((this.maximum - this.minimum)+1) / (this.noOfLabels-2));  
  123.         //var increment = parseInt((this.maximum - this.minimum-1) / (this.noOfLabels-2));  
  124.         console.log((this.maximum - this.minimum) / (this.noOfLabels-1));  
  125.         var increment = Math.round((this.maximum - this.minimum) / (this.noOfLabels-1));  
  126.         console.log("increment - " + increment);  
  127.         var firstNum=this.minimum;  
  128.         var map=new Map();  
  129.         for(var f=0;f<this.noOfLabels-2;f++)  
  130.         {  
  131.             firstNum=firstNum+increment;  
  132.             map.put(firstNum,firstNum);  
  133.               
  134.         }  
  135.         for(var t=1;t<this.customSliderArray.length-1;t++)  
  136.         {  
  137.             var temp=map.get(t);  
  138.             if(temp)  
  139.             {  
  140.                 if (this.showTopLabel == true)  
  141.                 this.topLabels.push(this.customSliderArray[temp].label);  
  142.                   
  143.                 if (this.showBottomLabel == true)  
  144.                 this.bottomlabels.push(this.customSliderArray[temp].label);  
  145.                 map.remove(t);  
  146.             }  
  147.             else  
  148.             {  
  149.                 if (this.showTopLabel == true)  
  150.                 this.topLabels.push(null);  
  151.                   
  152.                 if (this.showBottomLabel == true)  
  153.                 this.bottomlabels.push(null);  
  154.             }  
  155.         }  
  156.         map=null;  
  157.         var val = this.maximum;  
  158.             if (this.customSliderArray) {  
  159.                 var obj=this.customSliderArray[this.maximum];  
  160.                 val = obj.label;  
  161.             }  
  162.                   
  163.             if (this.showTopLabel == true)  
  164.                 this.topLabels.push(val);  
  165.                   
  166.             if (this.showBottomLabel == true)  
  167.                 this.bottomlabels.push(val);  
  168.               
  169.             if(this.sliderType=="CustomSliderH")  
  170.             {  
  171.                 this._customType="H";  
  172.             }  
  173.             else if(this.sliderType=="CustomSliderV")  
  174.             {  
  175.                 this._customType="V";  
  176.             }  
  177.         }  
  178.         if(this.defaultValue==0)this.defaultValue=this.minimum;   
  179.         self=this;  
  180.         _totleStep=(this.maximum-this.minimum+1)/this.increase;       
  181.         this.sliderId=getUniqueId(this.sliderName);   
  182.         //alert(this.customSliderArray.length);  
  183.     },  
  184.   
  185.     createSlider:function(){  
  186.         var self=this;  
  187.         if(this.sliderType=="HorizontalSlider" || this._customType=="H")  
  188.         {  
  189.               
  190.             require(["dojo/parser", "dijit/form/HorizontalSlider", "dijit/form/HorizontalRule", "dijit/form/HorizontalRuleLabels","dojo/dom-class","dijit/form/Button","dojo/dom-attr"],   
  191.                 function(parser,HorizontalSlider,HorizontalRule,HorizontalRuleLabels,domClass,Button,domAttr){   
  192.                   parser.parse();   
  193.                   try  
  194.                     {  
  195.                         // Destroy the div and then create  
  196.                         dojo.destroy(dojo.query("[id^="+self.sliderName+"]"));  
  197.                         // Create new Div and add to divSlidersContainer  
  198.                         //var sliderNode = dojo.byId(this.divId);   
  199.                         //alert(this.divId);  
  200.                         //domClass.add(dojo.byId(_self.divId),"ttt");  
  201.                         self._wrapString=self._wrapString.replace(/sliderId/g,self.sliderId);  
  202.                         //console.dir(self._wrapString);  
  203.                         dojo.place(self._wrapString,dojo.byId(self.divId));  
  204.                         dojo.addClass(dojo.byId(self.sliderId),self.SliderCssClass)  
  205.                           
  206.                         //domAttr.set(dojo.query(".esriTimeSlider")[0],"id",self.sliderId);  
  207.                         //dojo.query(".tsTmp");  
  208.                         new Button({  
  209.                             //label: "Click me!",  
  210.                             id:self.sliderId+"_playpauseButtonNodeID",  
  211.                             iconClass:"tsButton tsPlayButton",  
  212.                             showLabel:false,  
  213.                             style:{"margin-top":"-20px","display":"none"},  
  214.                             onClick: function(){  
  215.                                 // Do something:  
  216.                                 //self._playPauseButton.destory();  
  217.                                 console.dir(self);  
  218.                                 domAttr.set(this,"iconClass",self._intervalObj?"tsButton tsPlayButton":"tsButton tsPauseButton");  
  219.                                 self.playPause();  
  220.                             }  
  221.                         }, self.sliderId+"_playpauseButtonNode").startup();  
  222.                         //self._playPauseButton = dijit.byId(self.sliderId+"_playpauseButtonNode");  
  223.                         new Button({  
  224.                             //label: "Click me!",  
  225.                             id:self.sliderId+"_preButtonNodeID",  
  226.                             iconClass:"tsButton tsPrevButton",  
  227.                             showLabel:false,  
  228.                             style:{"margin-top":"-20px","display":"none"},  
  229.                             onClick: function(){  
  230.                                 // Do something:  
  231.                                 //alert("ggg");  
  232.                                 self.previous();  
  233.                             }  
  234.                               
  235.                         }, self.sliderId+"_preButtonNode").startup();  
  236.                         //self._preButton = dijit.byId(self.sliderId+"_preButtonNode");  
  237.                         new Button({  
  238.                             //label: "Click me!",  
  239.                             id:self.sliderId+"_nextButtonNodeID",  
  240.                             iconClass:"tsButton tsNextButton",  
  241.                             showLabel:false,  
  242.                             style:{"margin-top":"-20px","display":"none"},  
  243.                             onClick: function(){  
  244.                                 // Do something:  
  245.                                 //alert("hhhh");  
  246.                                 console.dir(self);  
  247.                                 self.next();  
  248.                             }  
  249.                         }, self.sliderId+"_nextButtonNode").startup();  
  250.                         //self._nextButton=dijit.byId(self.sliderId+"_nextButtonNode");  
  251.                         var sliderNode=dojo.create("div",null,dojo.query(".tsTmp",dojo.byId(self.sliderId))[0]);  
  252.                         //sliderNode.id=self.sliderId;  
  253.                           
  254.                         if(self.showTopLabel&&!self.showCustomTopLabel)  
  255.                         {  
  256.                         //alert("show Top");  
  257.                             var rulesNodeLabelsTop = dojo.create("div", null, sliderNode);                
  258.                               
  259.                             //_labelsPackage:function(noOfLabels,labelsArray,showImageLabel,showSingleImageLabel,imageLabelsArray,isTop)  
  260.                               
  261.                             var newtopLabels=self._labelsPackage(self.noOfTopLabels,self.topLabels,self.showTopImageLabel,self.showSingleTopImageLabel,self.topImageLabels,self.labelTopFormat,true);  
  262.                             var labelsHeight="1em";  
  263.                             if(self.showTopImageLabel)  
  264.                             labelsHeight="2em";       
  265.                             if((self.sliderType=="CustomSliderH" || self.sliderType=="CustomSliderV" )&&(self.customSliderArray && self.customSliderArray.length>0) )  
  266.                                     markCount=self.customSliderArray.length;  
  267.                                 else  
  268.                                     markCount=self.noOfTopLabels;                         
  269.                             var sliderLabelsTop= new dijit.form.HorizontalRuleLabels(  
  270.                                 {  
  271.                                     container: "topDecoration",  
  272.                                     count: markCount,  
  273.                                     labels: newtopLabels,  
  274.                                     style: "height:"+labelsHeight+";font-size:75%;color:gray;white-space: nowrap;",  
  275.                                     //class:self.toplabelCssClass  
  276.                                 },  
  277.                                 rulesNodeLabelsTop);  
  278.                                 domClass.add(rulesNodeLabelsTop, self.toplabelCssClass);  
  279.                                 if(self.showTopMark)  
  280.                                 {  
  281.                                     var rulesNodeTop = dojo.create("div", null, sliderNode);   
  282.                                     var sliderRuleTop= new dijit.form.HorizontalRule(  
  283.                                     {  
  284.                                     container: "topDecoration",  
  285.                                         count: markCount,  
  286.                                         style: "height:1em;font-size:75%;color:gray;",  
  287.                                         //class:self.topMarkCssClass  
  288.                                     },  
  289.                                     rulesNodeTop);  
  290.                                     domClass.add(rulesNodeTop, self.topMarkCssClass);  
  291.                                 }  
  292.                                   
  293.                         }  
  294.                           
  295.                         if(self.showBottomLabel)  
  296.                         {  
  297.                               
  298.                             if(self.showBottomMark)  
  299.                             {  
  300.                                 var markCount=0;  
  301.                                 //alert(self.sliderType);  
  302.                                 //alert(self.customSliderArray.length);  
  303.                                 if((self.sliderType=="CustomSliderH" || self.sliderType=="CustomSliderV" )&&(self.customSliderArray && self.customSliderArray.length>0) )  
  304.                                     markCount=self.customSliderArray.length;  
  305.                                 else  
  306.                                     markCount=self.noOfLabels;  
  307.                                 var rulesNodeBottom = dojo.create("div", null, sliderNode);   
  308.                                 var sliderBottomRule= new dijit.form.HorizontalRule(  
  309.                                 {  
  310.                                 container: "bottomDecoration",  
  311.                                     count: markCount,  
  312.                                     style: "height:1em;font-size:75%;color:gray;",  
  313.                                     //class:self.bottomMarkCssClass  
  314.                                 },  
  315.                                 rulesNodeBottom);  
  316.                                 domClass.add(rulesNodeBottom, self.bottomMarkCssClass);  
  317.                             }  
  318.                             //  sliderNode.appendChild(rulesNode);  
  319.                               
  320.                             var rulesNodeLabelsBottom = dojo.create("div", null, sliderNode);   
  321.                             //alert(self.bottomlabels.length);  
  322.                             var newBottomLabels=self._labelsPackage(self.noOfLabels,self.bottomlabels,self.showBottomImageLabel,self.showSingleBottomImageLabel,self.bottomImageLabels,self.labelFooterFormat,false);  
  323.                             //alert(self.bottomlabels.length);  
  324.              &n

    以上是关于二次封装dojo slider的主要内容,如果未能解决你的问题,请参考以下文章

    转为前端工程师

    iOS(Swift) 基于 Moya 的二次封装

    axios 二次封装

    010 selenium 二次封装

    react axios的二次封装

    easyspa angular二次封装框架介绍