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

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

[extensions] Autosize fix compatibility with 2.4, new options

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