]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/jquery/jquery.autogrowtextarea.js
Show an animation while a preview is loading
[rails.git] / vendor / assets / jquery / jquery.autogrowtextarea.js
1 /*!
2  * Autogrow Textarea Plugin Version v2.0
3  * http://www.technoreply.com/autogrow-textarea-plugin-version-2-0
4  *
5  * Copyright 2011, Jevin O. Sewaruth
6  *
7  * Date: March 13, 2011
8  */
9 jQuery.fn.autoGrow = function(){
10         return this.each(function(){
11                 // Variables
12                 var colsDefault = this.cols;
13                 var rowsDefault = this.rows;
14                 
15                 //Functions
16                 var grow = function() {
17                         growByRef(this);
18                 }
19                 
20                 var growByRef = function(obj) {
21                         var linesCount = 0;
22                         var lines = obj.value.split('\n');
23                         
24                         for (var i=lines.length-1; i>=0; --i)
25                         {
26                                 linesCount += Math.floor((lines[i].length / colsDefault) + 1);
27                         }
28
29                         if (linesCount >= rowsDefault)
30                                 obj.rows = linesCount + 1;
31                         else
32                                 obj.rows = rowsDefault;
33                 }
34                 
35                 var characterWidth = function (obj){
36                         var characterWidth = 0;
37                         var temp1 = 0;
38                         var temp2 = 0;
39                         var tempCols = obj.cols;
40                         
41                         obj.cols = 1;
42                         temp1 = obj.offsetWidth;
43                         obj.cols = 2;
44                         temp2 = obj.offsetWidth;
45                         characterWidth = temp2 - temp1;
46                         obj.cols = tempCols;
47                         
48                         return characterWidth;
49                 }
50                 
51                 // Manipulations
52                 this.style.width = "auto";
53                 this.style.height = "auto";
54                 this.style.overflow = "hidden";
55                 this.style.width = ((characterWidth(this) * this.cols) + 6) + "px";
56                 this.onkeyup = grow;
57                 this.onfocus = grow;
58                 this.onblur = grow;
59                 growByRef(this);
60         });
61 };