author | Lennard de Rijk <ljvderijk@gmail.com> |
Fri, 20 Mar 2009 19:11:56 +0000 | |
changeset 1956 | 1fcddf90eccc |
parent 1727 | 718744a10daa |
permissions | -rw-r--r-- |
1727
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
/** |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
* jquery.spin-button |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
* (c) 2008 Semooh (http://semooh.jp/) |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
* |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
* Dual licensed under the MIT (MIT-LICENSE.txt) |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
* and GPL (GPL-LICENSE.txt) licenses. |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
* |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
**/ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
(function($){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
$.fn.extend({ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
spin: function(opt){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
return this.each(function(){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
opt = $.extend({ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
imageBasePath: '/soc/content/images/', |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
spinBtnImage: 'spin-button.png', |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
spinUpImage: 'spin-up.png', |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
spinDownImage: 'spin-down.png', |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
interval: 1, |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
max: null, |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
min: null, |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
timeInterval: 500, |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
timeBlink: 200 |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
}, opt || {}); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
var txt = $(this); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
|
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
var spinBtnImage = opt.imageBasePath+opt.spinBtnImage; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
var btnSpin = new Image(); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
btnSpin.src = spinBtnImage; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
var spinUpImage = opt.imageBasePath+opt.spinUpImage; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
var btnSpinUp = new Image(); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
32 |
btnSpinUp.src = spinUpImage; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
var spinDownImage = opt.imageBasePath+opt.spinDownImage; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
var btnSpinDown = new Image(); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
btnSpinDown.src = spinDownImage; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
36 |
|
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
var btn = $(document.createElement('img')); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
38 |
btn.attr('src', spinBtnImage); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
btn.css({cursor: 'pointer', verticalAlign: 'bottom', padding: 0, margin: 0}); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
txt.after(btn); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
txt.css({marginRight:0, paddingRight:0}); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
|
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
function spin(vector){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
var val = txt.val(); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
if(!isNaN(val)){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
val = parseFloat(val) + (vector*opt.interval); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
if(opt.min!=null && val<opt.min) val=opt.min; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
if(opt.min!=null && val>opt.max) val=opt.max; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
if(val != txt.val()){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
txt.val(val); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
txt.change(); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
src = (vector > 0 ? spinUpImage : spinDownImage); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
btn.attr('src', src); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
if(opt.timeBlink<opt.timeInterval) |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
setTimeout(function(){btn.attr('src', spinBtnImage);}, opt.timeBlink); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
} |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
} |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
} |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
|
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
btn.mousedown(function(e){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
61 |
var pos = e.pageY - btn.offset().top; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
var vector = (btn.height()/2 > pos ? 1 : -1); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
(function(){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
spin(vector); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
65 |
var tk = setTimeout(arguments.callee, opt.timeInterval); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
$(document).one('mouseup', function(){ |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
clearTimeout(tk); btn.attr('src', spinBtnImage); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
}); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
})(); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
return false; |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
}); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
}); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
} |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
}); |
718744a10daa
Added the spin jquery plugin
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
})(jQuery); |