From bd86a1962baaa34f36646a649686e78460522254 Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Sun, 20 Oct 2013 17:25:10 +0100 Subject: [PATCH 1/2] Trigger autogrow:grow and autogrow:shrink events when an action completes. --- README.md | 12 ++++++++++++ autogrow.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d52c9e2..b459036 100644 --- a/README.md +++ b/README.md @@ -26,3 +26,15 @@ Example: , cloneClass: 'faketextarea' }; $('.mytextareas').autogrow(opts); + +### Events + +`autogrow:grow` and `autogrow:shrink` events will fire when a grow or shrink animation completes (or immediately if there are no animations). You can get the textarea this applies to by looking at the `event.target` + +The event will be raised against the `context` you pass into the options. + +Example + $('#context-element').on('autogrow:grow', function(e) { + var box = $(e.target); + console.log(box.height()); + }); \ No newline at end of file diff --git a/autogrow.js b/autogrow.js index 6e3ce01..81b8850 100644 --- a/autogrow.js +++ b/autogrow.js @@ -57,7 +57,13 @@ ; 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); + if(opts.animate) { + box.stop().animate({height: newHeight}, {duration: opts.speed, complete: notifyGrown}); + } else { + box.innerHeight(newHeight); + notifyGrown(); + } + } 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 @@ -80,11 +86,29 @@ //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); + if(oldHeight > newHeight) { + if(opts.animate) { + box.stop().animate({height: newHeight}, {duration: opts.speed, complete: notifyShrunk}); + } else { + box.innerHeight(newHeight); + notifyShrunk(); + } + } + } else { //just set to the minHeight box.innerHeight(minHeight); } } } + + // Trigger event to indicate a textarea has grown. + function notifyGrown() { + opts.context.trigger('autogrow:grow'); + } + + // Trigger event to indicate a textarea has shrunk. + function notifyShrunk() { + opts.context.trigger('autogrow:shrink'); + } } })(jQuery); From 7f568d911ff7538f7414b914f28fae282d4b585f Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Sun, 20 Oct 2013 17:27:42 +0100 Subject: [PATCH 2/2] Syntax highlighting in readme. --- README.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b459036..d1cdbd5 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,9 @@ autogrow.js Basic usage ([Interactive Demo](http://jsfiddle.net/edelman/HrnHb/)): - $('textarea').autogrow(); //or some selector that will grab textareas +```javascript +$('textarea').autogrow(); //or some selector that will grab textareas +``` **autogrow.js** has some options that you can set: @@ -20,12 +22,14 @@ Basic usage ([Interactive Demo](http://jsfiddle.net/edelman/HrnHb/)): Example: - var opts = { - context: $('li') - , animate: false - , cloneClass: 'faketextarea' - }; - $('.mytextareas').autogrow(opts); +```javascript +var opts = { + context: $('li') + , animate: false + , cloneClass: 'faketextarea' +}; +$('.mytextareas').autogrow(opts); +``` ### Events @@ -33,8 +37,11 @@ Example: The event will be raised against the `context` you pass into the options. -Example - $('#context-element').on('autogrow:grow', function(e) { - var box = $(e.target); - console.log(box.height()); - }); \ No newline at end of file +Example: + +```javascript +$('#context-element').on('autogrow:grow', function(e) { + var box = $(e.target); + console.log(box.height()); +}); +``` \ No newline at end of file