]> git.sur5r.net Git - contagged/blob - scripts/interface/iexpander.js
JavaScript updates (migration to JQuery + Interface)
[contagged] / scripts / interface / iexpander.js
1 /**\r
2  * Interface Elements for jQuery\r
3  * Expander\r
4  * \r
5  * http://interface.eyecon.ro\r
6  * \r
7  * Copyright (c) 2006 Stefan Petre\r
8  * Dual licensed under the MIT (MIT-LICENSE.txt) \r
9  * and GPL (GPL-LICENSE.txt) licenses.\r
10  *   \r
11  *\r
12  */\r
13  \r
14 /**\r
15  * Expands text and textarea elements while new characters are typed to the a miximum width\r
16  *\r
17  * @name Expander\r
18  * @description Expands text and textarea elements while new characters are typed to the a miximum width\r
19  * @param Mixed limit integer if only expands in width, array if expands in width and height\r
20  * @type jQuery\r
21  * @cat Plugins/Interface\r
22  * @author Stefan Petre\r
23  */\r
24 \r
25 jQuery.iExpander =\r
26 {\r
27         helper : null,\r
28         expand : function()\r
29         {\r
30                 \r
31                 text = this.value;\r
32                 if (!text)\r
33                         return;\r
34                 style = {\r
35                         fontFamily: jQuery(this).css('fontFamily')||'',\r
36                         fontSize: jQuery(this).css('fontSize')||'',\r
37                         fontWeight: jQuery(this).css('fontWeight')||'',\r
38                         fontStyle: jQuery(this).css('fontStyle')||'',\r
39                         fontStretch: jQuery(this).css('fontStretch')||'',\r
40                         fontVariant: jQuery(this).css('fontVariant')||'',\r
41                         letterSpacing: jQuery(this).css('letterSpacing')||'',\r
42                         wordSpacing: jQuery(this).css('wordSpacing')||''\r
43                 };\r
44                 jQuery.iExpander.helper.css(style);\r
45                 html = jQuery.iExpander.htmlEntities(text);\r
46                 html = html.replace(new RegExp( "\\n", "g" ), "<br />");\r
47                 jQuery.iExpander.helper.html('pW');\r
48                 spacer = jQuery.iExpander.helper.get(0).offsetWidth;\r
49                 jQuery.iExpander.helper.html(html);\r
50                 width = jQuery.iExpander.helper.get(0).offsetWidth + spacer;\r
51                 if (this.Expander.limit && width > this.Expander.limit[0]) {\r
52                         width = this.Expander.limit[0];\r
53                 }\r
54                 this.style.width = width + 'px';\r
55                 if (this.tagName == 'TEXTAREA') {\r
56                         height = jQuery.iExpander.helper.get(0).offsetHeight + spacer;\r
57                         if (this.Expander.limit && height > this.Expander.limit[1]) {\r
58                                 height = this.Expander.limit[1];\r
59                         }\r
60                         this.style.height = height + 'px';\r
61                 }\r
62         },\r
63         htmlEntities : function(text)\r
64         { \r
65                 entities = {\r
66                         '&':'&amp;',\r
67                         '<':'&lt;',\r
68                         '>':'&gt;',\r
69                         '"':'&quot;'\r
70                 };\r
71                 for(i in entities) {\r
72                         text = text.replace(new RegExp(i,'g'),entities[i]);\r
73                 }\r
74                 return text;\r
75         },\r
76         build : function(limit)\r
77         {\r
78                 if (jQuery.iExpander.helper == null) {\r
79                         jQuery('body', document).append('<div id="expanderHelper" style="position: absolute; top: 0; left: 0; visibility: hidden;"></div>');\r
80                         jQuery.iExpander.helper = jQuery('#expanderHelper');\r
81                 }\r
82                 return this.each(\r
83                         function()\r
84                         {\r
85                                 if (/TEXTAREA|INPUT/.test(this.tagName)) {\r
86                                         if (this.tagName == 'INPUT') {\r
87                                                 elType = this.getAttribute('type');\r
88                                                 if (!/text|password/.test(elType)) {\r
89                                                         return;\r
90                                                 }\r
91                                         }\r
92                                         if (limit && (limit.constructor == Number || (limit.constructor == Array && limit.length == 2))) {\r
93                                                 if (limit.constructor == Number)\r
94                                                         limit = [limit, limit];\r
95                                                 else {\r
96                                                         limit[0] = parseInt(limit[0])||400;\r
97                                                         limit[1] = parseInt(limit[1])||400;\r
98                                                 }\r
99                                                 this.Expander = {\r
100                                                         limit : limit\r
101                                                 };\r
102                                         }\r
103                                         jQuery(this)\r
104                                                 .blur(jQuery.iExpander.expand)\r
105                                                 .keyup(jQuery.iExpander.expand)\r
106                                                 .keypress(jQuery.iExpander.expand);\r
107                                         jQuery.iExpander.expand.apply(this);\r
108                                 }\r
109                         }\r
110                 );                      \r
111         }\r
112 };\r
113 \r
114 jQuery.fn.Autoexpand = jQuery.iExpander.build;