-
Notifications
You must be signed in to change notification settings - Fork 19
/
jquery.groupinputs.min.js
12 lines (12 loc) · 2.29 KB
/
jquery.groupinputs.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/**
* GroupInputs v. 0.8.3
* @author Pavel Kornilov <[email protected]> <[email protected]>
* https://github.com/lusever/groupinputs
* MIT Licensed
*/
(function($){'use strict';function m(j,h,a){var l;if(void 0!==h)j.setSelectionRange?j.setSelectionRange(h,a):document.selection&&document.selection.createRange&&(l=j.createTextRange(),l.collapse(!0),l.moveEnd("character",a),l.moveStart("character",h),l.select());else{a=h=0;if("selectionStart"in j)h=j.selectionStart,a=j.selectionEnd;else if(j.createTextRange){l=document.selection.createRange();var n=l.duplicate();l.parentElement()===j&&(h=-n.moveStart("character",-1E5),a=h+l.text.length)}return{start:h,end:a}}}
var p=!!window.opera,q="maxlength";
$.fn.groupinputs=function(){function j(e,d){var b=e[0].value,c=m(e[0]).end,f=b.slice(0,c),g=b.slice(c),j=d.a-g.length,h=!1,k;for(k=a.length-1;k>d.index;k--)j+=l[k]-a[k].value.length;f.length>j&&(f=f.slice(0,j),c=j);if(b.length>d.a){e[0].value=(f+g).slice(0,d.a);c<=d.a&&(m(e[0],c,c),h=!0);b=(f+g).slice(d.a);if(b.length)for(c-=Math.min(d.a,f.length);a[++k];){f=l[k];b+=a[k].value;a.eq(k).val(b.slice(0,f)).change();if(b.length<=f)break;g=a[k].value.length;h||(c<f&&(h=!0,a.eq(k).focus(),m(a[k],c,
c)),c-=g);b=b.slice(f)}h||(a.eq(k).focus(),m(a[k],c,c))}}function h(e){var d=e.type,b=e.data,c=e.data.c,f=e.data.index,g;p&&"keypress"===d&&(d="keydown");"keydown"===d&&39===e.keyCode&&(g=m(c[0]),g.start===this.value.length&&f!==a.length-1&&(a.eq(f+1).focus(),m(a[f+1],0,0),e.preventDefault()));if("keydown"===d&&(8===e.keyCode||37===e.keyCode))if(g=m(c[0]),g.start===g.end&&0===g.start&&0!==f){g=a.eq(f-1);var h=g.val().length;g.focus();m(g[0],h,h);37===e.keyCode&&e.preventDefault()}if("keyup"===d||
"keydown"===d){if(48>e.keyCode)return;if("keyup"===d&&b.b){b.b=!1;return}if(e.metaKey){b.b=!0;return}g=m(c[0]);g.start===g.end&&(g.start===this.value.length&&f!==a.length-1&&this.value.length===b.a)&&(a.eq(f+1).focus(),m(a[f+1],0,0))}"paste"===d&&c.attr(q,n);("propertychange"===d||"input"===d)&&c.attr(q)!==b.a&&setTimeout(function(){j(c,b);c.attr(q,b.a)},0);"input"===d&&p&&j(c,b)}if(/iPad|iPhone/.test(navigator.platform))return this;var a=this,l=[],n=0;a.each(function(e){var d=a.eq(e),b=+d.attr(q);
n+=b;l.push(b);d.on("keydown keypress keyup input paste propertychange",{c:d,index:e,a:b},h)});p&&a.attr(q,n);return a};})(jQuery);