]> git.sur5r.net Git - contagged/blob - scripts/interface/iutil.js
Make it possible to disable private contacts
[contagged] / scripts / interface / iutil.js
1 /**
2  * Interface Elements for jQuery
3  * utility function
4  *
5  * http://interface.eyecon.ro
6  *
7  * Copyright (c) 2006 Stefan Petre
8  * Dual licensed under the MIT (MIT-LICENSE.txt)
9  * and GPL (GPL-LICENSE.txt) licenses.
10  *
11  *
12  */
13
14 jQuery.iUtil = {
15         getPosition : function(e)
16         {
17                 var x = 0;
18                 var y = 0;
19                 var es = e.style;
20                 var restoreStyles = false;
21                 if (jQuery(e).css('display') == 'none') {
22                         var oldVisibility = es.visibility;
23                         var oldPosition = es.position;
24                         restoreStyles = true;
25                         es.visibility = 'hidden';
26                         es.display = 'block';
27                         es.position = 'absolute';
28                 }
29                 var el = e;
30                 while (el){
31                         x += el.offsetLeft + (el.currentStyle && !jQuery.browser.opera ?parseInt(el.currentStyle.borderLeftWidth)||0:0);
32                         y += el.offsetTop + (el.currentStyle && !jQuery.browser.opera ?parseInt(el.currentStyle.borderTopWidth)||0:0);
33                         el = el.offsetParent;
34                 }
35                 el = e;
36                 while (el && el.tagName  && el.tagName.toLowerCase() != 'body')
37                 {
38                         x -= el.scrollLeft||0;
39                         y -= el.scrollTop||0;
40                         el = el.parentNode;
41                 }
42                 if (restoreStyles == true) {
43                         es.display = 'none';
44                         es.position = oldPosition;
45                         es.visibility = oldVisibility;
46                 }
47                 return {x:x, y:y};
48         },
49         getPositionLite : function(el)
50         {
51                 var x = 0, y = 0;
52                 while(el) {
53                         x += el.offsetLeft || 0;
54                         y += el.offsetTop || 0;
55                         el = el.offsetParent;
56                 }
57                 return {x:x, y:y};
58         },
59         getSize : function(e)
60         {
61                 var w = jQuery.css(e,'width');
62                 var h = jQuery.css(e,'height');
63                 var wb = 0;
64                 var hb = 0;
65                 var es = e.style;
66                 if (jQuery(e).css('display') != 'none') {
67                         wb = e.offsetWidth;
68                         hb = e.offsetHeight;
69                 } else {
70                         var oldVisibility = es.visibility;
71                         var oldPosition = es.position;
72                         es.visibility = 'hidden';
73                         es.display = 'block';
74                         es.position = 'absolute';
75                         wb = e.offsetWidth;
76                         hb = e.offsetHeight;
77                         es.display = 'none';
78                         es.position = oldPosition;
79                         es.visibility = oldVisibility;
80                 }
81                 return {w:w, h:h, wb:wb, hb:hb};
82         },
83         getSizeLite : function(el)
84         {
85                 return {
86                         wb:el.offsetWidth||0,
87                         hb:el.offsetHeight||0
88                 };
89         },
90         getClient : function(e)
91         {
92                 var h, w, de;
93                 if (e) {
94                         w = e.clientWidth;
95                         h = e.clientHeight;
96                 } else {
97                         de = document.documentElement;
98                         w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
99                         h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
100                 }
101                 return {w:w,h:h};
102         },
103         getScroll : function (e)
104         {
105                 var t=0, l=0, w=0, h=0, iw=0, ih=0;
106                 if (e && e.nodeName.toLowerCase() != 'body') {
107                         t = e.scrollTop;
108                         l = e.scrollLeft;
109                         w = e.scrollWidth;
110                         h = e.scrollHeight;
111                         iw = 0;
112                         ih = 0;
113                 } else  {
114                         if (document.documentElement) {
115                                 t = document.documentElement.scrollTop;
116                                 l = document.documentElement.scrollLeft;
117                                 w = document.documentElement.scrollWidth;
118                                 h = document.documentElement.scrollHeight;
119                         } else if (document.body) {
120                                 t = document.body.scrollTop;
121                                 l = document.body.scrollLeft;
122                                 w = document.body.scrollWidth;
123                                 h = document.body.scrollHeight;
124                         }
125                         iw = self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0;
126                         ih = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;
127                 }
128                 return { t: t, l: l, w: w, h: h, iw: iw, ih: ih };
129         },
130         getMargins : function(e, toInteger)
131         {
132                 var el = jQuery(e);
133                 var t = el.css('marginTop') || '';
134                 var r = el.css('marginRight') || '';
135                 var b = el.css('marginBottom') || '';
136                 var l = el.css('marginLeft') || '';
137                 if (toInteger)
138                         return {
139                                 t: parseInt(t)||0,
140                                 r: parseInt(r)||0,
141                                 b: parseInt(b)||0,
142                                 l: parseInt(l)
143                         };
144                 else
145                         return {t: t, r: r,     b: b, l: l};
146         },
147         getPadding : function(e, toInteger)
148         {
149                 var el = jQuery(e);
150                 var t = el.css('paddingTop') || '';
151                 var r = el.css('paddingRight') || '';
152                 var b = el.css('paddingBottom') || '';
153                 var l = el.css('paddingLeft') || '';
154                 if (toInteger)
155                         return {
156                                 t: parseInt(t)||0,
157                                 r: parseInt(r)||0,
158                                 b: parseInt(b)||0,
159                                 l: parseInt(l)
160                         };
161                 else
162                         return {t: t, r: r,     b: b, l: l};
163         },
164         getBorder : function(e, toInteger)
165         {
166                 var el = jQuery(e);
167                 var t = el.css('borderTopWidth') || '';
168                 var r = el.css('borderRightWidth') || '';
169                 var b = el.css('borderBottomWidth') || '';
170                 var l = el.css('borderLeftWidth') || '';
171                 if (toInteger)
172                         return {
173                                 t: parseInt(t)||0,
174                                 r: parseInt(r)||0,
175                                 b: parseInt(b)||0,
176                                 l: parseInt(l)||0
177                         };
178                 else
179                         return {t: t, r: r,     b: b, l: l};
180         },
181         getPointer : function(event)
182         {
183                 var x = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;
184                 var y = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) || 0;
185                 return {x:x, y:y};
186         },
187         traverseDOM : function(nodeEl, func)
188         {
189                 func(nodeEl);
190                 nodeEl = nodeEl.firstChild;
191                 while(nodeEl){
192                         jQuery.iUtil.traverseDOM(nodeEl, func);
193                         nodeEl = nodeEl.nextSibling;
194                 }
195         },
196         purgeEvents : function(nodeEl)
197         {
198                 jQuery.iUtil.traverseDOM(
199                         nodeEl,
200                         function(el)
201                         {
202                                 for(var attr in el){
203                                         if(typeof el[attr] === 'function') {
204                                                 el[attr] = null;
205                                         }
206                                 }
207                         }
208                 );
209         },
210         centerEl : function(el, axis)
211         {
212                 var clientScroll = jQuery.iUtil.getScroll();
213                 var windowSize = jQuery.iUtil.getSize(el);
214                 if (!axis || axis == 'vertically')
215                         jQuery(el).css(
216                                 {
217                                         top: clientScroll.t + ((Math.max(clientScroll.h,clientScroll.ih) - clientScroll.t - windowSize.hb)/2) + 'px'
218                                 }
219                         );
220                 if (!axis || axis == 'horizontally')
221                         jQuery(el).css(
222                                 {
223                                         left:   clientScroll.l + ((Math.max(clientScroll.w,clientScroll.iw) - clientScroll.l - windowSize.wb)/2) + 'px'
224                                 }
225                         );
226         },
227         fixPNG : function (el, emptyGIF) {
228                 var images = jQuery('img[@src*="png"]', el||document), png;
229                 images.each( function() {
230                         png = this.src;                         
231                         this.src = emptyGIF;
232                         this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "')";
233                 });
234         }
235 };
236
237 // Helper function to support older browsers!
238 [].indexOf || (Array.prototype.indexOf = function(v, n){
239         n = (n == null) ? 0 : n;
240         var m = this.length;
241         for (var i=n; i<m; i++)
242                 if (this[i] == v)
243                         return i;
244         return -1;
245 });