input-autogrow
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "input-autogrow", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Autogrow plugin for inputs", | ||
@@ -5,0 +5,0 @@ "main": "dist/input-autogrow.min.js", |
@@ -1,7 +0,1 @@ | ||
/* | ||
* input-autogrow - Autogrow plugin for inputs | ||
* @version v1.0.2 | ||
* @link http://github.com/westonganger/input-autogrow | ||
* @license MIT | ||
*/ | ||
!function(t){var i=("oninput"in document.createElement("input")?"input":"keydown")+".autogrow change.autogrow autogrow";t.fn.inputAutogrow=function(a){if(a&&("destroy"===a||t(this).data("autogrow-span"))&&(this.each(function(){var a=t(this);a.off(i);var n=a.data("autogrow-span");n.remove(),a.data("autogrow-span","")}),"destroy"===a))return input.data("original-width",""),this;if(t(this).data("original-width"))var n=t(this).data("original-width");else{var n=t(this).width();t(this).data("original-width",n)}var o=t.extend({maxWidth:t(this).parent().width(),minWidth:n,trailingSpace:0},a);return this.each(function(){var n=t(this),e=" ",r=a&&"trailingSpace"in a?o.trailingSpace:parseInt(n.css("fontSize")),s=t("<span/>").css({position:"absolute",top:-9999,left:-9999,width:"auto",fontSize:n.css("fontSize"),fontFamily:n.css("fontFamily"),fontWeight:n.css("fontWeight"),letterSpacing:n.css("letterSpacing"),textTransform:n.css("textTransform"),whiteSpace:"nowrap",ariaHidden:!0}).appendTo("body"),d=function(t){if(e!==(e=n.val())||"autogrow"===t.type){e||(e=n.attr("placeholder")||""),s.html(e.replace(/&/g,"&").replace(/\s/g," ").replace(/</g,"<").replace(/>/g,">"));var i=s.width()+r,a="function"==typeof o.maxWidth?o.maxWidth():o.maxWidth;i>a?i=a:i<o.minWidth&&(i=o.minWidth),i!=n.width()&&n.width(i)}};n.data("autogrow-span",s),n.off(i).on(i,d),d()}),this}}(window.jQuery||window.Zepto||window.$); | ||
!function(s){var h=("oninput"in document.createElement("input")?"input":"keydown")+".autogrow change.autogrow autogrow";s.fn.inputAutogrow=function(d){return"destroy"===d?this.each(function(){if(s(this).data("autogrow-span")){var t=s(this);t.off(h),t.data("autogrow-span").remove(),t.data("autogrow-span",""),t.data("original-width","")}}):this.each(function(){var n=s(this);if(n.data("original-width"))var t=n.data("original-width");else{t=n.width();n.data("original-width",t)}var a,o={maxWidth:n.parent().width(),minWidth:t,trailingSpace:0};d&&(a=d)===Object(a)&&(o=s.extend(o,d));var e=" ",r=s("<span/>").css({position:"absolute",top:-9999,left:-9999,width:"auto",fontSize:n.css("fontSize"),fontFamily:n.css("fontFamily"),fontWeight:n.css("fontWeight"),letterSpacing:n.css("letterSpacing"),textTransform:n.css("textTransform"),whiteSpace:"nowrap",ariaHidden:!0}).appendTo("body"),i=function(t){if(e!==(e=n.val())||"autogrow"===t.type){e||(e=n.attr("placeholder")||""),r.html(e.replace(/&/g,"&").replace(/\s/g," ").replace(/</g,"<").replace(/>/g,">"));var a=r.width()+parseInt(n.css("fontSize"))+o.trailingSpace,i="function"==typeof o.maxWidth?o.maxWidth():o.maxWidth;i<a?a=i:a<o.minWidth&&(a=o.minWidth),a!=n.width()&&n.width(a)}};n.data("autogrow-span",r),n.off(h).on(h,i),i()}),this}}(window.jQuery||window.Zepto||window.$); |
@@ -20,9 +20,10 @@ var gulp = require('gulp'), | ||
pump([ | ||
gulp.src(paths.src), | ||
uglify({ | ||
preserveComments: 'license' | ||
}), | ||
gulp.src(paths.src), | ||
uglify(), | ||
rename({ | ||
suffix: '.min' | ||
}), | ||
gulp.dest(paths.dist) | ||
@@ -29,0 +30,0 @@ ], cb); |
{ | ||
"name": "input-autogrow", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Autogrow plugin for inputs", | ||
@@ -9,2 +9,5 @@ "main": "dist/input-autogrow.min.js", | ||
}, | ||
"scripts": { | ||
"test": "if [[ $OSTYPE == \"darwin\"* ]]; then open test/test.html; else xdg-open test/test.html; fi;" | ||
}, | ||
"repository": { | ||
@@ -29,11 +32,10 @@ "type": "git", | ||
"homepage": "https://github.com/westonganger/input-autogrow", | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"del": "*", | ||
"gulp": "*", | ||
"gulp-rename": "*", | ||
"gulp-uglify": "*", | ||
"gulp-rename": "*", | ||
"pump": "*", | ||
"del": "*" | ||
"pump": "*" | ||
} | ||
} |
# input-autogrow | ||
<a href='https://ko-fi.com/A5071NK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a> | ||
`input-autogrow` is a plugin for autogrowing inputs. This plugin is different from others because most usually target `textarea` tags, this library is instead targeted at `input` tags. | ||
Input-Autogrow is a plugin for autogrowing inputs. This plugin is different from others because most usually target `textarea` tags, this library is instead targeted at `input` tags. | ||
Requires a DOM library such as jQuery, Zepto, or any that supports $.fn plugins. | ||
Requires a DOM library such as jQuery, Zepto, or any that supports `$.fn` plugins. | ||
# Demo | ||
https://jsfiddle.net/xpvt214o/50743/ | ||
# Install | ||
#### Yarn, NPM, or Bower | ||
#### NPM, Yarn, or Bower | ||
``` | ||
npm install input-autogrow | ||
yarn add input-autogrow | ||
npm install input-autogrow | ||
bower install input-autogrow | ||
@@ -30,3 +34,3 @@ ``` | ||
*= require input-autogrow | ||
*/ | ||
*/ | ||
``` | ||
@@ -36,3 +40,3 @@ | ||
```javascript | ||
/* Makes elements readonly if they already have the readonly attribute */ | ||
/* Makes input elements autogrow */ | ||
$('input.autogrow').inputAutogrow(); | ||
@@ -45,5 +49,2 @@ | ||
/* Update Autogrow field with updated parent container width */ | ||
$('input.autogrow').inputAutogrow(); | ||
/* Custom Options */ | ||
@@ -54,14 +55,26 @@ $('input.autogrow').inputAutogrow({maxWidth: 500, minWidth: 25, trailingSpace: 10}); | ||
$('input.autogrow').inputAutogrow('destroy'); | ||
/* If the parent container width is changed (and maxWidth is not specified) | ||
OR the options need to be changed, then you must destroy & re-initialize inputAutogrow */ | ||
$('input.autogrow').inputAutogrow('destroy'); | ||
$('input.autogrow').inputAutogrow(); | ||
``` | ||
# Options | ||
`maxWidth` - Default: `Inputs parent width` - The max width of the autogrow | ||
`maxWidth` - The max width of the autogrow - Defaults to the inputs parent width | ||
`minWidth` - Default: `Original input start width` - The min width of the autogrow. | ||
`minWidth` - The min width of the autogrow - Defaults to the inputs original input start width | ||
`trailingSpace` - Default: `0` - This is extra spacing at the end of the input which is used until the maxWidth is hit. | ||
`trailingSpace` - This is extra spacing at the end of the input which is used until the maxWidth is hit - Default: `0` | ||
# Contributing | ||
- Only edit js files from `src/` and `test/` folders. | ||
- To run tests: `npm run test` | ||
- Remember to run `gulp` to run the dist build after your done adding and testing your changes. | ||
# Credits | ||
Created by Weston Ganger - @westonganger | ||
Created & Maintained by Weston Ganger - [@westonganger](https://github.com/westonganger) | ||
This plugin is based on a [Stackoverflow answer](http://stackoverflow.com/questions/931207) by James Padolsey | ||
Originally based on this [Stackoverflow answer](http://stackoverflow.com/questions/931207) by James Padolsey | ||
For any consulting or contract work please contact me via my company website: [Solid Foundation Web Development](https://solidfoundationwebdev.com) |
/* | ||
* input-autogrow - Autogrow plugin for inputs | ||
* @version v1.0.2 | ||
* @version v1.0.3 | ||
* @link http://github.com/westonganger/input-autogrow | ||
@@ -11,31 +11,42 @@ * @license MIT | ||
var isObject = function(x){ | ||
return x === Object(x); | ||
}; | ||
$.fn.inputAutogrow = function(options){ | ||
if(options && (options === 'destroy' || $(this).data('autogrow-span'))){ | ||
if(options === 'destroy'){ | ||
this.each(function() { | ||
var input = $(this); | ||
input.off(events); | ||
var span = input.data("autogrow-span"); | ||
span.remove(); | ||
input.data('autogrow-span',''); | ||
if($(this).data('autogrow-span')){ | ||
var input = $(this); | ||
input.off(events); | ||
var span = input.data("autogrow-span"); | ||
span.remove(); | ||
input.data('autogrow-span',''); | ||
input.data('original-width',''); | ||
} | ||
}); | ||
if(options === 'destroy'){ | ||
input.data('original-width',''); | ||
return this; | ||
} | ||
return this; | ||
} | ||
if($(this).data('original-width')){ | ||
var minWidth = $(this).data('original-width'); | ||
}else{ | ||
var minWidth = $(this).width(); | ||
$(this).data('original-width', minWidth); | ||
} | ||
this.each(function(){ | ||
var input = $(this); | ||
var opts = $.extend({ maxWidth: $(this).parent().width(), minWidth: minWidth, trailingSpace: 0 }, options); | ||
if(input.data('original-width')){ | ||
var minWidth = input.data('original-width'); | ||
}else{ | ||
var minWidth = input.width(); | ||
input.data('original-width', minWidth); | ||
} | ||
this.each(function(){ | ||
var input = $(this); | ||
var opts = { maxWidth: input.parent().width(), minWidth: minWidth, trailingSpace: 0 }; | ||
if(options && isObject(options)){ | ||
opts = $.extend(opts, options); | ||
} | ||
var val = ' '; | ||
var trailingSpace = (options && 'trailingSpace' in options) ? opts.trailingSpace : parseInt(input.css('fontSize')); | ||
@@ -67,3 +78,3 @@ var span = $('<span/>').css({ | ||
var newWidth = span.width() + trailingSpace; | ||
var newWidth = span.width() + parseInt(input.css('fontSize')) + opts.trailingSpace; | ||
var mw = typeof(opts.maxWidth) == "function" ? opts.maxWidth() : opts.maxWidth; | ||
@@ -70,0 +81,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13953
197
77