]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/jquery/jquery.autogrowtextarea.js
9a8f69417301a270c5f51d1955dbae2bf8586300
[rails.git] / vendor / assets / jquery / jquery.autogrowtextarea.js
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <jevin9@gmail.com> wrote this file. As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return. Jevin O. Sewaruth
7  * ----------------------------------------------------------------------------
8  *
9  * Autogrow Textarea Plugin Version v3.0
10  * http://www.technoreply.com/autogrow-textarea-plugin-3-0
11  * 
12  * THIS PLUGIN IS DELIVERD ON A PAY WHAT YOU WHANT BASIS. IF THE PLUGIN WAS USEFUL TO YOU, PLEASE CONSIDER BUYING THE PLUGIN HERE :
13  * https://sites.fastspring.com/technoreply/instant/autogrowtextareaplugin
14  *
15  * Date: October 15, 2012
16  */
17
18 jQuery.fn.autoGrow = function() {
19         return this.each(function() {
20
21                 var createMirror = function(textarea) {
22                         jQuery(textarea).after('<div class="autogrow-textarea-mirror"></div>');
23                         return jQuery(textarea).next('.autogrow-textarea-mirror')[0];
24                 }
25
26                 var sendContentToMirror = function (textarea) {
27                         mirror.innerHTML = String(textarea.value).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br />') + '.<br/>.';
28
29                         if (jQuery(textarea).height() != jQuery(mirror).height())
30                                 jQuery(textarea).height(jQuery(mirror).height());
31                 }
32
33                 var growTextarea = function () {
34                         sendContentToMirror(this);
35                 }
36
37                 // Create a mirror
38                 var mirror = createMirror(this);
39                 
40                 // Style the mirror
41                 mirror.style.display = 'none';
42                 mirror.style.wordWrap = 'break-word';
43                 mirror.style.padding = jQuery(this).css('padding');
44                 mirror.style.width = jQuery(this).css('width');
45                 mirror.style.fontFamily = jQuery(this).css('font-family');
46                 mirror.style.fontSize = jQuery(this).css('font-size');
47                 mirror.style.lineHeight = jQuery(this).css('line-height');
48
49                 // Style the textarea
50                 this.style.overflow = "hidden";
51                 this.style.minHeight = this.rows+"em";
52
53                 // Bind the textarea's event
54                 this.onkeyup = growTextarea;
55
56                 // Fire the event for text already present
57                 sendContentToMirror(this);
58
59         });
60 };