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

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

[extensions] Autosize fix compatibility with 2.4, new options

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