source: extensions/Autosize/js/jquery.dimensions.min.js @ 7931

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

[Autosize][beta] fix bugs with randompict

File size: 8.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 * Version: 1.2
9 *
10 * Requires: jQuery 1.2+
11 */
12(function (jQuery) {
13    jQuery.dimensions = { version: '1.3' };
14    /*
15    * Interception Height, Width
16    */
17    jQuery.each(['Height', 'Width'],
18            function (i, name) {
19                jQuery.fn['inner' + name] = function () {
20                    if (!this[0]) return;
21                    var torl = name == 'Height' ? 'Top' : 'Left',
22                        borr = name == 'Height' ? 'Bottom' : 'Right';
23                    return this.is(':visible') ? this[0]['client' + name] : num(this, name.toLowerCase()) + num(this, 'padding' + torl) + num(this, 'padding' + borr);
24                };
25
26                jQuery.fn['outer' + name] = function (options) {
27                    if (!this[0]) return;
28                    var torl = name == 'Height' ? 'Top' : 'Left',
29                        borr = name == 'Height' ? 'Bottom' : 'Right';
30                    options = jQuery.extend({ margin: false },
31                                                options || {});
32                    var val = this.is(':visible') ? this[0]['offset' + name] :
33                        num(this,
34                        name.toLowerCase()) +
35                        num(this, 'border' + torl + 'Width') +
36                        num(this, 'border' + borr + 'Width') +
37                        num(this, 'padding' + torl) +
38                        num(this, 'padding' + borr);
39                    return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
40                };
41            });
42    /*
43    *
44    */
45    jQuery.each(['Left', 'Top'],
46      function (i, name) {
47          jQuery.fn['scroll' + name] = function (val) {
48              if (!this[0]) return;
49              return val != undefined ? this.each(function () {
50                  this == window || this == document ? window.scrollTo(name == 'Left' ? val : $(window)['scrollLeft'](),
51           name == 'Top' ? val : $(window)['scrollTop']()) : this['scroll' + name] = val;
52              }) : 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];
53          };
54
55      });
56    jQuery.fn.extend({
57
58        info: function () {
59            var width = 0, height = 0;
60            var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
61            var borderwidth = { width: "0 0 0 0",
62                top: 0,
63                left: 0,
64                right: 0,
65                bottom: 0
66            }
67            var padding = { padding: "0 0 0 0",
68                top: 0,
69                left: 0,
70                right: 0,
71                bottom: 0
72            };
73            var margin = { margin: "0 0 0 0",
74                top: 0,
75                left: 0,
76                right: 0,
77                bottom: 0
78            };
79            //=====================================================================
80            results = {
81                position: jQuery(elem).css("position"),
82                top: 0,
83                left: 0,
84                width: 0,
85                height: 0,
86                right: 0,
87                bottom: 0,
88                borderwidth: borderwidth,
89                padding: padding,
90                margin: margin
91            };
92            ;
93            if (elem) {
94                Position = jQuery(elem).position();
95                left = Position.left;
96                top = Position.top;
97                width = jQuery(elem).outerWidth();
98                height = jQuery(elem).outerHeight();
99
100
101                borderwidth.left = jQuery(elem).css("borderLeftWidth");
102                borderwidth.right = jQuery(elem).css("borderRightWidth");
103                borderwidth.top = jQuery(elem).css("borderTopWidth");
104                borderwidth.bottom = jQuery(elem).css("borderBottomWidth");
105                try {
106                    borderwidth.width = jQuery(elem).css("borderWidth");
107                } catch (e) {
108                    borderwidth.width = '"' + borderwidth.left + ' ' + borderwidth.top + ' ' + borderwidth.right + ' ' + borderwidth.bottom + '"';
109                }
110
111
112                margin.left = jQuery(elem).css("marginLeft");
113                margin.right = jQuery(elem).css("marginRight");
114                margin.top = jQuery(elem).css("marginTop");
115                margin.bottom = jQuery(elem).css("marginBottom");
116                try {
117                    margin.margin = jQuery(elem).css("margin");
118                } catch (e) {
119                    margin.margin = '"' + margin.left + ' ' + margin.top + ' ' + margin.right + ' ' + margin.bottom + '"';
120                }
121
122                padding.left = jQuery(elem).css("paddingLeft");
123                padding.right = jQuery(elem).css("paddingRight");
124                padding.top = jQuery(elem).css("paddingTop");
125                padding.bottom = jQuery(elem).css("paddingBottom");
126                try { padding.padding = jQuery(elem).css("padding"); } catch (e) {
127                    padding.padding = '"' + padding.left + ' ' + padding.top + ' ' + padding.right + ' ' + padding.bottom + '"';
128                }
129
130                results = {
131                    position: jQuery(elem).css("position"),
132                    top: top,
133                    left: left,
134                    width: width,
135                    height: height,
136                    right: left + width,
137                    bottom: top + height,
138                    borderwidth: borderwidth,
139                    margin: margin,
140                    padding: padding
141                };
142
143                return results
144            } return results;
145        },
146        /*
147        * Position de l'object elem
148        */
149        position: function () {
150            var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
151            if (elem) {
152                offsetParent = this.offsetParent();
153                offset = this.offset();
154                if (offsetParent) {
155                    if (typeof (offsetParent.offset) != "undefined")
156                        parentOffset = offsetParent.offset();
157                    else
158                        parentOffset = { top: 0, left: 0 };
159
160                    offset.top -= num(elem, 'marginTop');
161                    offset.left -= num(elem, 'marginLeft');
162
163                    if (offsetParent.length > 0) {
164                        parentOffset.top += num(offsetParent, 'borderTopWidth');
165                        parentOffset.left += num(offsetParent, 'borderLeftWidth');
166                    } else {
167                        parentOffset = { top: 0, left: 0 };
168
169                    }
170                } else {
171                    parentOffset = { top: 0, left: 0 };
172                }
173                results = {
174                    top: offset.top - parentOffset.top, left: offset.left - parentOffset.left
175                };
176            } return results;
177        },
178        /*
179        * offsetParent
180        */
181        offsetParent: function () {
182            var offsetParent = this[0].offsetParent;
183            while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static'))
184                offsetParent = offsetParent.offsetParent;
185            return jQuery(offsetParent);
186        }
187    });
188    /*
189    *
190    */
191    function num(el, prop) {
192        return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
193    };
194})(jQuery);
Note: See TracBrowser for help on using the repository browser.