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

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

[extensions] autosize compatibility with theme gally

  • rename lang.php to plugin.lang.php
  • rename jquery.cookies.js to autosize.cookie.js
  • rename jquery.dimensions.js to autosize.dimensions.js
File size: 11.4 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         
148
149          width = jQuery(elem).outerWidth();
150          height = jQuery(elem).outerHeight();
151
152
153          borderwidth.left = jQuery(elem).Get_Val_int(Css.borderLeftWidth, "", "", 'borderLeftWidth');
154          borderwidth.right = jQuery(elem).Get_Val_int(Css.borderRightWidth, "", "", 'borderRightWidth');
155          borderwidth.top = jQuery(elem).Get_Val_int(Css.borderTopWidth, "", "", 'borderTopWidth');
156          borderwidth.bottom = jQuery(elem).Get_Val_int(Css.borderBottomWidth, "", "", 'borderBottomWidth');
157          try {
158            borderwidth.width = Css.borderWidth;
159          } catch (e) {
160            borderwidth.width = '"' + borderwidth.left + ' ' + borderwidth.top + ' ' + borderwidth.right + ' ' + borderwidth.bottom + '"';
161          }
162
163
164
165          margin.left = jQuery(elem).Get_Val_int(Css.marginLeft, "", "", 'marginLeft');
166          margin.right = jQuery(elem).Get_Val_int(Css.marginRight, "", "", 'marginRight');
167          margin.top = jQuery(elem).Get_Val_int(Css.marginTop, "", "", 'marginTop');
168          margin.bottom = jQuery(elem).Get_Val_int(Css.marginBottom, "", "", 'marginBottom');
169
170
171          try {
172            margin.margin = Css.margin;
173          } catch (e) {
174            margin.margin = '"' + margin.left + ' ' + margin.top + ' ' + margin.right + ' ' + margin.bottom + '"';
175          }
176
177
178          padding.left = jQuery(elem).Get_Val_int(Css.paddingLeft, "", "", 'paddingLeft');
179          padding.right = jQuery(elem).Get_Val_int(Css.paddingRight, "", "", 'paddingRight');
180          padding.top = jQuery(elem).Get_Val_int(Css.paddingTop, "", "", 'paddingTop');
181          padding.bottom = jQuery(elem).Get_Val_int(Css.paddingBottom, "", "", 'paddingBottom');
182
183          try {
184            padding.padding = Css.padding;
185          } catch (e) {
186            padding.padding = '"' + padding.left + ' ' + padding.top + ' ' + padding.right + ' ' + padding.bottom + '"';
187          }
188        }
189        if (elem == window) {
190          Cl_Position = { Top: 0, Left: 0 };
191        } else {
192          Cl_Position = jQuery(elem).d_position();
193        }
194        Left = Cl_Position.left;
195        Top = Cl_Position.top;
196
197        results = {
198          position: myposition,
199          top: Top,
200          left: Left,
201          width: width,
202          height: height,
203          right: Left + width,
204          bottom: Top + height,
205          borderwidth: borderwidth,
206          margin: margin,
207          padding: padding,
208          id: id,
209          nodeName: nodeName
210        };
211
212        return results
213      } return results;
214    },
215    /*
216    * Cl_Position de l'object elem
217    */
218    d_position: function () {
219      var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
220
221      if (elem) {
222        offsetParent = this.offsetParent();
223        offset = this.offset();
224
225        if (offsetParent) {
226          if (elem == window) {
227            parentOffset = { top: 0, left: 0 };
228          } else if (typeof (offsetParent.offset) != "undefined")
229            parentOffset = offsetParent.offset();
230          else
231            parentOffset = { top: 0, left: 0 };
232
233          if (!offset)
234            offset = { top: 0, left: 0 };
235
236          offset.top -= num(elem, 'marginTop');
237          offset.left -= num(elem, 'marginLeft');
238
239          if (offsetParent.length > 0) {
240            parentOffset.top += num(offsetParent, 'borderTopWidth');
241            parentOffset.left += num(offsetParent, 'borderLeftWidth');
242          } else {
243            parentOffset = { top: 0, left: 0 };
244
245          }
246        } else {
247          parentOffset = { top: 0, left: 0 };
248        }
249
250        results = {
251          top: Math.ceil(offset.top - parentOffset.top), left: Math.ceil(offset.left - parentOffset.left)
252        };
253      } return results;
254    },
255    /*
256    * offsetParent
257    */
258    offsetParent: function () {
259      var offsetParent = this[0].offsetParent;
260      while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static'))
261        offsetParent = offsetParent.offsetParent;
262      return jQuery(offsetParent);
263    },
264    // getStyles(Obj) Récupérer la valeur CSS
265    getStyles: function (elt) {
266      var element = elt;
267      if (window.getComputedStyle) // Mozilla Firefox & cie
268      {
269        var propriete = window.getComputedStyle(element, null);
270      }
271      else if (element.currentStyle) // Microsoft Internet Explorer
272      {
273
274        var propriete = element.currentStyle;
275      }
276      return propriete;
277    },
278    /*
279    * Get_val_int
280    * params: element,valeur maxi
281    * return: valeur entiere
282    */
283
284    Get_Val_int: function (myObj, Maxi_val, Mini_val, prop) {
285      var val = 0;
286      Maxi_val = parseInt(Maxi_val || "0");
287
288      /*
289      - numérique suivie de px ou % ou pt ou em,
290      - thin, bordure mince,
291      - medium, bordure moyenne (valeur par défaut),
292      - thick, bordure épaisse,
293      - inherit, hérite de son parent (css2).
294      */
295
296      if (!myObj) {
297        return Maxi_val;
298      }
299
300      if (typeof (myObj) == "string") {
301        switch (myObj) {
302          case 'thin':
303            return 1;
304            break;
305          case 'medium':
306            return 2;
307            break;
308          case 'thick':
309            return 4;
310            break;
311          case 'inherit':
312            break;
313          case 'none':
314            return Maxi_val;
315            break;
316          default:
317            break;
318        }
319        el = this.get(0);
320        var reg1 = new RegExp("auto", "g")
321        if (myObj.match(reg1)) {
322          return Maxi_val;
323        }
324        if (prop)
325          myObj = num(el, prop);
326        else {
327          reg1 = new RegExp("px", "g")
328          if (myObj.match(reg1)) {
329
330            myObj = parseInt(myObj);
331
332          } else {
333            reg1 = new RegExp("%", "g")
334            if (myObj.match(reg1)) {
335              val = Math.ceil(myObj * Maxi_val / 100);
336              return val;
337            }
338
339          }
340
341        }
342
343        if (Mini_val)
344          if (myObj < Mini_val) return Maxi_val;
345
346
347        val = parseInt(myObj);
348
349      } else {
350        val = parseInt(myObj);
351      }
352      if (typeof (val) == "NaN") return Maxi_val;
353      return val;
354    }
355  });
356  /*
357  *
358  */
359  function num(el, prop) {
360    return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
361  };
362
363  myjQuery = jQuery;
364  my$ = $;
365
366})(jQuery);
Note: See TracBrowser for help on using the repository browser.