1 line
3.7 KiB
JavaScript
1 line
3.7 KiB
JavaScript
$.fn.SpinButton=function(cfg){"use strict";function coord(el,prop){for(var c=el[prop],b=document.body;(el=el.offsetParent)&&el!==b;)$.browser.msie&&"relative"===el.currentStyle.position||(c+=el[prop]);return c}return this.each(function(){this.repeating=!1,this.spinCfg={min:cfg&&!isNaN(parseFloat(cfg.min))?Number(cfg.min):null,max:cfg&&!isNaN(parseFloat(cfg.max))?Number(cfg.max):null,step:cfg&&cfg.step?Number(cfg.step):1,stepfunc:!(!cfg||!cfg.stepfunc)&&cfg.stepfunc,page:cfg&&cfg.page?Number(cfg.page):10,upClass:cfg&&cfg.upClass?cfg.upClass:"up",downClass:cfg&&cfg.downClass?cfg.downClass:"down",reset:cfg&&cfg.reset?cfg.reset:this.value,delay:cfg&&cfg.delay?Number(cfg.delay):500,interval:cfg&&cfg.interval?Number(cfg.interval):100,_btn_width:20,_direction:null,_delay:null,_repeat:null,callback:cfg&&cfg.callback?cfg.callback:null},this.spinCfg.smallStep=cfg&&cfg.smallStep?cfg.smallStep:this.spinCfg.step/2,this.adjustValue=function(i){var v;v=isNaN(this.value)?this.spinCfg.reset:$.isFunction(this.spinCfg.stepfunc)?this.spinCfg.stepfunc(this,i):Number((Number(this.value)+Number(i)).toFixed(5)),null!==this.spinCfg.min&&(v=Math.max(v,this.spinCfg.min)),null!==this.spinCfg.max&&(v=Math.min(v,this.spinCfg.max)),this.value=v,$.isFunction(this.spinCfg.callback)&&this.spinCfg.callback(this)},$(this).addClass(cfg&&cfg.spinClass?cfg.spinClass:"spin-button").mousemove(function(e){var x=e.pageX||e.x,y=e.pageY||e.y,el=e.target||e.srcElement,scale=svgEditor.tool_scale||1,height=$(el).height()/2,direction=x>coord(el,"offsetLeft")+el.offsetWidth*scale-this.spinCfg._btn_width?y<coord(el,"offsetTop")+height*scale?1:-1:0;if(direction!==this.spinCfg._direction){switch(direction){case 1:$(this).removeClass(this.spinCfg.downClass).addClass(this.spinCfg.upClass);break;case-1:$(this).removeClass(this.spinCfg.upClass).addClass(this.spinCfg.downClass);break;default:$(this).removeClass(this.spinCfg.upClass).removeClass(this.spinCfg.downClass)}this.spinCfg._direction=direction}}).mouseout(function(){$(this).removeClass(this.spinCfg.upClass).removeClass(this.spinCfg.downClass),this.spinCfg._direction=null,window.clearInterval(this.spinCfg._repeat),window.clearTimeout(this.spinCfg._delay)}).mousedown(function(e){if(0===e.button&&0!==this.spinCfg._direction){var self=this,stepSize=e.shiftKey?self.spinCfg.smallStep:self.spinCfg.step,adjust=function(){self.adjustValue(self.spinCfg._direction*stepSize)};adjust(),self.spinCfg._delay=window.setTimeout(function(){adjust(),self.spinCfg._repeat=window.setInterval(adjust,self.spinCfg.interval)},self.spinCfg.delay)}}).mouseup(function(e){window.clearInterval(this.spinCfg._repeat),window.clearTimeout(this.spinCfg._delay)}).dblclick(function(e){$.browser.msie&&this.adjustValue(this.spinCfg._direction*this.spinCfg.step)}).keydown(function(e){switch(e.keyCode){case 38:this.adjustValue(this.spinCfg.step);break;case 40:this.adjustValue(-this.spinCfg.step);break;case 33:this.adjustValue(this.spinCfg.page);break;case 34:this.adjustValue(-this.spinCfg.page)}}).keypress(function(e){if(this.repeating)switch(e.keyCode){case 38:this.adjustValue(this.spinCfg.step);break;case 40:this.adjustValue(-this.spinCfg.step);break;case 33:this.adjustValue(this.spinCfg.page);break;case 34:this.adjustValue(-this.spinCfg.page)}else this.repeating=!0}).keyup(function(e){switch(this.repeating=!1,e.keyCode){case 38:case 40:case 33:case 34:case 13:this.adjustValue(0)}}).bind("mousewheel",function(e){e.wheelDelta>=120?this.adjustValue(this.spinCfg.step):e.wheelDelta<=-120&&this.adjustValue(-this.spinCfg.step),e.preventDefault()}).change(function(e){this.adjustValue(0)}),this.addEventListener&&this.addEventListener("DOMMouseScroll",function(e){e.detail>0?this.adjustValue(-this.spinCfg.step):e.detail<0&&this.adjustValue(this.spinCfg.step),e.preventDefault()},!1)})}; |