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

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

[Autosize][beta] setup cl_conflit

File size: 11.5 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 = jQuery(this).attr("position") || "";
112                    width = jQuery(elem).width();
113                    height = jQuery(elem).height();
114                } else {
115                    myposition = jQuery(this).css("position") || "";
116                    width = jQuery(elem).outerWidth();
117                    height = jQuery(elem).outerHeight();
118
119
120                    borderwidth.left = jQuery(elem).Get_Val_int(jQuery(elem).css("borderLeftWidth"));
121                    borderwidth.right = jQuery(elem).Get_Val_int(jQuery(elem).css("borderRightWidth"));
122                    borderwidth.top = jQuery(elem).Get_Val_int(jQuery(elem).css("borderTopWidth"));
123                    borderwidth.bottom = jQuery(elem).Get_Val_int(jQuery(elem).css("borderBottomWidth"));
124                    try {
125                        borderwidth.width = jQuery(elem).Get_Val_int(jQuery(elem).css("borderWidth"));
126                    } catch (e) {
127                        borderwidth.width = '"' + borderwidth.left + ' ' + borderwidth.top + ' ' + borderwidth.right + ' ' + borderwidth.bottom + '"';
128                    }
129
130
131                    margin.left = jQuery(elem).Get_Val_int(jQuery(elem).css("marginLeft"));
132                    margin.right = jQuery(elem).Get_Val_int(jQuery(elem).css("marginRight"));
133                    margin.top = jQuery(elem).Get_Val_int(jQuery(elem).css("marginTop"));
134                    margin.bottom = jQuery(elem).Get_Val_int(jQuery(elem).css("marginBottom"));
135                    try {
136                        margin.margin = jQuery(elem).Get_Val_int(jQuery(elem).css("margin"));
137                    } catch (e) {
138                        margin.margin = '"' + margin.left + ' ' + margin.top + ' ' + margin.right + ' ' + margin.bottom + '"';
139                    }
140
141                    padding.left = jQuery(elem).Get_Val_int(jQuery(elem).css("paddingLeft"));
142                    padding.right = jQuery(elem).Get_Val_int(jQuery(elem).css("paddingRight"));
143                    padding.top = jQuery(elem).Get_Val_int(jQuery(elem).css("paddingTop"));
144                    padding.bottom = jQuery(elem).Get_Val_int(jQuery(elem).css("paddingBottom"));
145                    try { padding.padding = jQuery(elem).Get_Val_int(jQuery(elem).css("padding")); } catch (e) {
146                        padding.padding = '"' + padding.left + ' ' + padding.top + ' ' + padding.right + ' ' + padding.bottom + '"';
147                    }
148                }
149                if (elem == window) {
150                    Cl_Position = { Top: 0, Left: 0 };
151                } else {
152                    Cl_Position = jQuery(elem).position();
153                }
154                Left = Cl_Position.left;
155                Top = Cl_Position.top;
156
157                results = {
158                    position: myposition,
159                    top: Top,
160                    left: Left,
161                    width: width,
162                    height: height,
163                    right: Left + width,
164                    bottom: Top + height,
165                    borderwidth: borderwidth,
166                    margin: margin,
167                    padding: padding,
168                    id: id,
169                    nodeName: nodeName
170                };
171
172                return results
173            } return results;
174        },
175        /*
176        * Cl_Position de l'object elem
177        */
178        position: function () {
179            var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
180
181            if (elem) {
182                offsetParent = this.offsetParent();
183                offset = this.offset();
184
185                if (offsetParent) {
186                    if (elem == window) {
187                        parentOffset = { top: 0, left: 0 };
188                    } else if (typeof (offsetParent.offset) != "undefined")
189                        parentOffset = offsetParent.offset();
190                    else
191                        parentOffset = { top: 0, left: 0 };
192
193                    if (!offset)
194                        offset = { top: 0, left: 0 };
195
196                    offset.top -= num(elem, 'marginTop');
197                    offset.left -= num(elem, 'marginLeft');
198
199                    if (offsetParent.length > 0) {
200                        parentOffset.top += num(offsetParent, 'borderTopWidth');
201                        parentOffset.left += num(offsetParent, 'borderLeftWidth');
202                    } else {
203                        parentOffset = { top: 0, left: 0 };
204
205                    }
206                } else {
207                    parentOffset = { top: 0, left: 0 };
208                }
209
210                results = {
211                    top: Math.ceil(offset.top - parentOffset.top), left: Math.ceil(offset.left - parentOffset.left)
212                };
213            } return results;
214        },
215        /*
216        * offsetParent
217        */
218        offsetParent: function () {
219            var offsetParent = this[0].offsetParent;
220            while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static'))
221                offsetParent = offsetParent.offsetParent;
222            return jQuery(offsetParent);
223        },
224        /*
225        * Get_val_int
226        * params: element,valeur maxi
227        * return: valeur entiere
228        */
229
230        Get_Val_int: function (myObj, Maxi_val, Mini_val) {
231            var val = 0;
232
233            Maxi_val = parseInt(Maxi_val || "0");
234
235            /*
236            - numérique suivie de px ou % ou pt ou em,
237            - thin, bordure mince,
238            - medium, bordure moyenne (valeur par défaut),
239            - thick, bordure épaisse,
240            - inherit, hérite de son parent (css2).
241            */
242
243            if (!myObj) {
244                return Maxi_val;
245            }
246
247            if (typeof (myObj) == "string") {
248                if (myObj == "none") return Maxi_val;
249                if (myObj == "auto") return Maxi_val;
250                var reg1 = new RegExp("px", "g")
251                if (myObj.match(reg1)) {
252
253                    myObj = parseInt(myObj);
254
255                }
256                else {
257                    reg1 = new RegExp("%", "g")
258                    if (myObj.match(reg1)) {
259                        val = Math.ceil(myObj * Maxi_val / 100);
260                        return val;
261                    }
262                }
263
264                if (Mini_val)
265                    if (myObj < Mini_val) return Maxi_val;
266
267                switch (myObj) {
268                    case 'thin':
269                        return 1;
270                        break;
271                    case 'medium':
272                        return 2;
273                        break;
274                    case 'thick':
275                        return 4;
276                        break;
277                    case 'inherit':
278                        break;
279                    default:
280                        break;
281                }
282
283
284
285                val = parseInt(myObj);
286
287            } else {
288                val = parseInt(myObj);
289            }
290            if (typeof (val) == "NaN") return Maxi_val;
291            return val;
292        }
293    });
294    /*
295    *
296    */
297    function num(el, prop) {
298        return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
299    };
300
301
302
303})(jQuery);
Note: See TracBrowser for help on using the repository browser.