Skip to content

Commit

Permalink
maxRows feature is off by default. when maxRows is reached allow the …
Browse files Browse the repository at this point in the history
…textarea to scroll
  • Loading branch information
brandonaaron committed Feb 8, 2013
1 parent 8d835d1 commit 9d3fcc6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions jquery.expandable.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $.fn.extend({
interval: 750,
within: 1,
by: 2,
maxRows: 20,
maxRows: false,
init: false
}, givenOptions);

Expand All @@ -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 = $('<div style="position:absolute;top:-999px;left:-999px;border-color:#000;border-style:solid;overflow-x:hidden;visibility:hidden;z-index:0;white-space: pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;" />').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));
Expand All @@ -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 );
Expand Down

0 comments on commit 9d3fcc6

Please sign in to comment.