source: extensions/Autosize/js/autosize.dimensions.js @ 16624

Last change on this file since 16624 was 16624, checked in by cljosse, 12 years ago

[extensions] Autosize fix compatibility with 2.4, fix left menu

File size: 12.1 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.toString() || "";
147          width = jQuery(elem).outerWidth();
148          height = jQuery(elem).outerHeight();
149          elem_parent =elem.parentElement;
150          if (typeof elem_parent == "undefined") {
151            elem_parent = window;
152          }
153          if (typeof Css.width != "undefined")
154            width = jQuery(elem).Get_Val_int(Css.width, jQuery(elem_parent).width(), 0);
155
156          if (typeof Css.height != "undefined")
157            height = jQuery(elem).Get_Val_int(Css.height, jQuery(elem_parent).height(), 0);
158
159
160
161
162
163
164          borderwidth.left = jQuery(elem).Get_Val_int(Css.borderLeftWidth, "", "", 'borderLeftWidth');
165          borderwidth.right = jQuery(elem).Get_Val_int(Css.borderRightWidth, "", "", 'borderRightWidth');
166          borderwidth.top = jQuery(elem).Get_Val_int(Css.borderTopWidth, "", "", 'borderTopWidth');
167          borderwidth.bottom = jQuery(elem).Get_Val_int(Css.borderBottomWidth, "", "", 'borderBottomWidth');
168          try {
169            borderwidth.width = Css.borderWidth;
170          } catch (e) {
171            borderwidth.width = '"' + borderwidth.left + ' ' + borderwidth.top + ' ' + borderwidth.right + ' ' + borderwidth.bottom + '"';
172          }
173
174
175
176          margin.left = jQuery(elem).Get_Val_int(Css.marginLeft, "", "", 'marginLeft');
177          margin.right = jQuery(elem).Get_Val_int(Css.marginRight, "", "", 'marginRight');
178          margin.top = jQuery(elem).Get_Val_int(Css.marginTop, "", "", 'marginTop');
179          margin.bottom = jQuery(elem).Get_Val_int(Css.marginBottom, "", "", 'marginBottom');
180
181
182          try {
183            margin.margin = Css.margin;
184          } catch (e) {
185            margin.margin = '"' + margin.left + ' ' + margin.top + ' ' + margin.right + ' ' + margin.bottom + '"';
186          }
187
188
189          padding.left = jQuery(elem).Get_Val_int(Css.paddingLeft, "", "", 'paddingLeft');
190          padding.right = jQuery(elem).Get_Val_int(Css.paddingRight, "", "", 'paddingRight');
191          padding.top = jQuery(elem).Get_Val_int(Css.paddingTop, "", "", 'paddingTop');
192          padding.bottom = jQuery(elem).Get_Val_int(Css.paddingBottom, "", "", 'paddingBottom');
193
194          try {
195            padding.padding = Css.padding;
196          } catch (e) {
197            padding.padding = '"' + padding.left + ' ' + padding.top + ' ' + padding.right + ' ' + padding.bottom + '"';
198          }
199        }
200        if (elem == window) {
201          Cl_Position = { top: 0, left: 0 };
202        } else {
203          Cl_Position = jQuery(elem).d_position();
204        }
205        Left = Cl_Position.left;
206        Top = Cl_Position.top;
207
208       visible= jQuery(elem).is(':visible');
209        results = {src: elem.src || "none",
210          visible: visible || "false",
211          position: myposition,
212          top: Top,
213          left: Left,
214          width: width,
215          height: height,
216          right: Left + width,
217          bottom: Top + height,
218          borderwidth: borderwidth,
219          margin: margin,
220          padding: padding,
221          id: id,
222          nodeName: nodeName
223        };
224
225        return results
226      } return results;
227    },
228    /*
229    * Cl_Position de l'object elem
230    */
231    d_position: function () {
232      var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
233      l1 = jQuery(elem).css("left");
234      if (elem) {
235        offsetParent = this.offsetParent();
236        offset = this.offset();
237
238        if (offsetParent) {
239          if (elem == window) {
240            parentOffset = { top: 0, left: 0 };
241          } else if (typeof (offsetParent.offset) != "undefined")
242            parentOffset = offsetParent.offset();
243          else
244            parentOffset = { top: 0, left: 0 };
245
246          if (!offset)
247            offset = { top: 0, left: 0 };
248
249          offset.top -= num(elem, 'marginTop');
250          offset.left -= num(elem, 'marginLeft');
251
252          if (offsetParent.length > 0) {
253            parentOffset.top += num(offsetParent, 'borderTopWidth');
254            parentOffset.left += num(offsetParent, 'borderLeftWidth');
255          } else {
256            parentOffset = { top: 0, left: 0 };
257
258          }
259        } else {
260          parentOffset = { top: 0, left: 0 };
261        }
262
263        results = {
264          top: Math.ceil(offset.top - parentOffset.top), left: Math.ceil(offset.left - parentOffset.left)
265        };
266      } return results;
267    },
268    /*
269    * offsetParent
270    */
271    offsetParent: function () {
272      var offsetParent = this[0].offsetParent;
273      while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static'))
274        offsetParent = offsetParent.offsetParent;
275      return jQuery(offsetParent);
276    },
277    // getStyles(Obj) Récupérer la valeur CSS
278    getStyles: function (elt) {
279      var element = jQuery(elt).get(0);
280      propriete = [];
281      try{
282      if (jQuery(element).length > 0)
283      if (window.getComputedStyle) // Mozilla Firefox & cie
284      {
285        var propriete = window.getComputedStyle(element, null);
286      }
287      else if (element.currentStyle) // Microsoft Internet Explorer
288      {
289
290        var propriete = element.currentStyle;
291      }
292        }catch(e){
293
294        }
295      return propriete;
296    },
297    /*
298    * Get_val_int
299    * params: element,valeur maxi
300    * return: valeur entiere
301    */
302
303    Get_Val_int: function (myObj, Maxi_val, Mini_val, prop) {
304      var val = 0;
305      Maxi_val = parseInt(Maxi_val || "0");
306
307      /*
308      - numérique suivie de px ou % ou pt ou em,
309      - thin, bordure mince,
310      - medium, bordure moyenne (valeur par défaut),
311      - thick, bordure épaisse,
312      - inherit, hérite de son parent (css2).
313      */
314
315      if (!myObj) {
316        return Maxi_val;
317      }
318
319      if (typeof (myObj) == "string") {
320        switch (myObj) {
321          case 'thin':
322            return 1;
323            break;
324          case 'medium':
325            return 2;
326            break;
327          case 'thick':
328            return 4;
329            break;
330          case 'inherit':
331            break;
332          case 'none':
333            return Maxi_val;
334            break;
335          default:
336            break;
337        }
338        el = this.get(0);
339        var reg1 = new RegExp("auto", "g");
340        if (myObj.match(reg1)) {
341          return Maxi_val;
342        }
343        if (prop)
344          myObj = num(el, prop);
345        else {
346          reg1 = new RegExp("px", "g");
347          if (myObj.match(reg1)) {
348
349            myObj = parseInt(myObj);
350
351          } else {
352            reg1 = new RegExp("%", "g");
353            if (myObj.match(reg1)) {
354              myObj = myObj.replace(reg1, "");
355              val = Math.ceil(myObj * Maxi_val / 100);
356              return val;
357            }
358
359          }
360
361        }
362
363        if (Mini_val)
364          if (myObj < Mini_val) return Maxi_val;
365
366
367        val = parseInt(myObj);
368
369      } else {
370        val = parseInt(myObj);
371      }
372      if (typeof (val) == "NaN") return Maxi_val;
373      return val;
374    }
375  });
376  /*
377  *
378  */
379  function num(el, prop) {
380    return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
381  };
382
383  myjQuery = jQuery;
384  my$ = $;
385
386})(jQuery);
Note: See TracBrowser for help on using the repository browser.