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