source: branches/version-2_13-dev/html/user_data/packages/sphone/js/jquery.autoResizeTextAreaQ-0.1.js @ 23180

Revision 23180, 3.5 KB checked in by nanasess, 11 years ago (diff)

#2150 #2140 jQuery 1.9.x に対応

Line 
1/*
2 * jQuery autoResizeTextAreaQ plugin
3 * @requires jQuery v1.4.2 or later
4 *
5 * Copyright (c) 2010 M. Brown (mbrowniebytes A gmail.com)
6 * Licensed under the Revised BSD license:
7 * http://www.opensource.org/licenses/bsd-license.php
8 * http://en.wikipedia.org/wiki/BSD_licenses
9 *
10 * Versions:
11 * 0.1 - 2010-07-21
12 *       initial
13 *
14 * usage:
15 *  $(document).ready( function() {
16 *      $('textarea').autoResizeTextAreaQ({"max_rows":8});
17 *  });
18 *
19 */
20
21(function($) {
22
23$.fn.autoResizeTextAreaQ = function(options) {
24    var opts = $.extend({
25        // ya prob want to use
26        max_rows: 10,   // # - max rows to resize too
27
28        // ya may want to use, but defaults should be ok
29        extra_rows: 1,  // # - nbr extra rows after last line ie padding; 0|1 optimal
30
31        // ya should really specify in html
32        rows: null,     // null|# - null infer from html; # override html
33        cols: null,     // null|# - null infer from html; # override html
34
35        debug: false    // true|false - turn on|off console.log()
36    }, options);
37
38    // extra padding based on browser
39    /* Removed jQuery 1.9.x
40    if ($.browser.msie) {
41        opts.extra_rows += 1;
42    } else if ($.browser.webkit) {
43        opts.extra_rows += 1;
44    } // else $.browser.mozilla
45     */
46    opts.extra_rows += 1;       // use webkit only.
47
48    // iterate over passed in selector, only process actual textareas
49    return $(this).filter('textarea').each(function(index) {
50
51        var ta = $(this);
52        var orig = {};
53
54        // textarea rows cols current state
55        if (opts.cols != null && opts.cols > 0) {
56            ta.attr('cols', opts.cols);
57        }
58        orig.cols = ta.attr('cols');
59
60        if (opts.rows != null && opts.rows > 0) {
61            ta.attr('rows', opts.rows);
62        }
63        orig.rows = ta.attr('rows');
64
65        // validate max extra_rows
66        if (opts.max_rows == null || opts.max_rows < orig.rows) {
67            opts.max_rows = orig.rows;
68        }
69        if (opts.extra_rows == null || opts.extra_rows < 0) {
70            opts.extra_rows = 0;
71        }
72
73        if (opts.debug) {
74            console.log('opts: ', opts, ' orig: ', orig);
75        }
76
77        // resize textares on load
78        resize(ta, orig);
79
80        // check resize on key input
81        ta.bind('keyup', function(e) {
82            resize(ta, orig);
83        });
84    }); // end each()
85
86    function resize(ta, orig) {
87
88        // nbr explicit rows
89        var nl_rows = ta.val().split('\n');
90
91        // nbr inferred rows
92        var nbr_ta_rows = 0;
93        for (index in nl_rows) {
94            // overly simple check to account for text being auto wrapped and thus a new line
95            nbr_ta_rows += Math.floor((nl_rows[index].length / orig.cols)) + 1;
96        }
97
98        // get final nbr ta rows
99        var final_nbr_ta_rows = nbr_ta_rows - 1; // deduct for current line
100        final_nbr_ta_rows += opts.extra_rows; // add on extra rows
101
102        // resize textarea
103        // note: $.animate() doesnt work well here since only inc/dec row by one
104        if (final_nbr_ta_rows >= opts.max_rows) {
105            ta.attr('rows', opts.max_rows);
106
107        } else if (final_nbr_ta_rows >= orig.rows) {
108            ta.attr('rows', final_nbr_ta_rows);
109
110        } else {
111            ta.attr('rows', orig.rows);
112        }
113
114        if (opts.debug) {
115            console.log('rows: ', ta.attr('rows'), ' nbr nl_rows: ', nl_rows.length, ' nbr_ta_rows: ', nbr_ta_rows, ' final_nbr_ta_rows: ', final_nbr_ta_rows);
116        }
117    } // end resize()
118
119}; // end autoResizeTextAreaQ()
120
121})(jQuery);
Note: See TracBrowser for help on using the repository browser.