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

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

[extensions] Autosize fix compatibility with 'charlies video'

File size: 11.9 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 = {
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 = elt;
278      if (window.getComputedStyle) // Mozilla Firefox & cie
279      {
280        var propriete = window.getComputedStyle(element, null);
281      }
282      else if (element.currentStyle) // Microsoft Internet Explorer
283      {
284
285        var propriete = element.currentStyle;
286      }
287      return propriete;
288    },
289    /*
290    * Get_val_int
291    * params: element,valeur maxi
292    * return: valeur entiere
293    */
294
295    Get_Val_int: function (myObj, Maxi_val, Mini_val, prop) {
296      var val = 0;
297      Maxi_val = parseInt(Maxi_val || "0");
298
299      /*
300      - numérique suivie de px ou % ou pt ou em,
301      - thin, bordure mince,
302      - medium, bordure moyenne (valeur par défaut),
303      - thick, bordure épaisse,
304      - inherit, hérite de son parent (css2).
305      */
306
307      if (!myObj) {
308        return Maxi_val;
309      }
310
311      if (typeof (myObj) == "string") {
312        switch (myObj) {
313          case 'thin':
314            return 1;
315            break;
316          case 'medium':
317            return 2;
318            break;
319          case 'thick':
320            return 4;
321            break;
322          case 'inherit':
323            break;
324          case 'none':
325            return Maxi_val;
326            break;
327          default:
328            break;
329        }
330        el = this.get(0);
331        var reg1 = new RegExp("auto", "g");
332        if (myObj.match(reg1)) {
333          return Maxi_val;
334        }
335        if (prop)
336          myObj = num(el, prop);
337        else {
338          reg1 = new RegExp("px", "g");
339          if (myObj.match(reg1)) {
340
341            myObj = parseInt(myObj);
342
343          } else {
344            reg1 = new RegExp("%", "g");
345            if (myObj.match(reg1)) {
346              myObj = myObj.replace(reg1, "");
347              val = Math.ceil(myObj * Maxi_val / 100);
348              return val;
349            }
350
351          }
352
353        }
354
355        if (Mini_val)
356          if (myObj < Mini_val) return Maxi_val;
357
358
359        val = parseInt(myObj);
360
361      } else {
362        val = parseInt(myObj);
363      }
364      if (typeof (val) == "NaN") return Maxi_val;
365      return val;
366    }
367  });
368  /*
369  *
370  */
371  function num(el, prop) {
372    return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
373  };
374
375  myjQuery = jQuery;
376  my$ = $;
377
378})(jQuery);
Note: See TracBrowser for help on using the repository browser.