|
1 /** |
|
2 * jQuery Spin 1.1.1 |
|
3 * |
|
4 * Copyright (c) 2009 Naohiko MORI |
|
5 * Dual licensed under the MIT and GPL licenses. |
|
6 * |
|
7 **/ |
|
8 (function($){ |
|
9 var calcFloat = { |
|
10 get: function(num){ |
|
11 var num = num.toString(); |
|
12 if(num.indexOf('.')==-1) return[0, eval(num)]; |
|
13 var nn = num.split('.'); |
|
14 var po = nn[1].length; |
|
15 var st = nn.join(''); |
|
16 var sign = ''; |
|
17 if(st.charAt(0)=='-'){ |
|
18 st = st.substr(1); |
|
19 sign = '-'; |
|
20 } |
|
21 for(var i=0; i<st.length; ++i) if(st.charAt(0)=='0') st=st.substr(1, st.length); |
|
22 st = sign + st; |
|
23 return [po, eval(st)]; |
|
24 }, |
|
25 getInt: function(num, figure){ |
|
26 var d = Math.pow(10, figure); |
|
27 var n = this.get(num); |
|
28 var v1 = eval('num * d'); |
|
29 var v2 = eval('n[1] * d'); |
|
30 if(this.get(v1)[1]==v2) return v1; |
|
31 return(n[0]==0 ? v1 : eval(v2 + '/Math.pow(10, n[0])')); |
|
32 }, |
|
33 sum: function(v1, v2){ |
|
34 var n1 = this.get(v1); |
|
35 var n2 = this.get(v2); |
|
36 var figure = (n1[0] > n2[0] ? n1[0] : n2[0]); |
|
37 v1 = this.getInt(v1, figure); |
|
38 v2 = this.getInt(v2, figure); |
|
39 return eval('v1 + v2')/Math.pow(10, figure); |
|
40 } |
|
41 }; |
|
42 $.extend({ |
|
43 spin: { |
|
44 imageBasePath: '/soc/content/images/', |
|
45 spinBtnImage: 'spin-button.png', |
|
46 spinUpImage: 'spin-up.png', |
|
47 spinDownImage: 'spin-down.png', |
|
48 interval: 1, |
|
49 max: null, |
|
50 min: null, |
|
51 timeInterval: 500, |
|
52 timeBlink: 200, |
|
53 btnClass: null, |
|
54 btnCss: {cursor: 'pointer', padding: 0, margin: 0, verticalAlign: 'middle'}, |
|
55 txtCss: {marginRight: 0, paddingRight: 0}, |
|
56 lock: false, |
|
57 decimal: null, |
|
58 beforeChange: null, |
|
59 changed: null, |
|
60 buttonUp: null, |
|
61 buttonDown: null |
|
62 } |
|
63 }); |
|
64 $.fn.extend({ |
|
65 spin: function(o){ |
|
66 return this.each(function(){ |
|
67 o = o || {}; |
|
68 var opt = {}; |
|
69 $.each($.spin, function(k,v){ |
|
70 opt[k] = (typeof o[k]!='undefined' ? o[k] : v); |
|
71 }); |
|
72 |
|
73 var txt = $(this); |
|
74 |
|
75 var spinBtnImage = opt.imageBasePath+opt.spinBtnImage; |
|
76 var btnSpin = new Image(); |
|
77 btnSpin.src = spinBtnImage; |
|
78 var spinUpImage = opt.imageBasePath+opt.spinUpImage; |
|
79 var btnSpinUp = new Image(); |
|
80 btnSpinUp.src = spinUpImage; |
|
81 var spinDownImage = opt.imageBasePath+opt.spinDownImage; |
|
82 var btnSpinDown = new Image(); |
|
83 btnSpinDown.src = spinDownImage; |
|
84 |
|
85 var btn = $(document.createElement('img')); |
|
86 btn.attr('src', spinBtnImage); |
|
87 if(opt.btnClass) btn.addClass(opt.btnClass); |
|
88 if(opt.btnCss) btn.css(opt.btnCss); |
|
89 if(opt.txtCss) txt.css(opt.txtCss); |
|
90 txt.after(btn); |
|
91 if(opt.lock){ |
|
92 txt.focus(function(){txt.blur();}); |
|
93 } |
|
94 |
|
95 function spin(vector){ |
|
96 var val = txt.val(); |
|
97 var org_val = val; |
|
98 if(opt.decimal) val=val.replace(opt.decimal, '.'); |
|
99 if(!isNaN(val)){ |
|
100 val = calcFloat.sum(val, vector * opt.interval); |
|
101 if(opt.min!==null && val<opt.min) val=opt.min; |
|
102 if(opt.max!==null && val>opt.max) val=opt.max; |
|
103 if(val != txt.val()){ |
|
104 if(opt.decimal) val=val.toString().replace('.', opt.decimal); |
|
105 var ret = ($.isFunction(opt.beforeChange) ? opt.beforeChange.apply(txt, [val, org_val]) : true); |
|
106 if(ret!==false){ |
|
107 txt.val(val); |
|
108 if($.isFunction(opt.changed)) opt.changed.apply(txt, [val]); |
|
109 txt.change(); |
|
110 src = (vector > 0 ? spinUpImage : spinDownImage); |
|
111 btn.attr('src', src); |
|
112 if(opt.timeBlink<opt.timeInterval) |
|
113 setTimeout(function(){btn.attr('src', spinBtnImage);}, opt.timeBlink); |
|
114 } |
|
115 } |
|
116 } |
|
117 if(vector > 0){ |
|
118 if($.isFunction(opt.buttonUp)) opt.buttonUp.apply(txt, [val]); |
|
119 }else{ |
|
120 if($.isFunction(opt.buttonDown)) opt.buttonDown.apply(txt, [val]); |
|
121 } |
|
122 } |
|
123 |
|
124 btn.mousedown(function(e){ |
|
125 var pos = e.pageY - btn.offset().top; |
|
126 var vector = (btn.height()/2 > pos ? 1 : -1); |
|
127 (function(){ |
|
128 spin(vector); |
|
129 var tk = setTimeout(arguments.callee, opt.timeInterval); |
|
130 $(document).one('mouseup', function(){ |
|
131 clearTimeout(tk); btn.attr('src', spinBtnImage); |
|
132 }); |
|
133 })(); |
|
134 return false; |
|
135 }); |
|
136 }); |
|
137 } |
|
138 }); |
|
139 })(jQuery); |