Passed
Push — development ( d00063...059805 )
by Nils
06:01
created

includes/js/numeric/jquery.numeric.js   F

Complexity

Total Complexity 98
Complexity/F 10.89

Size

Lines of Code 66
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 0
dl 0
loc 66
rs 1.5789
c 0
b 0
f 0
wmc 98
mnd 5
bc 42
fnc 9
bpm 4.6666
cpm 10.8888
noi 3

How to fix   Complexity   

Complexity

Complex classes like includes/js/numeric/jquery.numeric.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
(function($){$.fn.numeric=function(config,callback)
2
{if(typeof config==='boolean')
3
{config={decimal:config};}
4
config=config||{};if(typeof config.negative=="undefined")config.negative=true;var decimal=(config.decimal===false)?"":config.decimal||".";var negative=(config.negative===true)?true:false;var callback=typeof callback=="function"?callback:function(){};return this.data("numeric.decimal",decimal).data("numeric.negative",negative).data("numeric.callback",callback).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);}
5
$.fn.numeric.keypress=function(e)
6
{var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var key=e.charCode?e.charCode:e.keyCode?e.keyCode:0;if(key==13&&this.nodeName.toLowerCase()=="input")
7
{return true;}
8
else if(key==13)
9
{return false;}
10
var allow=false;if((e.ctrlKey&&key==97)||(e.ctrlKey&&key==65))return true;if((e.ctrlKey&&key==120)||(e.ctrlKey&&key==88))return true;if((e.ctrlKey&&key==99)||(e.ctrlKey&&key==67))return true;if((e.ctrlKey&&key==122)||(e.ctrlKey&&key==90))return true;if((e.ctrlKey&&key==118)||(e.ctrlKey&&key==86)||(e.shiftKey&&key==45))return true;if(key<48||key>57)
11
{if(this.value.indexOf("-")!=0&&negative&&key==45&&(this.value.length==0||($.fn.getSelectionStart(this))==0))return true;if(decimal&&key==decimal.charCodeAt(0)&&this.value.indexOf(decimal)!=-1)
12
{allow=false;}
13
if(key!=8&&key!=9&&key!=13&&key!=35&&key!=36&&key!=37&&key!=39&&key!=46)
14
{allow=false;}
15
else
16
{if(typeof e.charCode!="undefined")
17
{if(e.keyCode==e.which&&e.which!=0)
18
{allow=true;if(e.which==46)allow=false;}
19
else if(e.keyCode!=0&&e.charCode==0&&e.which==0)
20
{allow=true;}}}
21
if(decimal&&key==decimal.charCodeAt(0))
22
{if(this.value.indexOf(decimal)==-1)
23
{allow=true;}
24
else
25
{allow=false;}}}
26
else
27
{allow=true;}
28
return allow;}
29
$.fn.numeric.keyup=function(e)
30
{var val=this.value;if(val.length>0)
31
{var carat=$.fn.getSelectionStart(this);var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");if(decimal!="")
32
{var dot=val.indexOf(decimal);if(dot==0)
33
{this.value="0"+val;}
34
if(dot==1&&val.charAt(0)=="-")
35
{this.value="-0"+val.substring(1);}
36
val=this.value;}
37
var validChars=[0,1,2,3,4,5,6,7,8,9,'-',decimal];var length=val.length;for(var i=length-1;i>=0;i--)
38
{var ch=val.charAt(i);if(i!=0&&ch=="-")
39
{val=val.substring(0,i)+val.substring(i+1);}
40
else if(i==0&&!negative&&ch=="-")
41
{val=val.substring(1);}
42
var validChar=false;for(var j=0;j<validChars.length;j++)
43
{if(ch==validChars[j])
44
{validChar=true;break;}}
45
if(!validChar||ch==" ")
46
{val=val.substring(0,i)+val.substring(i+1);}}
47
var firstDecimal=val.indexOf(decimal);if(firstDecimal>0)
48
{for(var i=length-1;i>firstDecimal;i--)
49
{var ch=val.charAt(i);if(ch==decimal)
50
{val=val.substring(0,i)+val.substring(i+1);}}}
51
this.value=val;$.fn.setSelection(this,carat);}}
52
$.fn.numeric.blur=function()
53
{var decimal=$.data(this,"numeric.decimal");var callback=$.data(this,"numeric.callback");var val=this.value;if(val!="")
54
{var re=new RegExp("^\\d+$|\\d*"+decimal+"\\d+");if(!re.exec(val))
55
{callback.apply(this);}}}
56
$.fn.removeNumeric=function()
57
{return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).unbind("keypress",$.fn.numeric.keypress).unbind("blur",$.fn.numeric.blur);}
58
$.fn.getSelectionStart=function(o)
59
{if(o.createTextRange)
60
{var r=document.selection.createRange().duplicate();r.moveEnd('character',o.value.length);if(r.text=='')return o.value.length;return o.value.lastIndexOf(r.text);}else return o.selectionStart;}
61
$.fn.setSelection=function(o,p)
62
{if(typeof p=="number")p=[p,p];if(p&&p.constructor==Array&&p.length==2)
63
{if(o.createTextRange)
64
{var r=o.createTextRange();r.collapse(true);r.moveStart('character',p[0]);r.moveEnd('character',p[1]);r.select();}
65
else if(o.setSelectionRange)
66
{o.focus();o.setSelectionRange(p[0],p[1]);}}}})(jQuery);