Skip to content

Commit

Permalink
v3.0.0 - renamed events and added new methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmoore committed Apr 15, 2015
1 parent 37b3440 commit cea1350
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 186 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "autosize",
"description": "Autosize is a small, stand-alone script to automatically adjust textarea height to fit text.",
"version": "2.0.1",
"version": "3.0.0",
"dependencies": {},
"keywords": [
"textarea",
Expand Down
163 changes: 97 additions & 66 deletions dest/autosize.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
/*!
Autosize 2.0.1
Autosize 3.0.0
license: MIT
http://www.jacklmoore.com/autosize
*/
(function (root, factory) {
'use strict';

(function (global, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
define(['exports', 'module'], factory);
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
factory(exports, module);
} else {
// Browser globals (root is window)
root.autosize = factory();
}
}(this, function () {
function main(ta) {
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || ta.hasAttribute('data-autosize-on')) { return; }

var maxHeight;
var mod = {
exports: {}
};
factory(mod.exports, mod);
global.autosize = mod.exports;
}
})(this, function (exports, module) {
'use strict';

function assign(ta) {
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || ta.hasAttribute('data-autosize-on')) {
return;
}var maxHeight;
var heightOffset;

function init() {
Expand All @@ -34,11 +33,8 @@
ta.style.resize = 'horizontal';
}

// horizontal overflow is hidden, so break-word is necessary for handling words longer than the textarea width
ta.style.wordWrap = 'break-word';

// Chrome/Safari-specific fix:
// When the textarea y-overflow is hidden, Chrome/Safari doesn't reflow the text to account for the space
// When the textarea y-over is hidden, Chrome/Safari doesn't reflow the text to account for the space
// made available by removing the scrollbar. This workaround will cause the text to reflow.
var width = ta.style.width;
ta.style.width = '0px';
Expand All @@ -49,24 +45,24 @@
ta.style.width = width;

maxHeight = style.maxHeight !== 'none' ? parseFloat(style.maxHeight) : false;

if (style.boxSizing === 'content-box') {
heightOffset = -(parseFloat(style.paddingTop)+parseFloat(style.paddingBottom));
heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom));
} else {
heightOffset = parseFloat(style.borderTopWidth)+parseFloat(style.borderBottomWidth);
heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
}

adjust();
update();
}

function adjust() {
function update() {
var startHeight = ta.style.height;
var htmlTop = document.documentElement.scrollTop;
var bodyTop = document.body.scrollTop;

ta.style.height = 'auto';

var endHeight = ta.scrollHeight+heightOffset;
var endHeight = ta.scrollHeight + heightOffset;

if (maxHeight !== false && maxHeight < endHeight) {
endHeight = maxHeight;
Expand All @@ -77,70 +73,105 @@
ta.style.overflowY = 'hidden';
}

ta.style.height = endHeight+'px';
ta.style.height = endHeight + 'px';

// prevents scroll-position jumping
document.documentElement.scrollTop = htmlTop;
document.body.scrollTop = bodyTop;

if (startHeight !== ta.style.height) {
var evt = document.createEvent('Event');
evt.initEvent('autosize.resized', true, false);
evt.initEvent('autosize:resized', true, false);
ta.dispatchEvent(evt);
}
}

// IE9 does not fire onpropertychange or oninput for deletions,
// so binding to onkeyup to catch most of those events.
// There is no way that I know of to detect something like 'cut' in IE9.
if ('onpropertychange' in ta && 'oninput' in ta) {
ta.addEventListener('keyup', adjust);
}

window.addEventListener('resize', adjust);
ta.addEventListener('input', adjust);

ta.addEventListener('autosize.update', adjust);

ta.addEventListener('autosize.destroy', function(style){
window.removeEventListener('resize', adjust);
ta.removeEventListener('input', adjust);
ta.removeEventListener('keyup', adjust);
ta.removeEventListener('autosize.destroy');
ta.addEventListener('autosize:destroy', (function (style) {
window.removeEventListener('resize', update);
ta.removeEventListener('input', update);
ta.removeEventListener('keyup', update);
ta.removeAttribute('data-autosize-on');
ta.removeEventListener('autosize:destroy');

Object.keys(style).forEach(function(key){
Object.keys(style).forEach(function (key) {
ta.style[key] = style[key];
});

ta.removeAttribute('data-autosize-on');
}.bind(ta, {
}).bind(ta, {
height: ta.style.height,
overflow: ta.style.overflow,
overflowY: ta.style.overflowY,
wordWrap: ta.style.wordWrap,
resize: ta.style.resize
}));

// IE9 does not fire onpropertychange or oninput for deletions,
// so binding to onkeyup to catch most of those events.
// There is no way that I know of to detect something like 'cut' in IE9.
if ('onpropertychange' in ta && 'oninput' in ta) {
ta.addEventListener('keyup', update);
}

window.addEventListener('resize', update);
ta.addEventListener('input', update);
ta.addEventListener('autosize:update', update);
ta.setAttribute('data-autosize-on', true);
ta.style.overflow = 'hidden';
ta.style.overflowY = 'hidden';
init();
}

function destroy(ta) {
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA') {
return;
}var evt = document.createEvent('Event');
evt.initEvent('autosize:destroy', true, false);
ta.dispatchEvent(evt);
}

init();
function update(ta) {
if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) {
return;
}var evt = document.createEvent('Event');
evt.initEvent('autosize:update', true, false);
ta.dispatchEvent(evt);
}

var autosize;

// Do nothing in IE8 or lower
if (typeof window.getComputedStyle !== 'function') {
return function(elements) {
return elements;
autosize = function (el) {
return el;
};
autosize.destroy = function (el) {
return el;
};
autosize.update = function (el) {
return el;
};
} else {
return function(elements) {
if (elements && elements.length) {
Array.prototype.forEach.call(elements, main);
} else if (elements && elements.nodeName) {
main(elements);
autosize = function (el) {
if (el && el.length) {
Array.prototype.forEach.call(el, assign);
} else if (el && el.nodeName) {
assign(el);
}
return elements;
return el;
};
autosize.destroy = function (el) {
if (el && el.length) {
Array.prototype.forEach.call(el, destroy);
} else if (el && el.nodeName) {
destroy(el);
}
return el;
};
autosize.update = function (el) {
if (el && el.length) {
Array.prototype.forEach.call(el, update);
} else if (el && el.nodeName) {
update(el);
}
return el;
};
}
}));

module.exports = autosize;
});
4 changes: 2 additions & 2 deletions dest/autosize.min.js

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "autosize",
"description": "Autosize is a small, stand-alone script to automatically adjust textarea height to fit text.",
"version": "2.0.1",
"version": "3.0.0",
"keywords": [
"textarea",
"form",
Expand All @@ -24,8 +24,7 @@
"devDependencies": {
"babel": "^5.1.9",
"jshint": "^2.5.6",
"uglify-js": "^2.4.15",
"watch": "^0.14.0"
"uglify-js": "^2.4.15"
},
"config": {
"bower": {
Expand Down
Loading

0 comments on commit cea1350

Please sign in to comment.