Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for callback after resize #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ Basic usage ([Interactive Demo](http://jsfiddle.net/edelman/HrnHb/)):
- `fixMinHeight`: defaults to `true`. Set to `false` if you don't want the box to stop shrinking when it hits its initial size.
- `cloneClass`: defaults to `'autogrowclone'`. Helper CSS class for the clone used for measuring sizes. Use this if you need to apply special rules for a textbox that is right next to the one you're autogrowing, but not exactly it so that they are identical.
- `onInitialize`: defaults to `false`. Will trigger autogrow on init.
- `onResize`: defaults to `false`. Callback function, will be executed after the resize, will be passed the `event` object and the textarea object as parameters. On init, the event is undefined. If animation is enabled, it is executed after the animation completes.

Example:

var opts = {
context: $('li')
, animate: false
, cloneClass: 'faketextarea'
, onResize: function(event, textarea) {
'your stuff...';
}
};
$('.mytextareas').autogrow(opts);
14 changes: 12 additions & 2 deletions autogrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
, fixMinHeight: true //if you don't want the box to shrink below its initial size
, cloneClass: 'autogrowclone' //helper CSS class for clone if you need to add special rules
, onInitialize: false //resizes the textareas when the plugin is initialized
, onResize: false //function to call after the textarea resizes
}
;
opts = $.isPlainObject(opts) ? opts : {context: opts ? opts : $(document)};
Expand Down Expand Up @@ -54,10 +55,17 @@
, newHeight = this.scrollHeight
, minHeight = box.data('autogrow-start-height') || 0
, clone
, self = this
, executeCallback = function() {
if (typeof opts.onResize === 'function') {
opts.onResize.call(this, e, self);
}
return true;
}
;
if (oldHeight < newHeight) { //user is typing
this.scrollTop = 0; //try to reduce the top of the content hiding for a second
opts.animate ? box.stop().animate({height: newHeight}, opts.speed) : box.innerHeight(newHeight);
opts.animate ? box.stop().animate({height: newHeight}, opts.speed, 'swing', executeCallback) : box.innerHeight(newHeight) && executeCallback();
} else if (!e || e.which == 8 || e.which == 46 || (e.ctrlKey && e.which == 88)) { //user is deleting, backspacing, or cutting
if (oldHeight > minHeight) { //shrink!
//this cloning part is not particularly necessary. however, it helps with animation
Expand Down Expand Up @@ -85,7 +93,9 @@
//if user selects all and deletes or holds down delete til beginning
//user could get here and shrink whole box
newHeight < minHeight && (newHeight = minHeight);
oldHeight > newHeight && opts.animate ? box.stop().animate({height: newHeight}, opts.speed) : box.innerHeight(newHeight);
oldHeight > newHeight && opts.animate ?
box.stop().animate({height: newHeight}, opts.speed, 'swing', executeCallback) :
box.innerHeight(newHeight) && executeCallback();
} else { //just set to the minHeight
box.innerHeight(minHeight);
}
Expand Down
2 changes: 1 addition & 1 deletion autogrow.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.