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