diff --git a/wp-includes/js/imgareaselect/jquery.imgareaselect.js b/wp-includes/js/imgareaselect/jquery.imgareaselect.js index c0b573934f..d825b9cddf 100644 --- a/wp-includes/js/imgareaselect/jquery.imgareaselect.js +++ b/wp-includes/js/imgareaselect/jquery.imgareaselect.js @@ -1,6 +1,6 @@ /* * imgAreaSelect jQuery plugin - * version 0.9.10-wp + * version 0.9.10-wp2 * * Copyright (c) 2008-2013 Michal Wojciechowski (odyniec.net) * @@ -20,7 +20,7 @@ var abs = Math.abs, max = Math.max, min = Math.min, - round = Math.round; + floor = Math.floor; /** * Create a new HTML div element @@ -230,12 +230,12 @@ $.imgAreaSelect = function (img, options) { function getSelection(noScale) { var sx = noScale || scaleX, sy = noScale || scaleY; - return { x1: round(selection.x1 * sx), - y1: round(selection.y1 * sy), - x2: round(selection.x2 * sx), - y2: round(selection.y2 * sy), - width: round(selection.x2 * sx) - round(selection.x1 * sx), - height: round(selection.y2 * sy) - round(selection.y1 * sy) }; + return { x1: floor(selection.x1 * sx), + y1: floor(selection.y1 * sy), + x2: floor(selection.x2 * sx), + y2: floor(selection.y2 * sy), + width: floor(selection.x2 * sx) - floor(selection.x1 * sx), + height: floor(selection.y2 * sy) - floor(selection.y1 * sy) }; } /** @@ -257,10 +257,10 @@ $.imgAreaSelect = function (img, options) { var sx = noScale || scaleX, sy = noScale || scaleY; selection = { - x1: round(x1 / sx || 0), - y1: round(y1 / sy || 0), - x2: round(x2 / sx || 0), - y2: round(y2 / sy || 0) + x1: floor(x1 / sx || 0), + y1: floor(y1 / sy || 0), + x2: floor(x2 / sx || 0), + y2: floor(y2 / sy || 0) }; selection.width = selection.x2 - selection.x1; @@ -283,7 +283,7 @@ $.imgAreaSelect = function (img, options) { * Get image offset. The .offset() method returns float values, so they * need to be rounded. */ - imgOfs = { left: round($img.offset().left), top: round($img.offset().top) }; + imgOfs = { left: floor($img.offset().left), top: floor($img.offset().top) }; /* Get image dimensions */ imgWidth = $img.innerWidth(); @@ -293,10 +293,10 @@ $.imgAreaSelect = function (img, options) { imgOfs.left += ($img.outerWidth() - imgWidth) >> 1; /* Set minimum and maximum selection area dimensions */ - minWidth = round(options.minWidth / scaleX) || 0; - minHeight = round(options.minHeight / scaleY) || 0; - maxWidth = round(min(options.maxWidth / scaleX || 1<<24, imgWidth)); - maxHeight = round(min(options.maxHeight / scaleY || 1<<24, imgHeight)); + minWidth = floor(options.minWidth / scaleX) || 0; + minHeight = floor(options.minHeight / scaleY) || 0; + maxWidth = floor(min(options.maxWidth / scaleX || 1<<24, imgWidth)); + maxHeight = floor(min(options.maxHeight / scaleY || 1<<24, imgHeight)); /* * Workaround for jQuery 1.3.2 incorrect offset calculation, originally @@ -311,8 +311,8 @@ $.imgAreaSelect = function (img, options) { /* Determine parent element offset */ parOfs = /absolute|relative/.test($parent.css('position')) ? - { left: round($parent.offset().left) - $parent.scrollLeft(), - top: round($parent.offset().top) - $parent.scrollTop() } : + { left: floor($parent.offset().left) - $parent.scrollLeft(), + top: floor($parent.offset().top) - $parent.scrollTop() } : position == 'fixed' ? { left: $(document).scrollLeft(), top: $(document).scrollTop() } : { left: 0, top: 0 }; @@ -430,6 +430,13 @@ $.imgAreaSelect = function (img, options) { function doUpdate(resetKeyPress) { adjust(); update(resetKeyPress); + updateSelectionRelativeToParentElement(); + } + + /** + * Set the correct values of x1, y1, x2, and y2. + */ + function updateSelectionRelativeToParentElement() { x1 = viewX(selection.x1); y1 = viewY(selection.y1); x2 = viewX(selection.x2); y2 = viewY(selection.y2); } @@ -571,16 +578,16 @@ $.imgAreaSelect = function (img, options) { if (xFirst) { x2 = max(left, min(left + imgWidth, x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))); - y2 = round(max(top, min(top + imgHeight, + y2 = floor(max(top, min(top + imgHeight, y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)))); - x2 = round(x2); + x2 = floor(x2); } else { y2 = max(top, min(top + imgHeight, y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))); - x2 = round(max(left, min(left + imgWidth, + x2 = floor(max(left, min(left + imgWidth, x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)))); - y2 = round(y2); + y2 = floor(y2); } } @@ -589,6 +596,14 @@ $.imgAreaSelect = function (img, options) { * aspect ratio */ function doResize() { + /* + * Make sure x1, x2, y1, y2 are initialized to avoid the following calculation + * getting incorrect results. + */ + if ( x1 == null || x2 == null || y1 == null || y2 == null ) { + updateSelectionRelativeToParentElement(); + } + /* * Make sure the top left corner of the selection area stays within * image boundaries (it might not if the image source was dynamically diff --git a/wp-includes/js/imgareaselect/jquery.imgareaselect.min.js b/wp-includes/js/imgareaselect/jquery.imgareaselect.min.js index eea7a2e972..8df1357747 100644 --- a/wp-includes/js/imgareaselect/jquery.imgareaselect.min.js +++ b/wp-includes/js/imgareaselect/jquery.imgareaselect.min.js @@ -1 +1 @@ -!function(we){var Se=Math.abs,ze=Math.max,ke=Math.min,Ce=Math.round;function Ae(){return we("
")}we.imgAreaSelect=function(o,n){var T,L,r,c,d,a,t,j,D,R,X,i,Y,$,q,B,s,Q,u,l,h,f,F,m,p,y,g,G,v=we(o),b=Ae(),x=Ae(),w=Ae().add(Ae()).add(Ae()).add(Ae()),S=Ae().add(Ae()).add(Ae()).add(Ae()),z=we([]),k={left:0,top:0},C={left:0,top:0},A=0,J="absolute",W={x1:0,y1:0,x2:0,y2:0,width:0,height:0},U=document.documentElement,V=navigator.userAgent;function I(e){return e+k.left-C.left}function K(e){return e+k.top-C.top}function P(e){return e-k.left+C.left}function N(e){return e-k.top+C.top}function Z(e){return ze(e.pageX||0,ee(e).x)-C.left}function _(e){return ze(e.pageY||0,ee(e).y)-C.top}function ee(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 H(e){var t=e||R,e=e||X;return{x1:Ce(W.x1*t),y1:Ce(W.y1*e),x2:Ce(W.x2*t),y2:Ce(W.y2*e),width:Ce(W.x2*t)-Ce(W.x1*t),height:Ce(W.y2*e)-Ce(W.y1*e)}}function te(e,t,o,i,s){var n=s||R,s=s||X;(W={x1:Ce(e/n||0),y1:Ce(t/s||0),x2:Ce(o/n||0),y2:Ce(i/s||0)}).width=W.x2-W.x1,W.height=W.y2-W.y1}function M(){T&&v.width()&&(k={left:Ce(v.offset().left),top:Ce(v.offset().top)},d=v.innerWidth(),a=v.innerHeight(),k.top+=v.outerHeight()-a>>1,k.left+=v.outerWidth()-d>>1,Y=Ce(n.minWidth/R)||0,$=Ce(n.minHeight/X)||0,q=Ce(ke(n.maxWidth/R||1<<24,d)),B=Ce(ke(n.maxHeight/X||1<<24,a)),"1.3.2"!=we().jquery||"fixed"!=J||U.getBoundingClientRect||(k.top+=ze(document.body.scrollTop,U.scrollTop),k.left+=ze(document.body.scrollLeft,U.scrollLeft)),C=/absolute|relative/.test(t.css("position"))?{left:Ce(t.offset().left)-t.scrollLeft(),top:Ce(t.offset().top)-t.scrollTop()}:"fixed"==J?{left:we(document).scrollLeft(),top:we(document).scrollTop()}:{left:0,top:0},r=I(0),c=K(0),(W.x2>d||W.y2>a)&&de())}function oe(e){if(Q){switch(b.css({left:I(W.x1),top:K(W.y1)}).add(x).width(y=W.width).height(g=W.height),x.add(w).add(z).css({left:0,top:0}),w.width(ze(y-w.outerWidth()+w.innerWidth(),0)).height(ze(g-w.outerHeight()+w.innerHeight(),0)),we(S[0]).css({left:r,top:c,width:W.x1,height:a}),we(S[1]).css({left:r+W.x1,top:c,width:y,height:W.y1}),we(S[2]).css({left:r+W.x2,top:c,width:d-W.x2,height:a}),we(S[3]).css({left:r+W.x1,top:c+W.y2,width:y,height:a-W.y2}),y-=z.outerWidth(),g-=z.outerHeight(),z.length){case 8:we(z[4]).css({left:y>>1}),we(z[5]).css({left:y,top:g>>1}),we(z[6]).css({left:y>>1,top:g}),we(z[7]).css({top:g>>1});case 4:z.slice(1,3).css({left:y}),z.slice(2,4).css({top:g})}!1!==e&&(we.imgAreaSelect.onKeyPress!=ge&&we(document).off(we.imgAreaSelect.keyPress,we.imgAreaSelect.onKeyPress),n.keys&&we(document).on(we.imgAreaSelect.keyPress,function(){we.imgAreaSelect.onKeyPress=ge})),O&&w.outerWidth()-w.innerWidth()==2&&(w.css("margin",0),setTimeout(function(){w.css("margin","auto")},0))}}function ie(e){M(),oe(e),u=I(W.x1),l=K(W.y1),h=I(W.x2),f=K(W.y2)}function se(e,t){n.fadeSpeed?e.fadeOut(n.fadeSpeed,t):e.hide()}function E(e){var t=P(Z(e))-W.x1,e=N(_(e))-W.y1;G||(M(),G=!0,b.one("mouseout",function(){G=!1})),i="",n.resizable&&(e<=n.resizeMargin?i="n":e>=W.height-n.resizeMargin&&(i="s"),t<=n.resizeMargin?i+="w":t>=W.width-n.resizeMargin&&(i+="e")),b.css("cursor",i?i+"-resize":n.movable?"move":""),L&&L.toggle()}function ne(e){we("body").css("cursor",""),!n.autoHide&&W.width*W.height!=0||se(b.add(S),function(){we(this).hide()}),we(document).off("mousemove touchmove",ae),b.on("mousemove touchmove",E),n.onSelectEnd(o,H())}function re(e){return"mousedown"==e.type&&1!=e.which||(E(e),M(),i?(we("body").css("cursor",i+"-resize"),u=I(W[/w/.test(i)?"x2":"x1"]),l=K(W[/n/.test(i)?"y2":"y1"]),we(document).on("mousemove touchmove",ae).one("mouseup touchend",ne),b.off("mousemove touchmove",E)):n.movable?(j=r+W.x1-Z(e),D=c+W.y1-_(e),b.off("mousemove touchmove",E),we(document).on("mousemove touchmove",le).one("mouseup touchend",function(){n.onSelectEnd(o,H()),we(document).off("mousemove touchmove",le),b.on("mousemove touchmove",E)})):v.mousedown(e)),!1}function ce(e){s&&(e?(h=ze(r,ke(r+d,u+Se(f-l)*s*(uq&&(h=u-q*(hB&&(f=l-B*(f")}Se.imgAreaSelect=function(o,s){var T,L,r,c,d,a,t,j,D,R,X,i,Y,$,q,B,n,Q,u,l,h,f,F,m,p,y,g,G,v=Se(o),b=We(),x=We(),w=We().add(We()).add(We()).add(We()),S=We().add(We()).add(We()).add(We()),z=Se([]),k={left:0,top:0},C={left:0,top:0},A=0,J="absolute",W={x1:0,y1:0,x2:0,y2:0,width:0,height:0},U=document.documentElement,V=navigator.userAgent;function I(e){return e+k.left-C.left}function K(e){return e+k.top-C.top}function P(e){return e-k.left+C.left}function N(e){return e-k.top+C.top}function Z(e){return ke(e.pageX||0,ee(e).x)-C.left}function _(e){return ke(e.pageY||0,ee(e).y)-C.top}function ee(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 H(e){var t=e||R,e=e||X;return{x1:Ae(W.x1*t),y1:Ae(W.y1*e),x2:Ae(W.x2*t),y2:Ae(W.y2*e),width:Ae(W.x2*t)-Ae(W.x1*t),height:Ae(W.y2*e)-Ae(W.y1*e)}}function te(e,t,o,i,n){var s=n||R,n=n||X;(W={x1:Ae(e/s||0),y1:Ae(t/n||0),x2:Ae(o/s||0),y2:Ae(i/n||0)}).width=W.x2-W.x1,W.height=W.y2-W.y1}function M(){T&&v.width()&&(k={left:Ae(v.offset().left),top:Ae(v.offset().top)},d=v.innerWidth(),a=v.innerHeight(),k.top+=v.outerHeight()-a>>1,k.left+=v.outerWidth()-d>>1,Y=Ae(s.minWidth/R)||0,$=Ae(s.minHeight/X)||0,q=Ae(Ce(s.maxWidth/R||1<<24,d)),B=Ae(Ce(s.maxHeight/X||1<<24,a)),"1.3.2"!=Se().jquery||"fixed"!=J||U.getBoundingClientRect||(k.top+=ke(document.body.scrollTop,U.scrollTop),k.left+=ke(document.body.scrollLeft,U.scrollLeft)),C=/absolute|relative/.test(t.css("position"))?{left:Ae(t.offset().left)-t.scrollLeft(),top:Ae(t.offset().top)-t.scrollTop()}:"fixed"==J?{left:Se(document).scrollLeft(),top:Se(document).scrollTop()}:{left:0,top:0},r=I(0),c=K(0),(W.x2>d||W.y2>a)&&ae())}function oe(e){if(Q){switch(b.css({left:I(W.x1),top:K(W.y1)}).add(x).width(y=W.width).height(g=W.height),x.add(w).add(z).css({left:0,top:0}),w.width(ke(y-w.outerWidth()+w.innerWidth(),0)).height(ke(g-w.outerHeight()+w.innerHeight(),0)),Se(S[0]).css({left:r,top:c,width:W.x1,height:a}),Se(S[1]).css({left:r+W.x1,top:c,width:y,height:W.y1}),Se(S[2]).css({left:r+W.x2,top:c,width:d-W.x2,height:a}),Se(S[3]).css({left:r+W.x1,top:c+W.y2,width:y,height:a-W.y2}),y-=z.outerWidth(),g-=z.outerHeight(),z.length){case 8:Se(z[4]).css({left:y>>1}),Se(z[5]).css({left:y,top:g>>1}),Se(z[6]).css({left:y>>1,top:g}),Se(z[7]).css({top:g>>1});case 4:z.slice(1,3).css({left:y}),z.slice(2,4).css({top:g})}!1!==e&&(Se.imgAreaSelect.onKeyPress!=ve&&Se(document).off(Se.imgAreaSelect.keyPress,Se.imgAreaSelect.onKeyPress),s.keys&&Se(document).on(Se.imgAreaSelect.keyPress,function(){Se.imgAreaSelect.onKeyPress=ve})),O&&w.outerWidth()-w.innerWidth()==2&&(w.css("margin",0),setTimeout(function(){w.css("margin","auto")},0))}}function ie(e){M(),oe(e),ne()}function ne(){u=I(W.x1),l=K(W.y1),h=I(W.x2),f=K(W.y2)}function se(e,t){s.fadeSpeed?e.fadeOut(s.fadeSpeed,t):e.hide()}function E(e){var t=P(Z(e))-W.x1,e=N(_(e))-W.y1;G||(M(),G=!0,b.one("mouseout",function(){G=!1})),i="",s.resizable&&(e<=s.resizeMargin?i="n":e>=W.height-s.resizeMargin&&(i="s"),t<=s.resizeMargin?i+="w":t>=W.width-s.resizeMargin&&(i+="e")),b.css("cursor",i?i+"-resize":s.movable?"move":""),L&&L.toggle()}function re(e){Se("body").css("cursor",""),!s.autoHide&&W.width*W.height!=0||se(b.add(S),function(){Se(this).hide()}),Se(document).off("mousemove touchmove",ue),b.on("mousemove touchmove",E),s.onSelectEnd(o,H())}function ce(e){return"mousedown"==e.type&&1!=e.which||(E(e),M(),i?(Se("body").css("cursor",i+"-resize"),u=I(W[/w/.test(i)?"x2":"x1"]),l=K(W[/n/.test(i)?"y2":"y1"]),Se(document).on("mousemove touchmove",ue).one("mouseup touchend",re),b.off("mousemove touchmove",E)):s.movable?(j=r+W.x1-Z(e),D=c+W.y1-_(e),b.off("mousemove touchmove",E),Se(document).on("mousemove touchmove",he).one("mouseup touchend",function(){s.onSelectEnd(o,H()),Se(document).off("mousemove touchmove",he),b.on("mousemove touchmove",E)})):v.mousedown(e)),!1}function de(e){n&&(e?(h=ke(r,Ce(r+d,u+ze(f-l)*n*(uq&&(h=u-q*(hB&&(f=l-B*(f