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

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

[extensions] Autosize fix compatibility with 2.4

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