jquery.numberbox.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * jQuery EasyUI 1.4.1
  3. *
  4. * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
  7. * To use it on other terms please contact us at info@jeasyui.com
  8. *
  9. */
  10. (function($){
  11. function _1(_2){
  12. var _3=$.data(_2,"numberbox");
  13. var _4=_3.options;
  14. $(_2).addClass("numberbox-f").textbox(_4);
  15. $(_2).textbox("textbox").css({imeMode:"disabled"});
  16. $(_2).attr("numberboxName",$(_2).attr("textboxName"));
  17. _3.numberbox=$(_2).next();
  18. _3.numberbox.addClass("numberbox");
  19. var _5=_4.parser.call(_2,_4.value);
  20. var _6=_4.formatter.call(_2,_5);
  21. $(_2).numberbox("initValue",_5).numberbox("setText",_6);
  22. };
  23. function _7(_8,_9){
  24. var _a=$.data(_8,"numberbox");
  25. var _b=_a.options;
  26. var _9=_b.parser.call(_8,_9);
  27. var _c=_b.formatter.call(_8,_9);
  28. _b.value=_9;
  29. $(_8).textbox("setValue",_9).textbox("setText",_c);
  30. };
  31. $.fn.numberbox=function(_d,_e){
  32. if(typeof _d=="string"){
  33. var _f=$.fn.numberbox.methods[_d];
  34. if(_f){
  35. return _f(this,_e);
  36. }else{
  37. return this.textbox(_d,_e);
  38. }
  39. }
  40. _d=_d||{};
  41. return this.each(function(){
  42. var _10=$.data(this,"numberbox");
  43. if(_10){
  44. $.extend(_10.options,_d);
  45. }else{
  46. _10=$.data(this,"numberbox",{options:$.extend({},$.fn.numberbox.defaults,$.fn.numberbox.parseOptions(this),_d)});
  47. }
  48. _1(this);
  49. });
  50. };
  51. $.fn.numberbox.methods={options:function(jq){
  52. var _11=jq.data("textbox")?jq.textbox("options"):{};
  53. return $.extend($.data(jq[0],"numberbox").options,{width:_11.width,originalValue:_11.originalValue,disabled:_11.disabled,readonly:_11.readonly});
  54. },fix:function(jq){
  55. return jq.each(function(){
  56. $(this).numberbox("setValue",$(this).numberbox("getText"));
  57. });
  58. },setValue:function(jq,_12){
  59. return jq.each(function(){
  60. _7(this,_12);
  61. });
  62. },clear:function(jq){
  63. return jq.each(function(){
  64. $(this).textbox("clear");
  65. $(this).numberbox("options").value="";
  66. });
  67. },reset:function(jq){
  68. return jq.each(function(){
  69. $(this).textbox("reset");
  70. $(this).numberbox("setValue",$(this).numberbox("getValue"));
  71. });
  72. }};
  73. $.fn.numberbox.parseOptions=function(_13){
  74. var t=$(_13);
  75. return $.extend({},$.fn.textbox.parseOptions(_13),$.parser.parseOptions(_13,["decimalSeparator","groupSeparator","suffix",{min:"number",max:"number",precision:"number"}]),{prefix:(t.attr("prefix")?t.attr("prefix"):undefined)});
  76. };
  77. $.fn.numberbox.defaults=$.extend({},$.fn.textbox.defaults,{inputEvents:{keypress:function(e){
  78. var _14=e.data.target;
  79. var _15=$(_14).numberbox("options");
  80. return _15.filter.call(_14,e);
  81. },blur:function(e){
  82. var _16=e.data.target;
  83. $(_16).numberbox("setValue",$(_16).numberbox("getText"));
  84. },keydown:function(e){
  85. if(e.keyCode==13){
  86. var _17=e.data.target;
  87. $(_17).numberbox("setValue",$(_17).numberbox("getText"));
  88. }
  89. }},min:null,max:null,precision:0,decimalSeparator:".",groupSeparator:"",prefix:"",suffix:"",filter:function(e){
  90. var _18=$(this).numberbox("options");
  91. var s=$(this).numberbox("getText");
  92. if(e.which==13){
  93. return true;
  94. }
  95. if(e.which==45){
  96. return (s.indexOf("-")==-1?true:false);
  97. }
  98. var c=String.fromCharCode(e.which);
  99. if(c==_18.decimalSeparator){
  100. return (s.indexOf(c)==-1?true:false);
  101. }else{
  102. if(c==_18.groupSeparator){
  103. return true;
  104. }else{
  105. if((e.which>=48&&e.which<=57&&e.ctrlKey==false&&e.shiftKey==false)||e.which==0||e.which==8){
  106. return true;
  107. }else{
  108. if(e.ctrlKey==true&&(e.which==99||e.which==118)){
  109. return true;
  110. }else{
  111. return false;
  112. }
  113. }
  114. }
  115. }
  116. },formatter:function(_19){
  117. if(!_19){
  118. return _19;
  119. }
  120. _19=_19+"";
  121. var _1a=$(this).numberbox("options");
  122. var s1=_19,s2="";
  123. var _1b=_19.indexOf(".");
  124. if(_1b>=0){
  125. s1=_19.substring(0,_1b);
  126. s2=_19.substring(_1b+1,_19.length);
  127. }
  128. if(_1a.groupSeparator){
  129. var p=/(\d+)(\d{3})/;
  130. while(p.test(s1)){
  131. s1=s1.replace(p,"$1"+_1a.groupSeparator+"$2");
  132. }
  133. }
  134. if(s2){
  135. return _1a.prefix+s1+_1a.decimalSeparator+s2+_1a.suffix;
  136. }else{
  137. return _1a.prefix+s1+_1a.suffix;
  138. }
  139. },parser:function(s){
  140. s=s+"";
  141. var _1c=$(this).numberbox("options");
  142. if(parseFloat(s)!=s){
  143. if(_1c.prefix){
  144. s=$.trim(s.replace(new RegExp("\\"+$.trim(_1c.prefix),"g"),""));
  145. }
  146. if(_1c.suffix){
  147. s=$.trim(s.replace(new RegExp("\\"+$.trim(_1c.suffix),"g"),""));
  148. }
  149. if(_1c.groupSeparator){
  150. s=$.trim(s.replace(new RegExp("\\"+_1c.groupSeparator,"g"),""));
  151. }
  152. if(_1c.decimalSeparator){
  153. s=$.trim(s.replace(new RegExp("\\"+_1c.decimalSeparator,"g"),"."));
  154. }
  155. s=s.replace(/\s/g,"");
  156. }
  157. var val=parseFloat(s).toFixed(_1c.precision);
  158. if(isNaN(val)){
  159. val="";
  160. }else{
  161. if(typeof (_1c.min)=="number"&&val<_1c.min){
  162. val=_1c.min.toFixed(_1c.precision);
  163. }else{
  164. if(typeof (_1c.max)=="number"&&val>_1c.max){
  165. val=_1c.max.toFixed(_1c.precision);
  166. }
  167. }
  168. }
  169. return val;
  170. }});
  171. })(jQuery);