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

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