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

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

[extensions] Autosize fix compatibility with 2.4, fix offset right

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