随笔-57  评论-129  文章-0  trackbacks-0

Spinner(微调器)装饰器开发:


组件介绍:


仿照 window时间日期管理中,年份调节的控件,原型是一个文本输入筐,一般用于数字输入。筐后有上下微调按钮,用于增减控件值。

这种控件在
backbase框架中也有出现,JSIspinner就是仿照其外形设计。


显示效果:




在线演示见:http://www.xidea.org/project/jsi/decorator/spinner.html

代码:


org/xidea/decorator/spinner.js


 


  1. /** 

  2.  * @public 

  3.  * @decorator spinner 

  4.  * @attribute start 

  5.  * @attribute end  

  6.  * @attribute step 

  7.  */  

  8. function Spinner(){  

  9. }  

  10.   

  11. Spinner.prototype = new Decorator();  

  12. Spinner.prototype.decorate = function(){  

  13.   this.start = parseInt(this.attributes.get('start'))  

  14.   this.end = parseInt(this.attributes.get('end'))  

  15.   this.step = parseInt(this.attributes.get('step'))||1;  

  16.   var container = this.getContainer();  

  17.   var table = document.createElement('table');  

  18.   var outerDiv = document.createElement("div");  

  19.   var upDiv = document.createElement("div");  

  20.   var downDiv = document.createElement("div");  

  21.   table.border = 0;  

  22.   table.cellSpacing=0;  

  23.   table.cellPadding=0;  

  24.   container.insertBefore(table,container.firstChild);  

  25.   var row = table.insertRow(0);  

  26.   var cell = row.insertCell(0);   

  27.   var ele = table.nextSibling;  

  28.   do{  

  29.     container.removeChild(ele);  

  30.     cell.appendChild(ele);  

  31.   }while(ele = table.nextSibling)  

  32.   cell = row.insertCell(1);  

  33.   cell.style.verticalAlign = 'middle',  

  34.   cell.appendChild(outerDiv);  

  35.   outerDiv.style.position = 'relative'  

  36.   outerDiv.style.top = '0px'  

  37.   outerDiv.style.left = '0px'  

  38.   outerDiv.style.height = '0px'  

  39.   outerDiv.style.width = '0px'  

  40.   outerDiv.style.zIndex= 2;  

  41.   //outerDiv.style.border= "solid 1px red";  

  42.   outerDiv.appendChild(upDiv);  

  43.   initializeHandleDiv(this,upDiv);  

  44.   outerDiv.appendChild(downDiv);  

  45.   initializeHandleDiv(this,downDiv);  

  46.   //alert(this.start+'/'+this.end+'/'+this.step+':'+(this.start<0))  

  47. }  

  48. Spinner.prototype.jump = function(offset){  

  49.   if(offset){  

  50.     var input = this.getContainer().getElementsByTagName('input')[0];  

  51.     var value = value = input.value * 1 + offset*this.step;  

  52.     if(value>this.end){  

  53.       value=this.end;  

  54.     }else if(value<this.start){  

  55.       value = this.start;  

  56.     }  

  57.     input.value = value;  

  58.   }  

  59. }  

  60. /** 

  61.  * @internal 

  62.  */  

  63. var imagePath = 'url("'+this.scriptBase + 'spinner.gif")';  

  64. /** 

  65.  * @internal 

  66.  */  

  67. function initializeHandleDiv(spinner,handleDiv){  

  68.   var position = 0;  

  69.   var style = handleDiv.style;  

  70.   style.backgroundImage=imagePath;  

  71.   style.position='absolute';  

  72.   style.width='12px';  

  73.   style.height='8px';  

  74.   style.margin='1px';  

  75.   style.left = '-14px'  

  76.   style.overflow = 'hidden'  

  77.   if(handleDiv.previousSibling){  

  78.     style.backgroundPosition = '0 -32px';  

  79.     position = -32;  

  80.     style.top = '0px'  

  81.   }else{  

  82.     style.top = '-10px'  

  83.   }  

  84.   handleDiv.onmouseout = buildMouseHandle(spinner,position,0)  

  85.   position -= 8;  

  86.   handleDiv.onmouseup=handleDiv.onmouseover = buildMouseHandle(spinner,position,0)  

  87.   position -= 8;  

  88.   handleDiv.onmousedown = buildMouseHandle(spinner,position,0)  

  89.   handleDiv.onclick = buildMouseHandle(spinner,position,position<-32?-1:1)  

  90. }  

  91. /** 

  92.  * @internal 

  93.  */  

  94. function buildMouseHandle(spinner,imagePosition,offset){  

  95.   imagePosition = '0 '+imagePosition+'px';  

  96.   return function(){  

  97.     this.style.backgroundPosition = imagePosition;  

  98.     spinner.jump(offset);  

  99.   }  

  100. }  











使用方法见在 基于FCKEditor 开发JSI Editor装饰器已有详细介绍,不再叙述。


见:http://www.javaeye.com/article/79063




评论也很精彩,请点击查看精彩评论。欢迎您也添加评论。查看详细 >>





JavaEye推荐
杭州:外企高薪聘请系统维护工程师(10-15K)
杭州:国内大型网络公司高薪招聘系统架构师,资深JAVA开发工程师
北京:优秀公司NHNChina招聘:WEB开发,系统管理,JAVA开发, DBA
广州:急招 JAVA开发经理/系统架构师(10-15K/月)也招聘java程序员



文章来源: http://jindw.javaeye.com/blog/79064
posted on 2007-05-12 11:17 金大为 阅读(59) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: