1 |
- (function(e){function t(e){if(window.console&&window.console.log)window.console.log("textarea count: "+e.size())}function n(e,t,n){var s=e.scrollTop;if(e.setSelectionRange)r(e,t,n);else if(document.selection)i(e,t,n);e.scrollTop=s}function r(e,t,n){var r=e.selectionStart;var i=e.selectionEnd;if(r==i){if(t){if(r-n.tabString==e.value.substring(r-n.tabString.length,r)){e.value=e.value.substring(0,r-n.tabString.length)+e.value.substring(r);e.focus();e.setSelectionRange(r-n.tabString.length,r-n.tabString.length)}else if(r-n.tabString==e.value.substring(r,r+n.tabString.length)){e.value=e.value.substring(0,r)+e.value.substring(r+n.tabString.length);e.focus();e.setSelectionRange(r,r)}}else{e.value=e.value.substring(0,r)+n.tabString+e.value.substring(r);e.focus();e.setSelectionRange(r+n.tabString.length,r+n.tabString.length)}}else{while(r<e.value.length&&e.value.charAt(r).match(/[ \t]/))r++;var s=e.value.split("\n");var o=new Array;var u=0;var a=0;var f=false;for(var l in s){a=u+s[l].length;o.push({start:u,end:a,selected:u<=r&&a>r||a>=i&&u<i||u>r&&a<i});u=a+1}var c=0;for(var l in o){if(o[l].selected){var h=o[l].start+c;if(t&&n.tabString==e.value.substring(h,h+n.tabString.length)){e.value=e.value.substring(0,h)+e.value.substring(h+n.tabString.length);c-=n.tabString.length}else if(!t){e.value=e.value.substring(0,h)+n.tabString+e.value.substring(h);c+=n.tabString.length}}}e.focus();var p=r+(c>0?n.tabString.length:c<0?-n.tabString.length:0);var d=i+c;e.setSelectionRange(p,d)}}function i(t,n,r){var i=document.selection.createRange();if(t==i.parentElement()){if(""==i.text){if(n){var s=i.getBookmark();i.moveStart("character",-r.tabString.length);if(r.tabString==i.text){i.text=""}else{i.moveToBookmark(s);i.moveEnd("character",r.tabString.length);if(r.tabString==i.text)i.text=""}i.collapse(true);i.select()}else{i.text=r.tabString;i.collapse(false);i.select()}}else{var o=i.text;var u=o.length;var a=o.split("\r\n");var f=document.body.createTextRange();f.moveToElementText(t);f.setEndPoint("EndToStart",i);var l=f.text;var c=l.split("\r\n");var h=l.length;var p=document.body.createTextRange();p.moveToElementText(t);p.setEndPoint("StartToEnd",i);var d=p.text;var v=document.body.createTextRange();v.moveToElementText(t);v.setEndPoint("StartToEnd",f);var m=v.text;var g=e(t).html();e("#r3").text(h+" + "+u+" + "+d.length+" = "+g.length);if(h+m.length<g.length){c.push("");h+=2;if(n&&r.tabString==a[0].substring(0,r.tabString.length))a[0]=a[0].substring(r.tabString.length);else if(!n)a[0]=r.tabString+a[0]}else{if(n&&r.tabString==c[c.length-1].substring(0,r.tabString.length))c[c.length-1]=c[c.length-1].substring(r.tabString.length);else if(!n)c[c.length-1]=r.tabString+c[c.length-1]}for(var y=1;y<a.length;y++){if(n&&r.tabString==a[y].substring(0,r.tabString.length))a[y]=a[y].substring(r.tabString.length);else if(!n)a[y]=r.tabString+a[y]}if(1==c.length&&0==h){if(n&&r.tabString==a[0].substring(0,r.tabString.length))a[0]=a[0].substring(r.tabString.length);else if(!n)a[0]=r.tabString+a[0]}if(h+u+d.length<g.length){a.push("");u+=2}f.text=c.join("\r\n");i.text=a.join("\r\n");var b=document.body.createTextRange();b.moveToElementText(t);if(0<h)b.setEndPoint("StartToEnd",f);else b.setEndPoint("StartToStart",f);b.setEndPoint("EndToEnd",i);b.select()}}}e.fn.tabby=function(t){var r=e.extend({},e.fn.tabby.defaults,t);var i=e.fn.tabby.pressed;return this.each(function(){$this=e(this);var t=e.meta?e.extend({},r,$this.data()):r;$this.bind("keydown",function(r){var s=e.fn.tabby.catch_kc(r);if(16==s)i.shft=true;if(17==s){i.ctrl=true;setTimeout(function(){e.fn.tabby.pressed.ctrl=false},1e3)}if(18==s){i.alt=true;setTimeout(function(){e.fn.tabby.pressed.alt=false},1e3)}if(9==s&&!i.ctrl&&!i.alt){r.preventDefault;i.last=s;setTimeout(function(){e.fn.tabby.pressed.last=null},0);n(e(r.target).get(0),i.shft,t);return false}}).bind("keyup",function(t){if(16==e.fn.tabby.catch_kc(t))i.shft=false}).bind("blur",function(t){if(9==i.last)e(t.target).one("focus",function(e){i.last=null}).get(0).focus()})})};e.fn.tabby.catch_kc=function(e){return e.keyCode?e.keyCode:e.charCode?e.charCode:e.which};e.fn.tabby.pressed={shft:false,ctrl:false,alt:false,last:null};e.fn.tabby.defaults={tabString:String.fromCharCode(9)}})(jQuery)
|