From 9d3fcc6282df207a8d310746be77c820b615e9d2 Mon Sep 17 00:00:00 2001 From: Brandon Aaron Date: Fri, 8 Feb 2013 15:22:06 -0600 Subject: [PATCH] maxRows feature is off by default. when maxRows is reached allow the textarea to scroll --- README.markdown | 2 +- jquery.expandable.js | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.markdown b/README.markdown index 3b395ab..4078888 100644 --- a/README.markdown +++ b/README.markdown @@ -12,7 +12,7 @@ The expandable plugin has 5 settings: * `interval` - The interval at which it checks the textarea. Default is 750. * `within` - The number of rows left before expanding. Default is 1. * `by` - The number of rows to expand by. Default is 2. -* `maxRows` - The maximum number of rows the textarea can be expanded to. Default is 20. +* `maxRows` - The maximum number of rows the textarea can be expanded to. Default is false which will allow the textarea to keep expanding. ## License diff --git a/jquery.expandable.js b/jquery.expandable.js index 4b7b50a..022ea11 100644 --- a/jquery.expandable.js +++ b/jquery.expandable.js @@ -16,7 +16,7 @@ $.fn.extend({ interval: 750, within: 1, by: 2, - maxRows: 20, + maxRows: false, init: false }, givenOptions); @@ -29,8 +29,13 @@ $.fn.extend({ // it isn't perfect but is pretty close // white-space rules from: http://petesbloggerama.blogspot.com/2007/02/firefox-ie-word-wrap-word-break-tables.html $mirror = $('
').appendTo('body'), + maxHeight = false, interval; + if ( options.maxRows ) { + maxHeight = options.maxRows * rowSize; + } + // copy styles from textarea to mirror to mirror the textarea as best possible $.each('borderTopWidth borderRightWidth borderBottomWidth borderLeftWidth paddingTop paddingRight paddingBottom paddingLeft fontSize fontFamily fontWeight fontStyle fontStretch fontVariant wordSpacing lineHeight width'.split(' '), function(i,prop) { $mirror.css(prop, $this.css(prop)); @@ -56,13 +61,17 @@ $.fn.extend({ usedRows = Math.floor(usedHeight / rowSize); availableRows = Math.floor((height / rowSize) - usedRows); + if ( usedHeight >= maxHeight ) { + $this.css({ display: 'auto', overflow: 'auto' }); + return; + } + // adjust height if needed by either growing or shrinking the text area to within the specified bounds if ( availableRows <= options.within ) { - var numRows = usedRows + Math.max(availableRows, 0) + options.by; - if( numRows > options.maxRows ) { - numRows = options.maxRows; + newHeight = rowSize * (usedRows + Math.max(availableRows, 0) + options.by); + if ( maxHeight ) { + newHeight = Math.min(newHeight, maxHeight); } - newHeight = rowSize * numRows; $this.stop().animate({ height: newHeight }, options.duration); } else if ( availableRows > options.by + options.within ) { newHeight = Math.max( height - (rowSize * (availableRows - (options.by + options.within))), minHeight );