linedtextarea.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * jQuery Lined Textarea Plugin
  3. * http://alan.blog-city.com/jquerylinedtextarea.htm
  4. *
  5. * Copyright (c) 2010 Alan Williamson
  6. *
  7. * Version:
  8. * $Id: jquery-linedtextarea.js 464 2010-01-08 10:36:33Z alan $
  9. *
  10. * Released under the MIT License:
  11. * http://www.opensource.org/licenses/mit-license.php
  12. *
  13. * Usage:
  14. * Displays a line number count column to the left of the textarea
  15. *
  16. * Class up your textarea with a given class, or target it directly
  17. * with JQuery Selectors
  18. *
  19. * $(".lined").linedtextarea({
  20. * selectedLine: 10,
  21. * selectedClass: 'lineselect'
  22. * });
  23. *
  24. * History:
  25. * - 2010.01.08: Fixed a Google Chrome layout problem
  26. * - 2010.01.07: Refactored code for speed/readability; Fixed horizontal sizing
  27. * - 2010.01.06: Initial Release
  28. *
  29. */
  30. (function($) {
  31. $.fn.linedtextarea = function(options) {
  32. // Get the Options
  33. var opts = $.extend({}, $.fn.linedtextarea.defaults, options);
  34. /*
  35. * Helper function to make sure the line numbers are always
  36. * kept up to the current system
  37. */
  38. var fillOutLines = function(codeLines, h, lineNo){
  39. while ( (codeLines.height() - h ) <= 0 ){
  40. if ( lineNo == opts.selectedLine )
  41. codeLines.append("<div class='lineno lineselect'>" + lineNo + "</div>");
  42. else
  43. codeLines.append("<div class='lineno'>" + lineNo + "</div>");
  44. lineNo++;
  45. }
  46. return lineNo;
  47. };
  48. /*
  49. * Iterate through each of the elements are to be applied to
  50. */
  51. return this.each(function() {
  52. var lineNo = 1;
  53. var textarea = $(this);
  54. /* Turn off the wrapping of as we don't want to screw up the line numbers */
  55. textarea.attr("wrap", "off");
  56. textarea.css({resize:'none'});
  57. var originalTextAreaWidth = textarea.outerWidth();
  58. /* Wrap the text area in the elements we need */
  59. textarea.wrap("<div class='linedtextarea'></div>");
  60. var linedTextAreaDiv = textarea.parent().wrap("<div class='linedwrap' style='width:" + originalTextAreaWidth + "px'></div>");
  61. var linedWrapDiv = linedTextAreaDiv.parent();
  62. linedWrapDiv.prepend("<div class='lines' style='width:50px'></div>");
  63. var linesDiv = linedWrapDiv.find(".lines");
  64. linesDiv.height( textarea.height() + 6 );
  65. /* Draw the number bar; filling it out where necessary */
  66. linesDiv.append( "<div class='codelines'></div>" );
  67. var codeLinesDiv = linesDiv.find(".codelines");
  68. lineNo = fillOutLines( codeLinesDiv, linesDiv.height(), 1 );
  69. /* Move the textarea to the selected line */
  70. if ( opts.selectedLine != -1 && !isNaN(opts.selectedLine) ){
  71. var fontSize = parseInt( textarea.height() / (lineNo-2) );
  72. var position = parseInt( fontSize * opts.selectedLine ) - (textarea.height()/2);
  73. textarea[0].scrollTop = position;
  74. }
  75. /* Set the width */
  76. var sidebarWidth = linesDiv.outerWidth();
  77. var paddingHorizontal = parseInt( linedWrapDiv.css("border-left-width") ) + parseInt( linedWrapDiv.css("border-right-width") ) + parseInt( linedWrapDiv.css("padding-left") ) + parseInt( linedWrapDiv.css("padding-right") );
  78. var linedWrapDivNewWidth = originalTextAreaWidth - paddingHorizontal;
  79. var textareaNewWidth = originalTextAreaWidth - sidebarWidth - paddingHorizontal - 20;
  80. textarea.width( textareaNewWidth );
  81. linedWrapDiv.width( linedWrapDivNewWidth );
  82. /* React to the scroll event */
  83. textarea.scroll( function(tn){
  84. var domTextArea = $(this)[0];
  85. var scrollTop = domTextArea.scrollTop;
  86. var clientHeight = domTextArea.clientHeight;
  87. codeLinesDiv.css( {'margin-top': (-1*scrollTop) + "px"} );
  88. lineNo = fillOutLines( codeLinesDiv, scrollTop + clientHeight, lineNo );
  89. });
  90. /* Should the textarea get resized outside of our control */
  91. textarea.resize( function(tn){
  92. var domTextArea = $(this)[0];
  93. linesDiv.height( domTextArea.clientHeight + 6 );
  94. });
  95. });
  96. };
  97. // default options
  98. $.fn.linedtextarea.defaults = {
  99. selectedLine: -1,
  100. selectedClass: 'lineselect'
  101. };
  102. })(jQuery);