Skip to content

Commit

Permalink
smaller, simplier code-base
Browse files Browse the repository at this point in the history
new API.  Example usage:

	autosize(document.querySelectorAll(textarea));

dropped jQuery dependency
dropped IE7-IE8 support
dropped optional parameters
closes jackmoore#98, closes jackmoore#106, closes jackmoore#123, fixes jackmoore#129, fixes jackmoore#132, fixes jackmoore#139, closes jackmoore#140, closes jackmoore#166, closes jackmoore#168, closes jackmoore#192, closes jackmoore#193, closes jackmoore#197
  • Loading branch information
jackmoore committed Feb 26, 2015
1 parent 64e9f33 commit 93ae38d
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 533 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/*
test/*
publish.js
30 changes: 13 additions & 17 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
{
"name": "jquery-autosize",
"description": "Automatically adjust textarea height based on user input.",
"version": "1.18.18",
"dependencies": {
"jquery": ">=1.7"
},
"name": "autosize",
"description": "Autosize is a small, stand-alone script to automatically adjust textarea height to fit text.",
"version": "2.0.0",
"dependencies": {},
"keywords": [
"form",
"textarea",
"ui",
"jQuery",
"jquery-plugin"
"form",
"ui"
],
"authors": [
{
Expand All @@ -19,12 +15,12 @@
"email": "[email protected]"
}
],
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
],
"license": "MIT",
"homepage": "http://www.jacklmoore.com/autosize",
"main": "jquery.autosize.js"
"ignore": [],
"repository": {
"type": "git",
"url": "http://github.com/jackmoore/autosize.git"
},
"main": "dest/autosize.js"
}
68 changes: 68 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var pkg = require('./package.json');
var fs = require('fs');
var ugly = require("uglify-js");
var jshint = require("jshint").JSHINT;

function writeBower() {
var bower = {
name: pkg.config.bower.name,
description: pkg.description,
version: pkg.version,
dependencies: pkg.dependencies,
keywords: pkg.keywords,
authors: [pkg.author],
license: pkg.license,
homepage: pkg.homepage,
ignore: pkg.config.bower.ignore,
repository: pkg.repository,
main: pkg.config.bower.main,
};
fs.writeFile('bower.json', JSON.stringify(bower, null, '\t'));
return true;
}

function build(full) {
var mini = ugly.minify(full, {fromString: true}).code;
var header = [
"/*!",
" "+pkg.config.title+" "+pkg.version,
" license: MIT",
" "+pkg.homepage,
"*/",
""
].join("\n");

fs.writeFile('dest/'+pkg.config.fileName+'.js', header+full);
fs.writeFile('dest/'+pkg.config.fileName+'.min.js', header+mini);

return true;
}

function lint(full) {
jshint(full.toString(), {
browser: true,
undef: true,
unused: true,
immed: true,
eqeqeq: true,
eqnull: true,
noarg: true,
predef: ['define', 'module']
});

if (jshint.errors.length) {
jshint.errors.forEach(function (err) {
console.log(err.line+':'+err.character+' '+err.reason);
});
} else {
return true;
}
}

fs.readFile('src/'+pkg.config.fileName+'.js', 'utf8', function (err,data) {
if (err) {
return console.log(err);
} else {
lint(data) && build(data) && writeBower();
}
});
13 changes: 13 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Changelog

##### v.2.0.0 - 2015-02-25

* smaller, simplier code-base
* new API. Example usage:

autosize(document.querySelectorAll(textarea));

* dropped jQuery dependency
* dropped IE7-IE8 support
* dropped optional parameters
* closes #98, closes #106, closes #123, fixes #129, fixes #132, fixes #139, closes #140, closes #166, closes #168, closes #192, closes #193, closes #197
36 changes: 0 additions & 36 deletions demo.html

This file was deleted.

146 changes: 146 additions & 0 deletions dest/autosize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*!
Autosize 2.0.0
license: MIT
http://www.jacklmoore.com/autosize
*/
(function (root, factory) {
'use strict';

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();
} 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 heightOffset;

function init() {
var style = window.getComputedStyle(ta, null);

if (style.resize === 'vertical') {
ta.style.resize = 'none';
} else if (style.resize === 'both') {
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
// made available by removing the scrollbar. This workaround will cause the text to reflow.
var width = ta.style.width;
ta.style.width = '0px';
// Force reflow:
/* jshint ignore:start */
ta.offsetWidth;
/* jshint ignore:end */
ta.style.width = width;

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

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

adjust();
}

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

ta.style.height = 'auto';

var endHeight = ta.scrollHeight+heightOffset;

if (maxHeight !== false && maxHeight < endHeight) {
endHeight = maxHeight;
if (ta.style.overflowY !== 'scroll') {
ta.style.overflowY = 'scroll';
}
} else if (ta.style.overflowY !== 'hidden') {
ta.style.overflowY = 'hidden';
}

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);
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');

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

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

ta.setAttribute('data-autosize-on', true);
ta.style.overflow = 'hidden';
ta.style.overflowY = 'hidden';

init();
}

// Do nothing in IE8 or lower
if (typeof window.getComputedStyle !== 'function') {
return function(elements) {
return elements;
};
} else {
return function(elements) {
if (elements && elements.length) {
Array.prototype.forEach.call(elements, main);
} else if (elements && elements.nodeName) {
main(elements);
}
return elements;
};
}
}));
6 changes: 6 additions & 0 deletions dest/autosize.min.js

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

25 changes: 25 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>Simple Autosize for textareas</title>
<style>
textarea {
padding: 10px;
vertical-align: top;
width: 200px;
}
</style>
</head>
<body>
<h3>max-height 300px</h3>
<textarea style='max-height: 300px'>The coconut palm (also, cocoanut), Cocos nucifera, is a member of the family Arecaceae (palm family). It is the only accepted species in the genus Cocos.[2] The term coconut can refer to the entire coconut palm, the seed, or the fruit, which, botanically, is a drupe, not a nut. The spelling cocoanut is an archaic form of the word.[3] The term is derived from 16th-century Portuguese and Spanish coco, meaning "head" or "skull",[4] from the three small holes on the coconut shell that resemble human facial features.</textarea>

<h3>no max-height</h3>
<textarea>The coconut palm (also, cocoanut), Cocos nucifera, is a member of the family Arecaceae (palm family). It is the only accepted species in the genus Cocos.[2] The term coconut can refer to the entire coconut palm, the seed, or the fruit, which, botanically, is a drupe, not a nut. The spelling cocoanut is an archaic form of the word.[3] The term is derived from 16th-century Portuguese and Spanish coco, meaning "head" or "skull",[4] from the three small holes on the coconut shell that resemble human facial features.</textarea>
</body>
<script src='../src/autosize.js'></script>
<script>
autosize(document.querySelectorAll('textarea'));
</script>
</html>
Loading

0 comments on commit 93ae38d

Please sign in to comment.