You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
3.2 KiB

2 years ago
  1. (function(n,t){typeof define=="function"&&define.amd?define(["$"],t):typeof exports=="object"?module.exports=t():n.FormatNumber=t(window.Zepto||window.jQuery||$)})(this,function(n){n.fn.FormatNumber=function(i){var r=[];return n(this).each(function(){var f=n.extend({trigger:n(this)},i),u=new t;u.init(f);r.push(u)}),r};var t=function(){};return t.prototype={init:function(t){function e(t){var f=n(this).attr("data-name"),e=f.split(".")[1]||f.split(".")[0],r;o(t,n(this)[0],u);r=i.getMoneyfloat(n(this).val());n("#"+e).val(r);n(this).attr("data-value",r)}function o(n,t,i){var o=t.value.length,r=h(t),e=window.event?n.keyCode:n.which,u;"17,65".indexOf(e)>-1||(u=c(e,t.value,i),t.value=u,"8,46".indexOf(e)>-1?setTimeout(function(){r+=u.length-o;f(t,r)},10):(r+=u.length-o,f(t,r)))}function s(n){for(var t in r)if(n==r[t])return!0;return!1}function h(n){var t=n,i=-1,r;return t.selectionStart!=undefined?i=t.selectionStart:(r=document.selection.createRange(),r.moveStart("character",-t.value.length),i=r.text.length),i}function c(n,r,u){var c,v,e,h,o,y;if(s(n))return r;var f=r,l=!1,a=/[^\d\.]/g;if(t.minus&&/^\-/.test(f)&&(f=f.slice(1),l=!0),f.indexOf(".")<=0?f=f.replace(a,""):(f=f.replace(a,""),c=parseFloat(f+"00"),isNaN(c)&&(f=c),/\./.test(f)&&t.decimal==0&&(f=f.toString().replace(/\./g,""))),v=new RegExp(u),v.exec(f)||f==""||(f="0"),e=f.split("."),f=e.length>1?(e[0].length>14?e[0].substr(0,14):e[0])+"."+e[1]:f.length>14?f.substr(0,14):f,h=i.doFormat(f),l&&(h="-"+h),h!=null)return o=h.split("."),f.lastIndexOf(".")>=0?f.lastIndexOf(".")==f.length-1?f=o[0]+".":(y=f.length-(f.lastIndexOf(".")+1),f=o[0]+"."+(o[1]?o[1].substring(0,y):"0")):f=o[0],f}function f(n,t){var i=n,r;i.selectionStart!=undefined?i.setSelectionRange(t,t):(r=i.createTextRange(),r.move("character",t),r.select())}var i=this,r,u;t=n.extend({trigger:'[data-type="money"]',decimal:2,minus:!1,parent:"body"},t);this.settings=t;r={left:37,right:39,top:38,down:40,home:36,end:35,shift:16};u="([1-9]\\d*(\\.\\d{1,2})?|0(\\.\\d{1,2})?)";u=t.decimal<=0&&t.minus==!0?"(^-?[1-9]\\d*$)|(^[0]$)":t.decimal==0?"(^[1-9]\\d*$)|(^[0]$)":t.minus==!0?"(^-)?((([1-9]d*(\\.\\d{1,"+t.decimal+"})?)|((^[0]{1}\\.(\\d{1,"+t.decimal+"})?$))|^[0]$))":"([1-9]\\d*(\\.\\d{1,"+t.decimal+"})?|([^0]{1}(\\.\\d{1,"+t.decimal+"}))?)";parent=parent||"body";n(t.trigger,parent).each(function(t,r){var u,f,e,o;n(r)[0].tagName=="INPUT"?(u=n(r).val(),n(r).attr("data-name")&&(e=n(r).attr("data-name"),o=e.split(".")[1]||e.split(".")[0],n(r).parent().append('<input data-rule="number" id="'+o+'" type="hidden" />'),n("#"+o).val(i.getMoneyfloat(n(r).val()))),f=i.doFormat(u),n(r).val(f),n(this).attr("data-value",i.getMoneyfloat(n(r).val()))):(u=n(r).text(),f=i.doFormat(u),n(r).attr("data-value",u).text(f))});n(t.trigger,parent).on("keyup keydown",function(n){var i=this,t=n;setTimeout(function(){if(t.keyCode===9)return!1;e.call(i,t)},10)})},getMoneyfloat:function(n){return n==""?null:parseFloat((n+"").replace(/[^\d\.-]/g,""))},doFormat:function(t){var i=this;return t?(n.isNumeric(t)&&(t=t.toString()),typeof t=="string"&&(t=t.replace(/^(\d+)((\.\d*)?)$/,function(n,t,r){var u=t.replace(/\d{1,3}(?=(\d{3})+$)/g,"$&,"),f=(r||".00000000000").slice(0,i.settings.decimal+1);return i.settings.decimal===0?u:u+f})),t.replace(/^\./,"0.")):""}},t});