├── LICENCE.txt ├── README.md ├── bower.json ├── elastic.js ├── index.html └── package.json /LICENCE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Monospaced 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [DEPRECATED] Angular Elastic 2 | =============== 3 | 4 | **Since [AngularJS support has been discontinued](https://blog.angular.io/discontinued-long-term-support-for-angularjs-cc066b82e65a) this package is deprecated and no longer maintained.** 5 | 6 | --- 7 | 8 | Elastic (autosize) textareas for AngularJS, without jQuery dependency. 9 | 10 | [See it in action](http://monospaced.github.io/angular-elastic). 11 | 12 | Usage 13 | ----- 14 | 15 | as attribute 16 | 17 | 20 | 21 | as class 22 | 23 | 26 | 27 | optionally append whitespace to the end of the height calculation (an extra newline improves the appearance when animating) 28 | 29 | 32 | 33 | 36 | 37 | or configure whitespace globally 38 | 39 | app.config(['msdElasticConfig', function(msdElasticConfig) { 40 | msdElasticConfig.append = '\n'; 41 | }]) 42 | 43 | the directive also emits an `elastic:resize` event which you can listen for 44 | 45 | $scope.$on('elastic:resize', function(event, element, oldHeight, newHeight) { 46 | // do stuff 47 | }); 48 | 49 | Single line textareas 50 | -------------- 51 | 52 | Set the `rows` attribute to `1`, as browsers default to `2`. 53 | 54 | 57 | 58 | Install 59 | ------- 60 | 61 | bower install angular-elastic 62 | 63 | npm install angular-elastic 64 | 65 | Include the `elastic.js` script provided by this component in your app. 66 | 67 | Make sure to add `monospaced.elastic` to your app’s module dependencies. 68 | 69 | ``` 70 | angular 71 | .module('yourApp', [ 72 | 'monospaced.elastic' 73 | ]); 74 | ```` 75 | 76 | Support 77 | ------- 78 | 79 | __Modern browsers__ only—Internet Explorer 6, 7 & 8 retain their default textarea behaviour. 80 | 81 | Demo 82 | ---------------- 83 | 84 | * [monospaced.github.io/angular-elastic](http://monospaced.github.io/angular-elastic) 85 | * [plunker](http://plnkr.co/edit/9y6YLriAwsK9hqdu72WT?p=preview) 86 | 87 | 88 | How it works 89 | ------------ 90 | 91 | By creating a hidden textarea that mirrors the textarea to which the directive was applied, Angular Elastic can measure the required height and adjust the textarea accordingly. Adjustments are done on: 92 | 93 | * Keystroke events 94 | * Window resize events 95 | * Model changes 96 | 97 | This works well in most cases with no additional code required other than described in the Usage section above. However, it may occur that the adjustment must be invoked manually at a time that is not covered by the events listed above. E.g. textareas with the style `display: none;` may not have a valid width in Safari which produces incorrect adjustments. In this case the adjustment needs to be invoked once these textareas become visible. For that Angular Elastic listens to the `elastic:adjust` event on its scope. To invoke the adjustment for all textareas covered by Angular Elastic use: 98 | 99 | $rootScope.$broadcast('elastic:adjust'); 100 | 101 | Inspiration 102 | ---------------- 103 | 104 | * [jQuery Autosize](http://www.jacklmoore.com/autosize/) 105 | 106 | * [jQuery Elastic](http://unwrongest.com/projects/elastic/) 107 | 108 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-elastic", 3 | "version": "2.5.1", 4 | "main": "elastic.js", 5 | "ignore": [ 6 | "**/.*", 7 | "node_modules", 8 | "components" 9 | ], 10 | "dependencies": { 11 | "angular": ">=1.0.6" 12 | } 13 | } -------------------------------------------------------------------------------- /elastic.js: -------------------------------------------------------------------------------- 1 | /* 2 | * angular-elastic v2.5.1 3 | * (c) 2014 Monospaced http://monospaced.com 4 | * License: MIT 5 | */ 6 | 7 | if (typeof module !== 'undefined' && 8 | typeof exports !== 'undefined' && 9 | module.exports === exports){ 10 | module.exports = 'monospaced.elastic'; 11 | } 12 | 13 | angular.module('monospaced.elastic', []) 14 | 15 | .constant('msdElasticConfig', { 16 | append: '' 17 | }) 18 | 19 | .directive('msdElastic', [ 20 | '$timeout', '$window', 'msdElasticConfig', 21 | function($timeout, $window, config) { 22 | 'use strict'; 23 | 24 | return { 25 | require: 'ngModel', 26 | restrict: 'A, C', 27 | link: function(scope, element, attrs, ngModel) { 28 | 29 | // cache a reference to the DOM element 30 | var ta = element[0], 31 | $ta = element; 32 | 33 | // ensure the element is a textarea, and browser is capable 34 | if (ta.nodeName !== 'TEXTAREA' || !$window.getComputedStyle) { 35 | return; 36 | } 37 | 38 | // set these properties before measuring dimensions 39 | $ta.css({ 40 | 'overflow': 'hidden', 41 | 'overflow-y': 'hidden', 42 | 'word-wrap': 'break-word' 43 | }); 44 | 45 | // force text reflow 46 | var text = ta.value; 47 | ta.value = ''; 48 | ta.value = text; 49 | 50 | var append = attrs.msdElastic ? attrs.msdElastic.replace(/\\n/g, '\n') : config.append, 51 | $win = angular.element($window), 52 | mirrorInitStyle = 'position: absolute; top: -999px; right: auto; bottom: auto;' + 53 | 'left: 0; overflow: hidden; -webkit-box-sizing: content-box;' + 54 | '-moz-box-sizing: content-box; box-sizing: content-box;' + 55 | 'min-height: 0 !important; height: 0 !important; padding: 0;' + 56 | 'word-wrap: break-word; border: 0;', 57 | $mirror = angular.element(' 67 |

68 | Hide | 69 | Show | 70 | Change model 71 |

72 | 73 |

74 |

75 | 76 |

77 | 78 | 79 | 80 | https://github.com/monospaced/angular-elastic
81 | Monospaced Labs 82 |
83 | 84 | 85 | 86 | 108 | 109 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-elastic", 3 | "version": "2.5.1", 4 | "description": "Elastic (autosize) textareas for AngularJS, without jQuery dependency.", 5 | "keywords": [ 6 | "angular", 7 | "angularjs", 8 | "textarea", 9 | "elastic", 10 | "autosize" 11 | ], 12 | "homepage": "http://monospaced.github.io/angular-elastic/", 13 | "main": "elastic", 14 | "bugs": "http://github.com/monospaced/angular-elastic/issues", 15 | "license" : "MIT", 16 | "repository" : { 17 | "type" : "git", 18 | "url" : "http://github.com/monospaced/angular-elastic" 19 | }, 20 | "author": { 21 | "name" : "Scott Boyle", 22 | "email" : "scott@monospaced.com", 23 | "url" : "http://scottboyle.co.uk/about" 24 | }, 25 | "files": [ 26 | "elastic.js", 27 | "package.json" 28 | ], 29 | "dependencies": { 30 | "angular": ">=1.0.6" 31 | } 32 | } --------------------------------------------------------------------------------