Update imgareaselect Jquery plugin, props duck_, fixes #17446

git-svn-id: http://svn.automattic.com/wordpress/trunk@17934 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2011-05-15 18:03:51 +00:00
parent dc12474561
commit 091e96b27c
3 changed files with 126 additions and 95 deletions

View File

@ -1,8 +1,8 @@
/* /*
* imgAreaSelect jQuery plugin * imgAreaSelect jQuery plugin
* version 0.9.1 * version 0.9.6
* *
* Copyright (c) 2008-2009 Michal Wojciechowski (odyniec.net) * Copyright (c) 2008-2011 Michal Wojciechowski (odyniec.net)
* *
* Dual licensed under the MIT (MIT-LICENSE.txt) * Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses. * and GPL (GPL-LICENSE.txt) licenses.
@ -39,13 +39,13 @@ $.imgAreaSelect = function (img, options) {
left, top, left, top,
imgOfs, imgOfs = { left: 0, top: 0 },
imgWidth, imgHeight, imgWidth, imgHeight,
$parent, $parent,
parOfs, parOfs = { left: 0, top: 0 },
zIndex = 0, zIndex = 0,
@ -59,6 +59,8 @@ $.imgAreaSelect = function (img, options) {
resize, resize,
minWidth, minHeight, maxWidth, maxHeight,
aspectRatio, aspectRatio,
shown, shown,
@ -67,6 +69,8 @@ $.imgAreaSelect = function (img, options) {
selection = { x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0 }, selection = { x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0 },
docElem = document.documentElement,
$p, d, i, o, w, h, adjusted; $p, d, i, o, w, h, adjusted;
function viewX(x) { function viewX(x) {
@ -108,14 +112,14 @@ $.imgAreaSelect = function (img, options) {
var sx = noScale || scaleX, sy = noScale || scaleY; var sx = noScale || scaleX, sy = noScale || scaleY;
selection = { selection = {
x1: round(x1 / sx), x1: round(x1 / sx || 0),
y1: round(y1 / sy), y1: round(y1 / sy || 0),
x2: round(x2 / sx), x2: round(x2 / sx || 0),
y2: round(y2 / sy) y2: round(y2 / sy || 0)
}; };
selection.width = (x2 = viewX(selection.x2)) - (x1 = viewX(selection.x1)); selection.width = selection.x2 - selection.x1;
selection.height = (y2 = viewX(selection.y2)) - (y1 = viewX(selection.y1)); selection.height = selection.y2 - selection.y1;
} }
function adjust() { function adjust() {
@ -124,13 +128,22 @@ $.imgAreaSelect = function (img, options) {
imgOfs = { left: round($img.offset().left), top: round($img.offset().top) }; imgOfs = { left: round($img.offset().left), top: round($img.offset().top) };
imgWidth = $img.width(); imgWidth = $img.innerWidth();
imgHeight = $img.height(); imgHeight = $img.innerHeight();
if ($().jquery == '1.3.2' && $.browser.safari && position == 'fixed') { imgOfs.top += ($img.outerHeight() - imgHeight) >> 1;
imgOfs.top += max(document.documentElement.scrollTop, $('body').scrollTop()); imgOfs.left += ($img.outerWidth() - imgWidth) >> 1;
imgOfs.left += max(document.documentElement.scrollLeft, $('body').scrollLeft()); minWidth = options.minWidth || 0;
minHeight = options.minHeight || 0;
maxWidth = min(options.maxWidth || 1<<24, imgWidth);
maxHeight = min(options.maxHeight || 1<<24, imgHeight);
if ($().jquery == '1.3.2' && position == 'fixed' &&
!docElem['getBoundingClientRect'])
{
imgOfs.top += max(document.body.scrollTop, docElem.scrollTop);
imgOfs.left += max(document.body.scrollLeft, docElem.scrollLeft);
} }
parOfs = $.inArray($parent.css('position'), ['absolute', 'relative']) + 1 ? parOfs = $.inArray($parent.css('position'), ['absolute', 'relative']) + 1 ?
@ -142,6 +155,9 @@ $.imgAreaSelect = function (img, options) {
left = viewX(0); left = viewX(0);
top = viewY(0); top = viewY(0);
if (selection.x2 > imgWidth || selection.y2 > imgHeight)
doResize();
} }
function update(resetKeyPress) { function update(resetKeyPress) {
@ -170,10 +186,10 @@ $.imgAreaSelect = function (img, options) {
switch ($handles.length) { switch ($handles.length) {
case 8: case 8:
$($handles[4]).css({ left: w / 2 }); $($handles[4]).css({ left: w >> 1 });
$($handles[5]).css({ left: w, top: h / 2 }); $($handles[5]).css({ left: w, top: h >> 1 });
$($handles[6]).css({ left: w / 2, top: h }); $($handles[6]).css({ left: w >> 1, top: h });
$($handles[7]).css({ top: h / 2 }); $($handles[7]).css({ top: h >> 1 });
case 4: case 4:
$handles.slice(1,3).css({ left: w }); $handles.slice(1,3).css({ left: w });
$handles.slice(2,4).css({ top: h }); $handles.slice(2,4).css({ top: h });
@ -221,13 +237,13 @@ $.imgAreaSelect = function (img, options) {
resize = ''; resize = '';
if (options.resizable) { if (options.resizable) {
if (y <= resizeMargin) if (y <= options.resizeMargin)
resize = 'n'; resize = 'n';
else if (y >= selection.height - resizeMargin) else if (y >= selection.height - options.resizeMargin)
resize = 's'; resize = 's';
if (x <= resizeMargin) if (x <= options.resizeMargin)
resize += 'w'; resize += 'w';
else if (x >= selection.width - resizeMargin) else if (x >= selection.width - options.resizeMargin)
resize += 'e'; resize += 'e';
} }
@ -239,14 +255,13 @@ $.imgAreaSelect = function (img, options) {
function docMouseUp(event) { function docMouseUp(event) {
$('body').css('cursor', ''); $('body').css('cursor', '');
if (options.autoHide || selection.width * selection.height == 0) if (options.autoHide || selection.width * selection.height == 0)
hide($box.add($outer), function () { $(this).hide(); }); hide($box.add($outer), function () { $(this).hide(); });
options.onSelectEnd(img, getSelection());
$(document).unbind('mousemove', selectingMouseMove); $(document).unbind('mousemove', selectingMouseMove);
$box.mousemove(areaMouseMove); $box.mousemove(areaMouseMove);
options.onSelectEnd(img, getSelection());
} }
function areaMouseDown(event) { function areaMouseDown(event) {
@ -284,59 +299,60 @@ $.imgAreaSelect = function (img, options) {
return false; return false;
} }
function aspectRatioXY() { function fixAspectRatio(xFirst) {
x2 = max(left, min(left + imgWidth, if (aspectRatio)
x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))); if (xFirst) {
x2 = max(left, min(left + imgWidth,
x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)));
y2 = round(max(top, min(top + imgHeight, y2 = round(max(top, min(top + imgHeight,
y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)))); y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))));
x2 = round(x2); x2 = round(x2);
} }
else {
function aspectRatioYX() { y2 = max(top, min(top + imgHeight,
y2 = max(top, min(top + imgHeight, y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)));
y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))); x2 = round(max(left, min(left + imgWidth,
x2 = round(max(left, min(left + imgWidth, x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))));
x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)))); y2 = round(y2);
y2 = round(y2); }
} }
function doResize() { function doResize() {
if (abs(x2 - x1) < options.minWidth) { x1 = min(x1, left + imgWidth);
x2 = x1 - options.minWidth * (x2 < x1 || -1); y1 = min(y1, top + imgHeight);
if (abs(x2 - x1) < minWidth) {
x2 = x1 - minWidth * (x2 < x1 || -1);
if (x2 < left) if (x2 < left)
x1 = left + options.minWidth; x1 = left + minWidth;
else if (x2 > left + imgWidth) else if (x2 > left + imgWidth)
x1 = left + imgWidth - options.minWidth; x1 = left + imgWidth - minWidth;
} }
if (abs(y2 - y1) < options.minHeight) { if (abs(y2 - y1) < minHeight) {
y2 = y1 - options.minHeight * (y2 < y1 || -1); y2 = y1 - minHeight * (y2 < y1 || -1);
if (y2 < top) if (y2 < top)
y1 = top + options.minHeight; y1 = top + minHeight;
else if (y2 > top + imgHeight) else if (y2 > top + imgHeight)
y1 = top + imgHeight - options.minHeight; y1 = top + imgHeight - minHeight;
} }
x2 = max(left, min(x2, left + imgWidth)); x2 = max(left, min(x2, left + imgWidth));
y2 = max(top, min(y2, top + imgHeight)); y2 = max(top, min(y2, top + imgHeight));
if (aspectRatio) fixAspectRatio(abs(x2 - x1) < abs(y2 - y1) * aspectRatio);
if (abs(x2 - x1) / aspectRatio > abs(y2 - y1))
aspectRatioYX();
else
aspectRatioXY();
if (abs(x2 - x1) > options.maxWidth) { if (abs(x2 - x1) > maxWidth) {
x2 = x1 - options.maxWidth * (x2 < x1 || -1); x2 = x1 - maxWidth * (x2 < x1 || -1);
if (aspectRatio) aspectRatioYX(); fixAspectRatio();
} }
if (abs(y2 - y1) > options.maxHeight) { if (abs(y2 - y1) > maxHeight) {
y2 = y1 - options.maxHeight * (y2 < y1 || -1); y2 = y1 - maxHeight * (y2 < y1 || -1);
if (aspectRatio) aspectRatioXY(); fixAspectRatio(true);
} }
selection = { x1: selX(min(x1, x2)), x2: selX(max(x1, x2)), selection = { x1: selX(min(x1, x2)), x2: selX(max(x1, x2)),
@ -362,8 +378,8 @@ $.imgAreaSelect = function (img, options) {
x2 = (x1 = newX1) + selection.width; x2 = (x1 = newX1) + selection.width;
y2 = (y1 = newY1) + selection.height; y2 = (y1 = newY1) + selection.height;
selection = $.extend(selection, { x1: selX(x1), y1: selY(y1), $.extend(selection, { x1: selX(x1), y1: selY(y1), x2: selX(x2),
x2: selX(x2), y2: selY(y2) }); y2: selY(y2) });
update(); update();
@ -382,6 +398,7 @@ $.imgAreaSelect = function (img, options) {
} }
function startSelection() { function startSelection() {
$(document).unbind('mousemove', startSelection);
adjust(); adjust();
x2 = x1; x2 = x1;
@ -404,11 +421,11 @@ $.imgAreaSelect = function (img, options) {
} }
function cancelSelection() { function cancelSelection() {
$(document).unbind('mousemove', startSelection); $(document).unbind('mousemove', startSelection)
.unbind('mouseup', cancelSelection);
hide($box.add($outer)); hide($box.add($outer));
selection = { x1: selX(x1), y1: selY(y1), x2: selX(x1), y2: selY(y1), setSelection(selX(x1), selY(y1), selX(x1), selY(y1));
width: 0, height: 0 };
options.onSelectChange(img, getSelection()); options.onSelectChange(img, getSelection());
options.onSelectEnd(img, getSelection()); options.onSelectEnd(img, getSelection());
@ -421,13 +438,12 @@ $.imgAreaSelect = function (img, options) {
startX = x1 = evX(event); startX = x1 = evX(event);
startY = y1 = evY(event); startY = y1 = evY(event);
$(document).one('mousemove', startSelection) $(document).mousemove(startSelection).mouseup(cancelSelection);
.one('mouseup', cancelSelection);
return false; return false;
} }
function parentScroll() { function windowResize() {
doUpdate(false); doUpdate(false);
} }
@ -437,8 +453,9 @@ $.imgAreaSelect = function (img, options) {
setOptions(options = $.extend({ setOptions(options = $.extend({
classPrefix: 'imgareaselect', classPrefix: 'imgareaselect',
movable: true, movable: true,
resizable: true,
parent: 'body', parent: 'body',
resizable: true,
resizeMargin: 10,
onInit: function () {}, onInit: function () {},
onSelectStart: function () {}, onSelectStart: function () {},
onSelectChange: function () {}, onSelectChange: function () {},
@ -458,7 +475,7 @@ $.imgAreaSelect = function (img, options) {
} }
var docKeyPress = function(event) { var docKeyPress = function(event) {
var k = options.keys, d, t, key = event.keyCode || event.which; var k = options.keys, d, t, key = event.keyCode;
d = !isNaN(k.alt) && (event.altKey || event.originalEvent.altKey) ? k.alt : d = !isNaN(k.alt) && (event.altKey || event.originalEvent.altKey) ? k.alt :
!isNaN(k.ctrl) && event.ctrlKey ? k.ctrl : !isNaN(k.ctrl) && event.ctrlKey ? k.ctrl :
@ -476,7 +493,7 @@ $.imgAreaSelect = function (img, options) {
t = max(x1, x2); t = max(x1, x2);
x1 = min(x1, x2); x1 = min(x1, x2);
x2 = max(t + d, x1); x2 = max(t + d, x1);
if (aspectRatio) aspectRatioYX(); fixAspectRatio();
break; break;
case 38: case 38:
d = -d; d = -d;
@ -484,7 +501,7 @@ $.imgAreaSelect = function (img, options) {
t = max(y1, y2); t = max(y1, y2);
y1 = min(y1, y2); y1 = min(y1, y2);
y2 = max(t + d, y1); y2 = max(t + d, y1);
if (aspectRatio) aspectRatioXY(); fixAspectRatio(true);
break; break;
default: default:
return; return;
@ -527,7 +544,7 @@ $.imgAreaSelect = function (img, options) {
if (newOptions.parent) if (newOptions.parent)
($parent = $(newOptions.parent)).append($box.add($outer)); ($parent = $(newOptions.parent)).append($box.add($outer));
options = $.extend(options, newOptions); $.extend(options, newOptions);
adjust(); adjust();
@ -546,7 +563,7 @@ $.imgAreaSelect = function (img, options) {
zIndex: zIndex + 1 || 1 zIndex: zIndex + 1 || 1
}); });
if (!parseInt($handles.css('width'))) if (!parseInt($handles.css('width')) >= 0)
$handles.width(5).height(5); $handles.width(5).height(5);
if (o = options.borderWidth) if (o = options.borderWidth)
@ -562,7 +579,7 @@ $.imgAreaSelect = function (img, options) {
if (newOptions.x1 != null) { if (newOptions.x1 != null) {
setSelection(newOptions.x1, newOptions.y1, newOptions.x2, setSelection(newOptions.x1, newOptions.y1, newOptions.x2,
newOptions.y2); newOptions.y2);
newOptions.show = !newOptions.hide; newOptions.show = !newOptions.hide;
} }
@ -586,7 +603,7 @@ $.imgAreaSelect = function (img, options) {
if (o = options.borderColor2) if (o = options.borderColor2)
$($border[1]).css({ borderStyle: 'dashed', borderColor: o }); $($border[1]).css({ borderStyle: 'dashed', borderColor: o });
$box.append($area.add($border).add($handles).add($areaOpera)); $box.append($area.add($border).add($areaOpera).add($handles));
if ($.browser.msie) { if ($.browser.msie) {
if (o = $outer.css('filter').match(/opacity=([0-9]+)/)) if (o = $outer.css('filter').match(/opacity=([0-9]+)/))
@ -605,25 +622,32 @@ $.imgAreaSelect = function (img, options) {
aspectRatio = (d = (options.aspectRatio || '').split(/:/))[0] / d[1]; aspectRatio = (d = (options.aspectRatio || '').split(/:/))[0] / d[1];
$img.add($outer).unbind('mousedown', imgMouseDown);
if (options.disable || options.enable === false) { if (options.disable || options.enable === false) {
$box.unbind('mousemove', areaMouseMove).unbind('mousedown', areaMouseDown); $box.unbind('mousemove', areaMouseMove).unbind('mousedown', areaMouseDown);
$img.add($outer).unbind('mousedown', imgMouseDown); $(window).unbind('resize', windowResize);
$(window).unbind('resize', parentScroll);
$img.add($img.parents()).unbind('scroll', parentScroll);
} }
else if (options.enable || options.disable === false) { else {
if (options.resizable || options.movable) if (options.enable || options.disable === false) {
$box.mousemove(areaMouseMove).mousedown(areaMouseDown); if (options.resizable || options.movable)
$box.mousemove(areaMouseMove).mousedown(areaMouseDown);
$(window).resize(windowResize);
}
if (!options.persistent) if (!options.persistent)
$img.add($outer).mousedown(imgMouseDown); $img.add($outer).mousedown(imgMouseDown);
$(window).resize(parentScroll);
$img.add($img.parents()).scroll(parentScroll);
} }
options.enable = options.disable = undefined; options.enable = options.disable = undefined;
} }
this.remove = function () {
setOptions({ disable: true });
$box.add($outer).remove();
};
this.getOptions = function () { return options; }; this.getOptions = function () { return options; };
this.setOptions = setOptions; this.setOptions = setOptions;
@ -636,17 +660,16 @@ $.imgAreaSelect = function (img, options) {
$p = $img; $p = $img;
while ($p.length && !$p.is('body')) { while ($p.length) {
if (!isNaN($p.css('z-index')) && $p.css('z-index') > zIndex) zIndex = max(zIndex,
zIndex = $p.css('z-index'); !isNaN($p.css('z-index')) ? $p.css('z-index') : zIndex);
if ($p.css('position') == 'fixed') if ($p.css('position') == 'fixed')
position = 'fixed'; position = 'fixed';
$p = $p.parent(); $p = $p.parent(':not(body)');
} }
if (!isNaN(options.zIndex)) zIndex = options.zIndex || zIndex;
zIndex = options.zIndex;
if ($.browser.msie) if ($.browser.msie)
$img.attr('unselectable', 'on'); $img.attr('unselectable', 'on');
@ -661,20 +684,28 @@ $.imgAreaSelect = function (img, options) {
$box.add($outer).css({ visibility: 'hidden', position: position, $box.add($outer).css({ visibility: 'hidden', position: position,
overflow: 'hidden', zIndex: zIndex || '0' }); overflow: 'hidden', zIndex: zIndex || '0' });
$box.css({ zIndex: zIndex + 2 || 2 }); $box.css({ zIndex: zIndex + 2 || 2 });
$area.add($border).css({ position: 'absolute' }); $area.add($border).css({ position: 'absolute', fontSize: 0 });
img.complete || img.readyState == 'complete' || !$img.is('img') ? img.complete || img.readyState == 'complete' || !$img.is('img') ?
imgLoad() : $img.one('load', imgLoad); imgLoad() : $img.one('load', imgLoad);
if ($.browser.msie && $.browser.version >= 9)
img.src = img.src;
}; };
$.fn.imgAreaSelect = function (options) { $.fn.imgAreaSelect = function (options) {
options = options || {}; options = options || {};
this.each(function () { this.each(function () {
if ($(this).data('imgAreaSelect')) if ($(this).data('imgAreaSelect')) {
$(this).data('imgAreaSelect').setOptions(options); if (options.remove) {
else { $(this).data('imgAreaSelect').remove();
$(this).removeData('imgAreaSelect');
}
else
$(this).data('imgAreaSelect').setOptions(options);
}
else if (!options.remove) {
if (options.enable === undefined && options.disable === undefined) if (options.enable === undefined && options.disable === undefined)
options.enable = true; options.enable = true;

File diff suppressed because one or more lines are too long

View File

@ -250,7 +250,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'json2', "/wp-includes/js/json2$suffix.js", false, '2011-02-23'); $scripts->add( 'json2', "/wp-includes/js/json2$suffix.js", false, '2011-02-23');
$scripts->add( 'imgareaselect', "/wp-includes/js/imgareaselect/jquery.imgareaselect$suffix.js", array('jquery'), '0.9.1-20110113' ); $scripts->add( 'imgareaselect', "/wp-includes/js/imgareaselect/jquery.imgareaselect$suffix.js", array('jquery'), '0.9.6-20110515' );
$scripts->add_data( 'imgareaselect', 'group', 1 ); $scripts->add_data( 'imgareaselect', 'group', 1 );
$scripts->add( 'password-strength-meter', "/wp-admin/js/password-strength-meter$suffix.js", array('jquery'), '20101027' ); $scripts->add( 'password-strength-meter', "/wp-admin/js/password-strength-meter$suffix.js", array('jquery'), '20101027' );