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

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

[Autosize][beta] save

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