utils.js
252 lines
| 1 | /** |
| 2 | * |
| 3 | * Utilities |
| 4 | * Author: Stefan Petre www.eyecon.ro |
| 5 | * |
| 6 | */ |
| 7 | (function($) { |
| 8 | EYE.extend({ |
| 9 | getPosition : function(e, forceIt) |
| 10 | { |
| 11 | var x = 0; |
| 12 | var y = 0; |
| 13 | var es = e.style; |
| 14 | var restoreStyles = false; |
| 15 | if (forceIt && jQuery.curCSS(e,'display') == 'none') { |
| 16 | var oldVisibility = es.visibility; |
| 17 | var oldPosition = es.position; |
| 18 | restoreStyles = true; |
| 19 | es.visibility = 'hidden'; |
| 20 | es.display = 'block'; |
| 21 | es.position = 'absolute'; |
| 22 | } |
| 23 | var el = e; |
| 24 | if (el.getBoundingClientRect) { // IE |
| 25 | var box = el.getBoundingClientRect(); |
| 26 | x = box.left + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) - 2; |
| 27 | y = box.top + Math.max(document.documentElement.scrollTop, document.body.scrollTop) - 2; |
| 28 | } else { |
| 29 | x = el.offsetLeft; |
| 30 | y = el.offsetTop; |
| 31 | el = el.offsetParent; |
| 32 | if (e != el) { |
| 33 | while (el) { |
| 34 | x += el.offsetLeft; |
| 35 | y += el.offsetTop; |
| 36 | el = el.offsetParent; |
| 37 | } |
| 38 | } |
| 39 | if (jQuery.browser.safari && jQuery.curCSS(e, 'position') == 'absolute' ) { |
| 40 | x -= document.body.offsetLeft; |
| 41 | y -= document.body.offsetTop; |
| 42 | } |
| 43 | el = e.parentNode; |
| 44 | while (el && el.tagName.toUpperCase() != 'BODY' && el.tagName.toUpperCase() != 'HTML') |
| 45 | { |
| 46 | if (jQuery.curCSS(el, 'display') != 'inline') { |
| 47 | x -= el.scrollLeft; |
| 48 | y -= el.scrollTop; |
| 49 | } |
| 50 | el = el.parentNode; |
| 51 | } |
| 52 | } |
| 53 | if (restoreStyles == true) { |
| 54 | es.display = 'none'; |
| 55 | es.position = oldPosition; |
| 56 | es.visibility = oldVisibility; |
| 57 | } |
| 58 | return {x:x, y:y}; |
| 59 | }, |
| 60 | getSize : function(e) |
| 61 | { |
| 62 | var w = parseInt(jQuery.curCSS(e,'width'), 10); |
| 63 | var h = parseInt(jQuery.curCSS(e,'height'), 10); |
| 64 | var wb = 0; |
| 65 | var hb = 0; |
| 66 | if (jQuery.curCSS(e, 'display') != 'none') { |
| 67 | wb = e.offsetWidth; |
| 68 | hb = e.offsetHeight; |
| 69 | } else { |
| 70 | var es = e.style; |
| 71 | var oldVisibility = es.visibility; |
| 72 | var oldPosition = es.position; |
| 73 | es.visibility = 'hidden'; |
| 74 | es.display = 'block'; |
| 75 | es.position = 'absolute'; |
| 76 | wb = e.offsetWidth; |
| 77 | hb = e.offsetHeight; |
| 78 | es.display = 'none'; |
| 79 | es.position = oldPosition; |
| 80 | es.visibility = oldVisibility; |
| 81 | } |
| 82 | return {w:w, h:h, wb:wb, hb:hb}; |
| 83 | }, |
| 84 | getClient : function(e) |
| 85 | { |
| 86 | var h, w; |
| 87 | if (e) { |
| 88 | w = e.clientWidth; |
| 89 | h = e.clientHeight; |
| 90 | } else { |
| 91 | var de = document.documentElement; |
| 92 | w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth; |
| 93 | h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight; |
| 94 | } |
| 95 | return {w:w,h:h}; |
| 96 | }, |
| 97 | getScroll : function (e) |
| 98 | { |
| 99 | var t=0, l=0, w=0, h=0, iw=0, ih=0; |
| 100 | if (e && e.nodeName.toLowerCase() != 'body') { |
| 101 | t = e.scrollTop; |
| 102 | l = e.scrollLeft; |
| 103 | w = e.scrollWidth; |
| 104 | h = e.scrollHeight; |
| 105 | } else { |
| 106 | if (document.documentElement) { |
| 107 | t = document.documentElement.scrollTop; |
| 108 | l = document.documentElement.scrollLeft; |
| 109 | w = document.documentElement.scrollWidth; |
| 110 | h = document.documentElement.scrollHeight; |
| 111 | } else if (document.body) { |
| 112 | t = document.body.scrollTop; |
| 113 | l = document.body.scrollLeft; |
| 114 | w = document.body.scrollWidth; |
| 115 | h = document.body.scrollHeight; |
| 116 | } |
| 117 | if (typeof pageYOffset != 'undefined') { |
| 118 | t = pageYOffset; |
| 119 | l = pageXOffset; |
| 120 | } |
| 121 | iw = self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0; |
| 122 | ih = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0; |
| 123 | } |
| 124 | return { t: t, l: l, w: w, h: h, iw: iw, ih: ih }; |
| 125 | }, |
| 126 | getMargins : function(e, toInteger) |
| 127 | { |
| 128 | var t = jQuery.curCSS(e,'marginTop') || ''; |
| 129 | var r = jQuery.curCSS(e,'marginRight') || ''; |
| 130 | var b = jQuery.curCSS(e,'marginBottom') || ''; |
| 131 | var l = jQuery.curCSS(e,'marginLeft') || ''; |
| 132 | if (toInteger) |
| 133 | return { |
| 134 | t: parseInt(t, 10)||0, |
| 135 | r: parseInt(r, 10)||0, |
| 136 | b: parseInt(b, 10)||0, |
| 137 | l: parseInt(l, 10) |
| 138 | }; |
| 139 | else |
| 140 | return {t: t, r: r, b: b, l: l}; |
| 141 | }, |
| 142 | getPadding : function(e, toInteger) |
| 143 | { |
| 144 | var t = jQuery.curCSS(e,'paddingTop') || ''; |
| 145 | var r = jQuery.curCSS(e,'paddingRight') || ''; |
| 146 | var b = jQuery.curCSS(e,'paddingBottom') || ''; |
| 147 | var l = jQuery.curCSS(e,'paddingLeft') || ''; |
| 148 | if (toInteger) |
| 149 | return { |
| 150 | t: parseInt(t, 10)||0, |
| 151 | r: parseInt(r, 10)||0, |
| 152 | b: parseInt(b, 10)||0, |
| 153 | l: parseInt(l, 10) |
| 154 | }; |
| 155 | else |
| 156 | return {t: t, r: r, b: b, l: l}; |
| 157 | }, |
| 158 | getBorder : function(e, toInteger) |
| 159 | { |
| 160 | var t = jQuery.curCSS(e,'borderTopWidth') || ''; |
| 161 | var r = jQuery.curCSS(e,'borderRightWidth') || ''; |
| 162 | var b = jQuery.curCSS(e,'borderBottomWidth') || ''; |
| 163 | var l = jQuery.curCSS(e,'borderLeftWidth') || ''; |
| 164 | if (toInteger) |
| 165 | return { |
| 166 | t: parseInt(t, 10)||0, |
| 167 | r: parseInt(r, 10)||0, |
| 168 | b: parseInt(b, 10)||0, |
| 169 | l: parseInt(l, 10)||0 |
| 170 | }; |
| 171 | else |
| 172 | return {t: t, r: r, b: b, l: l}; |
| 173 | }, |
| 174 | traverseDOM : function(nodeEl, func) |
| 175 | { |
| 176 | func(nodeEl); |
| 177 | nodeEl = nodeEl.firstChild; |
| 178 | while(nodeEl){ |
| 179 | EYE.traverseDOM(nodeEl, func); |
| 180 | nodeEl = nodeEl.nextSibling; |
| 181 | } |
| 182 | }, |
| 183 | getInnerWidth : function(el, scroll) { |
| 184 | var offsetW = el.offsetWidth; |
| 185 | return scroll ? Math.max(el.scrollWidth,offsetW) - offsetW + el.clientWidth:el.clientWidth; |
| 186 | }, |
| 187 | getInnerHeight : function(el, scroll) { |
| 188 | var offsetH = el.offsetHeight; |
| 189 | return scroll ? Math.max(el.scrollHeight,offsetH) - offsetH + el.clientHeight:el.clientHeight; |
| 190 | }, |
| 191 | getExtraWidth : function(el) { |
| 192 | if($.boxModel) |
| 193 | return (parseInt($.curCSS(el, 'paddingLeft'))||0) |
| 194 | + (parseInt($.curCSS(el, 'paddingRight'))||0) |
| 195 | + (parseInt($.curCSS(el, 'borderLeftWidth'))||0) |
| 196 | + (parseInt($.curCSS(el, 'borderRightWidth'))||0); |
| 197 | return 0; |
| 198 | }, |
| 199 | getExtraHeight : function(el) { |
| 200 | if($.boxModel) |
| 201 | return (parseInt($.curCSS(el, 'paddingTop'))||0) |
| 202 | + (parseInt($.curCSS(el, 'paddingBottom'))||0) |
| 203 | + (parseInt($.curCSS(el, 'borderTopWidth'))||0) |
| 204 | + (parseInt($.curCSS(el, 'borderBottomWidth'))||0); |
| 205 | return 0; |
| 206 | }, |
| 207 | isChildOf: function(parentEl, el, container) { |
| 208 | if (parentEl == el) { |
| 209 | return true; |
| 210 | } |
| 211 | if (!el || !el.nodeType || el.nodeType != 1) { |
| 212 | return false; |
| 213 | } |
| 214 | if (parentEl.contains && !$.browser.safari) { |
| 215 | return parentEl.contains(el); |
| 216 | } |
| 217 | if ( parentEl.compareDocumentPosition ) { |
| 218 | return !!(parentEl.compareDocumentPosition(el) & 16); |
| 219 | } |
| 220 | var prEl = el.parentNode; |
| 221 | while(prEl && prEl != container) { |
| 222 | if (prEl == parentEl) |
| 223 | return true; |
| 224 | prEl = prEl.parentNode; |
| 225 | } |
| 226 | return false; |
| 227 | }, |
| 228 | centerEl : function(el, axis) |
| 229 | { |
| 230 | var clientScroll = EYE.getScroll(); |
| 231 | var size = EYE.getSize(el); |
| 232 | if (!axis || axis == 'vertically') |
| 233 | $(el).css( |
| 234 | { |
| 235 | top: clientScroll.t + ((Math.min(clientScroll.h,clientScroll.ih) - size.hb)/2) + 'px' |
| 236 | } |
| 237 | ); |
| 238 | if (!axis || axis == 'horizontally') |
| 239 | $(el).css( |
| 240 | { |
| 241 | left: clientScroll.l + ((Math.min(clientScroll.w,clientScroll.iw) - size.wb)/2) + 'px' |
| 242 | } |
| 243 | ); |
| 244 | } |
| 245 | }); |
| 246 | if (!$.easing.easeout) { |
| 247 | $.easing.easeout = function(p, n, firstNum, delta, duration) { |
| 248 | return -delta * ((n=n/duration-1)*n*n*n - 1) + firstNum; |
| 249 | }; |
| 250 | } |
| 251 | |
| 252 | })(jQuery); |