r90677 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90676‎ | r90677 | r90678 >
Date:20:58, 23 June 2011
Author:kaldari
Status:ok
Tags:
Comment:
updating jQuery elastic plugin
Modified paths:
  • /trunk/extensions/WikiLove/modules/jquery.elastic/jquery.elastic.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiLove/modules/jquery.elastic/jquery.elastic.js
@@ -1,7 +1,7 @@
22 /**
33 * @name Elastic
44 * @descripton Elastic is jQuery plugin that grow and shrink your textareas automatically
5 -* @version 1.6.5
 5+* @version 1.6.10
66 * @requires jQuery 1.2.6+
77 *
88 * @author Jan Jarfalk
@@ -29,22 +29,35 @@
3030 'lineHeight',
3131 'fontFamily',
3232 'width',
33 - 'fontWeight'];
 33+ 'fontWeight',
 34+ 'border-top-width',
 35+ 'border-right-width',
 36+ 'border-bottom-width',
 37+ 'border-left-width',
 38+ 'borderTopStyle',
 39+ 'borderTopColor',
 40+ 'borderRightStyle',
 41+ 'borderRightColor',
 42+ 'borderBottomStyle',
 43+ 'borderBottomColor',
 44+ 'borderLeftStyle',
 45+ 'borderLeftColor'
 46+ ];
3447
3548 return this.each( function() {
3649
3750 // Elastic only works on textareas
38 - if ( this.type != 'textarea' ) {
 51+ if ( this.type !== 'textarea' ) {
3952 return false;
4053 }
 54+
 55+ var $textarea = jQuery(this),
 56+ $twin = jQuery('<div />').css({'position': 'absolute','display':'none','word-wrap':'break-word'}),
 57+ lineHeight = parseInt($textarea.css('line-height'),10) || parseInt($textarea.css('font-size'),'10'),
 58+ minheight = parseInt($textarea.css('height'),10) || lineHeight*3,
 59+ maxheight = parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE,
 60+ goalheight = 0;
4161
42 - var $textarea = jQuery(this),
43 - $twin = jQuery('<div />').css({'position': 'absolute','display':'none','word-wrap':'break-word'}),
44 - lineHeight = parseInt($textarea.css('line-height'),10) || parseInt($textarea.css('font-size'),'10'),
45 - minheight = parseInt($textarea.css('height'),10) || lineHeight*3,
46 - maxheight = parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE,
47 - goalheight = 0;
48 -
4962 // Opera returns max-height of -1 if not set
5063 if (maxheight < 0) { maxheight = Number.MAX_VALUE; }
5164
@@ -58,18 +71,32 @@
5972 $twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));
6073 }
6174
 75+ // Updates the width of the twin. (solution for textareas with widths in percent)
 76+ function setTwinWidth(){
 77+ curatedWidth = Math.floor(parseInt($textarea.width(),10));
 78+ if($twin.width() !== curatedWidth){
 79+ $twin.css({'width': curatedWidth + 'px'});
 80+
 81+ // Update height of textarea
 82+ update(true);
 83+ }
 84+ }
6285
6386 // Sets a given height and overflow state on the textarea
6487 function setHeightAndOverflow(height, overflow){
 88+
6589 var curratedHeight = Math.floor(parseInt(height,10));
66 - if($textarea.height() != curratedHeight){
 90+ if($textarea.height() !== curratedHeight){
6791 $textarea.css({'height': curratedHeight + 'px','overflow':overflow});
 92+
 93+ // Fire the custom event resize
 94+ $textarea.trigger('resize');
 95+
6896 }
6997 }
7098
71 -
7299 // This function will update the height of the textarea if necessary
73 - function update() {
 100+ function update(forced) {
74101
75102 // Get curated content from the textarea.
76103 var textareaContent = $textarea.val().replace(/&/g,'&amp;').replace(/ {2}/g, '&nbsp;').replace(/<|>/g, '&gt;').replace(/\n/g, '<br />');
@@ -77,7 +104,7 @@
78105 // Compare curated content with curated twin.
79106 var twinContent = $twin.html().replace(/<br>/ig,'<br />');
80107
81 - if(textareaContent+'&nbsp;' != twinContent){
 108+ if(forced || textareaContent+'&nbsp;' !== twinContent){
82109
83110 // Add an extra white space so new rows are added when you are at the end of a row.
84111 $twin.html(textareaContent+'&nbsp;');
@@ -108,8 +135,12 @@
109136 update();
110137 });
111138
 139+ // Update width of twin if browser or textarea is resized (solution for textareas with widths in percent)
 140+ $(window).bind('resize', setTwinWidth);
 141+ $textarea.bind('resize', setTwinWidth);
 142+ $textarea.bind('update', update);
 143+
112144 // Compact textarea on blur
113 - // Lets animate this....
114145 $textarea.bind('blur',function(){
115146 if($twin.height() < maxheight){
116147 if($twin.height() > minheight) {
@@ -121,7 +152,7 @@
122153 });
123154
124155 // And this line is to catch the browser paste event
125 - $textarea.live('input paste',function(e){ setTimeout( update, 250); });
 156+ $textarea.bind('input paste',function(e){ setTimeout( update, 250); });
126157
127158 // Run update once when elastic is initialized
128159 update();

Status & tagging log