WordPress/wp-includes/js/imgareaselect/jquery.imgareaselect.js

1 line
9.1 KiB
JavaScript

(function(e){var b=Math.abs,a=Math.max,d=Math.min,c=Math.round;function f(){return e("<div/>")}e.imgAreaSelect=function(q,S){var aw=e(q),U,ar=f(),af=f(),H=f().add(f()).add(f()).add(f()),Y=f().add(f()).add(f()).add(f()),L=e([]),R,n,p,az,N,j,A,M,B=0,ad="absolute",Q,P,aa,Z,V=10,I,T,K,y,aA,x,ay,v={x1:0,y1:0,x2:0,y2:0,width:0,height:0},l,aq,am,ag,ac,an,u;function G(h){return h+az.left-M.left}function F(h){return h+az.top-M.top}function E(h){return h-az.left+M.left}function z(h){return h-az.top+M.top}function ak(h){return h.pageX-M.left}function ai(h){return h.pageY-M.top}function D(h){var o=h||aa,i=h||Z;return{x1:c(v.x1*o),y1:c(v.y1*i),x2:c(v.x2*o),y2:c(v.y2*i),width:c(v.x2*o)-c(v.x1*o),height:c(v.y2*i)-c(v.y1*i)}}function ae(i,w,h,o,aB){var aD=aB||aa,aC=aB||Z;v={x1:c(i/aD),y1:c(w/aC),x2:c(h/aD),y2:c(o/aC)};v.width=(h=G(v.x2))-(i=G(v.x1));v.height=(o=G(v.y2))-(w=G(v.y1))}function ao(){if(!aw.width()){return}az={left:c(aw.offset().left),top:c(aw.offset().top)};N=aw.width();j=aw.height();if(e().jquery=="1.3.2"&&e.browser.safari&&ad=="fixed"){az.top+=a(document.documentElement.scrollTop,e("body").scrollTop());az.left+=a(document.documentElement.scrollLeft,e("body").scrollLeft())}M=e.inArray(A.css("position"),["absolute","relative"])+1?{left:c(A.offset().left)-A.scrollLeft(),top:c(A.offset().top)-A.scrollTop()}:ad=="fixed"?{left:e(document).scrollLeft(),top:e(document).scrollTop()}:{left:0,top:0};n=G(0);p=F(0)}function X(h){if(!K){return}ar.css({left:G(v.x1),top:F(v.y1)}).add(af).width(ac=v.width).height(an=v.height);af.add(H).add(L).css({left:0,top:0});H.width(a(ac-H.outerWidth()+H.innerWidth(),0)).height(a(an-H.outerHeight()+H.innerHeight(),0));e(Y[0]).css({left:n,top:p,width:v.x1,height:j});e(Y[1]).css({left:n+v.x1,top:p,width:ac,height:v.y1});e(Y[2]).css({left:n+v.x2,top:p,width:N-v.x2,height:j});e(Y[3]).css({left:n+v.x1,top:p+v.y2,width:ac,height:j-v.y2});ac-=L.outerWidth();an-=L.outerHeight();switch(L.length){case 8:e(L[4]).css({left:ac/2});e(L[5]).css({left:ac,top:an/2});e(L[6]).css({left:ac/2,top:an});e(L[7]).css({top:an/2});case 4:L.slice(1,3).css({left:ac});L.slice(2,4).css({top:an})}if(h!==false){if(e.imgAreaSelect.keyPress!=at){e(document).unbind(e.imgAreaSelect.keyPress,e.imgAreaSelect.onKeyPress)}if(S.keys){e(document)[e.imgAreaSelect.keyPress](e.imgAreaSelect.onKeyPress=at)}}if(e.browser.msie&&H.outerWidth()-H.innerWidth()==2){H.css("margin",0);setTimeout(function(){H.css("margin","auto")},0)}}function t(h){ao();X(h);y=G(v.x1);aA=F(v.y1);x=G(v.x2);ay=F(v.y2)}function ah(h,i){S.fadeSpeed?h.fadeOut(S.fadeSpeed,i):h.hide()}function C(i){var h=E(ak(i))-v.x1,o=z(ai(i))-v.y1;if(!u){ao();u=true;ar.one("mouseout",function(){u=false})}I="";if(S.resizable){if(o<=V){I="n"}else{if(o>=v.height-V){I="s"}}if(h<=V){I+="w"}else{if(h>=v.width-V){I+="e"}}}ar.css("cursor",I?I+"-resize":S.movable?"move":"");if(R){R.toggle()}}function aj(h){e("body").css("cursor","");if(S.autoHide||v.width*v.height==0){ah(ar.add(Y),function(){e(this).hide()})}S.onSelectEnd(q,D());e(document).unbind("mousemove",ab);ar.mousemove(C)}function s(h){if(h.which!=1){return false}ao();if(I){e("body").css("cursor",I+"-resize");y=G(v[/w/.test(I)?"x2":"x1"]);aA=F(v[/n/.test(I)?"y2":"y1"]);e(document).mousemove(ab).one("mouseup",aj);ar.unbind("mousemove",C)}else{if(S.movable){Q=n+v.x1-ak(h);P=p+v.y1-ai(h);ar.unbind("mousemove",C);e(document).mousemove(g).one("mouseup",function(){S.onSelectEnd(q,D());e(document).unbind("mousemove",g);ar.mousemove(C)})}else{aw.mousedown(h)}}return false}function r(){x=a(n,d(n+N,y+b(ay-aA)*T*(x>y||-1)));ay=c(a(p,d(p+j,aA+b(x-y)/T*(ay>aA||-1))));x=c(x)}function al(){ay=a(p,d(p+j,aA+b(x-y)/T*(ay>aA||-1)));x=c(a(n,d(n+N,y+b(ay-aA)*T*(x>y||-1))));ay=c(ay)}function av(){if(b(x-y)<S.minWidth){x=y-S.minWidth*(x<y||-1);if(x<n){y=n+S.minWidth}else{if(x>n+N){y=n+N-S.minWidth}}}if(b(ay-aA)<S.minHeight){ay=aA-S.minHeight*(ay<aA||-1);if(ay<p){aA=p+S.minHeight}else{if(ay>p+j){aA=p+j-S.minHeight}}}x=a(n,d(x,n+N));ay=a(p,d(ay,p+j));if(T){if(b(x-y)/T>b(ay-aA)){al()}else{r()}}if(b(x-y)>S.maxWidth){x=y-S.maxWidth*(x<y||-1);if(T){al()}}if(b(ay-aA)>S.maxHeight){ay=aA-S.maxHeight*(ay<aA||-1);if(T){r()}}v={x1:E(d(y,x)),x2:E(a(y,x)),y1:z(d(aA,ay)),y2:z(a(aA,ay)),width:b(x-y),height:b(ay-aA)};X();S.onSelectChange(q,D())}function ab(h){x=I==""||/w|e/.test(I)||T?ak(h):G(v.x2);ay=I==""||/n|s/.test(I)||T?ai(h):F(v.y2);av();return false}function O(h,i){x=(y=h)+v.width;ay=(aA=i)+v.height;v=e.extend(v,{x1:E(y),y1:z(aA),x2:E(x),y2:z(ay)});X();S.onSelectChange(q,D())}function g(h){y=a(n,d(Q+ak(h),n+N-v.width));aA=a(p,d(P+ai(h),p+j-v.height));O(y,aA);h.preventDefault();return false}function ax(){ao();x=y;ay=aA;av();I="";if(Y.is(":not(:visible)")){ar.add(Y).hide().fadeIn(S.fadeSpeed||0)}K=true;e(document).unbind("mouseup",ap).mousemove(ab).one("mouseup",aj);ar.unbind("mousemove",C);S.onSelectStart(q,D())}function ap(){e(document).unbind("mousemove",ax);ah(ar.add(Y));v={x1:E(y),y1:z(aA),x2:E(y),y2:z(aA),width:0,height:0};S.onSelectChange(q,D());S.onSelectEnd(q,D())}function m(h){if(h.which!=1||Y.is(":animated")){return false}ao();Q=y=ak(h);P=aA=ai(h);e(document).one("mousemove",ax).one("mouseup",ap);return false}function W(){t(false)}function au(){U=true;J(S=e.extend({classPrefix:"imgareaselect",movable:true,resizable:true,parent:"body",onInit:function(){},onSelectStart:function(){},onSelectChange:function(){},onSelectEnd:function(){}},S));ar.add(Y).css({visibility:""});if(S.show){K=true;ao();X();ar.add(Y).hide().fadeIn(S.fadeSpeed||0)}setTimeout(function(){S.onInit(q,D())},0)}var at=function(w){var h=S.keys,aB,o,i=w.keyCode||w.which;aB=!isNaN(h.alt)&&(w.altKey||w.originalEvent.altKey)?h.alt:!isNaN(h.ctrl)&&w.ctrlKey?h.ctrl:!isNaN(h.shift)&&w.shiftKey?h.shift:!isNaN(h.arrows)?h.arrows:10;if(h.arrows=="resize"||(h.shift=="resize"&&w.shiftKey)||(h.ctrl=="resize"&&w.ctrlKey)||(h.alt=="resize"&&(w.altKey||w.originalEvent.altKey))){switch(i){case 37:aB=-aB;case 39:o=a(y,x);y=d(y,x);x=a(o+aB,y);if(T){al()}break;case 38:aB=-aB;case 40:o=a(aA,ay);aA=d(aA,ay);ay=a(o+aB,aA);if(T){r()}break;default:return}av()}else{y=d(y,x);aA=d(aA,ay);switch(i){case 37:O(a(y-aB,n),aA);break;case 38:O(y,a(aA-aB,p));break;case 39:O(y+d(aB,N-E(x)),aA);break;case 40:O(y,aA+d(aB,j-z(ay)));break;default:return}}return false};function k(h,i){for(option in i){if(S[option]!==undefined){h.css(i[option],S[option])}}}function J(h){if(h.parent){(A=e(h.parent)).append(ar.add(Y))}S=e.extend(S,h);ao();if(h.handles!=null){L.remove();L=e([]);am=h.handles?h.handles=="corners"?4:8:0;while(am--){L=L.add(f())}L.addClass(S.classPrefix+"-handle").css({position:"absolute",fontSize:0,zIndex:B+1||1});if(!parseInt(L.css("width"))){L.width(5).height(5)}if(ag=S.borderWidth){L.css({borderWidth:ag,borderStyle:"solid"})}k(L,{borderColor1:"border-color",borderColor2:"background-color",borderOpacity:"opacity"})}aa=S.imageWidth/N||1;Z=S.imageHeight/j||1;if(h.x1!=null){ae(h.x1,h.y1,h.x2,h.y2);h.show=!h.hide}if(h.keys){S.keys=e.extend({shift:1,ctrl:"resize"},h.keys)}Y.addClass(S.classPrefix+"-outer");af.addClass(S.classPrefix+"-selection");for(am=0;am++<4;){e(H[am-1]).addClass(S.classPrefix+"-border"+am)}k(af,{selectionColor:"background-color",selectionOpacity:"opacity"});k(H,{borderOpacity:"opacity",borderWidth:"border-width"});k(Y,{outerColor:"background-color",outerOpacity:"opacity"});if(ag=S.borderColor1){e(H[0]).css({borderStyle:"solid",borderColor:ag})}if(ag=S.borderColor2){e(H[1]).css({borderStyle:"dashed",borderColor:ag})}ar.append(af.add(H).add(L).add(R));if(e.browser.msie){if(ag=Y.css("filter").match(/opacity=([0-9]+)/)){Y.css("opacity",ag[1]/100)}if(ag=H.css("filter").match(/opacity=([0-9]+)/)){H.css("opacity",ag[1]/100)}}if(h.hide){ah(ar.add(Y))}else{if(h.show&&U){K=true;ar.add(Y).fadeIn(S.fadeSpeed||0);t()}}T=(aq=(S.aspectRatio||"").split(/:/))[0]/aq[1];if(S.disable||S.enable===false){ar.unbind("mousemove",C).unbind("mousedown",s);aw.add(Y).unbind("mousedown",m);e(window).unbind("resize",W);aw.add(aw.parents()).unbind("scroll",W)}else{if(S.enable||S.disable===false){if(S.resizable||S.movable){ar.mousemove(C).mousedown(s)}if(!S.persistent){aw.add(Y).mousedown(m)}e(window).resize(W);aw.add(aw.parents()).scroll(W)}}S.enable=S.disable=undefined}this.getOptions=function(){return S};this.setOptions=J;this.getSelection=D;this.setSelection=ae;this.update=t;l=aw;while(l.length&&!l.is("body")){if(!isNaN(l.css("z-index"))&&l.css("z-index")>B){B=l.css("z-index")}if(l.css("position")=="fixed"){ad="fixed"}l=l.parent()}if(!isNaN(S.zIndex)){B=S.zIndex}if(e.browser.msie){aw.attr("unselectable","on")}e.imgAreaSelect.keyPress=e.browser.msie||e.browser.safari?"keydown":"keypress";if(e.browser.opera){R=f().css({width:"100%",height:"100%",position:"absolute",zIndex:B+2||2})}ar.add(Y).css({visibility:"hidden",position:ad,overflow:"hidden",zIndex:B||"0"});ar.css({zIndex:B+2||2});af.add(H).css({position:"absolute"});q.complete||q.readyState=="complete"||!aw.is("img")?au():aw.one("load",au)};e.fn.imgAreaSelect=function(g){g=g||{};this.each(function(){if(e(this).data("imgAreaSelect")){e(this).data("imgAreaSelect").setOptions(g)}else{if(g.enable===undefined&&g.disable===undefined){g.enable=true}e(this).data("imgAreaSelect",new e.imgAreaSelect(this,g))}});if(g.instance){return e(this).data("imgAreaSelect")}return this}})(jQuery);