Index: trunk/extensions/UsabilityInitiative/Resources/jquery.scrolltextarea.js |
— | — | @@ -0,0 +1,171 @@ |
| 2 | +/**
|
| 3 | + * Plugin that can get the byte offset of the cursor in a textarea, move the
|
| 4 | + * cursor to a byte offset and scroll the textarea to the cursor's new position.
|
| 5 | + */
|
| 6 | +(function($){
|
| 7 | + $.fn.extend({
|
| 8 | + // The getCaret(), getLineLength() and getCaretPosition()
|
| 9 | + // functions were copied from Wikia's LinkSuggest extension and
|
| 10 | + // modified slightly.
|
| 11 | + // https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest/LinkSuggest.js
|
| 12 | +
|
| 13 | + /**
|
| 14 | + * Get the byte position in a textarea
|
| 15 | + */
|
| 16 | + bytePos: function() {
|
| 17 | + function getCaret(control) {
|
| 18 | + var caretPos = 0;
|
| 19 | + // IE Support
|
| 20 | + if($.browser.msie) {
|
| 21 | + // This code was copied from
|
| 22 | + // http://www.dedestruct.com/2008/03/22/howto-cross-browser-cursor-position-in-textareas/
|
| 23 | + var selection_range = document.selection.createRange().duplicate();
|
| 24 | +
|
| 25 | + // Create three ranges, one containing all the text before the selection,
|
| 26 | + // one containing all the text in the selection (this already exists), and one containing all
|
| 27 | + // the text after the selection.
|
| 28 | + var before_range = document.body.createTextRange();
|
| 29 | + before_range.moveToElementText(control); // Selects all the text
|
| 30 | + before_range.setEndPoint("EndToStart", selection_range); // Moves the end where we need it
|
| 31 | +
|
| 32 | + var after_range = document.body.createTextRange();
|
| 33 | + after_range.moveToElementText(control); // Selects all the text
|
| 34 | + after_range.setEndPoint("StartToEnd", selection_range); // Moves the start where we need it
|
| 35 | +
|
| 36 | + var before_finished = false, selection_finished = false, after_finished = false;
|
| 37 | + var before_text, untrimmed_before_text, selection_text, untrimmed_selection_text, after_text, untrimmed_after_text;
|
| 38 | +
|
| 39 | + // Load the text values we need to compare
|
| 40 | + before_text = untrimmed_before_text = before_range.text;
|
| 41 | + selection_text = untrimmed_selection_text = selection_range.text;
|
| 42 | + after_text = untrimmed_after_text = after_range.text;
|
| 43 | +
|
| 44 | +
|
| 45 | + // Check each range for trimmed newlines by shrinking the range by 1 character and seeing
|
| 46 | + // if the text property has changed. If it has not changed then we know that IE has trimmed
|
| 47 | + // a \r\n from the end.
|
| 48 | + do {
|
| 49 | + if (!before_finished) {
|
| 50 | + if (before_range.compareEndPoints("StartToEnd", before_range) == 0) {
|
| 51 | + before_finished = true;
|
| 52 | + } else {
|
| 53 | + before_range.moveEnd("character", -1)
|
| 54 | + if (before_range.text == before_text) {
|
| 55 | + untrimmed_before_text += "\r\n";
|
| 56 | + } else {
|
| 57 | + before_finished = true;
|
| 58 | + }
|
| 59 | + }
|
| 60 | + }
|
| 61 | + if (!selection_finished) {
|
| 62 | + if (selection_range.compareEndPoints("StartToEnd", selection_range) == 0) {
|
| 63 | + selection_finished = true;
|
| 64 | + } else {
|
| 65 | + selection_range.moveEnd("character", -1)
|
| 66 | + if (selection_range.text == selection_text) {
|
| 67 | + untrimmed_selection_text += "\r\n";
|
| 68 | + } else {
|
| 69 | + selection_finished = true;
|
| 70 | + }
|
| 71 | + }
|
| 72 | + }
|
| 73 | + if (!after_finished) {
|
| 74 | + if (after_range.compareEndPoints("StartToEnd", after_range) == 0) {
|
| 75 | + after_finished = true;
|
| 76 | + } else {
|
| 77 | + after_range.moveEnd("character", -1)
|
| 78 | + if (after_range.text == after_text) {
|
| 79 | + untrimmed_after_text += "\r\n";
|
| 80 | + } else {
|
| 81 | + after_finished = true;
|
| 82 | + }
|
| 83 | + }
|
| 84 | + }
|
| 85 | +
|
| 86 | + } while ((!before_finished || !selection_finished || !after_finished));
|
| 87 | +
|
| 88 | + caretPos = untrimmed_before_text.replace(/\r\n/g, "\n").length;
|
| 89 | + // Firefox support
|
| 90 | + } else if (control.selectionStart || control.selectionStart == '0') {
|
| 91 | + caretPos = control.selectionStart;
|
| 92 | + }
|
| 93 | + return caretPos;
|
| 94 | + }
|
| 95 | +
|
| 96 | + return getCaret( this.get( 0 ) );
|
| 97 | + },
|
| 98 | +
|
| 99 | + /**
|
| 100 | + * Scroll a textarea to a certain offset
|
| 101 | + * @param pos Byte offset in the contents
|
| 102 | + */
|
| 103 | + scrollToPosition: function( pos ) {
|
| 104 | + function getLineLength(control) {
|
| 105 | + var width = control.scrollWidth;
|
| 106 | + return Math.floor(width/($.os.name == 'linux' ? 7 : 8));
|
| 107 | + }
|
| 108 | +
|
| 109 | + function getCaretPosition(control) {
|
| 110 | + var text = control.value.replace(/\r/g, "");
|
| 111 | + var caret = $(control).bytePos();
|
| 112 | + var lineLength = getLineLength(control);
|
| 113 | +
|
| 114 | + var row = 0;
|
| 115 | + var charInLine = 0;
|
| 116 | + var lastSpaceInLine = 0;
|
| 117 | +
|
| 118 | + for(i = 0; i < caret; i++) {
|
| 119 | + charInLine++;
|
| 120 | + if(text.charAt(i) == " ") {
|
| 121 | + lastSpaceInLine = charInLine;
|
| 122 | + } else if(text.charAt(i) == "\n") {
|
| 123 | + lastSpaceInLine = 0;
|
| 124 | + charInLine = 0;
|
| 125 | + row++;
|
| 126 | + }
|
| 127 | + if(charInLine > lineLength) {
|
| 128 | + if(lastSpaceInLine > 0) {
|
| 129 | + charInLine = charInLine - lastSpaceInLine;
|
| 130 | +
|
| 131 | + lastSpaceInLine = 0;
|
| 132 | + row++;
|
| 133 | + }
|
| 134 | + }
|
| 135 | + }
|
| 136 | + var nextSpace = 0;
|
| 137 | + for(j = caret; j < caret + lineLength; j++) {
|
| 138 | + if(text.charAt(j) == " " || text.charAt(j) == "\n" || caret == text.length) {
|
| 139 | + nextSpace = j;
|
| 140 | + break;
|
| 141 | + }
|
| 142 | + }
|
| 143 | +
|
| 144 | + if(nextSpace > lineLength && caret <= lineLength) {
|
| 145 | + charInLine = caret - lastSpaceInLine;
|
| 146 | + row++;
|
| 147 | + }
|
| 148 | +
|
| 149 | +
|
| 150 | + return ($.os.name == 'mac' ? 13 : ($.os.name == 'linux' ? 15 : 16))*row;
|
| 151 | + }
|
| 152 | +
|
| 153 | + return this.each(function() {
|
| 154 | + // Put the cursor at the desired position
|
| 155 | + this.focus();
|
| 156 | + if ( this.selectionStart || this.selectionStart == '0' ) { // Mozilla
|
| 157 | + this.selectionStart = this.selectionEnd = pos;
|
| 158 | + } else if ( document.selection && document.selection.createRange ) { // IE/Opera
|
| 159 | + var range = document.selection.createRange();
|
| 160 | + range.moveToElementText( this );
|
| 161 | + range.collapse();
|
| 162 | + //range.moveStart( 'character', pos );
|
| 163 | + range.move( 'character', pos );
|
| 164 | + //alert(range.text);
|
| 165 | + range.select();
|
| 166 | + }
|
| 167 | + $(this).scrollTop( getCaretPosition( this ) );
|
| 168 | + $(this).trigger( 'scrollToPosition' );
|
| 169 | + });
|
| 170 | + }
|
| 171 | + });
|
| 172 | +})(jQuery);
|
Index: trunk/extensions/UsabilityInitiative/Resources/jquery.combined.js |
— | — | @@ -4631,6 +4631,177 @@ |
4632 | 4632 | } |
4633 | 4633 | }; |
4634 | 4634 | |
| 4635 | +/** |
| 4636 | + * Plugin that can get the byte offset of the cursor in a textarea, move the |
| 4637 | + * cursor to a byte offset and scroll the textarea to the cursor's new position. |
| 4638 | + */ |
| 4639 | +(function($){ |
| 4640 | + $.fn.extend({ |
| 4641 | + // The getCaret(), getLineLength() and getCaretPosition() |
| 4642 | + // functions were copied from Wikia's LinkSuggest extension and |
| 4643 | + // modified slightly. |
| 4644 | + // https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest/LinkSuggest.js |
| 4645 | + |
| 4646 | + /** |
| 4647 | + * Get the byte position in a textarea |
| 4648 | + */ |
| 4649 | + bytePos: function() { |
| 4650 | + function getCaret(control) { |
| 4651 | + var caretPos = 0; |
| 4652 | + // IE Support |
| 4653 | + if($.browser.msie) { |
| 4654 | + // This code was copied from |
| 4655 | + // http://www.dedestruct.com/2008/03/22/howto-cross-browser-cursor-position-in-textareas/ |
| 4656 | + var selection_range = document.selection.createRange().duplicate(); |
| 4657 | + |
| 4658 | + // Create three ranges, one containing all the text before the selection, |
| 4659 | + // one containing all the text in the selection (this already exists), and one containing all |
| 4660 | + // the text after the selection. |
| 4661 | + var before_range = document.body.createTextRange(); |
| 4662 | + before_range.moveToElementText(control); // Selects all the text |
| 4663 | + before_range.setEndPoint("EndToStart", selection_range); // Moves the end where we need it |
| 4664 | + |
| 4665 | + var after_range = document.body.createTextRange(); |
| 4666 | + after_range.moveToElementText(control); // Selects all the text |
| 4667 | + after_range.setEndPoint("StartToEnd", selection_range); // Moves the start where we need it |
| 4668 | + |
| 4669 | + var before_finished = false, selection_finished = false, after_finished = false; |
| 4670 | + var before_text, untrimmed_before_text, selection_text, untrimmed_selection_text, after_text, untrimmed_after_text; |
| 4671 | + |
| 4672 | + // Load the text values we need to compare |
| 4673 | + before_text = untrimmed_before_text = before_range.text; |
| 4674 | + selection_text = untrimmed_selection_text = selection_range.text; |
| 4675 | + after_text = untrimmed_after_text = after_range.text; |
| 4676 | + |
| 4677 | + |
| 4678 | + // Check each range for trimmed newlines by shrinking the range by 1 character and seeing |
| 4679 | + // if the text property has changed. If it has not changed then we know that IE has trimmed |
| 4680 | + // a \r\n from the end. |
| 4681 | + do { |
| 4682 | + if (!before_finished) { |
| 4683 | + if (before_range.compareEndPoints("StartToEnd", before_range) == 0) { |
| 4684 | + before_finished = true; |
| 4685 | + } else { |
| 4686 | + before_range.moveEnd("character", -1) |
| 4687 | + if (before_range.text == before_text) { |
| 4688 | + untrimmed_before_text += "\r\n"; |
| 4689 | + } else { |
| 4690 | + before_finished = true; |
| 4691 | + } |
| 4692 | + } |
| 4693 | + } |
| 4694 | + if (!selection_finished) { |
| 4695 | + if (selection_range.compareEndPoints("StartToEnd", selection_range) == 0) { |
| 4696 | + selection_finished = true; |
| 4697 | + } else { |
| 4698 | + selection_range.moveEnd("character", -1) |
| 4699 | + if (selection_range.text == selection_text) { |
| 4700 | + untrimmed_selection_text += "\r\n"; |
| 4701 | + } else { |
| 4702 | + selection_finished = true; |
| 4703 | + } |
| 4704 | + } |
| 4705 | + } |
| 4706 | + if (!after_finished) { |
| 4707 | + if (after_range.compareEndPoints("StartToEnd", after_range) == 0) { |
| 4708 | + after_finished = true; |
| 4709 | + } else { |
| 4710 | + after_range.moveEnd("character", -1) |
| 4711 | + if (after_range.text == after_text) { |
| 4712 | + untrimmed_after_text += "\r\n"; |
| 4713 | + } else { |
| 4714 | + after_finished = true; |
| 4715 | + } |
| 4716 | + } |
| 4717 | + } |
| 4718 | + |
| 4719 | + } while ((!before_finished || !selection_finished || !after_finished)); |
| 4720 | + |
| 4721 | + caretPos = untrimmed_before_text.replace(/\r\n/g, "\n").length; |
| 4722 | + // Firefox support |
| 4723 | + } else if (control.selectionStart || control.selectionStart == '0') { |
| 4724 | + caretPos = control.selectionStart; |
| 4725 | + } |
| 4726 | + return caretPos; |
| 4727 | + } |
| 4728 | + |
| 4729 | + return getCaret( this.get( 0 ) ); |
| 4730 | + }, |
| 4731 | + |
| 4732 | + /** |
| 4733 | + * Scroll a textarea to a certain offset |
| 4734 | + * @param pos Byte offset in the contents |
| 4735 | + */ |
| 4736 | + scrollToPosition: function( pos ) { |
| 4737 | + function getLineLength(control) { |
| 4738 | + var width = control.scrollWidth; |
| 4739 | + return Math.floor(width/($.os.name == 'linux' ? 7 : 8)); |
| 4740 | + } |
| 4741 | + |
| 4742 | + function getCaretPosition(control) { |
| 4743 | + var text = control.value.replace(/\r/g, ""); |
| 4744 | + var caret = $(control).bytePos(); |
| 4745 | + var lineLength = getLineLength(control); |
| 4746 | + |
| 4747 | + var row = 0; |
| 4748 | + var charInLine = 0; |
| 4749 | + var lastSpaceInLine = 0; |
| 4750 | + |
| 4751 | + for(i = 0; i < caret; i++) { |
| 4752 | + charInLine++; |
| 4753 | + if(text.charAt(i) == " ") { |
| 4754 | + lastSpaceInLine = charInLine; |
| 4755 | + } else if(text.charAt(i) == "\n") { |
| 4756 | + lastSpaceInLine = 0; |
| 4757 | + charInLine = 0; |
| 4758 | + row++; |
| 4759 | + } |
| 4760 | + if(charInLine > lineLength) { |
| 4761 | + if(lastSpaceInLine > 0) { |
| 4762 | + charInLine = charInLine - lastSpaceInLine; |
| 4763 | + |
| 4764 | + lastSpaceInLine = 0; |
| 4765 | + row++; |
| 4766 | + } |
| 4767 | + } |
| 4768 | + } |
| 4769 | + var nextSpace = 0; |
| 4770 | + for(j = caret; j < caret + lineLength; j++) { |
| 4771 | + if(text.charAt(j) == " " || text.charAt(j) == "\n" || caret == text.length) { |
| 4772 | + nextSpace = j; |
| 4773 | + break; |
| 4774 | + } |
| 4775 | + } |
| 4776 | + |
| 4777 | + if(nextSpace > lineLength && caret <= lineLength) { |
| 4778 | + charInLine = caret - lastSpaceInLine; |
| 4779 | + row++; |
| 4780 | + } |
| 4781 | + |
| 4782 | + |
| 4783 | + return ($.os.name == 'mac' ? 13 : ($.os.name == 'linux' ? 15 : 16))*row; |
| 4784 | + } |
| 4785 | + |
| 4786 | + return this.each(function() { |
| 4787 | + // Put the cursor at the desired position |
| 4788 | + this.focus(); |
| 4789 | + if ( this.selectionStart || this.selectionStart == '0' ) { // Mozilla |
| 4790 | + this.selectionStart = this.selectionEnd = pos; |
| 4791 | + } else if ( document.selection && document.selection.createRange ) { // IE/Opera |
| 4792 | + var range = document.selection.createRange(); |
| 4793 | + range.moveToElementText( this ); |
| 4794 | + range.collapse(); |
| 4795 | + //range.moveStart( 'character', pos ); |
| 4796 | + range.move( 'character', pos ); |
| 4797 | + //alert(range.text); |
| 4798 | + range.select(); |
| 4799 | + } |
| 4800 | + $(this).scrollTop( getCaretPosition( this ) ); |
| 4801 | + $(this).trigger( 'scrollToPosition' ); |
| 4802 | + }); |
| 4803 | + } |
| 4804 | + }); |
| 4805 | +})(jQuery); |
4635 | 4806 | /* |
4636 | 4807 | * Ported from skins/common/edit.js by Trevor Parscal |
4637 | 4808 | * (c) 2009 Wikimedia Foundation (GPLv2) - http://www.wikimedia.org |
Index: trunk/extensions/UsabilityInitiative/Resources/jquery.combined.min.js |
— | — | @@ -448,7 +448,21 @@ |
449 | 449 | var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;} |
450 | 450 | expires='; expires='+date.toUTCString();} |
451 | 451 | var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}} |
452 | | -return cookieValue;}};(function($){$.fn.extend({encapsulateSelection:function(pre,peri,post){var e=this.jquery?this[0]:this;var selText;var isSample=false;if(document.selection&&document.selection.createRange){if(document.documentElement&&document.documentElement.scrollTop) |
| 452 | +return cookieValue;}};(function($){$.fn.extend({bytePos:function(){function getCaret(control){var caretPos=0;if($.browser.msie){var selection_range=document.selection.createRange().duplicate();var before_range=document.body.createTextRange();before_range.moveToElementText(control);before_range.setEndPoint("EndToStart",selection_range);var after_range=document.body.createTextRange();after_range.moveToElementText(control);after_range.setEndPoint("StartToEnd",selection_range);var before_finished=false,selection_finished=false,after_finished=false;var before_text,untrimmed_before_text,selection_text,untrimmed_selection_text,after_text,untrimmed_after_text;before_text=untrimmed_before_text=before_range.text;selection_text=untrimmed_selection_text=selection_range.text;after_text=untrimmed_after_text=after_range.text;do{if(!before_finished){if(before_range.compareEndPoints("StartToEnd",before_range)==0){before_finished=true;}else{before_range.moveEnd("character",-1) |
| 453 | +if(before_range.text==before_text){untrimmed_before_text+="\r\n";}else{before_finished=true;}}} |
| 454 | +if(!selection_finished){if(selection_range.compareEndPoints("StartToEnd",selection_range)==0){selection_finished=true;}else{selection_range.moveEnd("character",-1) |
| 455 | +if(selection_range.text==selection_text){untrimmed_selection_text+="\r\n";}else{selection_finished=true;}}} |
| 456 | +if(!after_finished){if(after_range.compareEndPoints("StartToEnd",after_range)==0){after_finished=true;}else{after_range.moveEnd("character",-1) |
| 457 | +if(after_range.text==after_text){untrimmed_after_text+="\r\n";}else{after_finished=true;}}}}while((!before_finished||!selection_finished||!after_finished));caretPos=untrimmed_before_text.replace(/\r\n/g,"\n").length;}else if(control.selectionStart||control.selectionStart=='0'){caretPos=control.selectionStart;} |
| 458 | +return caretPos;} |
| 459 | +return getCaret(this.get(0));},scrollToPosition:function(pos){function getLineLength(control){var width=control.scrollWidth;return Math.floor(width/($.os.name=='linux'?7:8));} |
| 460 | +function getCaretPosition(control){var text=control.value.replace(/\r/g,"");var caret=$(control).bytePos();var lineLength=getLineLength(control);var row=0;var charInLine=0;var lastSpaceInLine=0;for(i=0;i<caret;i++){charInLine++;if(text.charAt(i)==" "){lastSpaceInLine=charInLine;}else if(text.charAt(i)=="\n"){lastSpaceInLine=0;charInLine=0;row++;} |
| 461 | +if(charInLine>lineLength){if(lastSpaceInLine>0){charInLine=charInLine-lastSpaceInLine;lastSpaceInLine=0;row++;}}} |
| 462 | +var nextSpace=0;for(j=caret;j<caret+lineLength;j++){if(text.charAt(j)==" "||text.charAt(j)=="\n"||caret==text.length){nextSpace=j;break;}} |
| 463 | +if(nextSpace>lineLength&&caret<=lineLength){charInLine=caret-lastSpaceInLine;row++;} |
| 464 | +return($.os.name=='mac'?13:($.os.name=='linux'?15:16))*row;} |
| 465 | +return this.each(function(){this.focus();if(this.selectionStart||this.selectionStart=='0'){this.selectionStart=this.selectionEnd=pos;}else if(document.selection&&document.selection.createRange){var range=document.selection.createRange();range.moveToElementText(this);range.collapse();range.move('character',pos);range.select();} |
| 466 | +$(this).scrollTop(getCaretPosition(this));$(this).trigger('scrollToPosition');});}});})(jQuery);(function($){$.fn.extend({encapsulateSelection:function(pre,peri,post){var e=this.jquery?this[0]:this;var selText;var isSample=false;if(document.selection&&document.selection.createRange){if(document.documentElement&&document.documentElement.scrollTop) |
453 | 467 | var winScroll=document.documentElement.scrollTop |
454 | 468 | else if(document.body) |
455 | 469 | var winScroll=document.body.scrollTop;e.focus();var range=document.selection.createRange();selText=range.text;checkSelectedText();range.text=pre+selText+post;if(isSample&&range.moveStart){if(window.opera) |
Index: trunk/extensions/UsabilityInitiative/NavigableTOC/NavigableTOC.js |
— | — | @@ -1,176 +1,5 @@ |
2 | 2 | /* JavaScript for NavigableTOC extension */ |
3 | 3 | |
4 | | -/** |
5 | | - * Add a plugin that can scroll a textarea to a certain location |
6 | | - */ |
7 | | -(function($){ |
8 | | - $.fn.extend({ |
9 | | - // The next getCaret(), getLineLength() and getCaretPosition() |
10 | | - // functions were copied from Wikia's LinkSuggest extension and |
11 | | - // modified slightly. |
12 | | - // https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest/LinkSuggest.js |
13 | | - |
14 | | - /** |
15 | | - * Get the byte position in a textarea |
16 | | - */ |
17 | | - bytePos: function() { |
18 | | - function getCaret(control) { |
19 | | - var caretPos = 0; |
20 | | - // IE Support |
21 | | - if($.browser.msie) { |
22 | | - // This code was copied from |
23 | | - // http://www.dedestruct.com/2008/03/22/howto-cross-browser-cursor-position-in-textareas/ |
24 | | - var selection_range = document.selection.createRange().duplicate(); |
25 | | - |
26 | | - // Create three ranges, one containing all the text before the selection, |
27 | | - // one containing all the text in the selection (this already exists), and one containing all |
28 | | - // the text after the selection. |
29 | | - var before_range = document.body.createTextRange(); |
30 | | - before_range.moveToElementText(control); // Selects all the text |
31 | | - before_range.setEndPoint("EndToStart", selection_range); // Moves the end where we need it |
32 | | - |
33 | | - var after_range = document.body.createTextRange(); |
34 | | - after_range.moveToElementText(control); // Selects all the text |
35 | | - after_range.setEndPoint("StartToEnd", selection_range); // Moves the start where we need it |
36 | | - |
37 | | - var before_finished = false, selection_finished = false, after_finished = false; |
38 | | - var before_text, untrimmed_before_text, selection_text, untrimmed_selection_text, after_text, untrimmed_after_text; |
39 | | - |
40 | | - // Load the text values we need to compare |
41 | | - before_text = untrimmed_before_text = before_range.text; |
42 | | - selection_text = untrimmed_selection_text = selection_range.text; |
43 | | - after_text = untrimmed_after_text = after_range.text; |
44 | | - |
45 | | - |
46 | | - // Check each range for trimmed newlines by shrinking the range by 1 character and seeing |
47 | | - // if the text property has changed. If it has not changed then we know that IE has trimmed |
48 | | - // a \r\n from the end. |
49 | | - do { |
50 | | - if (!before_finished) { |
51 | | - if (before_range.compareEndPoints("StartToEnd", before_range) == 0) { |
52 | | - before_finished = true; |
53 | | - } else { |
54 | | - before_range.moveEnd("character", -1) |
55 | | - if (before_range.text == before_text) { |
56 | | - untrimmed_before_text += "\r\n"; |
57 | | - } else { |
58 | | - before_finished = true; |
59 | | - } |
60 | | - } |
61 | | - } |
62 | | - if (!selection_finished) { |
63 | | - if (selection_range.compareEndPoints("StartToEnd", selection_range) == 0) { |
64 | | - selection_finished = true; |
65 | | - } else { |
66 | | - selection_range.moveEnd("character", -1) |
67 | | - if (selection_range.text == selection_text) { |
68 | | - untrimmed_selection_text += "\r\n"; |
69 | | - } else { |
70 | | - selection_finished = true; |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | - if (!after_finished) { |
75 | | - if (after_range.compareEndPoints("StartToEnd", after_range) == 0) { |
76 | | - after_finished = true; |
77 | | - } else { |
78 | | - after_range.moveEnd("character", -1) |
79 | | - if (after_range.text == after_text) { |
80 | | - untrimmed_after_text += "\r\n"; |
81 | | - } else { |
82 | | - after_finished = true; |
83 | | - } |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - } while ((!before_finished || !selection_finished || !after_finished)); |
88 | | - |
89 | | - caretPos = untrimmed_before_text.replace(/\r\n/g, "\n").length; |
90 | | - // Firefox support |
91 | | - } else if (control.selectionStart || control.selectionStart == '0') { |
92 | | - caretPos = control.selectionStart; |
93 | | - } |
94 | | - return caretPos; |
95 | | - } |
96 | | - |
97 | | - return getCaret( this.get( 0 ) ); |
98 | | - }, |
99 | | - |
100 | | - /** |
101 | | - * Scroll a textarea to a certain offset |
102 | | - * @param pos Byte offset in the contents |
103 | | - */ |
104 | | - scrollToPosition: function( pos ) { |
105 | | - function getLineLength(control) { |
106 | | - var width = control.scrollWidth; |
107 | | - return Math.floor(width/($.os.name == 'linux' ? 7 : 8)); |
108 | | - } |
109 | | - |
110 | | - function getCaretPosition(control) { |
111 | | - var text = control.value.replace(/\r/g, ""); |
112 | | - var caret = $(control).bytePos(); |
113 | | - var lineLength = getLineLength(control); |
114 | | - |
115 | | - var row = 0; |
116 | | - var charInLine = 0; |
117 | | - var lastSpaceInLine = 0; |
118 | | - |
119 | | - for(i = 0; i < caret; i++) { |
120 | | - charInLine++; |
121 | | - if(text.charAt(i) == " ") { |
122 | | - lastSpaceInLine = charInLine; |
123 | | - } else if(text.charAt(i) == "\n") { |
124 | | - lastSpaceInLine = 0; |
125 | | - charInLine = 0; |
126 | | - row++; |
127 | | - } |
128 | | - if(charInLine > lineLength) { |
129 | | - if(lastSpaceInLine > 0) { |
130 | | - charInLine = charInLine - lastSpaceInLine; |
131 | | - |
132 | | - lastSpaceInLine = 0; |
133 | | - row++; |
134 | | - } |
135 | | - } |
136 | | - } |
137 | | - var nextSpace = 0; |
138 | | - for(j = caret; j < caret + lineLength; j++) { |
139 | | - if(text.charAt(j) == " " || text.charAt(j) == "\n" || caret == text.length) { |
140 | | - nextSpace = j; |
141 | | - break; |
142 | | - } |
143 | | - } |
144 | | - |
145 | | - if(nextSpace > lineLength && caret <= lineLength) { |
146 | | - charInLine = caret - lastSpaceInLine; |
147 | | - row++; |
148 | | - } |
149 | | - |
150 | | - |
151 | | - return ($.os.name == 'mac' ? 13 : ($.os.name == 'linux' ? 15 : 16))*row; |
152 | | - } |
153 | | - |
154 | | - return this.each(function() { |
155 | | - // Put the cursor at the desired position |
156 | | - this.focus(); |
157 | | - if ( this.selectionStart || this.selectionStart == '0' ) { // Mozilla |
158 | | - this.selectionStart = this.selectionEnd = pos; |
159 | | - } else if ( document.selection && document.selection.createRange ) { // IE/Opera |
160 | | - var range = document.selection.createRange(); |
161 | | - range.moveToElementText( this ); |
162 | | - range.collapse(); |
163 | | - //range.moveStart( 'character', pos ); |
164 | | - range.move( 'character', pos ); |
165 | | - //alert(range.text); |
166 | | - range.select(); |
167 | | - } |
168 | | - $(this).scrollTop( getCaretPosition( this ) ); |
169 | | - $(this).trigger( 'scrollToPosition' ); |
170 | | - }); |
171 | | - } |
172 | | - }); |
173 | | -})(jQuery); |
174 | | - |
175 | 4 | $( document ).ready( function() { |
176 | 5 | if ( $.section == '' ) { |
177 | 6 | // Full page edit |