source: extensions/Autosize/js/jquery.dimensions.js @ 7394

Last change on this file since 7394 was 7394, checked in by cljosse, 13 years ago

[Autosize][beta] Add the option validation of the autosize according to the status

File size: 9.9 KB
Line 
1/* Copyright (c) 2007 Paul Bakaus (paul.bakaus@googlemail.com) and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
2 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
3 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
4 *
5 * $LastChangedDate: 2007-12-20 08:43:48 -0600 (Thu, 20 Dec 2007) $
6 * $Rev: 4257 $
7 *
8 * $LastChangedDate: 2010-10-23 08:43:48 -0600 (Thu, 20 Dec 2007) $
9 * By cljosse
10 * Version: 1.2
11 *
12 * Requires: jQuery 1.2+
13 *
14 */
15(function (jQuery) {
16    jQuery.dimensions = { version: '1.3' };
17    /*
18    * Interception Height, Width
19    */
20    jQuery.each(['Height', 'Width'],
21            function (i, name) {
22                jQuery.fn['inner' + name] = function () {
23                    if (!this[0]) return;
24                    var torl = name == 'Height' ? 'Top' : 'Left',
25                        borr = name == 'Height' ? 'Bottom' : 'Right';
26                    return this.is(':visible') ? this[0]['client' + name] : num(this, name.toLowerCase()) + num(this, 'padding' + torl) + num(this, 'padding' + borr);
27                };
28
29                jQuery.fn['outer' + name] = function (options) {
30                    if (!this[0]) return;
31                    var torl = name == 'Height' ? 'Top' : 'Left',
32                        borr = name == 'Height' ? 'Bottom' : 'Right';
33                    options = jQuery.extend({ margin: false },
34                                                options || {});
35                    var val = this.is(':visible') ? this[0]['offset' + name] :
36                        num(this,
37                        name.toLowerCase()) +
38                        num(this, 'border' + torl + 'Width') +
39                        num(this, 'border' + borr + 'Width') +
40                        num(this, 'padding' + torl) +
41                        num(this, 'padding' + borr);
42                    return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
43                };
44            });
45    /*
46    *
47    */
48    jQuery.each(['Left', 'Top'],
49      function (i, name) {
50          jQuery.fn['scroll' + name] = function (val) {
51              if (!this[0]) return;
52              return val != undefined ? this.each(function () {
53                  this == window || this == document ? window.scrollTo(name == 'Left' ? val : $(window)['scrollLeft'](),
54           name == 'Top' ? val : $(window)['scrollTop']()) : this['scroll' + name] = val;
55              }) : this[0] == window || this[0] == document ? self[(name == 'Left' ? 'pageXOffset' : 'pageYOffset')] || jQuery.boxModel && document.documentElement['scroll' + name] || document.body['scroll' + name] : this[0]['scroll' + name];
56          };
57
58      });
59    jQuery.fn.extend({
60
61        info: function () {
62            var width = 0, height = 0;
63            var elem = jQuery(this).get(0);
64
65
66            var left = 0, top = 0, offset, parentOffset, offsetParent, results;
67            var borderwidth = { width: "0 0 0 0",
68                top: 0,
69                left: 0,
70                right: 0,
71                bottom: 0
72            }
73            var padding = { padding: "0 0 0 0",
74                top: 0,
75                left: 0,
76                right: 0,
77                bottom: 0
78            };
79            var margin = { margin: "0 0 0 0",
80                top: 0,
81                left: 0,
82                right: 0,
83                bottom: 0
84            };
85            //=====================================================================
86            results = {
87                position: jQuery(elem).css("position"),
88                top: 0,
89                left: 0,
90                width: 0,
91                height: 0,
92                right: 0,
93                bottom: 0,
94                borderwidth: borderwidth,
95                padding: padding,
96                margin: margin
97            };
98            ;
99            if (elem) {
100                Position = jQuery(elem).position();
101                left = Position.left;
102                top = Position.top;
103                width = jQuery(elem).outerWidth();
104                height = jQuery(elem).outerHeight();
105
106
107                borderwidth.left = jQuery(elem).Get_Val_int(jQuery(elem).css("borderLeftWidth"));
108                borderwidth.right = jQuery(elem).Get_Val_int(jQuery(elem).css("borderRightWidth"));
109                borderwidth.top = jQuery(elem).Get_Val_int(jQuery(elem).css("borderTopWidth"));
110                borderwidth.bottom = jQuery(elem).Get_Val_int(jQuery(elem).css("borderBottomWidth"));
111                try {
112                    borderwidth.width = jQuery(elem).Get_Val_int(jQuery(elem).css("borderWidth"));
113                } catch (e) {
114                    borderwidth.width = '"' + borderwidth.left + ' ' + borderwidth.top + ' ' + borderwidth.right + ' ' + borderwidth.bottom + '"';
115                }
116
117
118                margin.left = jQuery(elem).Get_Val_int(jQuery(elem).css("marginLeft"));
119                margin.right = jQuery(elem).Get_Val_int(jQuery(elem).css("marginRight"));
120                margin.top = jQuery(elem).Get_Val_int(jQuery(elem).css("marginTop"));
121                margin.bottom = jQuery(elem).Get_Val_int(jQuery(elem).css("marginBottom"));
122                try {
123                    margin.margin = jQuery(elem).Get_Val_int(jQuery(elem).css("margin"));
124                } catch (e) {
125                    margin.margin = '"' + margin.left + ' ' + margin.top + ' ' + margin.right + ' ' + margin.bottom + '"';
126                }
127
128                padding.left = jQuery(elem).Get_Val_int(jQuery(elem).css("paddingLeft"));
129                padding.right = jQuery(elem).Get_Val_int(jQuery(elem).css("paddingRight"));
130                padding.top = jQuery(elem).Get_Val_int(jQuery(elem).css("paddingTop"));
131                padding.bottom = jQuery(elem).Get_Val_int(jQuery(elem).css("paddingBottom"));
132                try { padding.padding = jQuery(elem).Get_Val_int(jQuery(elem).css("padding")); } catch (e) {
133                    padding.padding = '"' + padding.left + ' ' + padding.top + ' ' + padding.right + ' ' + padding.bottom + '"';
134                }
135
136                results = {
137                    position: jQuery(elem).css("position"),
138                    top: top,
139                    left: left,
140                    width: width,
141                    height: height,
142                    right: left + width,
143                    bottom: top + height,
144                    borderwidth: borderwidth,
145                    margin: margin,
146                    padding: padding
147                };
148
149                return results
150            } return results;
151        },
152        /*
153        * Position de l'object elem
154        */
155        position: function () {
156            var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
157            if (elem) {
158                offsetParent = this.offsetParent();
159                offset = this.offset();
160                if (offsetParent) {
161                    if (typeof (offsetParent.offset) != "undefined")
162                        parentOffset = offsetParent.offset();
163                    else
164                        parentOffset = { top: 0, left: 0 };
165
166                    offset.top -= num(elem, 'marginTop');
167                    offset.left -= num(elem, 'marginLeft');
168
169                    if (offsetParent.length > 0) {
170                        parentOffset.top += num(offsetParent, 'borderTopWidth');
171                        parentOffset.left += num(offsetParent, 'borderLeftWidth');
172                    } else {
173                        parentOffset = { top: 0, left: 0 };
174
175                    }
176                } else {
177                    parentOffset = { top: 0, left: 0 };
178                }
179                results = {
180                    top: offset.top - parentOffset.top, left: offset.left - parentOffset.left
181                };
182            } return results;
183        },
184        /*
185        * offsetParent
186        */
187        offsetParent: function () {
188            var offsetParent = this[0].offsetParent;
189            while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static'))
190                offsetParent = offsetParent.offsetParent;
191            return jQuery(offsetParent);
192        },
193    /*
194    * Get_val_int
195    * params: element,valeur maxi
196    * return: valeur entiere
197    */
198 
199     Get_Val_int: function(myObj, Maxi_val, Mini_val) {
200        var val = 0;
201        var reg1 = new RegExp("%", "g")
202        Maxi_val = parseInt(Maxi_val || "0");
203
204        /*
205        - numérique suivie de px ou % ou pt ou em,
206        - thin, bordure mince,
207        - medium, bordure moyenne (valeur par défaut),
208        - thick, bordure épaisse,
209        - inherit, hérite de son parent (css2).
210        */
211
212        if (!myObj) {
213            return Maxi_val;
214        }
215        if (typeof (myObj) == "string") {
216            if (myObj == "none") return Maxi_val;
217            if (myObj == "auto") return Maxi_val;
218            if (Mini_val)
219                if (myObj < Mini_val) return Maxi_val;
220            switch (myObj) {
221                case 'thin':
222                    return 1;
223                    break;
224                case 'medium':
225                    return 2;
226                    break;
227                case 'thick':
228                    return 4;
229                    break;
230                case 'inherit':
231
232
233                    break;
234
235                default:
236
237
238                    break;
239            }
240
241
242
243
244
245            if (myObj.match(reg1)) val = myObj * Maxi_val / 100;
246            val = parseInt(myObj);
247
248        } else
249            val = parseInt(myObj);
250
251
252        if (typeof (val) == "NaN") return Maxi_val;
253        return val;
254    }
255    });
256    /*
257    *
258    */
259    function num(el, prop) {
260        return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
261    };
262
263
264
265})(jQuery);
Note: See TracBrowser for help on using the repository browser.