-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.multioperator.min.js
11 lines (10 loc) · 2.09 KB
/
jquery.multioperator.min.js
1
2
3
4
5
6
7
8
9
10
11
/*!
Multioperator v0.0.1 - 2014-04-24
jQuery plugin to make operations from-this-target input, from-this input or same input
check ./index.html and readme.md files
(c) 2014 Christian Rodriguez - http://www.rcchristiane.com.mx/
- https://github.com/kalmecak/jquery-multioperate-input
license: http://www.opensource.org/licenses/mit-license.php
Depends jquery v1.10.1
*/
(function(a){a.fn.multioperator=function(m){var e=a.extend({trigger:"keypress",keyPress:13,isMoney:true,decimals:2,comma:true,from:false,target:false},m);e.from=e.from?e.from:a(this).data("from")?a(this).data("from"):false;e.target=e.target?e.target:a(this).data("target")?a(this).data("target"):false;var g=new RegExp(/\-|\+/);var l=new RegExp(/\%/);var j=new RegExp(/\/|\*|[x]/i);a(this).on("focusin",function(){var n=a(this).val().trim();if(n!=""){a(this).val(n+" ")}});a(this).on("focusout",function(){a(this).val(a(this).val().trim())});function k(p,o){var q=parseFloat(o);if(!q){return 0}q=Math.abs(q);var n=p*(q/100);q=/\-/.test(o)?p-n:p+n;return q}function b(o,n){var p=parseFloat(n);if(!p){return 0}return o+p}function c(o,n){var p=n.replace(j,"");p=parseFloat(p);if(!p){return}if(/\*|[x]/i.test(n)){return(o*p)}else{if(/\//.test(n)){return(o/p)}}}function f(n,o,p){if(j.test(o)){return c(p,o)}else{if(l.test(o)&&g.test(o)){return k(p,o)}else{if(g.test(o)&&!l.test(o)){return b(p,o)}else{return n}}}}function h(n){n=n.split(".");n[0]=n[0].replace(/\B(?=(\d{3})+(?!\d))/g,",");return n.join(".")}var d=(e.trigger==="keyup"||e.trigger==="keydown"||e.trigger==="keypress");a(this).on(e.trigger,function(s){if(d&&e.keyPress&&s.keyCode!==e.keyPress){return}var t=e.from;var p=t?a("#"+t).val():a(this).val();var r=p.replace("$","").replace(",","").trim();var n=t?" "+a(this).val():r;n=n.split(" ");var o=t?r:n[0];o=parseFloat(o);if(!o){return}var q=parseFloat(o);for(i in n){if(!t){if(n[i]!=""&&i!=0){q=f(o,n[i],q)}}else{if(n[i]!=""){q=f(o,n[i],q)}}}q=q.toFixed(e.decimals);if(e.comma){q=h(q)}if(e.isMoney){q="$"+q}if(e.target){a("#"+e.target).val(q+" ")}else{a(this).val(q+" ")}});return this}}(jQuery));