PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/webclient/static/tabby.js

https://code.google.com/p/quickdiagrams/
JavaScript | 268 lines | 181 code | 43 blank | 44 comment | 89 complexity | 32ae5c05b7609e6a72923dab2dc47d79 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /*
  2. * Tabby jQuery plugin version 0.12
  3. *
  4. * Ted Devito - http://teddevito.com/demos/textarea.html
  5. *
  6. * You should have received a copy of the GNU General Public License
  7. * along with Easy Widgets. If not, see <http://www.gnu.org/licenses/>
  8. *
  9. * Plugin development pattern based on: http://www.learningjquery.com/2007/10/a-plugin-development-pattern
  10. *
  11. */
  12. // create closure
  13. (function($) {
  14. // plugin definition
  15. $.fn.tabby = function(options) {
  16. debug(this);
  17. // build main options before element iteration
  18. var opts = $.extend({}, $.fn.tabby.defaults, options);
  19. var pressed = $.fn.tabby.pressed;
  20. // iterate and reformat each matched element
  21. return this.each(function() {
  22. $this = $(this);
  23. // build element specific options
  24. var options = $.meta ? $.extend({}, opts, $this.data()) : opts;
  25. $this.bind('keydown',function (e) {
  26. var kc = $.fn.tabby.catch_kc(e);
  27. if (16 == kc) pressed.shft = true;
  28. /*
  29. because both CTRL+TAB and ALT+TAB default to an event (changing tab/window) that
  30. will prevent js from capturing the keyup event, we'll set a timer on releasing them.
  31. */
  32. if (17 == kc) {pressed.ctrl = true; setTimeout("$.fn.tabby.pressed.ctrl = false;",1000);}
  33. if (18 == kc) {pressed.alt = true; setTimeout("$.fn.tabby.pressed.alt = false;",1000);}
  34. if (9 == kc && !pressed.ctrl && !pressed.alt)
  35. {
  36. e.preventDefault; // does not work in O9.63 ??
  37. pressed.last = kc; setTimeout("$.fn.tabby.pressed.last = null;",0);
  38. process_keypress ($(e.target).get(0), pressed.shft, options);
  39. return false;
  40. }
  41. if (190 == kc && !pressed.shft && !pressed.ctrl && !pressed.alt)
  42. {
  43. e.preventDefault; // does not work in O9.63 ??
  44. pressed.last = kc; setTimeout("$.fn.tabby.pressed.last = null;",0);
  45. process_keypress ($(e.target).get(0), pressed.shft, options);
  46. return false;
  47. }
  48. }).bind('keyup',function (e) {
  49. if (16 == $.fn.tabby.catch_kc(e)) pressed.shft = false;
  50. }).bind('blur',function (e) { // workaround for Opera -- http://www.webdeveloper.com/forum/showthread.php?p=806588
  51. if (9 == pressed.last) $(e.target).one('focus',function (e) {pressed.last = null;}).get(0).focus();
  52. });
  53. });
  54. };
  55. // define and expose any extra methods
  56. $.fn.tabby.catch_kc = function(e) { return e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which; };
  57. $.fn.tabby.pressed = {shft : false, ctrl : false, alt : false, last: null};
  58. // private function for debugging
  59. function debug($obj) {
  60. if (window.console && window.console.log)
  61. window.console.log('textarea count: ' + $obj.size());
  62. };
  63. function process_keypress (o,shft,options) {
  64. var scrollTo = o.scrollTop;
  65. //var tabString = String.fromCharCode(9);
  66. // gecko; o.setSelectionRange is only available when the text box has focus
  67. if (o.setSelectionRange) gecko_tab (o, shft, options);
  68. // ie; document.selection is always available
  69. else if (document.selection) ie_tab (o, shft, options);
  70. o.scrollTop = scrollTo;
  71. }
  72. // plugin defaults
  73. $.fn.tabby.defaults = {tabString: String.fromCharCode(9)};
  74. function gecko_tab (o, shft, options) {
  75. var ss = o.selectionStart;
  76. var es = o.selectionEnd;
  77. // 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
  78. if(ss == es) {
  79. // SHIFT+TAB
  80. if (shft) {
  81. // check to the left of the caret first
  82. if ("\t" == o.value.substring(ss-options.tabString.length, ss)) {
  83. o.value = o.value.substring(0, ss-options.tabString.length) + o.value.substring(ss); // put it back together omitting one character to the left
  84. o.focus();
  85. o.setSelectionRange(ss - options.tabString.length, ss - options.tabString.length);
  86. }
  87. // then check to the right of the caret
  88. else if ("\t" == o.value.substring(ss, ss + options.tabString.length)) {
  89. o.value = o.value.substring(0, ss) + o.value.substring(ss + options.tabString.length); // put it back together omitting one character to the right
  90. o.focus();
  91. o.setSelectionRange(ss,ss);
  92. }
  93. }
  94. // TAB
  95. else {
  96. o.value = o.value.substring(0, ss) + options.tabString + o.value.substring(ss);
  97. o.focus();
  98. o.setSelectionRange(ss + options.tabString.length, ss + options.tabString.length);
  99. }
  100. }
  101. // selections will always add/remove tabs from the start of the line
  102. else {
  103. // split the textarea up into lines and figure out which lines are included in the selection
  104. var lines = o.value.split("\n");
  105. var indices = new Array();
  106. var sl = 0; // start of the line
  107. var el = 0; // end of the line
  108. var sel = false;
  109. for (var i in lines) {
  110. el = sl + lines[i].length;
  111. indices.push({start: sl, end: el, selected: (sl <= ss && el > ss) || (el >= es && sl < es) || (sl > ss && el < es)});
  112. sl = el + 1;// for "\n"
  113. }
  114. // walk through the array of lines (indices) and add tabs where appropriate
  115. var modifier = 0;
  116. for (var i in indices) {
  117. if (indices[i].selected) {
  118. var pos = indices[i].start + modifier; // adjust for tabs already inserted/removed
  119. // SHIFT+TAB
  120. 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
  121. o.value = o.value.substring(0,pos) + o.value.substring(pos + options.tabString.length); // omit the tabstring to the right
  122. modifier -= options.tabString.length;
  123. }
  124. // TAB
  125. else if (!shft) {
  126. o.value = o.value.substring(0,pos) + options.tabString + o.value.substring(pos); // insert the tabstring
  127. modifier += options.tabString.length;
  128. }
  129. }
  130. }
  131. o.focus();
  132. var ns = ss + ((modifier > 0) ? options.tabString.length : (modifier < 0) ? -options.tabString.length : 0);
  133. var ne = es + modifier;
  134. o.setSelectionRange(ns,ne);
  135. }
  136. }
  137. function ie_tab (o, shft, options) {
  138. var range = document.selection.createRange();
  139. if (o == range.parentElement()) {
  140. // 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
  141. if ('' == range.text) {
  142. // SHIFT+TAB
  143. if (shft) {
  144. var bookmark = range.getBookmark();
  145. //first try to the left by moving opening up our empty range to the left
  146. range.moveStart('character', -options.tabString.length);
  147. if (options.tabString == range.text) {
  148. range.text = '';
  149. } else {
  150. // if that didn't work then reset the range and try opening it to the right
  151. range.moveToBookmark(bookmark);
  152. range.moveEnd('character', options.tabString.length);
  153. if (options.tabString == range.text)
  154. range.text = '';
  155. }
  156. // move the pointer to the start of them empty range and select it
  157. range.collapse(true);
  158. range.select();
  159. }
  160. else {
  161. // very simple here. just insert the tab into the range and put the pointer at the end
  162. range.text = options.tabString;
  163. range.collapse(false);
  164. range.select();
  165. }
  166. }
  167. // selections will always add/remove tabs from the start of the line
  168. else {
  169. var selection_text = range.text;
  170. var selection_len = selection_text.length;
  171. var selection_arr = selection_text.split("\r\n");
  172. var before_range = document.body.createTextRange();
  173. before_range.moveToElementText(o);
  174. before_range.setEndPoint("EndToStart", range);
  175. var before_text = before_range.text;
  176. var before_arr = before_text.split("\r\n");
  177. var before_len = before_text.length; // - before_arr.length + 1;
  178. var after_range = document.body.createTextRange();
  179. after_range.moveToElementText(o);
  180. after_range.setEndPoint("StartToEnd", range);
  181. 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
  182. var end_range = document.body.createTextRange();
  183. end_range.moveToElementText(o);
  184. end_range.setEndPoint("StartToEnd", before_range);
  185. 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
  186. var check_html = $(o).html();
  187. $("#r3").text(before_len + " + " + selection_len + " + " + after_text.length + " = " + check_html.length);
  188. if((before_len + end_text.length) < check_html.length) {
  189. before_arr.push("");
  190. before_len += 2; // for the \r\n that was trimmed
  191. if (shft && options.tabString == selection_arr[0].substring(0,options.tabString.length))
  192. selection_arr[0] = selection_arr[0].substring(options.tabString.length);
  193. else if (!shft) selection_arr[0] = options.tabString + selection_arr[0];
  194. } else {
  195. if (shft && options.tabString == before_arr[before_arr.length-1].substring(0,options.tabString.length))
  196. before_arr[before_arr.length-1] = before_arr[before_arr.length-1].substring(options.tabString.length);
  197. else if (!shft) before_arr[before_arr.length-1] = options.tabString + before_arr[before_arr.length-1];
  198. }
  199. for (var i = 1; i < selection_arr.length; i++) {
  200. if (shft && options.tabString == selection_arr[i].substring(0,options.tabString.length))
  201. selection_arr[i] = selection_arr[i].substring(options.tabString.length);
  202. else if (!shft) selection_arr[i] = options.tabString + selection_arr[i];
  203. }
  204. if (1 == before_arr.length && 0 == before_len) {
  205. if (shft && options.tabString == selection_arr[0].substring(0,options.tabString.length))
  206. selection_arr[0] = selection_arr[0].substring(options.tabString.length);
  207. else if (!shft) selection_arr[0] = options.tabString + selection_arr[0];
  208. }
  209. if ((before_len + selection_len + after_text.length) < check_html.length) {
  210. selection_arr.push("");
  211. selection_len += 2; // for the \r\n that was trimmed
  212. }
  213. before_range.text = before_arr.join("\r\n");
  214. range.text = selection_arr.join("\r\n");
  215. var new_range = document.body.createTextRange();
  216. new_range.moveToElementText(o);
  217. if (0 < before_len) new_range.setEndPoint("StartToEnd", before_range);
  218. else new_range.setEndPoint("StartToStart", before_range);
  219. new_range.setEndPoint("EndToEnd", range);
  220. new_range.select();
  221. }
  222. }
  223. }
  224. // end of closure
  225. })(jQuery);