jQuery Spin plugin updated to version 1.1.1
authorMario Ferraro <fadinlight@gmail.com>
Fri, 19 Jun 2009 23:13:06 +0100
changeset 2415 69e9d5cc643f
parent 2414 a95ba3595554
child 2416 96ff51144dca
jQuery Spin plugin updated to version 1.1.1 Former version had a bug if max was not specified
app/jquery/jquery-spin-1.0.2.js
app/jquery/jquery-spin-1.1.1.js
app/soc/content/images/spin-button.png
app/soc/content/images/spin-down.png
app/soc/content/images/spin-up.png
app/soc/templates/soc/base.html
--- a/app/jquery/jquery-spin-1.0.2.js	Fri Jun 19 20:19:03 2009 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
-/**
- *  jquery.spin-button
- *  (c) 2008 Semooh (http://semooh.jp/)
- *
- *  Dual licensed under the MIT (MIT-LICENSE.txt)
- *  and GPL (GPL-LICENSE.txt) licenses.
- *
- **/
-(function($){
-  $.fn.extend({
-    spin: function(opt){
-      return this.each(function(){
-        opt = $.extend({
-            imageBasePath: '/soc/content/images/',
-            spinBtnImage: 'spin-button.png',
-            spinUpImage: 'spin-up.png',
-            spinDownImage: 'spin-down.png',
-            interval: 1,
-            max: null,
-            min: null,
-            timeInterval: 500,
-            timeBlink: 200
-          }, opt || {});
-
-        var txt = $(this);
-
-        var spinBtnImage = opt.imageBasePath+opt.spinBtnImage;
-        var btnSpin = new Image();
-        btnSpin.src = spinBtnImage;
-        var spinUpImage = opt.imageBasePath+opt.spinUpImage;
-        var btnSpinUp = new Image();
-        btnSpinUp.src = spinUpImage;
-        var spinDownImage = opt.imageBasePath+opt.spinDownImage;
-        var btnSpinDown = new Image();
-        btnSpinDown.src = spinDownImage;
-
-        var btn = $(document.createElement('img'));
-        btn.attr('src', spinBtnImage);
-        btn.css({cursor: 'pointer', verticalAlign: 'bottom', padding: 0, margin: 0});
-        txt.after(btn);
-        txt.css({marginRight:0, paddingRight:0});
-
-        function spin(vector){
-          var val = txt.val();
-          if(!isNaN(val)){
-            val = parseFloat(val) + (vector*opt.interval);
-            if(opt.min!=null && val<opt.min) val=opt.min;
-            if(opt.min!=null && val>opt.max) val=opt.max;
-            if(val != txt.val()){
-              txt.val(val);
-              txt.change();
-              src = (vector > 0 ? spinUpImage : spinDownImage);
-              btn.attr('src', src);
-              if(opt.timeBlink<opt.timeInterval)
-                setTimeout(function(){btn.attr('src', spinBtnImage);}, opt.timeBlink);
-            }
-          }
-        }
-
-        btn.mousedown(function(e){
-          var pos = e.pageY - btn.offset().top;
-          var vector = (btn.height()/2 > pos ? 1 : -1);
-          (function(){
-            spin(vector);
-            var tk = setTimeout(arguments.callee, opt.timeInterval);
-            $(document).one('mouseup', function(){
-              clearTimeout(tk); btn.attr('src', spinBtnImage);
-            });
-          })();
-          return false;
-        });
-      });
-    }
-  });
-})(jQuery);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/jquery/jquery-spin-1.1.1.js	Fri Jun 19 23:13:06 2009 +0100
@@ -0,0 +1,139 @@
+/**
+ * jQuery Spin 1.1.1
+ *
+ * Copyright (c) 2009 Naohiko MORI
+ * Dual licensed under the MIT and GPL licenses.
+ *
+ **/
+(function($){
+  var calcFloat = {
+    get: function(num){
+      var num = num.toString();
+      if(num.indexOf('.')==-1) return[0, eval(num)];
+      var nn = num.split('.');
+      var po = nn[1].length;
+      var st = nn.join('');
+      var sign = '';
+      if(st.charAt(0)=='-'){
+        st = st.substr(1);
+        sign = '-';
+      }
+      for(var i=0; i<st.length; ++i) if(st.charAt(0)=='0') st=st.substr(1, st.length);
+      st = sign + st;
+      return [po, eval(st)];
+    },
+    getInt: function(num, figure){
+      var d = Math.pow(10, figure);
+      var n = this.get(num);
+      var v1 = eval('num * d');
+      var v2 = eval('n[1] * d');
+      if(this.get(v1)[1]==v2) return v1;
+      return(n[0]==0 ? v1 : eval(v2 + '/Math.pow(10, n[0])'));
+    },
+    sum: function(v1, v2){
+      var n1 = this.get(v1);
+      var n2 = this.get(v2);
+      var figure = (n1[0] > n2[0] ? n1[0] : n2[0]);
+      v1 = this.getInt(v1, figure);
+      v2 = this.getInt(v2, figure);
+      return eval('v1 + v2')/Math.pow(10, figure);
+    }
+  };
+  $.extend({
+    spin: {
+      imageBasePath: '/soc/content/images/',
+      spinBtnImage: 'spin-button.png',
+      spinUpImage: 'spin-up.png',
+      spinDownImage: 'spin-down.png',
+      interval: 1,
+      max: null,
+      min: null,
+      timeInterval: 500,
+      timeBlink: 200,
+      btnClass: null,
+      btnCss: {cursor: 'pointer', padding: 0, margin: 0, verticalAlign: 'middle'},
+      txtCss: {marginRight: 0, paddingRight: 0},
+      lock: false,
+      decimal: null,
+      beforeChange: null,
+      changed: null,
+      buttonUp: null,
+      buttonDown: null
+    }
+  });
+  $.fn.extend({
+    spin: function(o){
+      return this.each(function(){
+				o = o || {};
+				var opt = {};
+				$.each($.spin, function(k,v){
+					opt[k] = (typeof o[k]!='undefined' ? o[k] : v);
+				});
+        
+        var txt = $(this);
+        
+        var spinBtnImage = opt.imageBasePath+opt.spinBtnImage;
+        var btnSpin = new Image();
+        btnSpin.src = spinBtnImage;
+        var spinUpImage = opt.imageBasePath+opt.spinUpImage;
+        var btnSpinUp = new Image();
+        btnSpinUp.src = spinUpImage;
+        var spinDownImage = opt.imageBasePath+opt.spinDownImage;
+        var btnSpinDown = new Image();
+        btnSpinDown.src = spinDownImage;
+        
+        var btn = $(document.createElement('img'));
+        btn.attr('src', spinBtnImage);
+        if(opt.btnClass) btn.addClass(opt.btnClass);
+        if(opt.btnCss) btn.css(opt.btnCss);
+        if(opt.txtCss) txt.css(opt.txtCss);
+        txt.after(btn);
+				if(opt.lock){
+					txt.focus(function(){txt.blur();});
+        }
+        
+        function spin(vector){
+          var val = txt.val();
+          var org_val = val;
+          if(opt.decimal) val=val.replace(opt.decimal, '.');
+          if(!isNaN(val)){
+            val = calcFloat.sum(val, vector * opt.interval);
+            if(opt.min!==null && val<opt.min) val=opt.min;
+            if(opt.max!==null && val>opt.max) val=opt.max;
+            if(val != txt.val()){
+              if(opt.decimal) val=val.toString().replace('.', opt.decimal);
+              var ret = ($.isFunction(opt.beforeChange) ? opt.beforeChange.apply(txt, [val, org_val]) : true);
+              if(ret!==false){
+                txt.val(val);
+                if($.isFunction(opt.changed)) opt.changed.apply(txt, [val]);
+                txt.change();
+                src = (vector > 0 ? spinUpImage : spinDownImage);
+                btn.attr('src', src);
+                if(opt.timeBlink<opt.timeInterval)
+                  setTimeout(function(){btn.attr('src', spinBtnImage);}, opt.timeBlink);
+              }
+            }
+          }
+          if(vector > 0){
+            if($.isFunction(opt.buttonUp)) opt.buttonUp.apply(txt, [val]);
+          }else{
+            if($.isFunction(opt.buttonDown)) opt.buttonDown.apply(txt, [val]);
+          }
+        }
+        
+        btn.mousedown(function(e){
+          var pos = e.pageY - btn.offset().top;
+          var vector = (btn.height()/2 > pos ? 1 : -1);
+          (function(){
+            spin(vector);
+            var tk = setTimeout(arguments.callee, opt.timeInterval);
+            $(document).one('mouseup', function(){
+              clearTimeout(tk); btn.attr('src', spinBtnImage);
+            });
+          })();
+          return false;
+        });
+      });
+    }
+  });
+})(jQuery);
Binary file app/soc/content/images/spin-button.png has changed
Binary file app/soc/content/images/spin-down.png has changed
Binary file app/soc/content/images/spin-up.png has changed
--- a/app/soc/templates/soc/base.html	Fri Jun 19 20:19:03 2009 +0200
+++ b/app/soc/templates/soc/base.html	Fri Jun 19 23:13:06 2009 +0100
@@ -62,7 +62,7 @@
     <script type='text/javascript' src="/jquery/jquery-purr.js"></script>
   {% endif %}
   {% if uses_jq_spin %}
-    <script type='text/javascript' src="/jquery/jquery-spin-1.0.2.js"></script>
+    <script type='text/javascript' src="/jquery/jquery-spin-1.1.1.js"></script>
   {% endif %}
   {% if uses_jq_bgiframe %}
   <script type='text/javascript' src='/jquery/jquery-bgiframe.js'></script>