source: extensions/cl_conflit/js/jquery.dimensions.js @ 8926

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

[cl_conflit] script compatibility with piwigo 2.2

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