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

Last change on this file since 31937 was 22218, checked in by cljosse, 11 years ago

[extensions] Autosize update to piwigo 2.5 (fix bugs)

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