-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.js
51 lines (50 loc) · 1.58 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Number.prototype.toMax=function(n){
out=this.toPrecision(n);
if(out.match(/[.,]/)) out=out.replace(/[.,]?0*$/,'');
return out;
};
jQuery.fn.floatVal=function(){
v=parseFloat(this.val());
return isNaN(v)?0.0:v;
};
jQuery.fn.intVal=function(v){
v=parseInt(this.val());
return isNaN(v)?0:v;
};
jQuery.fn.fancyput=function(unit){
var e=$('<div class="fancyputouter text ui-widget-content ui-corner-all"></div>');
this.replaceWith(e).appendTo(e).css('display: inline');
if(unit) $('<span></span>').addClass('unit').html(unit).appendTo(e);
this.css('border-style: none;');
return this;
}
jQuery.fn.fancyOutput=function(unit){
if(!this.parent().hasClass('fancyputouter')){
this.fancyput(unit).addClass('fancyoutput');
this.parent().addClass('fancyoutputouter');
}
return this;
}
jQuery.fn.fancyInput=function(unit){
if(!this.parent().hasClass('fancyputouter')){
this.fancyput(unit).addClass('fancyinput text ui-widget-content');
this.parent().addClass('fancyinputouter').click(function(){
$('input',this).focus().setCursorPosition(-1);
});
}
return this;
}
jQuery.fn.setCursorPosition=function(pos){
if(pos<0) pos=$(this).val().length+pos+1;
if($(this).get(0).setSelectionRange){
$(this).get(0).setSelectionRange(pos,pos);
}
else if($(this).get(0).createTextRange){
var range=$(this).get(0).createTextRange();
range.collapse(true);
range.moveEnd('character',pos);
range.moveStart('character',pos);
range.select();
}
return this;
}