plexpy/data/interfaces/default/js/jquery.inputaffix.min.js
2020-10-04 14:14:20 -07:00

1 line
8.3 KiB
JavaScript

"use strict";!function(t){if(!t)throw new Error("Input Affix: no jQuery found. Make sure window.jQuery exists.");t.fn.caret=function(t,e){var a,r,n=this[0];if("object"==typeof t&&"number"==typeof t.start)a=t.start,r="number"==typeof t.end?t.end:t.start;else if("number"==typeof t)a=t,r="number"==typeof e?e:a;else if("string"==typeof t)(a=n.value.indexOf(t))>-1?r=a+t.length:a=null;else if("[object RegExp]"===Object.prototype.toString.call(t)){var s=t.exec(n.value);null!=s&&(r=(a=s.index)+s[0].length)}if(void 0!==a){if(n.createTextRange){var i=n.createTextRange();i.collapse(!0),i.moveStart("character",a),i.moveEnd("character",r-a),i.select()}else n.selectionStart=a,n.selectionEnd=r;return n.focus(),this}if(document.selection){var l=document.selection;if("textarea"!=n.tagName.toLowerCase()){var h=this.val();(g=l.createRange().duplicate()).moveEnd("character",h.length);var f=""==g.text?h.length:h.lastIndexOf(g.text);(g=l.createRange().duplicate()).moveStart("character",-h.length);var u=g.text.length}else{var g,d=(g=l.createRange()).duplicate();d.moveToElementText(n),d.setEndPoint("EndToEnd",g);u=(f=d.text.length-g.text.length)+g.text.length}}else f=n.selectionStart,u=n.selectionEnd;var c=n.value.substring(f,u);return{start:f,end:u,text:c,replace:function(t){return n.value.substring(0,f)+t+n.value.substring(u,n.value.length)}}},t.fn.affixValue=function(t){if(void 0===t){t=this.val();var e=this.data("prefix"),a=this.data("suffix");return e&&t.substr(0,e.length)==e&&(t=t.substr(e.length)),a&&t.substr(t.length-a.length)==a&&(t=t.substr(0,t.length-a.length)),t}return t+="",this.data("prefix")&&(t=this.data("prefix")+t),this.data("suffix")&&(t+=this.data("suffix")),this.val(t),this},t.fn.prefix=function(e,a){if(void 0===e)return this.data("prefix");var r=this.val(),n=this.data("prefix");return n&&r.substr(0,n.length)==n&&(this.val(r.substr(n.length,r.length-n.length)),r=this.val()),"object"==typeof e?(this.data("prefix-list",e),e=e[a=a||0]):"number"==typeof e&&this.data("prefix-list")?(a=e,e=this.data("prefix-list")[a]):this.data("prefix-list",!1),this.data("prefix",e),this.data("prefixQueue",0),this.trigger("prefixchange",[e,a||0]),""==this.affixValue()&&this.attr("placeholder")||0==r.indexOf(e)||this.val(e+this.val()),this.on("keypress",function(a){var r=t(this),n=r.caret(),s=r.data("prefix"),i=r.val();if(!a.ctrlKey&&n.start<s.length){var l=r.data("prefix-list"),h=String.fromCharCode(a.which);if(l&&0==n.start)for(var f=0;f<l.length;f++)if(l[f].substr(0,1)==h){r.prefix(f);r.prefix();return r.caret({start:1}),void a.preventDefault()}if(l&&i.substr(0,e.length)!=e)return;var u=r.data("prefixQueue");if(n.start==n.end&&s.substr(n.start,1)==h)r.caret(n.start),r.data("prefixQueue",n.start+1),a.preventDefault();else if(n.start!=n.end&&s.substr(n.start,1)==h){var g=s+i.substr(n.end);r.data("suffix")&&(g+=r.data("suffix")),r.val(g),r.caret(n.start+1),a.preventDefault()}else if(n.start==n.end&&u==n.start){var d=n.start+s.length;r.val(s+i.substr(0,n.start)+h+i.substr(s.length)),r.caret(d+1),r.data("prefixQueue",0),a.preventDefault()}else r.caret({start:s.length,end:Math.max(n.end,s.length)})}}).on("keydown",function(e){var a=t(this),r=a.caret(),n=a.data("prefix");if(8==e.which||46==e.which){if(a.data("prefix-list"))return;8==e.which&&r.end<=n.length?(a.caret(n.length),e.preventDefault()):r.end<n.length?a.caret(n.length):r.start<n.length&&a.caret({start:n.length,end:r.end})}else if(86==e.which&&e.ctrlKey){if(a.data("prefix-list"))return;if(r.start<n.length){var s=r.end;s<n.length&&(s+=n.length),a.caret({start:n.length,end:s})}}else 36==e.which&&e.ctrlKey&&(e.shiftKey?a.caret({start:n.length,end:Math.max(r.start,n.length)}):a.caret({start:n.length,end:n.length}),e.preventDefault())}).on("paste",function(e){var a=t(this);setTimeout(function(){var t=a.data("prefix"),r=a.caret(),n=a.val(),s=r.end;if(a.data("prefix-list"))for(var i=a.data("prefix-list"),l=0;l<i.length;l++)if(n.substr(0,i[l].length)==i[l]){t=i[l],a.data("prefix",t),a.trigger("prefixchange",[t,l]);break}n.substr(0,t.length)!=t&&(n=t+n,s+=t.length);var h=n.substr(t.length),f=h;for(l=t.length-1;l>=0;l--)if(h.substr(0,l+1)==t.substr(t.length-1-l)){f=f.substr(l+1);break}a.val(t+f),a.caret({start:s,end:s}),e.preventDefault()})}).on("change",function(e){var a=t(this).data("prefix"),r=t(this).val();r.substr(0,a.length)!=a&&(r=a+r,t(this).val(r))}).on("blur",function(e){var a=t(this).data("prefix-list"),r=t(this).data("prefix"),n=t(this).val();if(a&&n.substr(0,r.length)!=r){for(var s=0;s<a.length;s++)if(n.substr(0,a[s].length)==a[s])return void t(this).prefix(s);n=r+n,t(this).val(n)}""==t(this).affixValue()&&t(this).attr("placeholder")&&t(this).val("")}).on("focus",function(e){""==t(this).affixValue()&&t(this).affixValue("")}),this},t.fn.suffix=function(e,a){if(void 0===e)return this.data("suffix");var r=this.val(),n=this.data("suffix");return n&&r.substr(r.length-n.length)==n&&this.val(r.substr(0,r.length-n.length)),"object"==typeof e?(this.data("suffix-list",e),e=e[a=a||0]):"number"==typeof e&&this.data("suffix-list")?(a=e,e=this.data("suffix-list")[a]):this.data("suffix-list",!1),this.data("suffix",e),this.data("suffixQueue",0),this.trigger("suffixchange",[e,a||0]),""==this.affixValue()&&this.attr("placeholder")||this.val().substr(this.val().length-e.length)===e||this.val(this.val()+e),this.on("keypress",function(a){var r=t(this),n=r.caret(),s=r.val(),i=r.data("suffix");if(n.end>=s.length-i.length){var l=r.data("suffix-list"),h=String.fromCharCode(a.which);if(l&&n.start>=s.length-i.length)for(var f=0;f<l.length;f++)if(l[f].substr(0,1)==h)return r.val(s.substr(0,n.start)),r.suffix(f),r.caret({start:r.val().length,end:r.val().length}),void a.preventDefault();if(l&&s.substr(s.length-e.length)!=i)return;if(n.start==n.end&&i.substr(n.end-s.length+i.length,1)==h)r.caret({start:n.end+1,end:n.end+1}),r.data("prefixQueue",n.end-s.length+i.length+1),a.preventDefault();else{var u=document.queryCommandValue("OverWrite");n.start==n.end&&r.data("prefixQueue")==n.end-s.length+i.length?(r.val(s.substr(0,n.start)+h+i),r.caret({start:n.start+1,end:n.start+1}),a.preventDefault()):u?(r.val(s.substr(0,n.start)+h+i),r.caret(s.length-i.length+1),a.preventDefault()):r.caret({start:Math.min(n.start,s.length-i.length),end:s.length-i.length})}}else n.end==s.length-i.length&&n.start==n.end&&i.substr(n.end-s.length+i.length,1)==h&&(r.caret({start:n.end+1,end:n.end+1}),a.preventDefault())}).on("keydown",function(e){var a=t(this),r=a.caret(),n=a.val(),s=a.data("suffix");if(8==e.which||46==e.which){if(a.data("suffix-list"))return;if(46==e.which&&r.start>=n.length-s.length)a.caret(n.length-s.length),e.preventDefault();else if(r.end>n.length-s.length){a.caret({start:Math.min(r.start,n.length-s.length),end:n.length-s.length});var i=a.data("prefix");i&&n.length==i.length+s.length&&e.preventDefault()}}else if(86==e.which&&e.ctrlKey){if(r.end>n.length-s.length){var l=r.start;l>n.length-s.length&&(l-=s.length),a.caret({start:l,end:n.length-s.length})}}else 35==e.which&&e.ctrlKey&&(e.shiftKey?a.caret({start:Math.min(n.length-s.length,r.end),end:n.length-s.length}):a.caret({start:n.length-s.length,end:n.length-s.length}),e.preventDefault())}).on("paste",function(e){var a=t(this);setTimeout(function(){var r=a.data("suffix"),n=a.caret(),s=a.val(),i=n.end;if(a.data("suffix-list"))for(var l=a.data("suffix-list"),h=0;h<l.length;h++)if(s.substr(s.length-l[h].length)==l[h]){r=l[h],a.data("suffix",r),a.trigger("suffixchange",[r,h]);break}s.substr(s.length-r.length)!=r&&(s+=r);var f=s.substr(0,s.length-r.length),u=f;n.end>s.length-r.length&&((i=n.start)>s.length-r.length&&(i-=r.length),t(this).caret({start:i,end:s.length-r.length}));for(h=r.length-1;h>=0;h--)if(f.substr(f.length-r.length+h)==r.substr(0,r.length-h)){u=u.substr(0,f.length-r.length+h);break}a.val(u+r),a.caret({start:i,end:i}),e.preventDefault()},1)}).on("change",function(e){var a=t(this).data("suffix"),r=t(this).val();r.substr(r.length-a.length)!=a&&(r+=a,t(this).val(r))}).on("blur",function(e){var a=t(this).data("suffix-list"),r=t(this).data("suffix"),n=t(this).val();if(a&&n.substr(n.length-r.length)!=r){for(var s=0;s<a.length;s++)if(n.substr(n.length-a[s].length)==a[s])return void t(this).suffix(s);n+=r,t(this).val(n)}""==t(this).affixValue()&&t(this).attr("placeholder")&&t(this).val("")}).on("focus",function(e){var a=t(this);if(""==a.affixValue()){a.affixValue("");var r=0;a.data("prefix")&&(r=a.data("prefix").length),setTimeout(function(){a.caret({start:r,end:r})},1),e.preventDefault()}}),this}}(window.jQuery);