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

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

[Autosize][beta] compatibility whith simple


File size: 12.2 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 = options || false;
34                options = jQuery.extend({ margin: options });
35
36                var val = this.is(':visible') ? this[0]['offset' + name] :
37                    num(this,
38                    name.toLowerCase()) +
39                    num(this, 'border' + torl + 'Width') +
40                    num(this, 'border' + borr + 'Width') +
41                    num(this, 'padding' + torl) +
42                    num(this, 'padding' + borr)
43
44                    ;
45                return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
46            };
47        });
48    /*
49    *
50    */
51    jQuery.each(['Left', 'Top'],
52    function (i, name) {
53        jQuery.fn['scroll' + name] = function (val) {
54            if (!this[0]) return;
55            return val != undefined ? this.each(function () {
56                this == window || this == document ? window.scrollTo(name == 'Left' ? val : $(window)['scrollLeft'](),
57        name == 'Top' ? val : $(window)['scrollTop']()) : this['scroll' + name] = val;
58            }) : 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];
59        };
60
61    });
62    jQuery.fn.extend({
63
64        infos: function () {
65            var width = 0, height = 0;
66            var elem = jQuery(this).get(0);
67
68
69            var Left = 0, Top = 0, offset, parentOffset, offsetParent, results;
70            var borderwidth = { width: "0 0 0 0",
71                top: 0,
72                left: 0,
73                right: 0,
74                bottom: 0
75            }
76            var padding = { padding: "0 0 0 0",
77                top: 0,
78                left: 0,
79                right: 0,
80                bottom: 0
81            };
82            var margin = { margin: "0 0 0 0",
83                top: 0,
84                left: 0,
85                right: 0,
86                bottom: 0
87            };
88            //=====================================================================
89            myposition = "";
90            results = {
91                position: "",
92                top: 0,
93                left: 0,
94                width: 0,
95                height: 0,
96                right: 0,
97                bottom: 0,
98                borderwidth: borderwidth,
99                padding: padding,
100                margin: margin, id: "", nodeName: ""
101            };
102            ;
103            if (elem) {
104                id = ""; nodeName = "";
105                if (elem.id) id = elem.id;
106                if (elem.nodeName) nodeName = elem.nodeName;
107
108
109                if (elem == window) {
110
111                    myposition = "";
112                    width = jQuery(elem).width();
113                    height = jQuery(elem).height();
114                } else {
115
116                    Css = jQuery(elem).getStyles(elem);
117                    myposition = Css.position || "";
118                    width = jQuery(elem).outerWidth();
119                    height = jQuery(elem).outerHeight();
120
121
122                    borderwidth.left = jQuery(elem).Get_Val_int(Css.borderLeftWidth, "", "", 'borderLeftWidth');
123                    borderwidth.right = jQuery(elem).Get_Val_int(Css.borderRightWidth, "", "", 'borderRightWidth');
124                    borderwidth.top = jQuery(elem).Get_Val_int(Css.borderTopWidth, "", "", 'borderTopWidth');
125                    borderwidth.bottom = jQuery(elem).Get_Val_int(Css.borderBottomWidth, "", "", 'borderBottomWidth');
126                    try {
127                        borderwidth.width = Css.borderWidth;
128                    } catch (e) {
129                        borderwidth.width = '"' + borderwidth.left + ' ' + borderwidth.top + ' ' + borderwidth.right + ' ' + borderwidth.bottom + '"';
130                    }
131
132
133
134                    margin.left = jQuery(elem).Get_Val_int(Css.marginLeft, "", "", 'marginLeft');
135                    margin.right = jQuery(elem).Get_Val_int(Css.marginRight, "", "", 'marginRight');
136                    margin.top = jQuery(elem).Get_Val_int(Css.marginTop, "", "", 'marginTop');
137                    margin.bottom = jQuery(elem).Get_Val_int(Css.marginBottom, "", "", 'marginBottom');
138
139
140                    try {
141                        margin.margin = Css.margin;
142                    } catch (e) {
143                        margin.margin = '"' + margin.left + ' ' + margin.top + ' ' + margin.right + ' ' + margin.bottom + '"';
144                    }
145
146
147                    padding.left = jQuery(elem).Get_Val_int(Css.paddingLeft, "", "", 'paddingLeft');
148                    padding.right = jQuery(elem).Get_Val_int(Css.paddingRight, "", "", 'paddingRight');
149                    padding.top = jQuery(elem).Get_Val_int(Css.paddingTop, "", "", 'paddingTop');
150                    padding.bottom = jQuery(elem).Get_Val_int(Css.paddingBottom, "", "", 'paddingBottom');
151
152                    try {
153                        padding.padding = Css.padding;
154                    } catch (e) {
155                        padding.padding = '"' + padding.left + ' ' + padding.top + ' ' + padding.right + ' ' + padding.bottom + '"';
156                    }
157                }
158                if (elem == window) {
159                    Cl_Position = { Top: 0, Left: 0 };
160                } else {
161                    Cl_Position = jQuery(elem).position();
162                }
163                Left = Cl_Position.left;
164                Top = Cl_Position.top;
165
166                results = {
167                    position: myposition,
168                    top: Top,
169                    left: Left,
170                    width: width,
171                    height: height,
172                    right: Left + width,
173                    bottom: Top + height,
174                    borderwidth: borderwidth,
175                    margin: margin,
176                    padding: padding,
177                    id: id,
178                    nodeName: nodeName
179                };
180
181                return results
182            } return results;
183        },
184        /*
185        * Cl_Position de l'object elem
186        */
187        position: function () {
188            var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
189
190            if (elem) {
191                offsetParent = this.offsetParent();
192                offset = this.offset();
193
194                if (offsetParent) {
195                    if (elem == window) {
196                        parentOffset = { top: 0, left: 0 };
197                    } else if (typeof (offsetParent.offset) != "undefined")
198                        parentOffset = offsetParent.offset();
199                    else
200                        parentOffset = { top: 0, left: 0 };
201
202                    if (!offset)
203                        offset = { top: 0, left: 0 };
204
205                    offset.top -= num(elem, 'marginTop');
206                    offset.left -= num(elem, 'marginLeft');
207
208                    if (offsetParent.length > 0) {
209                        parentOffset.top += num(offsetParent, 'borderTopWidth');
210                        parentOffset.left += num(offsetParent, 'borderLeftWidth');
211                    } else {
212                        parentOffset = { top: 0, left: 0 };
213
214                    }
215                } else {
216                    parentOffset = { top: 0, left: 0 };
217                }
218
219                results = {
220                    top: Math.ceil(offset.top - parentOffset.top), left: Math.ceil(offset.left - parentOffset.left)
221                };
222            } return results;
223        },
224        /*
225        * offsetParent
226        */
227        offsetParent: function () {
228            var offsetParent = this[0].offsetParent;
229            while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static'))
230                offsetParent = offsetParent.offsetParent;
231            return jQuery(offsetParent);
232        },
233        // getStyles(Obj) Récupérer la valeur CSS
234        getStyles: function (elt) {
235            var element = elt;
236            if (window.getComputedStyle) // Mozilla Firefox & cie
237            {
238                var propriete = window.getComputedStyle(element, null);
239            }
240            else if (element.currentStyle) // Microsoft Internet Explorer
241            {
242
243                var propriete = element.currentStyle;
244            }
245            return propriete;
246        },
247        /*
248        * Get_val_int
249        * params: element,valeur maxi
250        * return: valeur entiere
251        */
252
253        Get_Val_int: function (myObj, Maxi_val, Mini_val, prop) {
254            var val = 0;
255            Maxi_val = parseInt(Maxi_val || "0");
256
257            /*
258            - numérique suivie de px ou % ou pt ou em,
259            - thin, bordure mince,
260            - medium, bordure moyenne (valeur par défaut),
261            - thick, bordure épaisse,
262            - inherit, hérite de son parent (css2).
263            */
264
265            if (!myObj) {
266                return Maxi_val;
267            }
268
269            if (typeof (myObj) == "string") {
270                switch (myObj) {
271                    case 'thin':
272                        return 1;
273                        break;
274                    case 'medium':
275                        return 2;
276                        break;
277                    case 'thick':
278                        return 4;
279                        break;
280                    case 'inherit':
281                        break;
282                    case 'none':
283                        return Maxi_val;
284                        break;
285                    default:
286                        break;
287                }
288                el = this.get(0);
289                var reg1 = new RegExp("auto", "g")
290                if (myObj.match(reg1)) {
291                    return Maxi_val;
292                }
293                if (prop)
294                    myObj = num(el, prop);
295                else {
296                    reg1 = new RegExp("px", "g")
297                    if (myObj.match(reg1)) {
298
299                        myObj = parseInt(myObj);
300
301                    } else {
302                        reg1 = new RegExp("%", "g")
303                        if (myObj.match(reg1)) {
304                            val = Math.ceil(myObj * Maxi_val / 100);
305                            return val;
306                        }
307
308                    }
309
310                }
311
312                if (Mini_val)
313                    if (myObj < Mini_val) return Maxi_val;
314
315
316                val = parseInt(myObj);
317
318            } else {
319                val = parseInt(myObj);
320            }
321            if (typeof (val) == "NaN") return Maxi_val;
322            return val;
323        }
324    });
325    /*
326    *
327    */
328    function num(el, prop) {
329        return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
330    };
331
332    myjQuery = jQuery;
333    my$ = $;
334
335})(jQuery);
Note: See TracBrowser for help on using the repository browser.