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

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

[extensions] Autosize fix compatibility with 2.4, update

File size: 17.8 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', 'Right', 'Bottom', 'All'],
52    function (i, name) {
53      //========== scroll =================
54      jQuery.fn['scroll' + name] = function (val) {
55        if (!this[0]) return;
56        return val != undefined ? this.each(function () {
57          this == window || this == document ? window.scrollTo(name == 'Left' ? val : $(window)['scrollLeft'](),
58        name == 'Top' ? val : $(window)['scrollTop']()) : this['scroll' + name] = val;
59        }) : 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];
60      };
61      //========= Absolute =====================
62      jQuery.fn['absolute' + name] = function (Args) {
63        if (!this[0]) return 0;
64        a = jQuery(this[0]);
65        m = { Left: 0, Top: 0, Right: 0, Bottom: 0,
66          All: {
67            Left: 0, Top: 0, Right: 0, Bottom: 0
68          }
69        };
70        pos = jQuery(this).offset();
71        pos.top = Math.ceil(pos.top);
72        pos.left = Math.ceil(pos.left);
73
74        size = { width: jQuery(this).outerWidth(), height: jQuery(this).outerHeight() };
75
76        if (name == "All") {
77          m[name]['Left'] = pos.left;
78          m[name]['Top'] = pos.top;
79          m[name]['Right'] = pos.left + size.width;
80          m[name]['Bottom'] = pos.top + size.height;
81        } else {
82
83          if (name == 'Left' || name == 'Top') {
84            m[name] = name == 'Left' ? pos.left : (pos.top);
85          } else {
86            m[name] = name == 'Right' ? pos.left + size.width : pos.top + size.height;
87          }
88
89
90        }
91
92
93        return m[name];
94
95
96      };
97
98
99    });
100  //=====================================================
101  jQuery.fn.extend({
102
103    infos: function (Arguments) {
104      if (Arguments) {
105        if (Arguments == true) {
106          Args = { pos_absolute: true || false, pos_externe: Arguments.pos_externe || false };
107        } else {
108          Args = { pos_absolute: Arguments.pos_absolute || false, pos_externe: Arguments.pos_externe || false };
109        }
110      } else {
111        Args = { pos_absolute: false, pos_externe: false };
112      }
113
114
115      var width = 0, height = 0;
116      var elem = jQuery(this).get(0);
117
118
119      var Left = 0, Top = 0, offset, parentOffset, offsetParent, results;
120      var borderwidth = { width: "0 0 0 0",
121        top: 0,
122        left: 0,
123        right: 0,
124        bottom: 0
125      }
126      var padding = { padding: "0 0 0 0",
127        top: 0,
128        left: 0,
129        right: 0,
130        bottom: 0
131      };
132      var margin = { margin: "0 0 0 0",
133        top: 0,
134        left: 0,
135        right: 0,
136        bottom: 0
137      };
138      //=====================================================================
139      myposition = "";
140      results = {
141        visible: false,
142        position: "",
143        top: 0,
144        left: 0,
145        width: 0,
146        height: 0,
147        right: 0,
148        bottom: 0,
149        borderwidth: borderwidth,
150        padding: padding,
151        margin: margin,
152
153        out: {
154          width: 0,
155          height: 0,
156          right: 0,
157          bottom: 0
158        },
159
160        exterieur: {
161          width: 0,
162          height: 0,
163          right: 0,
164          bottom: 0
165        },
166
167        general: {
168          width: 0,
169          height: 0,
170          right: 0,
171          bottom: 0,
172          marges: { height: 0,
173            top: 0,
174            bottom: 0,
175            width: 0,
176            left: 0,
177            right: 0
178          }
179        },
180        src: "",
181        useMap: "",
182        id: "",
183        nodeName: ""
184      };
185      ;
186      if (elem) {
187        id = ""; nodeName = "";
188        if (elem.id) id = elem.id;
189        if (elem.nodeName) nodeName = elem.nodeName;
190
191        width = jQuery(elem).width();
192        height = jQuery(elem).height();
193        if (elem == window) {
194
195          myposition = "";
196          var myWidth = 0, myHeight = 0;
197
198          var xHeight = jQuery(document).height();
199          var x2Height = jQuery(window).height();
200
201          if (typeof (window.innerWidth) == 'number') {
202            //Non-IE
203            myWidth = window.innerWidth;
204            myHeight = window.innerHeight;
205          } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
206            //IE 6+ in 'standards compliant mode'
207            myWidth = document.documentElement.clientWidth;
208            myHeight = document.documentElement.clientHeight;
209          } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
210            //IE 4 compatible
211            myWidth = document.body.clientWidth;
212            myHeight = document.body.clientHeight;
213          }
214
215          if (myHeight != x2Height) {
216            width = myWidth;
217          }
218
219          width = myWidth;
220          height = myHeight;
221          out_width = myWidth;
222          out_height = myHeight;
223          in_width = myWidth; ;
224          in_height = myHeight;
225
226        } else {
227
228          Css = jQuery(elem).getStyles(elem);
229          myposition = Css.position.toString() || "";
230          out_width = jQuery(elem).outerWidth();
231          out_height = jQuery(elem).outerHeight();
232          in_width = jQuery(elem).innerWidth();
233          in_height = jQuery(elem).innerHeight();
234          elem_parent = elem.parentElement;
235          if (typeof elem_parent == "undefined") {
236            elem_parent = window;
237          }
238
239          if (typeof Css.width != "undefined")
240            width = Get_Val_int(Css.width, jQuery(elem_parent).width(), 0);
241
242          if (typeof Css.height != "undefined")
243            height = Get_Val_int(Css.height, jQuery(elem_parent).height(), 0);
244
245
246          borderwidth.left = Get_Val_int(Css.borderLeftWidth, "", "", { elem: elem, prop: 'borderLeftWidth' });
247          borderwidth.right = Get_Val_int(Css.borderRightWidth, "", "", { elem: elem, prop: 'borderRightWidth' });
248          borderwidth.top = Get_Val_int(Css.borderTopWidth, "", "", { elem: elem, prop: 'borderTopWidth' });
249          borderwidth.bottom = Get_Val_int(Css.borderBottomWidth, "", "", { elem: elem, prop: 'borderBottomWidth' });
250          try {
251            borderwidth.width = Css.borderWidth;
252          } catch (e) {
253            borderwidth.width = '"' + borderwidth.left + ' ' + borderwidth.top + ' ' + borderwidth.right + ' ' + borderwidth.bottom + '"';
254          }
255
256          margin.left = Get_Val_int(Css.marginLeft, "", "", { elem: elem, prop: 'marginLeft' });
257          margin.right = Get_Val_int(Css.marginRight, "", "", { elem: elem, prop: 'marginRight' });
258          margin.top = Get_Val_int(Css.marginTop, "", "", { elem: elem, prop: 'marginTop' });
259          margin.bottom = Get_Val_int(Css.marginBottom, "", "", { elem: elem, prop: 'marginBottom' });
260
261
262          try {
263            margin.margin = Css.margin;
264          } catch (e) {
265            margin.margin = '"' + margin.left + ' ' + margin.top + ' ' + margin.right + ' ' + margin.bottom + '"';
266          }
267
268
269          padding.left = Get_Val_int(Css.paddingLeft, "", "", { elem: elem, prop: 'paddingLeft' });
270          padding.right = Get_Val_int(Css.paddingRight, "", "", { elem: elem, prop: 'paddingRight' });
271          padding.top = Get_Val_int(Css.paddingTop, "", "", { elem: elem, prop: 'paddingTop' });
272          padding.bottom = Get_Val_int(Css.paddingBottom, "", "", { elem: elem, prop: 'paddingBottom' });
273
274          try {
275            padding.padding = Css.padding;
276          } catch (e) {
277            padding.padding = '"' + padding.left + ' ' + padding.top + ' ' + padding.right + ' ' + padding.bottom + '"';
278          }
279        }
280        if (elem == window) {
281          Cl_Position = { top: 0, left: 0 };
282        } else {
283          Cl_Position = jQuery(elem).d_position();
284
285          /*
286          margin: 5px;
287          border: 10px ;
288          padding: 20px;
289
290          */
291          s_elem = elem;
292          Left = Cl_Position.left; //  291
293          Top = Cl_Position.top; // 812
294          if (Args.pos_absolute == true) {
295
296            all = jQuery(elem).absoluteAll();
297
298            Top = all.Top;
299            Left = all.Left;
300            Args.pos_externe = false;
301          }
302
303          if (Args.pos_externe == true) {
304            if (elem.tagName != "BODY") {
305              nodeName = nodeName || "";
306              L1 = jQuery(elem).position();  // sans les marges ;
307              L2 = jQuery(elem).position(true); //  296 x 817
308              Left = L1.left;
309              Top = L1.top;
310            }
311
312          }
313
314          elem = s_elem;
315        }
316        visible = jQuery(elem).is(':visible');
317        results = {
318          useMap: elem.useMap || "none",
319          src: elem.src || "none",
320          visible: visible || false,
321          position: myposition,
322          top: Top,
323          left: Left,
324          width: width,  // valeur interieur
325          height: height,
326          right: Left + width,
327          bottom: Top + height,
328
329          padding: padding,
330
331          out: {
332            width: in_width,
333            height: in_height,
334            right: Left + in_width,
335            bottom: Top + in_height
336          },
337          borderwidth: borderwidth,
338          exterieur: {
339            width: out_width,
340            height: out_height,
341            right: Left + out_width,
342            bottom: Top + out_height
343          },
344          margin: margin,
345          general: {
346            width: out_width + margin.left + margin.right,
347            height: out_height + margin.top + margin.bottom,
348            right: Left + out_width + margin.left + margin.right,
349            bottom: Top + out_height + margin.top + margin.bottom,
350            marges: { height: margin.top +
351                      borderwidth.top +
352                      padding.top +
353                      margin.bottom + borderwidth.bottom +
354                      padding.bottom,
355              top: margin.top +
356                            borderwidth.top +
357                            padding.top,
358              bottom: margin.bottom +
359                       borderwidth.bottom +
360                        padding.bottom,
361              width: margin.left +
362                              borderwidth.left +
363                              padding.left +
364                              margin.right +
365                              borderwidth.right +
366                              padding.right,
367              left: margin.left +
368                            borderwidth.left +
369                            padding.left,
370              right: margin.right +
371                             borderwidth.right +
372                             padding.right
373            }
374          },
375          id: id,
376          nodeName: nodeName || ""
377        };
378
379        return results
380      } return results;
381    },
382    /*
383    * Cl_Position de l'object elem
384    */
385    d_position: function () {
386      var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
387      l1 = jQuery(elem).css("left");
388      if (elem) {
389        offsetParent = this.offsetParent();
390        offset = this.offset();
391
392        if (offsetParent) {
393          if (elem == window) {
394            parentOffset = { top: 0, left: 0 };
395          } else if (typeof (offsetParent.offset) != "undefined")
396            parentOffset = offsetParent.offset();
397          else
398            parentOffset = { top: 0, left: 0 };
399
400          if (!offset)
401            offset = { top: 0, left: 0 };
402
403          offset.top -= num(elem, 'marginTop');
404          offset.left -= num(elem, 'marginLeft');
405
406          if (offsetParent.length > 0) {
407            parentOffset.top += num(offsetParent, 'borderTopWidth');
408            parentOffset.left += num(offsetParent, 'borderLeftWidth');
409          } else {
410            parentOffset = { top: 0, left: 0 };
411
412          }
413        } else {
414          parentOffset = { top: 0, left: 0 };
415        }
416
417        results = {
418          top: Math.ceil(offset.top - parentOffset.top), left: Math.ceil(offset.left - parentOffset.left)
419        };
420      } return results;
421    },
422    /*
423    * offsetParent
424    */
425    offsetParent: function () {
426      if (this[0] == window) return;
427      try {
428
429        n1 = this[0].nodeName;
430        var offsetParent = this[0].offsetParent;
431
432        while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static'))
433          offsetParent = offsetParent.offsetParent;
434        return jQuery(offsetParent);
435      } catch (e) {
436        jQuery(offsetParent);
437      }
438    },
439    // getStyles(Obj) Récupérer la valeur CSS
440    getStyles: function (elt) {
441      var element = jQuery(elt).get(0);
442      propriete = [];
443      try {
444        if (jQuery(element).length > 0)
445          if (window.getComputedStyle) // Mozilla Firefox & cie
446          {
447            var propriete = window.getComputedStyle(element, null);
448          }
449          else if (element.currentStyle) // Microsoft Internet Explorer
450          {
451
452            var propriete = element.currentStyle;
453          }
454      } catch (e) {
455
456      }
457      return propriete;
458    },
459    Get_Val_int: function (myObj, Maxi_val, Mini_val, prop) {
460
461      return Get_Val_int(myObj, Maxi_val, Mini_val, prop);
462    }
463  });
464
465  /**************************
466  * Get_val_int
467  * params: element,valeur maxi
468  * return: valeur entiere
469  ******************************/
470  Get_Val_int = function (myObj, Maxi_val, Mini_val, prop) {
471
472    var val = 0;
473    Maxi_val = parseInt(Maxi_val || "0");
474
475    /*
476    - numérique suivie de px ou % ou pt ou em,
477    - thin, bordure mince,
478    - medium, bordure moyenne (valeur par défaut),
479    - thick, bordure épaisse,
480    - inherit, hérite de son parent (css2).
481    */
482
483    if (!myObj) {
484      return Maxi_val;
485    }
486
487    if (typeof (myObj) == "string") {
488      switch (myObj) {
489        case 'thin':
490          return 1;
491          break;
492        case 'medium':
493          return 2;
494          break;
495        case 'thick':
496          return 4;
497          break;
498        case 'inherit':
499          break;
500        case 'none':
501          return Maxi_val;
502          break;
503        default:
504          break;
505      }
506
507      var reg1 = new RegExp("auto", "g");
508      if (myObj.match(reg1)) {
509        return Maxi_val;
510      }
511
512      if (prop) {
513
514        myObj = num(prop.elem, prop.prop);
515      } else {
516        reg1 = new RegExp("px", "g");
517        if (myObj.match(reg1)) {
518
519          myObj = Parse_Int(myObj);
520
521        } else {
522          reg1 = new RegExp("%", "g");
523          if (myObj.match(reg1)) {
524            myObj = myObj.replace(reg1, "");
525            val = Math.ceil(myObj * Maxi_val / 100);
526            return val;
527          }
528        }
529      }
530      if (Mini_val)
531        if (myObj < Mini_val) return Maxi_val;
532      val = Parse_Int(myObj);
533
534    } else {
535      val = Parse_Int(myObj);
536    }
537    if (typeof (val) == "NaN")
538      return Maxi_val;
539    return val;
540
541  }
542
543  /*
544  *
545  */
546  function Parse_Int(valeur_num) {
547    if (!jQuery.isNumeric(valeur_num)) {
548      valeur_num = valeur_num.toString();
549      reg1 = new RegExp("px", "gi");
550      if (valeur_num.match(reg1)) {
551        valeur_num = valeur_num.replace(reg1, "");
552      } if (valeur_num.match(RegExp("%", "gi"))) {
553        valeur_num = valeur_num.replace(reg1, "");
554        valeur_num = Math.ceil(valeur_num * Maxi_val / 100);
555      } else {
556        valeur_num = valeur_num.replace(reg1, "");
557      }
558    }
559    valeur_num = Math.ceil(valeur_num);
560    return valeur_num || 0;
561  }
562
563  function num(el, prop) {
564    valeur_num = jQuery.curCSS(el.jquery ? el[0] : el, prop, true);
565
566    return Parse_Int(valeur_num);
567  };
568
569  // myjQuery = jQuery;
570  // my$ = $;
571
572})(jQuery);
Note: See TracBrowser for help on using the repository browser.