1 line
9.5 KiB
JavaScript
1 line
9.5 KiB
JavaScript
!function(xe){var we=Math.abs,Se=Math.max,ze=Math.min,ke=Math.round;function Ce(){return xe("<div/>")}xe.imgAreaSelect=function(o,n){var t,i,r,c,d,a,s,u,l,h,f,m,e,p,y,g,v,b,x,w,S,z,k,C,A,W,I,K=xe(o),P=Ce(),N=Ce(),H=Ce().add(Ce()).add(Ce()).add(Ce()),M=Ce().add(Ce()).add(Ce()).add(Ce()),E=xe([]),O={left:0,top:0},T={left:0,top:0},L=0,j="absolute",D={x1:0,y1:0,x2:0,y2:0,width:0,height:0},R=document.documentElement,X=navigator.userAgent;function Y(e){return e+O.left-T.left}function $(e){return e+O.top-T.top}function q(e){return e-O.left+T.left}function B(e){return e-O.top+T.top}function Q(e){return Se(e.pageX||0,G(e).x)-T.left}function F(e){return Se(e.pageY||0,G(e).y)-T.top}function G(e){e=e.originalEvent||{};return e.touches&&e.touches.length?{x:e.touches[0].pageX,y:e.touches[0].pageY}:{x:0,y:0}}function J(e){var t=e||h,e=e||f;return{x1:ke(D.x1*t),y1:ke(D.y1*e),x2:ke(D.x2*t),y2:ke(D.y2*e),width:ke(D.x2*t)-ke(D.x1*t),height:ke(D.y2*e)-ke(D.y1*e)}}function U(e,t,o,i,s){var n=s||h,s=s||f;(D={x1:ke(e/n||0),y1:ke(t/s||0),x2:ke(o/n||0),y2:ke(i/s||0)}).width=D.x2-D.x1,D.height=D.y2-D.y1}function V(){t&&K.width()&&(O={left:ke(K.offset().left),top:ke(K.offset().top)},d=K.innerWidth(),a=K.innerHeight(),O.top+=K.outerHeight()-a>>1,O.left+=K.outerWidth()-d>>1,e=ke(n.minWidth/h)||0,p=ke(n.minHeight/f)||0,y=ke(ze(n.maxWidth/h||1<<24,d)),g=ke(ze(n.maxHeight/f||1<<24,a)),"1.3.2"!=xe().jquery||"fixed"!=j||R.getBoundingClientRect||(O.top+=Se(document.body.scrollTop,R.scrollTop),O.left+=Se(document.body.scrollLeft,R.scrollLeft)),T=/absolute|relative/.test(s.css("position"))?{left:ke(s.offset().left)-s.scrollLeft(),top:ke(s.offset().top)-s.scrollTop()}:"fixed"==j?{left:xe(document).scrollLeft(),top:xe(document).scrollTop()}:{left:0,top:0},r=Y(0),c=$(0),(D.x2>d||D.y2>a)&&ne())}function Z(e){if(b){switch(P.css({left:Y(D.x1),top:$(D.y1)}).add(N).width(A=D.width).height(W=D.height),N.add(H).add(E).css({left:0,top:0}),H.width(Se(A-H.outerWidth()+H.innerWidth(),0)).height(Se(W-H.outerHeight()+H.innerHeight(),0)),xe(M[0]).css({left:r,top:c,width:D.x1,height:a}),xe(M[1]).css({left:r+D.x1,top:c,width:A,height:D.y1}),xe(M[2]).css({left:r+D.x2,top:c,width:d-D.x2,height:a}),xe(M[3]).css({left:r+D.x1,top:c+D.y2,width:A,height:a-D.y2}),A-=E.outerWidth(),W-=E.outerHeight(),E.length){case 8:xe(E[4]).css({left:A>>1}),xe(E[5]).css({left:A,top:W>>1}),xe(E[6]).css({left:A>>1,top:W}),xe(E[7]).css({top:W>>1});case 4:E.slice(1,3).css({left:A}),E.slice(2,4).css({top:W})}!1!==e&&(xe.imgAreaSelect.onKeyPress!=me&&xe(document).unbind(xe.imgAreaSelect.keyPress,xe.imgAreaSelect.onKeyPress),n.keys&&xe(document)[xe.imgAreaSelect.keyPress](xe.imgAreaSelect.onKeyPress=me)),ge&&H.outerWidth()-H.innerWidth()==2&&(H.css("margin",0),setTimeout(function(){H.css("margin","auto")},0))}}function _(e){V(),Z(e),x=Y(D.x1),w=$(D.y1),S=Y(D.x2),z=$(D.y2)}function ee(e,t){n.fadeSpeed?e.fadeOut(n.fadeSpeed,t):e.hide()}function te(e){var t=q(Q(e))-D.x1,e=B(F(e))-D.y1;I||(V(),I=!0,P.one("mouseout",function(){I=!1})),m="",n.resizable&&(e<=n.resizeMargin?m="n":e>=D.height-n.resizeMargin&&(m="s"),t<=n.resizeMargin?m+="w":t>=D.width-n.resizeMargin&&(m+="e")),P.css("cursor",m?m+"-resize":n.movable?"move":""),i&&i.toggle()}function oe(e){xe("body").css("cursor",""),!n.autoHide&&D.width*D.height!=0||ee(P.add(M),function(){xe(this).hide()}),xe(document).off("mousemove touchmove",re),P.on("mousemove touchmove",te),n.onSelectEnd(o,J())}function ie(e){return"mousedown"==e.type&&1!=e.which||(te(e),V(),m?(xe("body").css("cursor",m+"-resize"),x=Y(D[/w/.test(m)?"x2":"x1"]),w=$(D[/n/.test(m)?"y2":"y1"]),xe(document).on("mousemove touchmove",re).one("mouseup touchend",oe),P.off("mousemove touchmove",te)):n.movable?(u=r+D.x1-Q(e),l=c+D.y1-F(e),P.off("mousemove touchmove",te),xe(document).on("mousemove touchmove",de).one("mouseup touchend",function(){n.onSelectEnd(o,J()),xe(document).off("mousemove touchmove",de),P.on("mousemove touchmove",te)})):K.mousedown(e)),!1}function se(e){v&&(e?(S=Se(r,ze(r+d,x+we(z-w)*v*(x<S||-1))),z=ke(Se(c,ze(c+a,w+we(S-x)/v*(w<z||-1)))),S=ke(S)):(z=Se(c,ze(c+a,w+we(S-x)/v*(w<z||-1))),S=ke(Se(r,ze(r+d,x+we(z-w)*v*(x<S||-1)))),z=ke(z)))}function ne(){x=ze(x,r+d),w=ze(w,c+a),we(S-x)<e&&((S=x-e*(S<x||-1))<r?x=r+e:r+d<S&&(x=r+d-e)),we(z-w)<p&&((z=w-p*(z<w||-1))<c?w=c+p:c+a<z&&(w=c+a-p)),S=Se(r,ze(S,r+d)),z=Se(c,ze(z,c+a)),se(we(S-x)<we(z-w)*v),we(S-x)>y&&(S=x-y*(S<x||-1),se()),we(z-w)>g&&(z=w-g*(z<w||-1),se(!0)),D={x1:q(ze(x,S)),x2:q(Se(x,S)),y1:B(ze(w,z)),y2:B(Se(w,z)),width:we(S-x),height:we(z-w)},Z(),n.onSelectChange(o,J())}function re(e){return S=/w|e|^$/.test(m)||v?Q(e):Y(D.x2),z=/n|s|^$/.test(m)||v?F(e):$(D.y2),ne(),!1}function ce(e,t){S=(x=e)+D.width,z=(w=t)+D.height,xe.extend(D,{x1:q(x),y1:B(w),x2:q(S),y2:B(z)}),Z(),n.onSelectChange(o,J())}function de(e){return x=Se(r,ze(u+Q(e),r+d-D.width)),w=Se(c,ze(l+F(e),c+a-D.height)),ce(x,w),e.preventDefault(),!1}function ae(){xe(document).off("mousemove touchmove",ae),V(),S=x,z=w,ne(),m="",M.is(":visible")||P.add(M).hide().fadeIn(n.fadeSpeed||0),b=!0,xe(document).off("mouseup touchend",ue).on("mousemove touchmove",re).one("mouseup touchend",oe),P.off("mousemove touchmove",te),n.onSelectStart(o,J())}function ue(){xe(document).off("mousemove touchmove",ae).off("mouseup touchend",ue),ee(P.add(M)),U(q(x),B(w),q(x),B(w)),this instanceof xe.imgAreaSelect||(n.onSelectChange(o,J()),n.onSelectEnd(o,J()))}function le(e){return 1<e.which||M.is(":animated")||(V(),u=x=Q(e),l=w=F(e),xe(document).on({"mousemove touchmove":ae,"mouseup touchend":ue})),!1}function he(){_(!1)}function fe(){t=!0,ye(n=xe.extend({classPrefix:"imgareaselect",movable:!0,parent:"body",resizable:!0,resizeMargin:10,onInit:function(){},onSelectStart:function(){},onSelectChange:function(){},onSelectEnd:function(){}},n)),P.add(M).css({visibility:""}),n.show&&(b=!0,V(),Z(),P.add(M).hide().fadeIn(n.fadeSpeed||0)),setTimeout(function(){n.onInit(o,J())},0)}var me=function(e){var t,o=n.keys,i=e.keyCode,s=isNaN(o.alt)||!e.altKey&&!e.originalEvent.altKey?!isNaN(o.ctrl)&&e.ctrlKey?o.ctrl:!isNaN(o.shift)&&e.shiftKey?o.shift:isNaN(o.arrows)?10:o.arrows:o.alt;if("resize"==o.arrows||"resize"==o.shift&&e.shiftKey||"resize"==o.ctrl&&e.ctrlKey||"resize"==o.alt&&(e.altKey||e.originalEvent.altKey)){switch(i){case 37:s=-s;case 39:t=Se(x,S),x=ze(x,S),S=Se(t+s,x),se();break;case 38:s=-s;case 40:t=Se(w,z),w=ze(w,z),z=Se(t+s,w),se(!0);break;default:return}ne()}else switch(x=ze(x,S),w=ze(w,z),i){case 37:ce(Se(x-s,r),w);break;case 38:ce(x,Se(w-s,c));break;case 39:ce(x+ze(s,d-q(S)),w);break;case 40:ce(x,w+ze(s,a-B(z)));break;default:return}return!1};function pe(e,t){for(var o in t)void 0!==n[o]&&e.css(t[o],n[o])}function ye(e){if(e.parent&&(s=xe(e.parent)).append(P.add(M)),xe.extend(n,e),V(),null!=e.handles){for(E.remove(),E=xe([]),k=e.handles?"corners"==e.handles?4:8:0;k--;)E=E.add(Ce());E.addClass(n.classPrefix+"-handle").css({position:"absolute",fontSize:0,zIndex:L+1||1}),0<=!parseInt(E.css("width"))&&E.width(5).height(5),(C=n.borderWidth)&&E.css({borderWidth:C,borderStyle:"solid"}),pe(E,{borderColor1:"border-color",borderColor2:"background-color",borderOpacity:"opacity"})}for(h=n.imageWidth/d||1,f=n.imageHeight/a||1,null!=e.x1&&(U(e.x1,e.y1,e.x2,e.y2),e.show=!e.hide),e.keys&&(n.keys=xe.extend({shift:1,ctrl:"resize"},e.keys)),M.addClass(n.classPrefix+"-outer"),N.addClass(n.classPrefix+"-selection"),k=0;k++<4;)xe(H[k-1]).addClass(n.classPrefix+"-border"+k);pe(N,{selectionColor:"background-color",selectionOpacity:"opacity"}),pe(H,{borderOpacity:"opacity",borderWidth:"border-width"}),pe(M,{outerColor:"background-color",outerOpacity:"opacity"}),(C=n.borderColor1)&&xe(H[0]).css({borderStyle:"solid",borderColor:C}),(C=n.borderColor2)&&xe(H[1]).css({borderStyle:"dashed",borderColor:C}),P.append(N.add(H).add(i)).append(E),ge&&((C=(M.css("filter")||"").match(/opacity=(\d+)/))&&M.css("opacity",C[1]/100),(C=(H.css("filter")||"").match(/opacity=(\d+)/))&&H.css("opacity",C[1]/100)),e.hide?ee(P.add(M)):e.show&&t&&(b=!0,P.add(M).fadeIn(n.fadeSpeed||0),_()),v=(C=(n.aspectRatio||"").split(/:/))[0]/C[1],K.add(M).unbind("mousedown",le),n.disable||!1===n.enable?(P.off({"mousemove touchmove":te,"mousedown touchstart":ie}),xe(window).off("resize",he)):(!n.enable&&!1!==n.disable||((n.resizable||n.movable)&&P.on({"mousemove touchmove":te,"mousedown touchstart":ie}),xe(window).resize(he)),n.persistent||K.add(M).on("mousedown touchstart",le)),n.enable=n.disable=void 0}this.remove=function(){ye({disable:!0}),P.add(M).remove()},this.getOptions=function(){return n},this.setOptions=ye,this.getSelection=J,this.setSelection=U,this.cancelSelection=ue,this.update=_;for(var ge=(/msie ([\w.]+)/i.exec(X)||[])[1],ve=/opera/i.test(X),X=/webkit/i.test(X)&&!/chrome/i.test(X),be=K;be.length;)L=Se(L,isNaN(be.css("z-index"))?L:be.css("z-index")),"fixed"==be.css("position")&&(j="fixed"),be=be.parent(":not(body)");L=n.zIndex||L,ge&&K.attr("unselectable","on"),xe.imgAreaSelect.keyPress=ge||X?"keydown":"keypress",ve&&(i=Ce().css({width:"100%",height:"100%",position:"absolute",zIndex:L+2||2})),P.add(M).css({visibility:"hidden",position:j,overflow:"hidden",zIndex:L||"0"}),P.css({zIndex:L+2||2}),N.add(H).css({position:"absolute",fontSize:0}),o.complete||"complete"==o.readyState||!K.is("img")?fe():K.one("load",fe),!t&&ge&&7<=ge&&(o.src=o.src)},xe.fn.imgAreaSelect=function(e){return e=e||{},this.each(function(){xe(this).data("imgAreaSelect")?e.remove?(xe(this).data("imgAreaSelect").remove(),xe(this).removeData("imgAreaSelect")):xe(this).data("imgAreaSelect").setOptions(e):e.remove||(void 0===e.enable&&void 0===e.disable&&(e.enable=!0),xe(this).data("imgAreaSelect",new xe.imgAreaSelect(this,e)))}),e.instance?xe(this).data("imgAreaSelect"):this}}(jQuery); |