From b9ae53f86b73ec13f7003f006679d5924ab1af22 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Fri, 8 Sep 2017 14:51:31 +0800 Subject: [PATCH] MDL-59758 js: auto_rows module easier to use Allow passing the direct text area, or a container. Trigger change events on the text area, not the container. Properly handle starting with rows > 1 --- lib/amd/build/auto_rows.min.js | 2 +- lib/amd/src/auto_rows.js | 43 +++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/amd/build/auto_rows.min.js b/lib/amd/build/auto_rows.min.js index 30ec60f6a8251..c95209bc9f0be 100644 --- a/lib/amd/build/auto_rows.min.js +++ b/lib/amd/build/auto_rows.min.js @@ -1 +1 @@ -define(["jquery"],function(a){var b={ELEMENT:"[data-auto-rows]"},c={ROW_CHANGE:"autorows:rowchange"},d=function(a){var b=a.attr("rows"),c=a.attr("data-max-rows"),d=a.height(),e=a.innerHeight(),f=e-d;a.height("1px");var g=a[0].scrollHeight,h=(g-f)/(d/b);return a.css("height",""),c&&h>=c?c:h},e=function(e){a(e).on("input propertychange",b.ELEMENT,function(b){var f=a(b.target),g=f.attr("rows"),h=d(f);h!=g&&(f.attr("rows",h),a(e).trigger(c.ROW_CHANGE))})};return{init:e,events:c}}); \ No newline at end of file +define(["jquery"],function(a){var b={ELEMENT:"[data-auto-rows]"},c={ROW_CHANGE:"autorows:rowchange"},d=function(a){var b=a.attr("rows"),c=a.data("min-rows"),d=a.attr("data-max-rows"),e=a.height(),f=a.innerHeight(),g=f-e;a.height("1px");var h=a[0].scrollHeight,i=(h-g)/(e/b);return a.css("height",""),i=d?d:i},e=function(b,e){var f=a(e.target),g=f.data("min-rows"),h=f.attr("rows");"undefined"==typeof g&&f.data("min-rows",h);var i=d(f);i!=h&&(f.attr("rows",i),f.trigger(c.ROW_CHANGE))},f=function(c){a(c).data("auto-rows")?a(c).on("input propertychange",e.bind(this,c)):a(c).on("input propertychange",b.ELEMENT,e.bind(this,c))};return{init:f,events:c}}); \ No newline at end of file diff --git a/lib/amd/src/auto_rows.js b/lib/amd/src/auto_rows.js index 1532ad2d7915a..2c04a47fed9b4 100644 --- a/lib/amd/src/auto_rows.js +++ b/lib/amd/src/auto_rows.js @@ -42,6 +42,7 @@ define(['jquery'], function($) { */ var calculateRows = function(element) { var currentRows = element.attr('rows'); + var minRows = element.data('min-rows'); var maxRows = element.attr('data-max-rows'); var height = element.height(); @@ -58,13 +59,38 @@ define(['jquery'], function($) { // based on the row attribute. element.css('height', ''); - if (maxRows && rows >= maxRows) { + if (rows < minRows) { + return minRows; + } else if (maxRows && rows >= maxRows) { return maxRows; } else { return rows; } }; + /** + * Listener for change events to trigger resizing of the element. + * + * @method changeListener + * @param {Event} e The triggered event. + * @private + */ + var changeListener = function(root, e) { + var element = $(e.target); + var minRows = element.data('min-rows'); + var currentRows = element.attr('rows'); + + if (typeof minRows === "undefined") { + element.data('min-rows', currentRows); + } + var rows = calculateRows(element); + + if (rows != currentRows) { + element.attr('rows', rows); + element.trigger(EVENTS.ROW_CHANGE); + } + }; + /** * Add the event listeners for all text areas within the given element. * @@ -73,16 +99,11 @@ define(['jquery'], function($) { * @public */ var init = function(root) { - $(root).on('input propertychange', SELECTORS.ELEMENT, function(e) { - var element = $(e.target); - var currentRows = element.attr('rows'); - var rows = calculateRows(element); - - if (rows != currentRows) { - element.attr('rows', rows); - $(root).trigger(EVENTS.ROW_CHANGE); - } - }); + if ($(root).data('auto-rows')) { + $(root).on('input propertychange', changeListener.bind(this, root)); + } else { + $(root).on('input propertychange', SELECTORS.ELEMENT, changeListener.bind(this, root)); + } }; return /** @module core/auto_rows */ {