1
0

jquery.textarea.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Tabby jQuery plugin version 0.12
  3. *
  4. * Ted Devito - http://teddevito.com/demos/textarea.html
  5. *
  6. * Copyright (c) 2009 Ted Devito
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written
  15. * permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. */
  25. // create closure
  26. (function($) {
  27. // plugin definition
  28. $.fn.tabby = function(options) {
  29. //debug(this);
  30. // build main options before element iteration
  31. var opts = $.extend({}, $.fn.tabby.defaults, options);
  32. var pressed = $.fn.tabby.pressed;
  33. // iterate and reformat each matched element
  34. return this.each(function() {
  35. $this = $(this);
  36. // build element specific options
  37. var options = $.meta ? $.extend({}, opts, $this.data()) : opts;
  38. $this.bind('keydown',function (e) {
  39. var kc = $.fn.tabby.catch_kc(e);
  40. if (16 == kc) pressed.shft = true;
  41. /*
  42. because both CTRL+TAB and ALT+TAB default to an event (changing tab/window) that
  43. will prevent js from capturing the keyup event, we'll set a timer on releasing them.
  44. */
  45. if (17 == kc) {pressed.ctrl = true; setTimeout("$.fn.tabby.pressed.ctrl = false;",1000);}
  46. if (18 == kc) {pressed.alt = true; setTimeout("$.fn.tabby.pressed.alt = false;",1000);}
  47. if (9 == kc && !pressed.ctrl && !pressed.alt) {
  48. e.preventDefault; // does not work in O9.63 ??
  49. pressed.last = kc; setTimeout("$.fn.tabby.pressed.last = null;",0);
  50. process_keypress ($(e.target).get(0), pressed.shft, options);
  51. return false;
  52. }
  53. }).bind('keyup',function (e) {
  54. if (16 == $.fn.tabby.catch_kc(e)) pressed.shft = false;
  55. }).bind('blur',function (e) { // workaround for Opera -- http://www.webdeveloper.com/forum/showthread.php?p=806588
  56. if (9 == pressed.last) $(e.target).one('focus',function (e) {pressed.last = null;}).get(0).focus();
  57. });
  58. });
  59. };
  60. // define and expose any extra methods
  61. $.fn.tabby.catch_kc = function(e) { return e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which; };
  62. $.fn.tabby.pressed = {shft : false, ctrl : false, alt : false, last: null};
  63. // private function for debugging
  64. function debug($obj) {
  65. if (window.console && window.console.log)
  66. window.console.log('textarea count: ' + $obj.size());
  67. };
  68. function process_keypress (o,shft,options) {
  69. var scrollTo = o.scrollTop;
  70. //var tabString = String.fromCharCode(9);
  71. // gecko; o.setSelectionRange is only available when the text box has focus
  72. if (o.setSelectionRange) gecko_tab (o, shft, options);
  73. // ie; document.selection is always available
  74. else if (document.selection) ie_tab (o, shft, options);
  75. o.scrollTop = scrollTo;
  76. }
  77. // plugin defaults
  78. $.fn.tabby.defaults = {tabString : String.fromCharCode(9)};
  79. function gecko_tab (o, shft, options) {
  80. var ss = o.selectionStart;
  81. var es = o.selectionEnd;
  82. // when there's no selection and we're just working with the caret, we'll add/remove the tabs at the caret, providing more control
  83. if(ss == es) {
  84. // SHIFT+TAB
  85. if (shft) {
  86. // check to the left of the caret first
  87. if ("\t" == o.value.substring(ss-options.tabString.length, ss)) {
  88. o.value = o.value.substring(0, ss-options.tabString.length) + o.value.substring(ss); // put it back together omitting one character to the left
  89. o.focus();
  90. o.setSelectionRange(ss - options.tabString.length, ss - options.tabString.length);
  91. }
  92. // then check to the right of the caret
  93. else if ("\t" == o.value.substring(ss, ss + options.tabString.length)) {
  94. o.value = o.value.substring(0, ss) + o.value.substring(ss + options.tabString.length); // put it back together omitting one character to the right
  95. o.focus();
  96. o.setSelectionRange(ss,ss);
  97. }
  98. }
  99. // TAB
  100. else {
  101. o.value = o.value.substring(0, ss) + options.tabString + o.value.substring(ss);
  102. o.focus();
  103. o.setSelectionRange(ss + options.tabString.length, ss + options.tabString.length);
  104. }
  105. }
  106. // selections will always add/remove tabs from the start of the line
  107. else {
  108. // split the textarea up into lines and figure out which lines are included in the selection
  109. var lines = o.value.split("\n");
  110. var indices = new Array();
  111. var sl = 0; // start of the line
  112. var el = 0; // end of the line
  113. var sel = false;
  114. for (var i in lines) {
  115. el = sl + lines[i].length;
  116. indices.push({start: sl, end: el, selected: (sl <= ss && el > ss) || (el >= es && sl < es) || (sl > ss && el < es)});
  117. sl = el + 1;// for "\n"
  118. }
  119. // walk through the array of lines (indices) and add tabs where appropriate
  120. var modifier = 0;
  121. for (var i in indices) {
  122. if (indices[i].selected) {
  123. var pos = indices[i].start + modifier; // adjust for tabs already inserted/removed
  124. // SHIFT+TAB
  125. if (shft && options.tabString == o.value.substring(pos,pos+options.tabString.length)) { // only SHIFT+TAB if there's a tab at the start of the line
  126. o.value = o.value.substring(0,pos) + o.value.substring(pos + options.tabString.length); // omit the tabstring to the right
  127. modifier -= options.tabString.length;
  128. }
  129. // TAB
  130. else if (!shft) {
  131. o.value = o.value.substring(0,pos) + options.tabString + o.value.substring(pos); // insert the tabstring
  132. modifier += options.tabString.length;
  133. }
  134. }
  135. }
  136. o.focus();
  137. var ns = ss + ((modifier > 0) ? options.tabString.length : (modifier < 0) ? -options.tabString.length : 0);
  138. var ne = es + modifier;
  139. o.setSelectionRange(ns,ne);
  140. }
  141. }
  142. function ie_tab (o, shft, options) {
  143. var range = document.selection.createRange();
  144. if (o == range.parentElement()) {
  145. // when there's no selection and we're just working with the caret, we'll add/remove the tabs at the caret, providing more control
  146. if ('' == range.text) {
  147. // SHIFT+TAB
  148. if (shft) {
  149. var bookmark = range.getBookmark();
  150. //first try to the left by moving opening up our empty range to the left
  151. range.moveStart('character', -options.tabString.length);
  152. if (options.tabString == range.text) {
  153. range.text = '';
  154. } else {
  155. // if that didn't work then reset the range and try opening it to the right
  156. range.moveToBookmark(bookmark);
  157. range.moveEnd('character', options.tabString.length);
  158. if (options.tabString == range.text)
  159. range.text = '';
  160. }
  161. // move the pointer to the start of them empty range and select it
  162. range.collapse(true);
  163. range.select();
  164. }
  165. else {
  166. // very simple here. just insert the tab into the range and put the pointer at the end
  167. range.text = options.tabString;
  168. range.collapse(false);
  169. range.select();
  170. }
  171. }
  172. // selections will always add/remove tabs from the start of the line
  173. else {
  174. var selection_text = range.text;
  175. var selection_len = selection_text.length;
  176. var selection_arr = selection_text.split("\r\n");
  177. var before_range = document.body.createTextRange();
  178. before_range.moveToElementText(o);
  179. before_range.setEndPoint("EndToStart", range);
  180. var before_text = before_range.text;
  181. var before_arr = before_text.split("\r\n");
  182. var before_len = before_text.length; // - before_arr.length + 1;
  183. var after_range = document.body.createTextRange();
  184. after_range.moveToElementText(o);
  185. after_range.setEndPoint("StartToEnd", range);
  186. var after_text = after_range.text; // we can accurately calculate distance to the end because we're not worried about MSIE trimming a \r\n
  187. var end_range = document.body.createTextRange();
  188. end_range.moveToElementText(o);
  189. end_range.setEndPoint("StartToEnd", before_range);
  190. var end_text = end_range.text; // we can accurately calculate distance to the end because we're not worried about MSIE trimming a \r\n
  191. var check_html = $(o).html();
  192. $("#r3").text(before_len + " + " + selection_len + " + " + after_text.length + " = " + check_html.length);
  193. if((before_len + end_text.length) < check_html.length) {
  194. before_arr.push("");
  195. before_len += 2; // for the \r\n that was trimmed
  196. if (shft && options.tabString == selection_arr[0].substring(0,options.tabString.length))
  197. selection_arr[0] = selection_arr[0].substring(options.tabString.length);
  198. else if (!shft) selection_arr[0] = options.tabString + selection_arr[0];
  199. } else {
  200. if (shft && options.tabString == before_arr[before_arr.length-1].substring(0,options.tabString.length))
  201. before_arr[before_arr.length-1] = before_arr[before_arr.length-1].substring(options.tabString.length);
  202. else if (!shft) before_arr[before_arr.length-1] = options.tabString + before_arr[before_arr.length-1];
  203. }
  204. for (var i = 1; i < selection_arr.length; i++) {
  205. if (shft && options.tabString == selection_arr[i].substring(0,options.tabString.length))
  206. selection_arr[i] = selection_arr[i].substring(options.tabString.length);
  207. else if (!shft) selection_arr[i] = options.tabString + selection_arr[i];
  208. }
  209. if (1 == before_arr.length && 0 == before_len) {
  210. if (shft && options.tabString == selection_arr[0].substring(0,options.tabString.length))
  211. selection_arr[0] = selection_arr[0].substring(options.tabString.length);
  212. else if (!shft) selection_arr[0] = options.tabString + selection_arr[0];
  213. }
  214. if ((before_len + selection_len + after_text.length) < check_html.length) {
  215. selection_arr.push("");
  216. selection_len += 2; // for the \r\n that was trimmed
  217. }
  218. before_range.text = before_arr.join("\r\n");
  219. range.text = selection_arr.join("\r\n");
  220. var new_range = document.body.createTextRange();
  221. new_range.moveToElementText(o);
  222. if (0 < before_len) new_range.setEndPoint("StartToEnd", before_range);
  223. else new_range.setEndPoint("StartToStart", before_range);
  224. new_range.setEndPoint("EndToEnd", range);
  225. new_range.select();
  226. }
  227. }
  228. }
  229. // end of closure
  230. })(jQuery);