dom-geometry.js 4.12 KB
//>>built
define(["./sniff","./_base/window","./dom","./dom-style"],function(n,l,m,h){function p(a,b,c,d,e,f){f=f||"px";a=a.style;isNaN(b)||(a.left=b+f);isNaN(c)||(a.top=c+f);0<=d&&(a.width=d+f);0<=e&&(a.height=e+f)}function q(a){return"button"==a.tagName.toLowerCase()||"input"==a.tagName.toLowerCase()&&"button"==(a.getAttribute("type")||"").toLowerCase()}function r(a){return"border-box"==e.boxModel||"table"==a.tagName.toLowerCase()||q(a)}var e={boxModel:"content-box"};n("ie")&&(e.boxModel="BackCompat"==document.compatMode?
"border-box":"content-box");e.getPadExtents=function(a,b){a=m.byId(a);var c=b||h.getComputedStyle(a),d=h.toPixelValue;b=d(a,c.paddingLeft);var e=d(a,c.paddingTop),f=d(a,c.paddingRight);a=d(a,c.paddingBottom);return{l:b,t:e,r:f,b:a,w:b+f,h:e+a}};e.getBorderExtents=function(a,b){a=m.byId(a);var c=h.toPixelValue,d=b||h.getComputedStyle(a);b="none"!=d.borderLeftStyle?c(a,d.borderLeftWidth):0;var e="none"!=d.borderTopStyle?c(a,d.borderTopWidth):0,f="none"!=d.borderRightStyle?c(a,d.borderRightWidth):0;
a="none"!=d.borderBottomStyle?c(a,d.borderBottomWidth):0;return{l:b,t:e,r:f,b:a,w:b+f,h:e+a}};e.getPadBorderExtents=function(a,b){a=m.byId(a);var c=b||h.getComputedStyle(a);b=e.getPadExtents(a,c);a=e.getBorderExtents(a,c);return{l:b.l+a.l,t:b.t+a.t,r:b.r+a.r,b:b.b+a.b,w:b.w+a.w,h:b.h+a.h}};e.getMarginExtents=function(a,b){a=m.byId(a);var c=b||h.getComputedStyle(a),d=h.toPixelValue;b=d(a,c.marginLeft);var e=d(a,c.marginTop),f=d(a,c.marginRight);a=d(a,c.marginBottom);return{l:b,t:e,r:f,b:a,w:b+f,h:e+
a}};e.getMarginBox=function(a,b){a=m.byId(a);b=b||h.getComputedStyle(a);b=e.getMarginExtents(a,b);var c=a.offsetLeft-b.l,d=a.offsetTop-b.t,g=a.parentNode,f=h.toPixelValue;8==n("ie")&&g&&(g=h.getComputedStyle(g),c-="none"!=g.borderLeftStyle?f(a,g.borderLeftWidth):0,d-="none"!=g.borderTopStyle?f(a,g.borderTopWidth):0);return{l:c,t:d,w:a.offsetWidth+b.w,h:a.offsetHeight+b.h}};e.getContentBox=function(a,b){a=m.byId(a);var c=b||h.getComputedStyle(a);b=a.clientWidth;var d,g=e.getPadExtents(a,c);d=e.getBorderExtents(a,
c);var c=a.offsetLeft+g.l+d.l,f=a.offsetTop+g.t+d.t;b?d=a.clientHeight:(b=a.offsetWidth-d.w,d=a.offsetHeight-d.h);if(8==n("ie")){var k=a.parentNode,l=h.toPixelValue;k&&(k=h.getComputedStyle(k),c-="none"!=k.borderLeftStyle?l(a,k.borderLeftWidth):0,f-="none"!=k.borderTopStyle?l(a,k.borderTopWidth):0)}return{l:c,t:f,w:b-g.w,h:d-g.h}};e.setContentSize=function(a,b,c){a=m.byId(a);var d=b.w;b=b.h;r(a)&&(c=e.getPadBorderExtents(a,c),0<=d&&(d+=c.w),0<=b&&(b+=c.h));p(a,NaN,NaN,d,b)};var t={l:0,t:0,w:0,h:0};
e.setMarginBox=function(a,b,c){a=m.byId(a);var d=c||h.getComputedStyle(a);c=b.w;var g=b.h,f=r(a)?t:e.getPadBorderExtents(a,d),d=e.getMarginExtents(a,d);if(n("webkit")&&q(a)){var k=a.style;0<=c&&!k.width&&(k.width="4px");0<=g&&!k.height&&(k.height="4px")}0<=c&&(c=Math.max(c-f.w-d.w,0));0<=g&&(g=Math.max(g-f.h-d.h,0));p(a,b.l,b.t,c,g)};e.isBodyLtr=function(a){a=a||l.doc;return"ltr"==(l.body(a).dir||a.documentElement.dir||"ltr").toLowerCase()};e.docScroll=function(a){a=a||l.doc;var b=l.doc.parentWindow||
l.doc.defaultView;return"pageXOffset"in b?{x:b.pageXOffset,y:b.pageYOffset}:(b=a.documentElement)&&{x:e.fixIeBiDiScrollLeft(b.scrollLeft||0,a),y:b.scrollTop||0}};e.getIeDocumentElementOffset=function(a){return{x:0,y:0}};e.fixIeBiDiScrollLeft=function(a,b){b=b||l.doc;var c=n("ie");if(c&&!e.isBodyLtr(b)){b=b.documentElement;var d=l.global;6==c&&d.frameElement&&b.scrollHeight>b.clientHeight&&(a+=b.clientLeft);return 8>c?a+b.clientWidth-b.scrollWidth:-a}return a};e.position=function(a,b){a=m.byId(a);
l.body(a.ownerDocument);var c=a.getBoundingClientRect(),c={x:c.left,y:c.top,w:c.right-c.left,h:c.bottom-c.top};9>n("ie")&&(c.x-=0,c.y-=0);b&&(a=e.docScroll(a.ownerDocument),c.x+=a.x,c.y+=a.y);return c};e.getMarginSize=function(a,b){a=m.byId(a);b=e.getMarginExtents(a,b||h.getComputedStyle(a));a=a.getBoundingClientRect();return{w:a.right-a.left+b.w,h:a.bottom-a.top+b.h}};e.normalizeEvent=function(a){"layerX"in a||(a.layerX=a.offsetX,a.layerY=a.offsetY);if(!("pageX"in a)){var b=a.target,b=b&&b.ownerDocument||
document,c=b.documentElement;a.pageX=a.clientX+e.fixIeBiDiScrollLeft(c.scrollLeft||0,b);a.pageY=a.clientY+(c.scrollTop||0)}};return e});